Spaces:
Runtime error
Runtime error
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) | |