aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem@fripost.org>2026-03-05 16:29:39 +0100
committerGuilhem Moulin <guilhem@fripost.org>2026-03-05 16:38:04 +0100
commit05ac5b52a224d6eddb23d748a9d8e3ee5571341b (patch)
treed90472278f19b356b6669a3183879d355c9b694c
parent3dc943e6972c93825c34b81fda89511a4763fffb (diff)
ExecuteSQL: Use the context manager instead of calling ReleaseResultSet() manually.
This assumes gdal ≥3.7, see https://gdal.org/en/stable/api/python/raster_api.html#osgeo.gdal.Dataset.ExecuteSQL .
-rw-r--r--export_mvt.py17
-rw-r--r--import_source.py11
2 files changed, 10 insertions, 18 deletions
diff --git a/export_mvt.py b/export_mvt.py
index 7b3137b..de4a351 100644
--- a/export_mvt.py
+++ b/export_mvt.py
@@ -186,11 +186,10 @@ def exportSourceLayer(lyr_src : ogr.Layer,
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)
- try:
+ with ds_src.ExecuteSQL(query, spatialFilter=spatialFilter) as lyr_src2:
count1 = -1
- if lyr_src.TestCapability(ogr.OLCFastFeatureCount):
- count1 = lyr_src.GetFeatureCount(force=0)
+ if lyr_src2.TestCapability(ogr.OLCFastFeatureCount):
+ count1 = lyr_src2.GetFeatureCount(force=0)
if count0 >= 0 and count1 >= 0:
logging.debug('Source layer "%s" has %d features, of which %d are to be exported',
layername, count0, count1)
@@ -198,13 +197,13 @@ def exportSourceLayer(lyr_src : ogr.Layer,
fieldMap = fieldMap[1]
logging.debug('Field map: %s', str(fieldMap))
- geom_type = lyr_src.GetGeomType()
+ geom_type = lyr_src2.GetGeomType()
bFlatten = geom_type == ogr.wkbUnknown or ogr.GT_HasM(geom_type) or ogr.GT_HasZ(geom_type)
bTransform = bFlatten or ct is not None
feature_count = 0
defn_dst = lyr_dst.GetLayerDefn()
- feature = lyr_src.GetNextFeature()
+ feature = lyr_src2.GetNextFeature()
while feature is not None:
feature2 = ogr.Feature(defn_dst)
feature2.SetFromWithMap(feature, False, fieldMap)
@@ -219,14 +218,10 @@ def exportSourceLayer(lyr_src : ogr.Layer,
if lyr_dst.CreateFeature(feature2) != ogr.OGRERR_NONE:
raise RuntimeError(f'Could not transfer source feature #{feature.GetFID()}')
feature_count += 1
- feature = lyr_src.GetNextFeature()
+ feature = lyr_src2.GetNextFeature()
logging.info('Exported %d features to MVT layer "%s" from "%s"',
feature_count, lyr_dst.GetName(), layername)
-
- finally:
- ds_src.ReleaseResultSet(lyr_src)
- lyr_src = None
return feature_count
def list_files(top : str,
diff --git a/import_source.py b/import_source.py
index 1271981..c04390e 100644
--- a/import_source.py
+++ b/import_source.py
@@ -984,18 +984,15 @@ def updateLayerCache(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'
- assert lyr2.GetLayerDefn().GetFieldDefn(1).GetName() == 'hash_geom'
+ with ds.ExecuteSQL(query) as lyr2:
+ defn2 = lyr2.GetLayerDefn()
+ assert defn2.GetFieldDefn(0).GetName() == 'hash_properties'
+ assert defn2.GetFieldDefn(1).GetName() == 'hash_geom'
feature = lyr2.GetNextFeature()
while feature is not None:
dgst.update(struct_dgst(feature.GetFID(), feature.GetFieldAsInteger64(0)))
dgst.update(feature.GetFieldAsBinary(1))
feature = lyr2.GetNextFeature()
- finally:
- ds.ReleaseResultSet(lyr2)
- lyr2 = None
fingerprint = dgst.digest()
attributeFilter = 'layername = ' + escape_literal_str(layername)