aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem@fripost.org>2025-08-14 13:46:48 +0200
committerGuilhem Moulin <guilhem@fripost.org>2025-08-14 16:02:20 +0200
commit27fc0d1bfdefbc69450781c16bb43198b857f95d (patch)
treee3c3505bc5cec12b845e7213d6458db00dc315ca
parent878d693d3cda9dc667508889732614bdd9e31abd (diff)
Use .GetDataSet() from gdal ≥3.9 instead of passing the ds around.
-rw-r--r--export_mvt.py12
-rw-r--r--import_source.py14
-rwxr-xr-xwebmap-import2
3 files changed, 14 insertions, 14 deletions
diff --git a/export_mvt.py b/export_mvt.py
index c3690a9..7b3137b 100644
--- a/export_mvt.py
+++ b/export_mvt.py
@@ -117,8 +117,7 @@ def createMVT(drv : gdal.Driver, path : str,
return drv.Create(path, 0, 0, 0, **kwargs)
# pylint: disable-next=too-many-branches
-def exportSourceLayer(ds_src : gdal.Dataset,
- lyr_src : ogr.Layer,
+def exportSourceLayer(lyr_src : ogr.Layer,
lyr_dst : ogr.Layer,
layerdef : dict[str,Any],
fieldMap : tuple[list[str],list[int]],
@@ -184,6 +183,7 @@ def exportSourceLayer(ds_src : gdal.Dataset,
if cond is not None:
query += ' WHERE ' + cond.strip()
+ ds_src = lyr_src.GetDataset()
logging.debug('ExecuteSQL(%s%s)', query,
'' if spatialFilter is None else ', spatialFilter=' + spatialFilter.ExportToWkt())
lyr_src = ds_src.ExecuteSQL(query, spatialFilter=spatialFilter)
@@ -363,7 +363,6 @@ def exportMetadata(basedir : Path, data : dict[str,Any],
os.close(fd)
def getFieldMap(lyr_dst : ogr.Layer, lyr_src : ogr.Layer,
- drv_src : gdal.Driver,
fieldMap : dict[str,str]|None) -> tuple[list[str],list[int]]:
"""Create fields on the destination MVT layer, and return a list of
column statements along with a field map for the MVT export."""
@@ -376,6 +375,7 @@ def getFieldMap(lyr_dst : ogr.Layer, lyr_src : ogr.Layer,
columns = {}
defn_src = lyr_src.GetLayerDefn()
+ drv_src = lyr_src.GetDataset().GetDriver()
for fld_dst, fld_src in fieldMap.items():
idx_src = defn_src.GetFieldIndex(fld_src)
if idx_src < 0:
@@ -394,7 +394,6 @@ def getFieldMap(lyr_dst : ogr.Layer, lyr_src : ogr.Layer,
# advantage of the reduced storage though)
defn_dst.SetSubType(ogr.OFSTInt16)
- # TODO[GDAL >=3.9] use lyr_src.GetDataset().GetDriver()
if drv_src.ShortName == 'PostgreSQL':
column = 'CAST(m.' + escape_identifier(fld_src)
column += ' - date \'1970-01-01\' AS smallint)'
@@ -528,13 +527,12 @@ def exportMVT(ds : gdal.Dataset,
if lyr_dst is None:
raise RuntimeError(f'Could not create destination layer "{layername}"')
- fieldMap = getFieldMap(lyr_dst, lyr_src, drv_src=ds.GetDriver(),
- fieldMap=layerdef.get('fields', None))
+ fieldMap = getFieldMap(lyr_dst, lyr_src, fieldMap=layerdef.get('fields', None))
# TODO: GDAL exports features to a temporary SQLite database even though the source
# is PostGIS hence is able to generate MVT with ST_AsMVT(). Letting PostGIS generate
# tiles is likely to speed up things.
- feature_count += exportSourceLayer(ds, lyr_src, lyr_dst, layerdef,
+ feature_count += exportSourceLayer(lyr_src, lyr_dst, layerdef,
fieldMap=fieldMap,
extent=extent)
layer_count += 1
diff --git a/import_source.py b/import_source.py
index 8a8ff1a..f103ce8 100644
--- a/import_source.py
+++ b/import_source.py
@@ -363,7 +363,7 @@ def validateOutputLayer(lyr : ogr.Layer,
return ok
-def clearLayer(ds : gdal.Dataset, lyr : ogr.Layer) -> None:
+def clearLayer(lyr : ogr.Layer) -> None:
"""Clear the given layer (wipe all its features)"""
n = -1
if lyr.TestCapability(ogr.OLCFastFeatureCount):
@@ -373,7 +373,7 @@ def clearLayer(ds : gdal.Dataset, lyr : ogr.Layer) -> None:
return
layername_esc = escape_identifier(lyr.GetName())
- # XXX GDAL <3.9 doesn't have lyr.GetDataset() so we pass the DS along with the layer
+ ds = lyr.GetDataset()
drv = ds.GetDriver()
if drv.ShortName == 'PostgreSQL':
# https://www.postgresql.org/docs/15/sql-truncate.html
@@ -454,7 +454,7 @@ class ImportStatus(Enum):
return self.name.removeprefix('IMPORT_')
# pylint: disable-next=dangerous-default-value
-def importSources(dso : gdal.Dataset, lyr : ogr.Layer,
+def importSources(lyr : ogr.Layer,
sources : dict[str,Any] = {},
cachedir : Path|None = None,
extent : ogr.Geometry|None = None,
@@ -463,6 +463,7 @@ def importSources(dso : gdal.Dataset, lyr : ogr.Layer,
force : bool = False) -> ImportStatus:
"""Clear lyr and import source layers to it."""
+ dso = lyr.GetDataset()
layername = lyr.GetName()
if dsoTransaction:
# declare a SAVEPOINT (nested transaction) within the DS-level transaction
@@ -483,7 +484,7 @@ def importSources(dso : gdal.Dataset, lyr : ogr.Layer,
rv = ImportStatus.IMPORT_NOCHANGE
now = datetime.now().astimezone()
try:
- clearLayer(dso, lyr) # TODO conditional (only if not new)?
+ clearLayer(lyr) # TODO conditional (only if not new)?
for source in sources:
importSource0(lyr, **source['source'],
@@ -499,7 +500,7 @@ def importSources(dso : gdal.Dataset, lyr : ogr.Layer,
if lyrcache is None:
rv = ImportStatus.IMPORT_SUCCESS
elif updateLayerCache(cache=lyrcache,
- ds=dso, lyr=lyr,
+ lyr=lyr,
force=force,
lyrTransaction=lyrTransaction,
last_updated=now):
@@ -912,7 +913,7 @@ def listFieldsOrderBy(defn : ogr.FeatureDefn,
yield c
# pylint: disable-next=too-many-branches, too-many-statements
-def updateLayerCache(ds : gdal.Dataset, lyr : ogr.Layer, cache : ogr.Layer,
+def updateLayerCache(lyr : ogr.Layer, cache : ogr.Layer,
last_updated : datetime,
lyrTransaction : str|bool|None = None,
force : bool = False) -> bool:
@@ -960,6 +961,7 @@ def updateLayerCache(ds : gdal.Dataset, lyr : ogr.Layer, cache : ogr.Layer,
struct_dgst : Final = struct.Struct('@qq').pack
logging.debug('%s', query)
+ ds = lyr.GetDataset()
lyr2 = ds.ExecuteSQL(query)
try:
assert lyr2.GetLayerDefn().GetFieldDefn(0).GetName() == 'hash_properties'
diff --git a/webmap-import b/webmap-import
index ef728a9..95c3b41 100755
--- a/webmap-import
+++ b/webmap-import
@@ -274,7 +274,7 @@ def processOutputLayer(ds : gdal.Dataset,
# setup output field mapping in the sources dictionary
setOutputFieldMap(lyr.GetLayerDefn(), sources)
- return importSources(dso=ds, lyr=lyr, sources=sources,
+ return importSources(lyr=lyr, sources=sources,
cachedir=cachedir, extent=extent,
dsoTransaction=dsTransaction,
lyrcache=lyrcache,