-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ecoulement des cours d'eau #10
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5372277
make Watercourses Flow API
B-Alica 06f1047
set max number of values for watercourses_flow api parameters
B-Alica 9909105
make function get_all_stations
B-Alica b85a6c9
make watercourses_flow documentation
B-Alica c030493
make high level function get_all_observations
B-Alica 372833f
Merge branch 'tgrandje:dev' into watercourses-flow
B-Alica 0e5a306
fix issues
B-Alica File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from .watercourses_flow_scraper import WatercoursesFlowSession | ||
from .utils import get_all_stations, get_all_observations | ||
|
||
|
||
__all__ = [ | ||
"get_all_stations", | ||
"get_all_observations", | ||
"WatercoursesFlowSession", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
import geopandas as gpd | ||
import pandas as pd | ||
from tqdm import tqdm | ||
from datetime import date, datetime | ||
from itertools import product | ||
|
||
from cl_hubeau.watercourses_flow.watercourses_flow_scraper import ( | ||
WatercoursesFlowSession, | ||
) | ||
from cl_hubeau import _config | ||
from cl_hubeau.utils import get_departements, prepare_kwargs_loops | ||
|
||
|
||
def get_all_stations(**kwargs) -> gpd.GeoDataFrame: | ||
""" | ||
Retrieve all stations from France. | ||
|
||
Parameters | ||
---------- | ||
**kwargs : | ||
kwargs passed to WatercoursesFlowSession.get_stations (hence mostly intended | ||
for hub'eau API's arguments). Do not use `format` or `code_departement` | ||
as they are set by the current function. | ||
|
||
Returns | ||
------- | ||
results : gpd.GeoDataFrame | ||
GeoDataFrame of stations | ||
|
||
""" | ||
|
||
with WatercoursesFlowSession() as session: | ||
|
||
deps = get_departements() | ||
results = [ | ||
session.get_stations(code_departement=dep, format="geojson", **kwargs) | ||
for dep in tqdm( | ||
deps, | ||
desc="querying dep/dep", | ||
leave=_config["TQDM_LEAVE"], | ||
position=tqdm._get_free_pos(), | ||
) | ||
] | ||
results = [x.dropna(axis=1, how="all") for x in results if not x.empty] | ||
results = gpd.pd.concat(results, ignore_index=True) | ||
try: | ||
results["code_station"] | ||
results = results.drop_duplicates("code_station") | ||
except KeyError: | ||
pass | ||
return results | ||
|
||
|
||
def get_all_observations(**kwargs) -> gpd.GeoDataFrame: | ||
""" | ||
Retrieve all observsations from France. | ||
|
||
Parameters | ||
---------- | ||
**kwargs : | ||
kwargs passed to WatercoursesFlowSession.get_observations (hence mostly intended | ||
for hub'eau API's arguments). Do not use `format` or `code_departement` | ||
as they are set by the current function. | ||
|
||
Returns | ||
------- | ||
results : gpd.GeoDataFrame | ||
GeoDataFrame of observations | ||
""" | ||
|
||
deps = get_departements() | ||
|
||
# Set a loop for yearly querying as dataset are big | ||
start_auto_determination = False | ||
if "date_observation_min" not in kwargs: | ||
start_auto_determination = True | ||
kwargs["date_observation_min"] = "1960-01-01" | ||
if "date_observation_max" not in kwargs: | ||
kwargs["date_observation_max"] = date.today().strftime("%Y-%m-%d") | ||
|
||
# ranges = pd.date_range( | ||
# start=datetime.strptime(kwargs.pop("date_observation_min"), "%Y-%m-%d").date(), | ||
# end=datetime.strptime(kwargs.pop("date_observation_max"), "%Y-%m-%d").date(), | ||
# ) | ||
# dates = pd.Series(ranges).to_frame("date") | ||
# dates["year"] = dates["date"].dt.year | ||
# dates = dates.groupby("year")["date"].agg(["min", "max"]) | ||
# for d in "min", "max": | ||
# dates[d] = dates[d].dt.strftime("%Y-%m-%d") | ||
# if start_auto_determination: | ||
# dates = pd.concat( | ||
# [ | ||
# dates, | ||
# pd.DataFrame([{"min": "1900-01-01", "max": "2015-12-31"}]), | ||
# ], | ||
# ignore_index=False, | ||
# ).sort_index() | ||
|
||
# args = list(product(deps, dates.values.tolist())) | ||
|
||
# with WatercoursesFlowSession() as session: | ||
|
||
# results = [ | ||
# session.get_observations( | ||
# format="geojson", | ||
# date_observation_min=date_min, | ||
# date_observation_max=date_max, | ||
# **{"code_departement": chunk}, | ||
# **kwargs, | ||
# ) | ||
# for chunk, (date_min, date_max) in tqdm( | ||
# args, | ||
# desc="querying station/station and year/year", | ||
# leave=_config["TQDM_LEAVE"], | ||
# position=tqdm._get_free_pos(), | ||
# ) | ||
# ] | ||
|
||
desc = "querying year/year" + (" & dep/dep" if "code_departement" in kwargs else "") | ||
|
||
kwargs_loop = prepare_kwargs_loops( | ||
"date_observation_min", | ||
"date_observation_max", | ||
kwargs, | ||
start_auto_determination, | ||
) | ||
|
||
with WatercoursesFlowSession() as session: | ||
|
||
results = [ | ||
session.get_observations( | ||
format="geojson", | ||
**kwargs, | ||
**kw_loop, | ||
) | ||
for kw_loop in tqdm( | ||
kwargs_loop, | ||
desc=desc, | ||
leave=_config["TQDM_LEAVE"], | ||
position=tqdm._get_free_pos(), | ||
) | ||
] | ||
|
||
results = [x.dropna(axis=1, how="all") for x in results if not x.empty] | ||
results = pd.concat(results, ignore_index=True) | ||
return results | ||
|
||
|
||
def get_all_campagnes(**kwargs) -> gpd.GeoDataFrame: | ||
""" | ||
Retrieve all campagnes from France. | ||
|
||
Parameters | ||
---------- | ||
**kwargs : | ||
kwargs passed to WatercoursesFlowSession.get_campagnes (hence mostly intended | ||
for hub'eau API's arguments). Do not use `code_departement` | ||
as they are set by the current function. | ||
|
||
Returns | ||
------- | ||
results : gpd.GeoDataFrame | ||
GeoDataFrame of campagnes | ||
""" | ||
|
||
with WatercoursesFlowSession() as session: | ||
try: | ||
results = session.get_campagnes(**kwargs) | ||
except ValueError: | ||
# If request is too big | ||
deps = get_departements() | ||
results = [ | ||
session.get_campagnes(code_departement=dep, **kwargs) | ||
for dep in tqdm( | ||
deps, | ||
desc="querying dep/dep", | ||
leave=_config["TQDM_LEAVE"], | ||
position=tqdm._get_free_pos(), | ||
) | ||
] | ||
results = [x.dropna(axis=1, how="all") for x in results if not x.empty] | ||
results = gpd.pd.concat(results, ignore_index=True) | ||
return results | ||
|
||
|
||
# if __name__ == "__main__": | ||
# # print(get_all_stations()) | ||
# # print(get_all_observations()) | ||
# print(get_all_campagnes()) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@B-Alica sur ta boucle interne, > 99% des doublons créés sont liés à cette ligne : tu as laissé un 2015-12-31 ici, alors que l'initialisation en ligne 77 était au 01/01/1960.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mais ça n'explique pas les 50 doublons que j'ai toujours sur l'année 2023...
EDIT : pour être précis, il me reste 50 doublons sur 2023 et 18 sur 2012... 🙁