diff options
| author | Guilhem Moulin <guilhem@fripost.org> | 2026-03-05 16:29:39 +0100 |
|---|---|---|
| committer | Guilhem Moulin <guilhem@fripost.org> | 2026-03-05 16:38:04 +0100 |
| commit | 05ac5b52a224d6eddb23d748a9d8e3ee5571341b (patch) | |
| tree | d90472278f19b356b6669a3183879d355c9b694c /export_mvt.py | |
| parent | 3dc943e6972c93825c34b81fda89511a4763fffb (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 .
Diffstat (limited to 'export_mvt.py')
| -rw-r--r-- | export_mvt.py | 17 |
1 files changed, 6 insertions, 11 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, |
