import external data and qualification (#17): improvements
This commit is contained in:
+43
-16
@@ -1,13 +1,27 @@
|
||||
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':
|
||||
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 """
|
||||
@@ -16,30 +30,36 @@ def import_geosjon_as_sql(filename, sql_filename, schema_name, table_name, colum
|
||||
|
||||
sql_f = open(sql_filename,'w')
|
||||
|
||||
for f in g['features']:
|
||||
for i,f in enumerate(g['features']):
|
||||
#print(f)
|
||||
sql = f'INSERT INTO "{schema_name}"."{table_name}" ('
|
||||
|
||||
col_name = ''
|
||||
col_value = ''
|
||||
for i,c in enumerate(column_list):
|
||||
for j,c in enumerate(column_list):
|
||||
|
||||
if values_list[i] == 'UUID':
|
||||
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[i] == 'NOW':
|
||||
elif values_list[j] == '__NOW__':
|
||||
col_name += f'"{c}",'
|
||||
col_value += 'now()::timestamptz,'
|
||||
elif c == 'geom':
|
||||
elif values_list[j] == '__POINT__':
|
||||
col_name += f'"{c}",'
|
||||
col_value += convert_coordinates_to_geom(f.geometry.coordinates, values_list[i])
|
||||
elif values_list[i].startswith('properties.'):
|
||||
col_value += convert_coordinates_to_geom(f.geometry.coordinates, values_list[j])
|
||||
elif values_list[j] == '__JSON__':
|
||||
col_name += f'"{c}",'
|
||||
prop = values_list[i].split('.')[1]
|
||||
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[i]}\','
|
||||
col_value += f'\'{values_list[j]}\','
|
||||
|
||||
sql += f'{col_name[:-1]}) VALUES ({col_value[:-1]});\n'
|
||||
|
||||
@@ -49,13 +69,20 @@ def import_geosjon_as_sql(filename, sql_filename, schema_name, table_name, colum
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
# import arbres.geojson into gnc_sites.t_sites
|
||||
filename = './gis/arbres.geojson'
|
||||
sql_filename = 'arbres.sql'
|
||||
sql_filename = './sql/arbres-sites.sql'
|
||||
schema_name= 'gnc_sites'
|
||||
table_name = 't_sites'
|
||||
column_list = ['uuid_sinp', 'id_program', 'name', 'geom', 'timestamp_create', 'id_type', 'obs_txt']
|
||||
values_list = ['UUID', '2', 'import arbres remarquables', 'POINT', 'NOW', '1', 'import' ]
|
||||
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)
|
||||
|
||||
# add CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; ??
|
||||
# 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)
|
||||
|
||||
Reference in New Issue
Block a user