Skip to content

Commit

Permalink
create FRANCE_ENTIERE_IDF_DROM_RAPPROCHES
Browse files Browse the repository at this point in the history
  • Loading branch information
tgrandje committed Oct 18, 2024
1 parent 4e4bd6a commit cacbef9
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 30 deletions.
40 changes: 24 additions & 16 deletions cartiflette/mapshaper/mapshaper_closer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
"saint-pierre-et-miquelon": "bbox=-6298822.299318486, 5894013.594517256, -6239181.296921183, 5973004.907786214",
"saint-barthelemy": "bbox=-7003557.376380256, 2018598.440800959, -6985037.106437805, 2033965.5078367123",
},
"AIRE_ATTRACTION_VILLES": 1.2,
"DEPARTEMENT": 3,
"ARRONDISSEMENT_MUNICIPAL": 3.5,
"EPT": 3.5,
"IRIS": 4,
"AIRE_ATTRACTION_VILLES": 2.4,
"DEPARTEMENT": 6,
"ARRONDISSEMENT_MUNICIPAL": 7,
"EPT": 7,
"IRIS": 8,
}

shift = {
Expand Down Expand Up @@ -54,6 +54,7 @@

def mapshaper_bring_closer(
input_file: str,
bring_out_idf: str = True,
output_dir: str = "temp",
output_name: str = "output",
output_format: str = "geojson",
Expand All @@ -66,6 +67,8 @@ def mapshaper_bring_closer(
----------
input_file : str
Path to the input file.
bring_out_idf : bool, optional
If True, will extract IdF and zoom on it. The default is True.
output_dir : str
Directory to store the output file. The default is "temp"
output_name : str, optional
Expand Down Expand Up @@ -102,22 +105,23 @@ def mapshaper_bring_closer(
logical_metropole = logical_conditions["EMPRISES"]["metropole"]

try:
idf_zoom = (
f"mapshaper -i {input_file} "
f"-proj EPSG:3857 "
f'-filter "{logical_idf}" '
f"-affine shift={shift_idf} scale={zoom_idf} "
f"-o {output_dir}/idf_zoom.{output_format}"
)

france_metropolitaine = (
f"mapshaper -i {input_file} "
f"-proj EPSG:3857 "
f'-filter "{logical_metropole}" '
f"-o {output_dir}/metropole.{output_format}"
)

run(idf_zoom)
if bring_out_idf:
idf_zoom = (
f"mapshaper -i {input_file} "
f"-proj EPSG:3857 "
f'-filter "{logical_idf}" '
f"-affine shift={shift_idf} scale={zoom_idf} "
f"-o {output_dir}/idf_zoom.{output_format}"
)

run(idf_zoom)

run(france_metropolitaine)

Expand All @@ -135,11 +139,14 @@ def mapshaper_bring_closer(
# fix_geo = "fix-geometry" if output_format == "topojson" else ""

output = f"{output_dir}/{output_name}.{output_format}"
bring_out_idf = (
f"{output_dir}/idf_zoom.{output_format} " if bring_out_idf else ""
)
cmd_combined = (
f"mapshaper "
f"{output_dir}/metropole.{output_format} "
f"{output_dir}/idf_zoom.{output_format} "
f"{output_dir}/guadeloupe.{output_format} "
+ bring_out_idf
+ f"{output_dir}/guadeloupe.{output_format} "
f"{output_dir}/martinique.{output_format} "
f"{output_dir}/guyane.{output_format} "
f"{output_dir}/reunion.{output_format} "
Expand All @@ -149,6 +156,7 @@ def mapshaper_bring_closer(
f"-rename-layers FRANCE,IDF,GDP,MTQ,GUY,REU,MAY "
f"-merge-layers target=FRANCE,IDF,GDP,MTQ,GUY,REU,MAY force "
f"-rename-layers FRANCE_TRANSFORMED "
"-explode "
f"-o {output} "
# f"{fix_geo}"
)
Expand Down
1 change: 1 addition & 0 deletions cartiflette/pipeline/mapshaper_split_from_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def mapshaperize_split_from_s3(
if field in [
"FRANCE_ENTIERE",
"FRANCE_ENTIERE_DROM_RAPPROCHES",
"FRANCE_ENTIERE_IDF_DROM_RAPPROCHES",
]:
continue
try:
Expand Down
18 changes: 10 additions & 8 deletions cartiflette/pipeline_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
"REGION",
"TERRITOIRE",
"FRANCE_ENTIERE",
"FRANCE_ENTIERE_DROM_RAPPROCHES",
"FRANCE_ENTIERE_IDF_DROM_RAPPROCHES",
],
"ARRONDISSEMENT_MUNICIPAL": [
"BASSIN_VIE",
Expand All @@ -158,7 +158,7 @@
"REGION",
"TERRITOIRE",
"FRANCE_ENTIERE",
"FRANCE_ENTIERE_DROM_RAPPROCHES",
"FRANCE_ENTIERE_IDF_DROM_RAPPROCHES",
],
"COMMUNE": [
"BASSIN_VIE",
Expand All @@ -171,41 +171,41 @@
"REGION",
"TERRITOIRE",
"FRANCE_ENTIERE",
"FRANCE_ENTIERE_DROM_RAPPROCHES",
"FRANCE_ENTIERE_IDF_DROM_RAPPROCHES",
],
"EPCI": [
"DEPARTEMENT",
"REGION",
"TERRITOIRE",
"FRANCE_ENTIERE",
"FRANCE_ENTIERE_DROM_RAPPROCHES",
"FRANCE_ENTIERE_IDF_DROM_RAPPROCHES",
],
"EPT": [
"DEPARTEMENT",
"REGION",
"TERRITOIRE",
"FRANCE_ENTIERE",
# "FRANCE_ENTIERE_DROM_RAPPROCHES",
"FRANCE_ENTIERE_IDF_DROM_RAPPROCHES",
],
"CANTON": [
"DEPARTEMENT",
"REGION",
"TERRITOIRE",
"FRANCE_ENTIERE",
"FRANCE_ENTIERE_DROM_RAPPROCHES",
"FRANCE_ENTIERE_IDF_DROM_RAPPROCHES",
],
"ARRONDISSEMENT": [
"DEPARTEMENT",
"REGION",
"TERRITOIRE",
"FRANCE_ENTIERE",
"FRANCE_ENTIERE_DROM_RAPPROCHES",
"FRANCE_ENTIERE_IDF_DROM_RAPPROCHES",
],
"DEPARTEMENT": [
"REGION",
"TERRITOIRE",
"FRANCE_ENTIERE",
"FRANCE_ENTIERE_DROM_RAPPROCHES",
"FRANCE_ENTIERE_IDF_DROM_RAPPROCHES",
],
"REGION": [
"TERRITOIRE",
Expand All @@ -230,6 +230,7 @@
"AIRE_ATTRACTION_VILLES": [
"TERRITOIRE",
"FRANCE_ENTIERE",
"FRANCE_ENTIERE_DROM_RAPPROCHES",
],
}

Expand All @@ -256,6 +257,7 @@
differences = (all_borders ^ all_dissolutions) - {
"FRANCE_ENTIERE",
"FRANCE_ENTIERE_DROM_RAPPROCHES",
"FRANCE_ENTIERE_IDF_DROM_RAPPROCHES",
}
if differences:
raise ValueError(
Expand Down
22 changes: 16 additions & 6 deletions cartiflette/s3/geodataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ def bring_drom_closer(
self,
level_agreg: str = "DEPARTEMENT",
format_output: str = "geojson",
bring_out_idf: bool = True,
):
"""
Bring ultramarine territories closer to France. This method is executed
Expand All @@ -351,6 +352,8 @@ def bring_drom_closer(
format_output : str, optional
The desired output format (which will also be used for intermediate
files creation). The default is "geojson".
bring_out_idf : bool, optional
If True, will extract IdF and zoom on it. The default is True.
Returns
-------
Expand All @@ -360,6 +363,7 @@ def bring_drom_closer(

out = mapshaper_bring_closer(
input_file=f"{self.local_dir}/{self.main_filename}",
bring_out_idf=bring_out_idf,
output_dir=self.local_dir,
output_name="idf_combined",
output_format=format_output,
Expand Down Expand Up @@ -460,6 +464,7 @@ def create_downstream_geodatasets(
- dissolve geometries if init_geometry_level != dissolve_by
- bring ultramarine territories closer
if niveau_agreg == "FRANCE_ENTIERE_DROM_RAPPROCHES"
- extract IDF if niveau_agreg=="FRANCE_ENTIERE_IDF_DROM_RAPPROCHES"
- split the geodataset based on niveau_agreg
- project the geodataset into the given CRS
- convert the file into the chosen output
Expand Down Expand Up @@ -604,7 +609,10 @@ def create_downstream_geodatasets(
gdf = self.to_frame()
available_columns = gdf.columns.tolist()
by = self.find_column_name(dissolve_by, available_columns)
if niveau_agreg != "FRANCE_ENTIERE_DROM_RAPPROCHES":
if niveau_agreg not in (
"FRANCE_ENTIERE_DROM_RAPPROCHES",
"FRANCE_ENTIERE_IDF_DROM_RAPPROCHES",
):
aggreg_col = self.find_column_name(
niveau_agreg, available_columns
)
Expand Down Expand Up @@ -632,9 +640,6 @@ def create_downstream_geodatasets(
calc += ["IDF=max(IDF)"]

by_keys = [by, aggreg_col]
# if niveau_agreg == "FRANCE_ENTIERE_DROM_RAPPROCHES":
# # Hack to avoid zooming on whole area outside IdF
# by_keys.append("IDF")

self.dissolve(
by=by_keys,
Expand All @@ -644,11 +649,16 @@ def create_downstream_geodatasets(
)

# Bring ultramarine territories closer to France if needed
if niveau_agreg == "FRANCE_ENTIERE_DROM_RAPPROCHES":

if niveau_agreg in (
"FRANCE_ENTIERE_DROM_RAPPROCHES",
"FRANCE_ENTIERE_IDF_DROM_RAPPROCHES",
):
self.bring_drom_closer(
level_agreg=dissolve_by,
format_output=INTERMEDIATE_FORMAT,
bring_out_idf=(
niveau_agreg == "FRANCE_ENTIERE_IDF_DROM_RAPPROCHES"
),
)

# Split datasets, based on the desired "niveau_agreg" and proceed to
Expand Down
1 change: 1 addition & 0 deletions cartiflette/utils/dict_correspondance.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"ARRONDISSEMENT": re.compile("ARR"),
"FRANCE_ENTIERE": re.compile("PAYS"),
"FRANCE_ENTIERE_DROM_RAPPROCHES": re.compile("PAYS"),
"FRANCE_ENTIERE_IDF_DROM_RAPPROCHES": re.compile("PAYS"),
# "LIBELLE_REGION": re.compile("LIBELLE_REGION"),
# "LIBELLE_DEPARTEMENT": re.compile("LIBELLE_DEPARTEMENT"),
"BASSIN_VIE": re.compile("BV[0-9]{4}"),
Expand Down

0 comments on commit cacbef9

Please sign in to comment.