feat(OGRProvider): allow to use 'geom_field' in the config for OGR pr… (#1141)

* feat(OGRProvider): allow to use 'geom_field' in the config for OGR provider

* docs: show an example of using OGR for publishing data from a PostGIS database

* fix: does not check for empty geometry in this PR

* docs: add back ESRIJSON source_type
This commit is contained in:
Mathieu Tachon
2023-02-15 01:29:09 +01:00
committed by GitHub
parent 74ddb1215d
commit 70804d22fa
2 changed files with 23 additions and 1 deletions
@@ -195,6 +195,20 @@ The OGR provider requires a recent (3+) version of GDAL to be installed.
id_field: BGS_ID
layer: ESRIJSON
.. code-block:: yaml
providers:
- type: feature
name: OGR
data:
source_type: PostgreSQL
source: "PG: host=127.0.0.1 dbname=test user=postgres password=postgres"
source_srs: EPSG:4326
target_srs: EPSG:4326 # Can be used to transform/reproject the data
id_field: osm_id
layer: osm.hotosm_bdi_waterways # Value follows a 'my_schema.my_table' structure
geom_field: foo_geom
MongoDB
+9 -1
View File
@@ -32,6 +32,7 @@
import functools
import importlib
import json
import logging
import os
from typing import Any
@@ -171,6 +172,8 @@ class OGRProvider(BaseProvider):
self._load_source_helper(self.data_def['source_type'])
self.geom_field = provider_def.get('geom_field')
# ID field is required
self.id_field = provider_def.get('id_field')
if not self.id_field:
@@ -461,7 +464,10 @@ class OGRProvider(BaseProvider):
raise gdalerr
def _ogr_feature_to_json(self, ogr_feature, skip_geometry=False):
geom = ogr_feature.GetGeometryRef()
if self.geom_field is not None:
geom = ogr_feature.GetGeomFieldRef(self.geom_field)
else:
geom = ogr_feature.GetGeometryRef()
if self.transform_out:
# Optionally reproject the geometry
geom.Transform(self.transform_out)
@@ -469,6 +475,8 @@ class OGRProvider(BaseProvider):
json_feature = ogr_feature.ExportToJson(as_object=True)
if skip_geometry:
json_feature['geometry'] = None
else:
json_feature['geometry'] = json.loads(geom.ExportToJson())
try:
json_feature['id'] = json_feature['properties'].pop(
self.id_field, json_feature['id']