From de3ee399872500903f5c7aeedc45e13bccf7235c Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Wed, 27 Sep 2023 20:40:26 +0200 Subject: Convert GeoJSON to GPKG and reproject to EPSG:3006. --- gis-observation-map | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'gis-observation-map') diff --git a/gis-observation-map b/gis-observation-map index 481dd3a..0f673fd 100755 --- a/gis-observation-map +++ b/gis-observation-map @@ -24,6 +24,7 @@ import json import re import requests import configparser +import tempfile from requests.exceptions import HTTPError from xdg import xdg_config_home from pathlib import Path @@ -366,6 +367,8 @@ def getObservations(taxonLists, taxonRedlistCategories, searchFilter): if obs['type'] == 'FeatureCollection' and 'features' in obs.keys() and type(obs['features']) == list: print(f'{len(obs["features"])} observations found', file=sys.stderr) + if 'name' not in obs.keys() or obs['name'] is None: + obs['name'] = 'Observations' for feat in obs['features']: if (type(feat) != dict or 'type' not in feat.keys() or feat['type'] != 'Feature' or 'properties' not in feat.keys() or type(feat['properties']) != dict): @@ -390,10 +393,15 @@ def getObservations(taxonLists, taxonRedlistCategories, searchFilter): print(f'WARN: #{tid} {k}: {properties[k]} → {v}', file=sys.stderr) properties[k] = v - # TODO generate GPKG instead and reproject to EPSG:3006 - path = projectHome.joinpath('observations.geojson') - with path.open(mode='w') as fp: - json.dump(obs, fp, indent=2, ensure_ascii=False) + #path = projectHome.joinpath('observations.geojson') + #with path.open(mode='w') as fp: + # json.dump(obs, fp, indent=2, ensure_ascii=False) + + path = projectHome.joinpath('observations.gpkg') + with tempfile.NamedTemporaryFile(suffix='.geojson', mode='w') as fp: + json.dump(obs, fp) + fp.flush() + gdal.VectorTranslate(path.as_posix(), fp.name, reproject=True, dstSRS=target_srs, format='GPKG') if projectInstance is not None: query = '''"IsPositiveObservation" AND "IsNaturalOccurrence" AND "CoordinateUncertaintyInMeters" <= 250''' -- cgit v1.2.3