#17 Add possibility to import ARHEM haies remarquables

This commit is contained in:
nobohan 2021-09-15 11:46:55 +02:00
parent 3ed4872b60
commit 177a54a444
3 changed files with 57 additions and 15 deletions

View File

@ -17,7 +17,7 @@ $ source bin/activate
## Import of the layer Arbres remarquables ## Import of the layer Arbres remarquables
- This data is downloadable by <http://geoportail.wallonie.be/catalogue/d594f5a3-34ac-4cc2-a357-aae5d5263f35.html> - This data is downloadable by <http://geoportail.wallonie.be/catalogue/d594f5a3-34ac-4cc2-a357-aae5d5263f35.html>
- The layer MUST be converted to a valid geojson format in WGS84 coordinates. - The layer MUST be converted to a valid geojson format in WGS84 coordinates. You can use QGIS to do so.
- Then simply run the script `geojson2sql` to produce a SQL file with some INSERT instructions. - Then simply run the script `geojson2sql` to produce a SQL file with some INSERT instructions.
```bash ```bash
@ -42,6 +42,9 @@ Some values in these list MUST follow some special conventions:
- the "__POINT__" value will be converted to a PostGIS ST_GeomFromText formula based on the coordinates of the geojson file - the "__POINT__" value will be converted to a PostGIS ST_GeomFromText formula based on the coordinates of the geojson file
- the "__NOW__" value will be converted to a formula that creates a timestamp - the "__NOW__" value will be converted to a formula that creates a timestamp
Furthermore, the `id_program` and `id_type` MUST match the correct values! The first field will decide which program the sites will be attached to.
Lastly, if the values starts with `properties.`, it will be mapped to a property in the geojson. E.g, `properties.COMMENTAIR`. Lastly, if the values starts with `properties.`, it will be mapped to a property in the geojson. E.g, `properties.COMMENTAIR`.
For populating the table of visits, the same function is applied. But there is another special convention that applies for building the json details of the visit: the "__JSON__" value will be converted to a dedicated formula that will build the json object in the database. For populating the table of visits, the same function is applied. But there is another special convention that applies for building the json details of the visit: the "__JSON__" value will be converted to a dedicated formula that will build the json object in the database.

View File

@ -2,25 +2,33 @@ import geojson
import json import json
def convert_coordinates_to_geom(coordinates, geometry_type): 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 """ """ Returns a string with a valid PostGIS command for creating a WGS84 geometry from an array of coordinates """
if geometry_type == '__POINT__': # TODO other cases if geometry_type == '__POINT__':
res = f'ST_GeomFromText(\'POINT({coordinates[0]} {coordinates[1]})\'),' res = f'ST_GeomFromText(\'POINT({coordinates[0]} {coordinates[1]})\', 4326),'
if geometry_type == '__LINESTRING__': # TODO POLYGON
s = ''
for c in coordinates[0]:
s += f'{c[0]} {c[1]},'
res = f'ST_GeomFromText(\'LINESTRING({s[:-1]})\', 4326),'
return res return res
def safe_json(string): def safe_json(string):
""" Encodes a string a json and escape single quote for postgresql""" """ Encodes a string a json and escape single quote for postgresql"""
return json.dumps(string.replace("'", "''")) return json.dumps(string.replace("'", "''"))
def convert_feature_to_json(feature): def convert_feature_to_json(feature, program_name):
""" Returns a string with a json of properties """ """ Returns a string with a json of properties """
res = f'"hauteur": {feature.properties["H"]},' if feature.properties["H"] else '' 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'"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 '' res += f'"remarques": {safe_json(feature.properties["COMMENTAIR"])},' if feature.properties["COMMENTAIR"] else ''
if program_name == 'ARHEM_ARBRES':
res += f'"circonference": {feature.properties["CIRC"]},' if feature.properties["CIRC"] else ''
res += f'"espece": {safe_json(feature.properties["SPFR"])},' if feature.properties["SPFR"] else ''
elif program_name == 'ARHEM_HAIES':
res += f'"espece_1": {safe_json(feature.properties["SPFR"])},' if feature.properties["SPFR"] else ''
return '\'{' + res[:-1] + '}\',' return '\'{' + res[:-1] + '}\','
def import_geosjon_as_sql(filename, sql_filename, schema_name, table_name, column_list, values_list): def import_geosjon_as_sql(filename, sql_filename, schema_name, table_name, column_list, values_list):
@ -31,28 +39,33 @@ def import_geosjon_as_sql(filename, sql_filename, schema_name, table_name, colum
sql_f = open(sql_filename,'w') sql_f = open(sql_filename,'w')
for i,f in enumerate(g['features']): for i,f in enumerate(g['features']):
#print(f)
sql = f'INSERT INTO "{schema_name}"."{table_name}" (' sql = f'INSERT INTO "{schema_name}"."{table_name}" ('
col_name = '' col_name = ''
col_value = '' col_value = ''
for j,c in enumerate(column_list): for j,c in enumerate(column_list):
if values_list[j] == '__ID__': if values_list[j] == '__ID1000000__':
col_name += f'"{c}",' col_name += f'"{c}",'
col_value += f'\'{1000000 + i}\',' col_value += f'\'{1000000 + i}\','
elif values_list[j] == '__ID2000000__':
col_name += f'"{c}",'
col_value += f'\'{2000000 + i}\','
elif values_list[j] == '__UUID__': elif values_list[j] == '__UUID__':
col_name += f'"{c}",' col_name += f'"{c}",'
col_value += 'uuid_generate_v4(),' col_value += 'uuid_generate_v4(),'
elif values_list[j] == '__NOW__': elif values_list[j] == '__NOW__':
col_name += f'"{c}",' col_name += f'"{c}",'
col_value += 'now()::timestamptz,' col_value += 'now()::timestamptz,'
elif values_list[j] == '__POINT__': elif values_list[j] == '__POINT__' or values_list[j] == '__LINESTRING__' :
col_name += f'"{c}",' col_name += f'"{c}",'
col_value += convert_coordinates_to_geom(f.geometry.coordinates, values_list[j]) col_value += convert_coordinates_to_geom(f.geometry.coordinates, values_list[j])
elif values_list[j] == '__JSON__': elif values_list[j] == '__JSON_ARHEM_ARBRES__':
col_name += f'"{c}",' col_name += f'"{c}",'
col_value += convert_feature_to_json(f) col_value += convert_feature_to_json(f, 'ARHEM_ARBRES')
elif values_list[j] == '__JSON_ARHEM_HAIES__':
col_name += f'"{c}",'
col_value += convert_feature_to_json(f, 'ARHEM_HAIES')
elif values_list[j].startswith('properties.'): elif values_list[j].startswith('properties.'):
col_name += f'"{c}",' col_name += f'"{c}",'
prop = values_list[j].split('.')[1] prop = values_list[j].split('.')[1]
@ -75,7 +88,7 @@ if __name__ == '__main__':
schema_name= 'gnc_sites' schema_name= 'gnc_sites'
table_name = 't_sites' table_name = 't_sites'
column_list = ['id_site', 'uuid_sinp', 'id_program', 'name', 'geom', 'timestamp_create', 'id_type', 'obs_txt'] 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' ] values_list = ['__ID1000000__', '__UUID__', '1', 'import arbres remarquables', '__POINT__', '__NOW__', '1', 'import' ]
import_geosjon_as_sql(filename, sql_filename, schema_name, table_name, column_list, values_list) import_geosjon_as_sql(filename, sql_filename, schema_name, table_name, column_list, values_list)
# import arbres.geojson into gnc_sites.t_visit # import arbres.geojson into gnc_sites.t_visit
@ -84,5 +97,23 @@ if __name__ == '__main__':
schema_name= 'gnc_sites' schema_name= 'gnc_sites'
table_name = 't_visit' table_name = 't_visit'
column_list = ['id_site', 'json_data', 'timestamp_create', 'obs_txt', 'date'] column_list = ['id_site', 'json_data', 'timestamp_create', 'obs_txt', 'date']
values_list = ['__ID__', '__JSON__', '__NOW__', 'import', '__NOW__'] values_list = ['__ID1000000__', '__JSON_ARHEM_ARBRES__', '__NOW__', 'import', '__NOW__']
import_geosjon_as_sql(filename, sql_filename, schema_name, table_name, column_list, values_list)
# import haies.geojson into gnc_sites.t_sites
filename = './gis/haies.geojson'
sql_filename = './sql/haies-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 = ['__ID2000000__', '__UUID__', '3', 'import haies remarquables', '__LINESTRING__', '__NOW__', '3', 'import' ]
import_geosjon_as_sql(filename, sql_filename, schema_name, table_name, column_list, values_list)
# import haies.geojson into gnc_sites.t_visit
filename = './gis/haies.geojson'
sql_filename = './sql/haies-visits.sql'
schema_name= 'gnc_sites'
table_name = 't_visit'
column_list = ['id_site', 'json_data', 'timestamp_create', 'obs_txt', 'date']
values_list = ['__ID2000000__', '__JSON_ARHEM_HAIES__', '__NOW__', 'import', '__NOW__']
import_geosjon_as_sql(filename, sql_filename, schema_name, table_name, column_list, values_list) import_geosjon_as_sql(filename, sql_filename, schema_name, table_name, column_list, values_list)

8
utils/gis/haies.geojson Normal file
View File

@ -0,0 +1,8 @@
{
"type": "FeatureCollection",
"name": "haies",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "OBJECTID": 2127.0, "INSLOC": 64075, "SITEAR": 2, "AR": 1, "TYPE": 602, "ADRESSE": "RUE DU CHAMP DES OISEAUX - AMBRESINEAU", "DOMAINE": "P", "ARBRHAIE": "A", "DATEOFF": "20130222000000", "SANIT": null, "TRAD": null, "CIRC": 100, "H": 20, "ANOBS": 1992, "INTERET": "D,P", "ENV1": "A", "ENV2": "V", "REF": null, "COMMENTAIR": "5 sujets.", "EVOLUTION": null, "PHOTO": null, "ETAPE": "O", "GENRE": "Corylus", "SP": "colurna", "CULTIVAR": null, "SPFR": "Noisetier de Byzance" }, "geometry": { "type": "MultiLineString", "coordinates": [ [ [ 5.030018725042887, 50.625644805470124 ], [ 5.030077773870591, 50.625760715854604 ], [ 5.030080893503914, 50.625983281318334 ], [ 5.030017972700604, 50.626248619803363 ] ] ] } }
]
}