2021-09-13 14:38:41 +00:00
|
|
|
import geojson
|
2021-09-15 08:57:31 +00:00
|
|
|
import json
|
2021-09-13 14:38:41 +00:00
|
|
|
|
|
|
|
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 """
|
2021-09-15 08:57:31 +00:00
|
|
|
if geometry_type == '__POINT__': # TODO other cases
|
2021-09-13 14:38:41 +00:00
|
|
|
res = f'ST_GeomFromText(\'POINT({coordinates[0]} {coordinates[1]})\'),'
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
2021-09-15 08:57:31 +00:00
|
|
|
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] + '}\','
|
2021-09-13 14:38:41 +00:00
|
|
|
|
|
|
|
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')
|
|
|
|
|
2021-09-15 08:57:31 +00:00
|
|
|
for i,f in enumerate(g['features']):
|
2021-09-13 14:38:41 +00:00
|
|
|
#print(f)
|
|
|
|
sql = f'INSERT INTO "{schema_name}"."{table_name}" ('
|
|
|
|
|
|
|
|
col_name = ''
|
|
|
|
col_value = ''
|
2021-09-15 08:57:31 +00:00
|
|
|
for j,c in enumerate(column_list):
|
2021-09-13 14:38:41 +00:00
|
|
|
|
2021-09-15 08:57:31 +00:00
|
|
|
if values_list[j] == '__ID__':
|
|
|
|
col_name += f'"{c}",'
|
|
|
|
col_value += f'\'{1000000 + i}\','
|
|
|
|
elif values_list[j] == '__UUID__':
|
2021-09-13 14:38:41 +00:00
|
|
|
col_name += f'"{c}",'
|
|
|
|
col_value += 'uuid_generate_v4(),'
|
2021-09-15 08:57:31 +00:00
|
|
|
elif values_list[j] == '__NOW__':
|
2021-09-13 14:38:41 +00:00
|
|
|
col_name += f'"{c}",'
|
|
|
|
col_value += 'now()::timestamptz,'
|
2021-09-15 08:57:31 +00:00
|
|
|
elif values_list[j] == '__POINT__':
|
2021-09-13 14:38:41 +00:00
|
|
|
col_name += f'"{c}",'
|
2021-09-15 08:57:31 +00:00
|
|
|
col_value += convert_coordinates_to_geom(f.geometry.coordinates, values_list[j])
|
|
|
|
elif values_list[j] == '__JSON__':
|
2021-09-13 14:38:41 +00:00
|
|
|
col_name += f'"{c}",'
|
2021-09-15 08:57:31 +00:00
|
|
|
col_value += convert_feature_to_json(f)
|
|
|
|
elif values_list[j].startswith('properties.'):
|
|
|
|
col_name += f'"{c}",'
|
|
|
|
prop = values_list[j].split('.')[1]
|
2021-09-13 14:38:41 +00:00
|
|
|
col_value += f'\'{f.properties[prop]}\','
|
|
|
|
else:
|
|
|
|
col_name += f'"{c}",'
|
2021-09-15 08:57:31 +00:00
|
|
|
col_value += f'\'{values_list[j]}\','
|
2021-09-13 14:38:41 +00:00
|
|
|
|
|
|
|
sql += f'{col_name[:-1]}) VALUES ({col_value[:-1]});\n'
|
|
|
|
|
|
|
|
sql_f.write(sql)
|
|
|
|
|
|
|
|
sql_f.close()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-09-15 08:57:31 +00:00
|
|
|
# import arbres.geojson into gnc_sites.t_sites
|
2021-09-13 14:38:41 +00:00
|
|
|
filename = './gis/arbres.geojson'
|
2021-09-15 08:57:31 +00:00
|
|
|
sql_filename = './sql/arbres-sites.sql'
|
2021-09-13 14:38:41 +00:00
|
|
|
schema_name= 'gnc_sites'
|
|
|
|
table_name = 't_sites'
|
2021-09-15 08:57:31 +00:00
|
|
|
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' ]
|
2021-09-13 14:38:41 +00:00
|
|
|
import_geosjon_as_sql(filename, sql_filename, schema_name, table_name, column_list, values_list)
|
|
|
|
|
2021-09-15 08:57:31 +00:00
|
|
|
# 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)
|