fiona SQL filter


Loïc Dutrieux
 

Hi everyone,

I'm trying to do some attribute filtering of a fiona feature collection using the where clause but I cannot find out the right syntax. Here's what I've tried.

Thanks for any help,
Kind regards,
Loïc


import string
import tempfile, os

import fiona
from fiona.crs import from_epsg
from shapely.geometry import Point, mapping

# Generate test data and save to tmp geopackage
gpkg = os.path.join(tempfile.gettempdir(), 'fiona_sql_test.gpkg')
sc = [Point(1,2), Point(3,4), Point(5,6)]
fc = [{'geometry': mapping(s),
'properties': {'letter': string.ascii_lowercase[idx]}}
for idx,s in enumerate(sc)]
schema = {'geometry': 'Point',
'properties': {'letter': 'str'}}
with fiona.open(gpkg, 'w', driver='GPKG', schema=schema, crs=from_epsg(4326),
layer='test') as dst:
dst.writerecords(fc)

# Filter using OGR SQL dialect
with fiona.open(gpkg, layer='test') as src:
fc = src.filter(where='"letter" = "a"')
for feat in fc:
print(feat)

Join main@fiona.groups.io to automatically receive all group messages.