File size: 3,297 Bytes
196cbb2
 
 
9c23216
 
 
 
 
 
 
196cbb2
 
9c23216
 
 
 
 
 
 
 
 
 
542edf7
9c23216
 
 
 
 
 
 
 
 
 
 
 
 
 
542edf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196cbb2
542edf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c23216
 
 
542edf7
 
 
 
 
9c23216
542edf7
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import os
from pathlib import Path

import geopandas as gpd
from dotenv import load_dotenv 
from geopy.geocoders import Nominatim
from shapely.geometry import Point

load_dotenv()

project_root = Path(__file__).parent.parent
file_rmqs = os.path.join(project_root, 'data/soil_data/raw_data/rmqs.geojson')
df = gpd.read_file(file_rmqs)

def get_city_coordinates(city_name):
    """Get latitude and longitude of a city using Nominatim."""
    geolocator = Nominatim(user_agent="geo_finder")
    location = geolocator.geocode(city_name)
    if location:
        return (location.longitude, location.latitude)
    return None

def find_nearest_point_to_city(city_name):
    """Find the closest GPS point in the dataset to the given city."""
    city_coords = get_city_coordinates(city_name)
    print("city coords", city_coords)
    if not city_coords:
        return "City not found"

    # Iterate through GeoJSON features
    df["distance"] = df["geometry"].distance(Point(city_coords[0], city_coords[1]))
    closest_point = df.loc[df["distance"].idxmin()]


    return closest_point  # Returns the closest feature


def find_nearest_point_to_coordinates(latitude, longitude):
    """Find the closest GPS point in the dataset to the given GPS coordinates."""
    # Iterate through GeoJSON features
    df["distance"] = df["geometry"].distance(Point(latitude, longitude))
    closest_point = df.loc[df["distance"].idxmin()]

    return closest_point  # Returns the closest feature


def get_soil_properties(latitude, longitude):
    closest_feature = find_nearest_point_to_coordinates(latitude, longitude)
    clay = closest_feature.clay
    silt = closest_feature.silt
    sand = closest_feature.sand
    soc = closest_feature.soc
    soil_mosture, field_capacity, wilting_point = calcul_metriques_sol(clay, silt, sand, soc)

    soil_properties = {
        "clay": clay,
        "silt": silt,
        "sand": sand,
        "soc": soc,
        "soil_moisture": soil_mosture,
        "field_capacity": field_capacity,
        "wilting_point": wilting_point,
    }

    return soil_properties


def calcul_metriques_sol(clay, silt, sand, soc):
    """
    Calcule le point de flétrissement (WP), l'humidité actuelle du sol (SM) et la capacité au champ (FC).

    :param clay: Pourcentage d'argil dans le sol (%)
    :param silt: Pourcentage de silt dans le sol (%)
    :param sand: Pourcentage de sand dans le sol (%)
    :param soc: Carbone organique du sol (%)
    :return: WP, SM et FC (%)
    """
    # Calcul de la capacité au champ (FC)
    FC = 0.2576 * silt + 0.2484 * clay + 0.3664 * soc + 20.83

    # Calcul du point de flétrissement (WP)
    WP = 0.026 * clay + 0.5 * soc + 3.0

    # Calcul de l'humidité actuelle du sol (SM) comme 50% de la plage entre FC et WP
    SM = WP + 0.5 * (FC - WP)

    # Retourne les résultats sous forme de dictionnaire
    return round(SM, 2), round(FC, 2), round(WP, 2)


if __name__ == "__main__":
    # Example usage
    city = "Paris"
    closest_feature = find_nearest_point_to_city(city)
    print(closest_feature)

    latitude, longitude = 47, 5
    closest_feature = find_nearest_point_to_coordinates(latitude, longitude)
    print(closest_feature)

    soil_properties = get_soil_properties(latitude, longitude)
    print(soil_properties)