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)


Alan Snow
 

I think the feature you want is slated for 2.0: https://github.com/Toblerity/Fiona/pull/961


Loïc Dutrieux
 

Thanks Alan, you're right. I wrongly assumed that this was an old or at least already existing feature since it is in the online doc.

Cheers,
Loïc
________________________________________
From: main@fiona.groups.io <main@fiona.groups.io> on behalf of Alan Snow <alansnow21@gmail.com>
Sent: 03 June 2021 20:05:29
To: main@fiona.groups.io
Subject: Re: [fiona] fiona SQL filter

I think the feature you want is slated for 2.0: https://github.com/Toblerity/Fiona/pull/961<https://urldefense.com/v3/__https://github.com/Toblerity/Fiona/pull/961__;!!DOxrgLBm!UB5tJPQHin0tXeTB6cruOVmD3nIa-Dq8tSRv6xve6RsrgL5eedhcf0yBmtwj9Z5O8Cik8ns$>