Skip to content

Instantly share code, notes, and snippets.

@wetneb
Last active May 22, 2020 12:42
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 wetneb/a39aaacb4526e67b93580185fc372320 to your computer and use it in GitHub Desktop.
Save wetneb/a39aaacb4526e67b93580185fc372320 to your computer and use it in GitHub Desktop.
Affigeo PEI geojson to OSM converter
#!/bin/env python3
# Ce script nécessite l'installation de "dateparser":
# pip install dateparser
import json, sys, dateparser
# Adaptez ces lignes pour changer la source des données
source = 'SDIS 71'
sdis_id_key = 'ref:SDIS71'
def translate_properties(orig):
orig = dict((key.lower(), value) for key, value in orig.items())
type_pei = orig.get('type_pei')
source_pei = orig.get('source_pei')
diam_pei = orig.get('diam_pei')
press_stat = orig.get('press_stat')
volume = orig.get('volume')
ref_terr = orig.get('ref_terr')
date_mes = orig.get('date_mes')
date_ro = orig.get('date_ro')
nom_gest = orig.get('nom_gest')
id_sdis = orig.get('id_sdis')
osm = {
'emergency': 'fire_hydrant',
}
## TYPE
if type_pei == 'PI':
osm['fire_hydrant:type'] = 'pillar'
elif type_pei == 'BI':
# could be "fire_hydrant:type" = "wall", "pipe" or "underground"
pass
elif type_pei == 'CI':
osm['water_source'] = 'water_tank'
## SOURCE
if source_pei == 'piscine':
osm['water_source'] = 'swimming_pool'
elif source_pei == 'puits':
osm['water_source'] = 'well'
elif source_pei == 'cours_eau':
# could be river, stream or canal
pass
elif source_pei == 'reseau_irrigation':
osm['water_source'] = 'groundwater'
elif source_pei == 'reseau_aep':
osm['water_source'] = 'main'
elif source_pei == 'citerne':
osm['water_source'] = 'water_tank'
## DIAMETER
if diam_pei:
osm['fire_hydrant:diameter'] = diam_pei
## PRESSURE
if press_stat:
osm['pressure'] = press_stat
## VOLUME
if volume:
osm['water_volume'] = volume
## REF
if ref_terr:
osm['ref'] = str(ref_terr).replace('.0', '')
## START_DATE
if date_mes:
osm['start_date'] = dateparser.parse(date_mes).date().isoformat()
## SURVEY DATE
if date_ro:
osm['survey:date'] = dateparser.parse(date_ro).date().isoformat()
## OPERATOR
if nom_gest:
osm['operator'] = nom_gest
## DISUSED
if str(orig.get('disponible')) == '0':
del osm['emergency']
osm['disused:emergency'] = 'fire_hydrant'
## SDIS ID
if id_sdis and sdis_id_key:
osm[sdis_id_key] = id_sdis
if source:
osm['source'] = source
return osm
if __name__ == '__main__':
if len(sys.argv) <= 1:
print('Utilisation: {} fichier.geojson'.format(sys.argv[0]))
print('Le fichier Geojson osm-ifié créé est fichier.osm.geojson')
exit(1)
input_filename = sys.argv[1]
output_filename = '.'.join(input_filename.split('.')[:-1]) + '.osm.geojson'
with open(input_filename, 'r') as input_file:
geojson = json.load(input_file)
for feature in geojson['features']:
feature['properties'] = translate_properties(feature['properties'])
with open(output_filename, 'w') as output_file:
json.dump(geojson, output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment