Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Last active September 26, 2023 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasG77/8bb59f04ed1f2e4ae0bafeb5d0126310 to your computer and use it in GitHub Desktop.
Save ThomasG77/8bb59f04ed1f2e4ae0bafeb5d0126310 to your computer and use it in GitHub Desktop.
Generation EPT 2023 "one-shot"

Compiler un EPT (établissements publics territoriaux)/des EPT

Les établissements publics territoriaux (EPT) sont parmis les "EPCI sans fiscalité propre".

Source des données

Fichiers résultats

"One-shot" pour un EPT en bash, avec mapshaper, jq et wget installé

list_insee=$(echo "94017
94015
94018
94033
94042
94058
94046
94052
94067
94068
94069
94079
94080
")

for i in $list_insee;
  do wget -O commune-${i}.geojson "https://geo.api.gouv.fr/communes/$i?format=geojson&geometry=contour";
done;

jq --slurp '{"type": "FeatureCollection","features": .}' commune-*.geojson >| communes-all.geojson

mapshaper communes-all.geojson -dissolve -o format=geojson geojson-type=FeatureCollection ept-200057941.geojson

Version généralisée à tous les EPT

Utilise Python et les bibliothèques associées Pandas et Geopandas

import urllib
import geopandas as gpd
import pandas as pd

insee_remote_url = "https://www.insee.fr/fr/statistiques/fichier/2510634/ept_au_01-01-2023.xlsx"
insee_filename = insee_remote_url.split('/')[-1]
urllib.request.urlretrieve(insee_remote_url, insee_filename)

from openpyxl.styles.colors import WHITE, RGB
__old_rgb_set__ = RGB.__set__

def __rgb_set_fixed__(self, instance, value):
    try:
        __old_rgb_set__(self, instance, value)
    except ValueError as e:
        if e.args[0] == 'Colors must be aRGB hex values':
            __old_rgb_set__(self, instance, WHITE)  # Change color here

RGB.__set__ = __rgb_set_fixed__

xl = pd.ExcelFile(insee_filename)
xl.sheet_names  # see all sheet names
df_composition_communale = xl.parse("Composition_communale", header=5)
df_composition_communale_short = df_composition_communale[['CODGEO', 'EPT', 'LIBEPT']]
df_composition_communale_short['CODGEO']= df_composition_communale_short['CODGEO'].astype(str)

gdf_communes = gpd.read_file("http://etalab-datasets.geo.data.gouv.fr/contours-administratifs/2023/geojson/communes-5m.geojson")

df_merged = communes.merge(df_composition_communale_short, left_on="code", right_on="CODGEO")
df_merged_short = df_merged[['geometry', 'CODGEO', 'EPT', 'LIBEPT']]
ept_zones = df_merged_short.dissolve(by=['EPT', 'LIBEPT'])
ept_zones.to_file("ept_2023.geojson", driver="GeoJSON")
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment