From 2c3ee79cb434fc4cf315ee3a6a526156053d76c4 Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Thu, 14 Aug 2025 15:44:34 +0200 Subject: Create destination layer with CreateLayerFromGeomFieldDefn(). Rather than CreateLayer(). Unfortunately not very helpful since .SetNullable() appears to be a no-op; it doesn't set a NOT NULL constraint on the geometry column. --- import_source.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'import_source.py') diff --git a/import_source.py b/import_source.py index f103ce8..795bf0f 100644 --- a/import_source.py +++ b/import_source.py @@ -117,7 +117,7 @@ def createOutputLayer(ds : gdal.Dataset, logging.info('Creating new destination layer "%s"', layername) geom_type = options['geometry-type'] - lco = options.get('options', None) + lco = options.get('options', []) drv = ds.GetDriver() if geom_type != ogr.wkbNone and drv.ShortName == 'PostgreSQL': @@ -137,21 +137,18 @@ def createOutputLayer(ds : gdal.Dataset, lco = [] lco = ['dim=' + dim] + lco # prepend dim= - kwargs = { 'geom_type': geom_type } + geom_field = ogr.GeomFieldDefn() + geom_field.SetType(geom_type) + geom_field.SetNullable(False) # XXX doesn't do anything?? if srs is not None: - kwargs['srs'] = srs - if lco is not None: - kwargs['options'] = lco - logging.debug('CreateLayer(%s, geom_type="%s"%s%s)', layername, - ogr.GeometryTypeToName(geom_type), - ', srs="' + kwargs['srs'].GetName() + '"' if 'srs' in kwargs else '', - ', options=' + str(kwargs['options']) if 'options' in kwargs else '') - lyr = ds.CreateLayer(layername, **kwargs) + geom_field.SetSpatialRef(srs) + logging.debug('CreateLayer("%s", geom_type="%s"%s%s)', + layername, ogr.GeometryTypeToName(geom_type), + ', srs="' + geom_field.GetSpatialRef().GetName() + '"' if srs is not None else '', + ', options=' + str(lco) if lco is not None else '') + lyr = ds.CreateLayerFromGeomFieldDefn(layername, geom_field, lco if lco is not None else []) if lyr is None: raise RuntimeError(f'Could not create destination layer "{layername}"') - # TODO use CreateLayerFromGeomFieldDefn() from ≥v3.9 as it's not - # possible to toggle the geomfield's nullable property after fact - # otherwise fields = options['fields'] if len(fields) > 0 and not lyr.TestCapability(ogr.OLCCreateField): -- cgit v1.2.3