diff options
author | Guilhem Moulin <guilhem@fripost.org> | 2025-08-14 15:44:34 +0200 |
---|---|---|
committer | Guilhem Moulin <guilhem@fripost.org> | 2025-08-14 16:02:20 +0200 |
commit | 2c3ee79cb434fc4cf315ee3a6a526156053d76c4 (patch) | |
tree | 0671ef831860bf88970ce521d78dfae28eb6b4ee | |
parent | 27fc0d1bfdefbc69450781c16bb43198b857f95d (diff) |
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.
-rw-r--r-- | import_source.py | 23 |
1 files changed, 10 insertions, 13 deletions
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): |