Skip to content

Commit

Permalink
set interest layers for OSM, release features read when counting
Browse files Browse the repository at this point in the history
  • Loading branch information
brendan-ward committed Aug 17, 2023
1 parent eaf75e9 commit 31d7df3
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions pyogrio/_io.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ cdef OGRLayerH get_ogr_layer(GDALDatasetH ogr_dataset, layer) except NULL:
except CPLE_BaseError as exc:
raise DataLayerError(str(exc))

# if the driver is OSM, we need to execute SQL to set the layer to read in
# order to read it properly
if get_driver(ogr_dataset) == "OSM":
# Note: this returns NULL and does not need to be freed via
# GDALDatasetReleaseResultSet()
layer_name = get_string(OGR_L_GetName(ogr_layer))
sql_b = f"SET interest_layers = {layer_name}".encode('utf-8')
sql_c = sql_b

GDALDatasetExecuteSQL(ogr_dataset, sql_c, NULL, NULL)

return ogr_layer


Expand Down Expand Up @@ -309,7 +320,7 @@ cdef get_driver(OGRDataSourceH ogr_dataset):
return driver


cdef get_feature_count(OGRLayerH ogr_layer):
cdef int get_feature_count(OGRLayerH ogr_layer):
"""Get the feature count of a layer.
If GDAL returns an unknown count (-1), this iterates over every feature
Expand All @@ -325,6 +336,7 @@ cdef get_feature_count(OGRLayerH ogr_layer):
count of features
"""

cdef OGRFeatureH ogr_feature = NULL
cdef int feature_count = OGR_L_GetFeatureCount(ogr_layer, 1)

# if GDAL refuses to give us the feature count, we have to loop over all
Expand All @@ -337,7 +349,7 @@ cdef get_feature_count(OGRLayerH ogr_layer):
feature_count = 0
while True:
try:
exc_wrap_pointer(OGR_L_GetNextFeature(ogr_layer))
ogr_feature = exc_wrap_pointer(OGR_L_GetNextFeature(ogr_layer))
feature_count +=1

except NullPointerError:
Expand All @@ -356,6 +368,11 @@ cdef get_feature_count(OGRLayerH ogr_layer):

raise DataLayerError(f"Could not iterate over features: {str(exc)}")

finally:
if ogr_feature != NULL:
OGR_F_Destroy(ogr_feature)
ogr_feature = NULL

return feature_count


Expand Down

0 comments on commit 31d7df3

Please sign in to comment.