Spaces:
Running on Zero
Running on Zero
| import requests | |
| import pandas as pd | |
| import geopandas as gpd | |
| from shapely.geometry import Point | |
| from tqdm import tqdm | |
| # Departments intersecting the study area | |
| departments = ["27", "61", "76"] | |
| url = "https://hubeau.eaufrance.fr/api/v1/niveaux_nappes/stations" | |
| stations = [] | |
| for dep in departments: | |
| params = { | |
| "code_departement": dep, | |
| "size": 20000, | |
| "format": "json" | |
| } | |
| r = requests.get(url, params=params) | |
| if r.status_code == 200: | |
| stations.extend(r.json()["data"]) | |
| stations = pd.DataFrame(stations) | |
| print(f"{len(stations)} stations downloaded.") | |
| print(stations.columns) | |
| stations = gpd.GeoDataFrame( | |
| stations.drop(columns="geometry", errors="ignore"), | |
| geometry=gpd.points_from_xy( | |
| stations["x"], | |
| stations["y"] | |
| ), | |
| crs="EPSG:4326" | |
| ) | |
| # Watershed Merging | |
| # Read shapefiles | |
| risle = gpd.read_file("data/Watershed_Risle.shp") | |
| eure = gpd.read_file("data/Watershed_Eure.shp") | |
| # Same CRS | |
| eure = eure.to_crs(risle.crs) | |
| # Combine | |
| watersheds = pd.concat([risle, eure], ignore_index=True) | |
| watersheds = gpd.GeoDataFrame(watersheds, crs=risle.crs) | |
| # Create one merged geometry | |
| watershed = gpd.GeoDataFrame( | |
| geometry=[watersheds.unary_union], | |
| crs=watersheds.crs | |
| ) | |
| # Convert to station CRS | |
| watershed = watershed.to_crs("EPSG:2154") | |
| stations = stations.to_crs("EPSG:2154") | |
| print(stations.total_bounds) | |
| print(watershed.total_bounds) | |
| stations_clip = gpd.clip(stations, watershed) | |
| print(f"Total stations downloaded: {len(stations)}") | |
| print(f"Stations inside Risle + Eure watersheds: {len(stations_clip)}") | |
| watershed.to_file( | |
| "output/shapefile/Risle_Eure_Watershed.gpkg", | |
| driver="GPKG" | |
| ) | |
| watershed.to_file( | |
| "output/shapefile/Risle_Eure_Watershed.shp" | |
| ) | |
| # Save station list | |
| stations_clip.to_file( | |
| "output/shapefile/groundwater_stations.gpkg", | |
| driver="GPKG" | |
| ) | |
| stations_clip.to_file( | |
| "output/shapefile/groundwater_stations.shp" | |
| ) | |
| stations_clip.drop(columns="geometry").to_csv( | |
| "output/csv/groundwater_stations.csv", | |
| index=False | |
| ) | |
| # Download ground water levels | |
| #levels_all = [] | |
| #for code in tqdm(stations_clip["code_bss"]): | |
| # | |
| # url = "https://hubeau.eaufrance.fr/api/v1/niveaux_nappes/chroniques" | |
| # | |
| # params = { | |
| # "code_bss": code, | |
| # "format": "json", | |
| # "size": 20000 | |
| # } | |
| # | |
| # r = requests.get(url, params=params) | |
| # | |
| # if r.status_code == 200: | |
| # data = r.json().get("data", []) | |
| # if data: | |
| # df = pd.DataFrame(data) | |
| # df["code_bss"] = code | |
| # levels_all.append(df) | |
| ## Combine | |
| #levels_df = pd.concat(levels_all, ignore_index=True) | |
| ## Save | |
| #levels_df.to_csv("output/csv/groundwater_levels_watershed.csv", index=False) | |
| #print("Groundwater levels downloaded:", len(levels_df)) | |
| # Download groundwater quality | |
| quality_all = [] | |
| for code in tqdm(stations_clip["code_bss"]): | |
| url = "https://hubeau.eaufrance.fr/api/v1/qualite_nappes/analyses" | |
| params = { | |
| "bss_id": code, | |
| "format": "json", | |
| "size": 20000 | |
| } | |
| r = requests.get(url, params=params) | |
| if r.status_code == 200: | |
| data = r.json().get("data", []) | |
| if data: | |
| df = pd.DataFrame(data) | |
| df["code_bss"] = code | |
| quality_all.append(df) | |
| quality_df = pd.concat(quality_all, ignore_index=True) | |
| quality_df.to_csv("output/csv/groundwater_quality_watershed.csv", index=False) | |
| print("Groundwater quality records:", len(quality_df)) |