From 05ac5b52a224d6eddb23d748a9d8e3ee5571341b Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Thu, 5 Mar 2026 16:29:39 +0100 Subject: ExecuteSQL: Use the context manager instead of calling ReleaseResultSet() manually. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This assumes gdal ≥3.7, see https://gdal.org/en/stable/api/python/raster_api.html#osgeo.gdal.Dataset.ExecuteSQL . --- export_mvt.py | 17 ++++++----------- import_source.py | 11 ++++------- 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) -- cgit v1.2.3