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
{"type":"FeatureCollection", "features": [
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[2.513513,48.850063],[2.513113,48.850328],[2.513266,48.850581],[2.513355,48.850666],[2.513376,48.850944],[2.513491,48.851144],[2.513462,48.85131],[2.51331,48.85144],[2.512261,48.852265],[2.511972,48.852527],[2.51147,48.852427],[2.511126,48.852562],[2.509891,48.852939],[2.508679,48.85336],[2.508459,48.853508],[2.508092,48.854092],[2.507918,48.854244],[2.50729,48.854636],[2.506665,48.855339],[2.506451,48.855635],[2.505535,48.857159],[2.505484,48.857212],[2.504572,48.857302],[2.504377,48.857279],[2.503232,48.857195],[2.502822,48.85695],[2.502567,48.856836],[2.500115,48.855779],[2.500003,48.855743],[2.499313,48.856548],[2.497638,48.856048],[2.497326,48.855961],[2.496819,48.855693],[2.496254,48.856534],[2.495936,48.857024],[2.495477,48.857826],[2.494887,48.858812],[2.497113,48.859192],[2.496733,48.859758],[2.496107,48.860663],[2.495507,48.860519],[2.495091,48.8604],[2.4944,48.860225],[2.494165,48.860556],[2.492936,48.860184],[2.492316,48.859956],[2.491577,48.859547],[2.491024,48.860797],[2.488697,48.860575],[2.488641,48.86081],[2.487736,48.860691],[2.487069,48.860662],[2.486703,48.860676],[2.486085,48.860763],[2.482061,48.86142],[2.481592,48.861487],[2.481402,48.861128],[2.480994,48.86031],[2.480807,48.860055],[2.480397,48.86006],[2.479542,48.860103],[2.479125,48.860107],[2.47899,48.860086],[2.478403,48.860098],[2.478188,48.860131],[2.476528,48.860472],[2.476222,48.860476],[2.47589,48.860418],[2.475351,48.860206],[2.474394,48.860203],[2.473615,48.860185],[2.473207,48.860201],[2.471088,48.860529],[2.470964,48.860568],[2.469865,48.860739],[2.469444,48.860774],[2.469198,48.860766],[2.468358,48.860668],[2.467728,48.860503],[2.467211,48.860266],[2.466752,48.860077],[2.466932,48.859817],[2.467104,48.859475],[2.467151,48.859075],[2.4671,48.858927],[2.467092,48.85855],[2.466947,48.858443],[2.467036,48.858021],[2.46728,48.857066],[2.467409,48.856626],[2.46572,48.85653],[2.464998,48.856454],[2.461438,48.856124],[2.458899,48.856024],[2.457814,48.855972],[2.457584,48.855933],[2.456723,48.855845],[2.455494,48.855693],[2.454463,48.855504],[2.4536,48.855325],[2.452691,48.855055],[2.451836,48.85472],[2.450966,48.854369],[2.450362,48.854106],[2.449176,48.853619],[2.448822,48.853456],[2.447895,48.852754],[2.447536,48.852536],[2.447895,48.852368],[2.447769,48.852156],[2.44714,48.851163],[2.446106,48.851437],[2.445252,48.851652],[2.444491,48.851816],[2.443632,48.85187],[2.443068,48.851802],[2.44292,48.851805],[2.442473,48.851898],[2.443292,48.852576],[2.442656,48.852873],[2.442062,48.852939],[2.441117,48.85302],[2.439749,48.853018],[2.438823,48.853028],[2.438589,48.853051],[2.437653,48.853217],[2.437344,48.852819],[2.435595,48.853279],[2.435233,48.853363],[2.434846,48.852425],[2.434635,48.851576],[2.434588,48.851281],[2.434578,48.850827],[2.432731,48.851126],[2.432349,48.851118],[2.431553,48.850926],[2.429576,48.851061],[2.429512,48.850618],[2.429401,48.849683],[2.429311,48.848745],[2.426436,48.848906],[2.424044,48.849058],[2.423164,48.849119],[2.422148,48.849177],[2.420389,48.849298],[2.41955,48.849366],[2.419004,48.849392],[2.417787,48.849348],[2.417537,48.849284],[2.416341,48.849234],[2.41635,48.8487],[2.416083,48.847258],[2.415971,48.846623],[2.415768,48.845445],[2.415664,48.844996],[2.415299,48.84385],[2.41485,48.842379],[2.414248,48.840488],[2.414047,48.83976],[2.413577,48.838278],[2.412852,48.836217],[2.412246,48.834539],[2.411472,48.833929],[2.412587,48.833779],[2.413505,48.833723],[2.413921,48.833712],[2.414633,48.833728],[2.41547,48.833794],[2.415968,48.833864],[2.416608,48.833997],[2.417338,48.834189],[2.41935,48.834832],[2.421016,48.835398],[2.422139,48.835798],[2.421801,48.836348],[2.421586,48.836756],[2.421447,48.837219],[2.421254,48.838161],[2.421062,48.838891],[2.42093,48.839329],[2.420719,48.839806],[2.420475,48.840314],[2.420304,48.840564],[2.420133,48.840753],[2.419776,48.841082],[2.419539,48.841449],[2.419394,48.841997],[2.419357,48.842498],[2.419469,48.842692],[2.419735,48.84294],[2.419849,48.843102],[2.41986,48.843456],[2.420115,48.843601],[2.421848,48.844437],[2.422225,48.84423],[2.422435,48.844058],[2.422653,48.843837],[2.422966,48.843417],[2.423283,48.842912],[2.423492,48.842656],[2.423948,48.842196],[2.424347,48.84195],[2.424535,48.841886],[2.42492,48.841808],[2.425819,48.841715],[2.427575,48.841571],[2.42813,48.841528],[2.43064,48.841299],[2.431533,48.841228],[2.432249,48.841161],[2.432916,48.841114],[2.433545,48.841046],[2.43367,48.841198],[2.435777,48.84102],[2.437184,48.840893],[2.437282,48.841326],[2.437958,48.844664],[2.439489,48.844521],[2.44052,48.84444],[2.440616,48.844892],[2.440801,48.844879],[2.440864,48.845252],[2.440681,48.845247],[2.440758,48.845964],[2.441832,48.845916],[2.444136,48.845878],[2.444921,48.845869],[2.446646,48.845802],[2.446497,48.844968],[2.446969,48.84494],[2.447853,48.844863],[2.448654,48.844781],[2.449527,48.844677],[2.450814,48.844502],[2.451866,48.844329],[2.452621,48.844217],[2.453503,48.844101],[2.455169,48.843919],[2.456779,48.843708],[2.458615,48.843428],[2.459873,48.843181],[2.461008,48.842938],[2.461794,48.842731],[2.462446,48.842532],[2.462848,48.84239],[2.463741,48.842023],[2.464181,48.841809],[2.464903,48.841406],[2.465411,48.841066],[2.465699,48.840836],[2.466164,48.840423],[2.466639,48.839925],[2.466929,48.839596],[2.467319,48.839099],[2.467787,48.838577],[2.468076,48.838303],[2.468547,48.837902],[2.469236,48.837289],[2.469473,48.837048],[2.469662,48.836788],[2.469803,48.836547],[2.469851,48.836399],[2.469834,48.835798],[2.469724,48.835195],[2.469483,48.834547],[2.469197,48.83412],[2.468891,48.833797],[2.468485,48.833507],[2.467882,48.833204],[2.467329,48.832952],[2.466939,48.832749],[2.466569,48.832521],[2.466273,48.832296],[2.465823,48.83187],[2.465575,48.831555],[2.465584,48.831471],[2.465426,48.83132],[2.465273,48.831018],[2.465077,48.830568],[2.464806,48.829684],[2.464702,48.829277],[2.46465,48.828956],[2.464608,48.828535],[2.464662,48.827623],[2.465095,48.827541],[2.465232,48.827667],[2.465434,48.827567],[2.466178,48.827328],[2.465731,48.826226],[2.465503,48.825586],[2.46532,48.824979],[2.465189,48.824957],[2.465299,48.824705],[2.465044,48.824084],[2.464026,48.822303],[2.463725,48.821678],[2.463403,48.821149],[2.462867,48.820178],[2.462697,48.819369],[2.462529,48.819246],[2.462696,48.81906],[2.461205,48.818355],[2.46053,48.818027],[2.459541,48.817571],[2.459115,48.817365],[2.459414,48.816848],[2.459119,48.816997],[2.458717,48.817034],[2.458054,48.81702],[2.455919,48.817082],[2.455102,48.817076],[2.453452,48.817149],[2.452526,48.817317],[2.451315,48.817619],[2.450558,48.817773],[2.449671,48.817893],[2.448935,48.81793],[2.447786,48.817966],[2.446618,48.81793],[2.445958,48.817928],[2.444713,48.817897],[2.444181,48.817875],[2.443731,48.817892],[2.442326,48.817904],[2.441855,48.817944],[2.441012,48.818093],[2.440137,48.818259],[2.439854,48.818291],[2.4394,48.81828],[2.437329,48.818177],[2.437442,48.819111],[2.437345,48.819187],[2.436925,48.819365],[2.436332,48.819543],[2.436028,48.819593],[2.434902,48.819669],[2.434655,48.819557],[2.434144,48.819216],[2.434262,48.819911],[2.434288,48.820158],[2.434108,48.820297],[2.433094,48.820991],[2.43248,48.821426],[2.43289,48.821693],[2.432634,48.821901],[2.432195,48.82162],[2.430371,48.822877],[2.430628,48.823157],[2.429861,48.823432],[2.429227,48.823622],[2.428768,48.823733],[2.427958,48.8239],[2.427421,48.823987],[2.426575,48.824097],[2.425481,48.824176],[2.424654,48.824191],[2.42345,48.824158],[2.422878,48.82413],[2.421826,48.824059],[2.420678,48.824048],[2.419963,48.82408],[2.419387,48.824156],[2.418603,48.824312],[2.417763,48.824531],[2.417131,48.824649],[2.416513,48.824736],[2.415976,48.824777],[2.415083,48.824782],[2.413785,48.824723],[2.413111,48.824718],[2.412623,48.824737],[2.411757,48.824803],[2.410907,48.82494],[2.41027,48.825087],[2.409955,48.825183],[2.409175,48.825502],[2.408675,48.825767],[2.408085,48.826112],[2.407589,48.82643],[2.40655,48.827191],[2.405816,48.827797],[2.405376,48.828134],[2.404994,48.828403],[2.403814,48.82906],[2.403551,48.829175],[2.402871,48.829415],[2.402237,48.82959],[2.400679,48.829095],[2.399775,48.828791],[2.399205,48.828626],[2.397894,48.828329],[2.396843,48.828054],[2.395707,48.827727],[2.395179,48.827645],[2.39434,48.827539],[2.393993,48.827401],[2.392762,48.826855],[2.391022,48.826117],[2.390072,48.825692],[2.393395,48.823426],[2.394949,48.822374],[2.395524,48.822001],[2.3968,48.821266],[2.397526,48.820894],[2.397956,48.820691],[2.399073,48.820258],[2.400406,48.819771],[2.402006,48.819233],[2.403732,48.818688],[2.405003,48.818262],[2.405798,48.81801],[2.406906,48.817698],[2.407645,48.817501],[2.408122,48.81734],[2.408554,48.817141],[2.409007,48.816872],[2.4094,48.816557],[2.409731,48.816509],[2.410701,48.816397],[2.412114,48.816287],[2.413576,48.816202],[2.414799,48.816183],[2.415669,48.816222],[2.415925,48.816248],[2.416896,48.814866],[2.417395,48.814237],[2.418054,48.813492],[2.418745,48.812822],[2.419368,48.81226],[2.419926,48.811813],[2.420928,48.811083],[2.421777,48.810314],[2.422232,48.809888],[2.422401,48.809761],[2.422576,48.809572],[2.423323,48.808555],[2.424333,48.807075],[2.424802,48.806329],[2.425525,48.804573],[2.42576,48.80397],[2.425945,48.803624],[2.426286,48.80276],[2.426716,48.801721],[2.427727,48.799039],[2.428621,48.796891],[2.428894,48.796325],[2.429569,48.79457],[2.430586,48.792217],[2.431339,48.790305],[2.432026,48.788619],[2.432598,48.787185],[2.432832,48.786619],[2.436026,48.786997],[2.435715,48.788237],[2.437358,48.788728],[2.439077,48.78919],[2.438474,48.79019],[2.438085,48.79079],[2.437849,48.791096],[2.43738,48.791658],[2.436774,48.792338],[2.438883,48.792943],[2.440912,48.793519],[2.44067,48.793722],[2.440405,48.794046],[2.440202,48.794366],[2.439842,48.795288],[2.441402,48.795692],[2.442387,48.795908],[2.442685,48.796297],[2.443967,48.798085],[2.445037,48.799289],[2.445442,48.799709],[2.445764,48.800811],[2.447345,48.802255],[2.446974,48.802455],[2.448651,48.803819],[2.448946,48.803623],[2.449432,48.803335],[2.449939,48.803007],[2.449968,48.803033],[2.45289,48.804937],[2.452757,48.805032],[2.454548,48.805961],[2.455233,48.805864],[2.455997,48.805694],[2.456603,48.805532],[2.456944,48.805399],[2.457464,48.805109],[2.457826,48.805268],[2.460323,48.806311],[2.460658,48.806058],[2.462682,48.806842],[2.463532,48.807144],[2.464012,48.80735],[2.46494,48.806678],[2.466949,48.805256],[2.468127,48.804563],[2.468487,48.803837],[2.468938,48.802057],[2.469678,48.80053],[2.47004,48.799756],[2.471406,48.795536],[2.47179,48.794306],[2.471886,48.793427],[2.471792,48.792515],[2.471885,48.79167],[2.472096,48.791069],[2.472383,48.790488],[2.473355,48.789218],[2.474117,48.788291],[2.474406,48.78797],[2.474626,48.787671],[2.4788,48.786019],[2.481789,48.785253],[2.48409,48.784702],[2.486348,48.784201],[2.487578,48.7841],[2.49089,48.784042],[2.493203,48.784075],[2.494368,48.784192],[2.495171,48.784303],[2.498244,48.784664],[2.499482,48.784715],[2.500391,48.784695],[2.506935,48.784161],[2.51023,48.784122],[2.51143,48.784358],[2.512445,48.784787],[2.513593,48.78565],[2.514226,48.785883],[2.516534,48.786268],[2.516758,48.786297],[2.516998,48.78638],[2.517743,48.786782],[2.518427,48.787173],[2.519014,48.787562],[2.519626,48.788057],[2.520144,48.788587],[2.520506,48.789011],[2.520772,48.789354],[2.521212,48.789972],[2.521568,48.790624],[2.521902,48.791261],[2.521987,48.791315],[2.522397,48.792044],[2.522673,48.79269],[2.522741,48.793131],[2.522959,48.793925],[2.523017,48.794263],[2.523282,48.795036],[2.523479,48.795727],[2.52353,48.796077],[2.523543,48.796382],[2.523477,48.796621],[2.523388,48.797564],[2.523181,48.798002],[2.523069,48.798659],[2.523133,48.798692],[2.523214,48.798948],[2.523235,48.799434],[2.523209,48.799589],[2.523089,48.79972],[2.523032,48.800056],[2.522992,48.800096],[2.522881,48.801534],[2.52289,48.802148],[2.522863,48.802308],[2.522636,48.803155],[2.522327,48.804172],[2.524243,48.804594],[2.524812,48.804728],[2.524936,48.804608],[2.525293,48.804713],[2.526203,48.804812],[2.526037,48.805081],[2.525995,48.805626],[2.525849,48.805879],[2.525516,48.806262],[2.525254,48.806618],[2.525038,48.806808],[2.525734,48.807135],[2.526004,48.806967],[2.526266,48.8067],[2.526484,48.806424],[2.526679,48.806259],[2.526796,48.806114],[2.527034,48.805632],[2.527107,48.805172],[2.528097,48.805231],[2.528813,48.805261],[2.528853,48.805568],[2.528896,48.806159],[2.530086,48.806082],[2.530162,48.80603],[2.532701,48.805994],[2.533075,48.805974],[2.533325,48.805812],[2.533762,48.8059],[2.534714,48.806069],[2.535324,48.806325],[2.535925,48.805858],[2.536751,48.805916],[2.53733,48.805968],[2.539375,48.806038],[2.539607,48.806063],[2.540802,48.805978],[2.541581,48.80589],[2.54269,48.805774],[2.543288,48.805722],[2.543532,48.806269],[2.543884,48.806104],[2.544377,48.805918],[2.544665,48.805668],[2.545128,48.805464],[2.545159,48.805336],[2.545344,48.805049],[2.54549,48.804906],[2.54573,48.804741],[2.546758,48.804186],[2.547136,48.804001],[2.547681,48.803812],[2.548175,48.803686],[2.549335,48.803467],[2.551025,48.803166],[2.551179,48.803267],[2.552295,48.803926],[2.552738,48.804091],[2.554012,48.804875],[2.554155,48.806011],[2.554181,48.806448],[2.554137,48.806674],[2.554029,48.806945],[2.553822,48.807325],[2.553787,48.807486],[2.553816,48.807604],[2.554212,48.808091],[2.554574,48.808107],[2.554818,48.80809],[2.556112,48.807906],[2.556563,48.807834],[2.557688,48.807628],[2.558213,48.807549],[2.558492,48.807575],[2.558461,48.807762],[2.559695,48.809063],[2.5606,48.810029],[2.561313,48.810818],[2.562057,48.8116],[2.562853,48.811696],[2.564094,48.812253],[2.56422,48.812323],[2.564814,48.812798],[2.565022,48.813213],[2.565087,48.813601],[2.565481,48.813974],[2.56518,48.81418],[2.564004,48.814855],[2.562845,48.815557],[2.562249,48.815957],[2.56097,48.816673],[2.560755,48.816747],[2.559668,48.817428],[2.557366,48.818766],[2.557903,48.819405],[2.556673,48.820425],[2.556054,48.82091],[2.556552,48.821174],[2.55907,48.822458],[2.55968,48.822773],[2.56053,48.822296],[2.560942,48.822106],[2.561644,48.821822],[2.562277,48.821524],[2.562574,48.821363],[2.563194,48.820914],[2.56382,48.820558],[2.564193,48.820479],[2.564776,48.820415],[2.565115,48.820334],[2.565918,48.820002],[2.566497,48.819699],[2.567026,48.819497],[2.567295,48.819415],[2.567574,48.819218],[2.56797,48.818816],[2.568255,48.818477],[2.568721,48.817959],[2.570999,48.822615],[2.572136,48.823412],[2.572415,48.823591],[2.569168,48.824084],[2.568749,48.824404],[2.568676,48.824436],[2.56738,48.824739],[2.566693,48.824954],[2.565,48.825461],[2.562806,48.826093],[2.561449,48.82648],[2.561309,48.82654],[2.560553,48.828945],[2.559074,48.829622],[2.558129,48.830035],[2.556767,48.831642],[2.556544,48.831537],[2.555409,48.83106],[2.555167,48.831246],[2.553836,48.832328],[2.553381,48.832668],[2.552744,48.832365],[2.552419,48.832626],[2.550682,48.833985],[2.550627,48.833954],[2.549164,48.835076],[2.549034,48.834971],[2.54829,48.83459],[2.547445,48.834172],[2.547462,48.834501],[2.547511,48.83487],[2.547479,48.835427],[2.547323,48.836107],[2.546797,48.835937],[2.545113,48.835436],[2.544565,48.835238],[2.544138,48.835005],[2.542601,48.835452],[2.541473,48.835799],[2.540172,48.836183],[2.538074,48.836883],[2.537807,48.837194],[2.53732,48.837691],[2.536667,48.838539],[2.536968,48.83862],[2.537909,48.838818],[2.539388,48.838935],[2.539971,48.839045],[2.540807,48.839363],[2.540219,48.839829],[2.539511,48.839806],[2.539153,48.839977],[2.537906,48.840828],[2.537562,48.840985],[2.537456,48.841064],[2.537443,48.841163],[2.537586,48.841368],[2.53756,48.841456],[2.537299,48.841677],[2.536046,48.842559],[2.535604,48.842971],[2.535561,48.843164],[2.535509,48.843669],[2.535276,48.844157],[2.534549,48.845327],[2.534425,48.845295],[2.531588,48.84443],[2.528762,48.843584],[2.527683,48.845135],[2.527054,48.845917],[2.525748,48.84768],[2.524657,48.849144],[2.524485,48.849085],[2.52413,48.849086],[2.523581,48.849004],[2.523564,48.8488],[2.523177,48.848692],[2.523033,48.848911],[2.52218,48.848711],[2.521574,48.848558],[2.520238,48.848199],[2.519949,48.848309],[2.519029,48.847966],[2.51902,48.84802],[2.517999,48.848985],[2.517141,48.849784],[2.516228,48.850649],[2.515592,48.85129],[2.514993,48.851013],[2.514541,48.850766],[2.514244,48.850583],[2.513513,48.850063]]]},"properties":null}
]}
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