-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
apply sql (e.g. ExecuteSQL) prior to Collection iteration #59
Comments
Can you be more specific? Do you mean an attribute filter or something else? |
Below is a code example. It amounts to an attribute filter (there may be a better way to do it). Fiona is perfect for my application except for iteration over large shapefiles when only a small subset of the geometries are actually needed. from osgeo import ogr
from shapely import wkb
shp_path = 'foo.shp'
select_ugid = [1,2,3]
ds = ogr.Open(shp_path)
try:
lyr = ds.GetLayerByIndex(0)
lyr.ResetReading()
if select_ugid is not None:
lyr_name = lyr.GetName()
## format where statement different for singletons
if len(select_ugid) == 1:
sql_where = 'UGID = {0}'.format(select_ugid[0])
else:
sql_where = 'UGID IN {0}'.format(tuple(select_ugid))
sql = 'SELECT * FROM {0} WHERE {1}'.format(lyr_name,sql_where)
features = ds.ExecuteSQL(sql)
else:
features = lyr
geoms = [None]*len(features)
for idx,feature in enumerate(features):
attrs = feature.items()
attrs.update({'geom':wkb.loads(feature.geometry().ExportToWkb())})
geoms[idx] = attrs
finally:
ds.Destroy() |
Understood. According to http://www.gdal.org/ogr/drv_shapefile.html unless you've made an attribute index for UGID, your code above is functionally equivalent to this:
Because of that and because I don't want to add any SQL to Fiona interfaces, I won't expose OGR's ExecuteSQL in Fiona. |
Thanks, Sean. This is very helpful! I went ahead and closed the ticket... |
Is it possible to apply a limiting SQL statement prior to iterating over a fiona Collection?
The text was updated successfully, but these errors were encountered: