biommap/utils/geojson2sql.py

89 lines
3.8 KiB
Python

import geojson
import json
def convert_coordinates_to_geom(coordinates, geometry_type):
""" Returns a string with a valid PostGIS command for creating a geometry from an array of coordinates """
if geometry_type == '__POINT__': # TODO other cases
res = f'ST_GeomFromText(\'POINT({coordinates[0]} {coordinates[1]})\'),'
return res
def safe_json(string):
""" Encodes a string a json and escape single quote for postgresql"""
return json.dumps(string.replace("'", "''"))
def convert_feature_to_json(feature):
""" Returns a string with a json of properties """
res = f'"hauteur": {feature.properties["H"]},' if feature.properties["H"] else ''
res += f'"circonference": {feature.properties["CIRC"]},' if feature.properties["CIRC"] else ''
res += f'"etatsanitaire": {feature.properties["SANIT"]},' if feature.properties["SANIT"] else ''
res += f'"espece": {safe_json(feature.properties["SPFR"])},' if feature.properties["SPFR"] else ''
res += f'"remarques": {safe_json(feature.properties["COMMENTAIR"])},' if feature.properties["COMMENTAIR"] else ''
return '\'{' + res[:-1] + '}\','
def import_geosjon_as_sql(filename, sql_filename, schema_name, table_name, column_list, values_list):
""" Import a geojson """
with open(filename) as f:
g = geojson.load(f)
sql_f = open(sql_filename,'w')
for i,f in enumerate(g['features']):
#print(f)
sql = f'INSERT INTO "{schema_name}"."{table_name}" ('
col_name = ''
col_value = ''
for j,c in enumerate(column_list):
if values_list[j] == '__ID__':
col_name += f'"{c}",'
col_value += f'\'{1000000 + i}\','
elif values_list[j] == '__UUID__':
col_name += f'"{c}",'
col_value += 'uuid_generate_v4(),'
elif values_list[j] == '__NOW__':
col_name += f'"{c}",'
col_value += 'now()::timestamptz,'
elif values_list[j] == '__POINT__':
col_name += f'"{c}",'
col_value += convert_coordinates_to_geom(f.geometry.coordinates, values_list[j])
elif values_list[j] == '__JSON__':
col_name += f'"{c}",'
col_value += convert_feature_to_json(f)
elif values_list[j].startswith('properties.'):
col_name += f'"{c}",'
prop = values_list[j].split('.')[1]
col_value += f'\'{f.properties[prop]}\','
else:
col_name += f'"{c}",'
col_value += f'\'{values_list[j]}\','
sql += f'{col_name[:-1]}) VALUES ({col_value[:-1]});\n'
sql_f.write(sql)
sql_f.close()
if __name__ == '__main__':
# import arbres.geojson into gnc_sites.t_sites
filename = './gis/arbres.geojson'
sql_filename = './sql/arbres-sites.sql'
schema_name= 'gnc_sites'
table_name = 't_sites'
column_list = ['id_site', 'uuid_sinp', 'id_program', 'name', 'geom', 'timestamp_create', 'id_type', 'obs_txt']
values_list = ['__ID__', '__UUID__', '2', 'import arbres remarquables', '__POINT__', '__NOW__', '1', 'import' ]
import_geosjon_as_sql(filename, sql_filename, schema_name, table_name, column_list, values_list)
# import arbres.geojson into gnc_sites.t_visit
filename = './gis/arbres.geojson'
sql_filename = './sql/arbres-visits.sql'
schema_name= 'gnc_sites'
table_name = 't_visit'
column_list = ['id_site', 'json_data', 'timestamp_create', 'obs_txt', 'date']
values_list = ['__ID__', '__JSON__', '__NOW__', 'import', '__NOW__']
import_geosjon_as_sql(filename, sql_filename, schema_name, table_name, column_list, values_list)