From 82d87323ad2a3ba06a7f69374433a43ae4d22bfb Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Wed, 27 Sep 2023 21:35:02 +0200 Subject: Add new option --observation-format to configure observation file format. Default: GPKG. --- gis-observation-map | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/gis-observation-map b/gis-observation-map index 0f673fd..ac9baa7 100755 --- a/gis-observation-map +++ b/gis-observation-map @@ -67,6 +67,7 @@ parser.add_argument('--project-name', help='Project filename (and title)') parser.add_argument('--project-home', help='Project home directory') parser.add_argument('--geometry', nargs='*', default=[], help='Geometry filename(s)') parser.add_argument('--point', nargs='*', default=[], help=f'Coordinates of interest (X,Y in {target_srs.GetName()})') +parser.add_argument('--observation-format', default='GPKG', help='Format for the observation file (default: %(default)s)') args = parser.parse_args() @@ -365,10 +366,11 @@ def getObservations(taxonLists, taxonRedlistCategories, searchFilter): if 'crs' not in obs.keys(): print('WARN: GeoJSON output lacks CRS', file=sys.stderr) + layername = 'Observations' 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' + obs['name'] = layername 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): @@ -393,15 +395,20 @@ def getObservations(taxonLists, taxonRedlistCategories, searchFilter): print(f'WARN: #{tid} {k}: {properties[k]} → {v}', file=sys.stderr) properties[k] = v - #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') + drv = ogr.GetDriverByName(args.observation_format) + if drv.name == geojson_drv.name: + path = projectHome.joinpath(layername.lower() + '.geojson') + with path.open(mode='w') as fp: + json.dump(obs, fp, indent=2, ensure_ascii=False) + else: + with tempfile.NamedTemporaryFile(suffix='.geojson', mode='w') as fp: + json.dump(obs, fp) + fp.flush() + + suffix = drv.GetMetadataItem(gdal.DMD_EXTENSIONS).split(' ')[0] + path = projectHome.joinpath(layername.lower()).with_suffix('.' + suffix) + gdal.VectorTranslate(path.as_posix(), fp.name, reproject=True, dstSRS=target_srs, format=drv.name) + obs = None if projectInstance is not None: query = '''"IsPositiveObservation" AND "IsNaturalOccurrence" AND "CoordinateUncertaintyInMeters" <= 250''' -- cgit v1.2.3