Spaces:
Running on Zero
Running on Zero
File size: 3,465 Bytes
a74054f | 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | 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)) |