Skip to content

Instantly share code, notes, and snippets.

@cquest
Last active October 22, 2023 14:53
Show Gist options
  • Save cquest/65a91eba35bf7979f53aa0d2e190ad75 to your computer and use it in GitHub Desktop.
Save cquest/65a91eba35bf7979f53aa0d2e190ad75 to your computer and use it in GitHub Desktop.
Conversion HTML arrêté "ZIPVA" en geojson
#! /usr/bin/python3
# script de conversion du code HTML de l'arrêté https://www.legifrance.gouv.fr/jorf/id/JORFTEXT000048234967
# au format geojson
# !!! MANQUE MURUROA non délimité dans le tableau d'origine !!!
import re, json
from bs4 import BeautifulSoup
cols = ['','immatriculation','communes','nom','dep','limites','ministere','zone_aero']
# regex pour extraire les coordonnées
dms = re.compile(r'(\d*)° (\d*)[\'′] ([\d,\.]*)["”] ?([NSEWO])')
geojson = {'type': 'FeatureCollection', 'features' : []}
with open('zipva.html','r') as html:
soup = BeautifulSoup(html, 'html.parser')
for tr in soup.find('table').find_all('tr'):
j = {}
g = {'type':'Polygon', 'coordinates':[]}
c = 0
for td in tr.find_all('td'):
c += 1
n = 0
for txt in td.strings:
if 'Zone' in txt:
if len(g['coordinates']) > 0:
# on termine le polygone avec le premier point
g['coordinates'].append(g['coordinates'][0])
g['coordinates'] = [g['coordinates']]
geojson['features'].append({'type':'Feature','properties': j,'geometry': g})
g = {'type':'Polygon', 'coordinates':[]}
n += 1
if cols[c] != 'limites': # properties
j[cols[c]] = txt
else:
point = []
for coord in re.findall(dms, txt.replace("''",'"')):
deg = round(float(coord[0]) + float(coord[1])/60 + float(coord[2].replace(',','.'))/3600,6)
if coord[3] in ['S','W','O']:
deg = -deg
point.append(deg)
if len(point) == 2:
g['coordinates'].append(point)
point = []
if len(g['coordinates'])>0:
# on termine le polygone avec le premier point
g['coordinates'].append(g['coordinates'][0])
g['coordinates'] = [g['coordinates']]
geojson['features'].append({'type':'Feature','properties': j,'geometry': g})
# dump du geojson final
print(json.dumps(geojson, ensure_ascii=False))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment