From cacbef9f59d728d4d5ab25d9ec83b163c4686d9a Mon Sep 17 00:00:00 2001 From: "thomas.grandjean" Date: Fri, 18 Oct 2024 21:41:01 +0200 Subject: [PATCH] create FRANCE_ENTIERE_IDF_DROM_RAPPROCHES --- cartiflette/mapshaper/mapshaper_closer.py | 40 +++++++++++-------- .../pipeline/mapshaper_split_from_s3.py | 1 + cartiflette/pipeline_constants.py | 18 +++++---- cartiflette/s3/geodataset.py | 22 +++++++--- cartiflette/utils/dict_correspondance.py | 1 + 5 files changed, 52 insertions(+), 30 deletions(-) diff --git a/cartiflette/mapshaper/mapshaper_closer.py b/cartiflette/mapshaper/mapshaper_closer.py index 7bc99fa..f845b62 100644 --- a/cartiflette/mapshaper/mapshaper_closer.py +++ b/cartiflette/mapshaper/mapshaper_closer.py @@ -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 = { @@ -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", @@ -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 @@ -102,14 +105,6 @@ 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 " @@ -117,7 +112,16 @@ def mapshaper_bring_closer( 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) @@ -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} " @@ -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}" ) diff --git a/cartiflette/pipeline/mapshaper_split_from_s3.py b/cartiflette/pipeline/mapshaper_split_from_s3.py index b9ba6cf..e55dc4f 100644 --- a/cartiflette/pipeline/mapshaper_split_from_s3.py +++ b/cartiflette/pipeline/mapshaper_split_from_s3.py @@ -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: diff --git a/cartiflette/pipeline_constants.py b/cartiflette/pipeline_constants.py index 14c45d9..5133914 100644 --- a/cartiflette/pipeline_constants.py +++ b/cartiflette/pipeline_constants.py @@ -145,7 +145,7 @@ "REGION", "TERRITOIRE", "FRANCE_ENTIERE", - "FRANCE_ENTIERE_DROM_RAPPROCHES", + "FRANCE_ENTIERE_IDF_DROM_RAPPROCHES", ], "ARRONDISSEMENT_MUNICIPAL": [ "BASSIN_VIE", @@ -158,7 +158,7 @@ "REGION", "TERRITOIRE", "FRANCE_ENTIERE", - "FRANCE_ENTIERE_DROM_RAPPROCHES", + "FRANCE_ENTIERE_IDF_DROM_RAPPROCHES", ], "COMMUNE": [ "BASSIN_VIE", @@ -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", @@ -230,6 +230,7 @@ "AIRE_ATTRACTION_VILLES": [ "TERRITOIRE", "FRANCE_ENTIERE", + "FRANCE_ENTIERE_DROM_RAPPROCHES", ], } @@ -256,6 +257,7 @@ differences = (all_borders ^ all_dissolutions) - { "FRANCE_ENTIERE", "FRANCE_ENTIERE_DROM_RAPPROCHES", + "FRANCE_ENTIERE_IDF_DROM_RAPPROCHES", } if differences: raise ValueError( diff --git a/cartiflette/s3/geodataset.py b/cartiflette/s3/geodataset.py index 8bd05c6..20f8828 100644 --- a/cartiflette/s3/geodataset.py +++ b/cartiflette/s3/geodataset.py @@ -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 @@ -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 ------- @@ -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, @@ -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 @@ -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 ) @@ -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, @@ -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 diff --git a/cartiflette/utils/dict_correspondance.py b/cartiflette/utils/dict_correspondance.py index 0e884d7..b19a7db 100644 --- a/cartiflette/utils/dict_correspondance.py +++ b/cartiflette/utils/dict_correspondance.py @@ -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}"),