6

I think the following is very confusing

sel_str = "ND_14 = '108'"
l.SetAttributeFilter(sel_str)
l.GetFeatureCount()
>>> 1 

In a for-loop this gives me

for f in l:
   g = f.GetGeometryRef()
   print g.GetEnvelope()
>>> (8.39746, 8.95788, 46.52757, 46.99331)

Whereas GetFeature() gives me

>>> feat0 = l.GetFeature(0)
>>> feat1 = l.GetFeature(1) 
>>> g0 = feat0.GetGeometryRef()
>>> g1 = feat1.GetGeometryRef()
>>> g0.GetEnvelope()
(8.21206, 10.65409, 56.55045, 57.75181)
>>> g1.GetEnvelope()
(10.60014, 10.6172, 57.48933, 57.4979)

Apparently GetFeature() is based on the FID of the whole set, not the selection. But using a for-loop on a selection of length one is pretty ridiculous. How do I get the FID of my selected feature then? Or what are other ways of using GetFeature() with your current selection?

1 Answer 1

7

Read Chris Garrard's Open Source RS/GIS Python,Week 3: Filters & Simple Analysis of Vector Data; Functions & Modules

Resets reading so GetNextFeature() gets the first feature that matches the filter

So, for a selection of length one use the iterator:

layer.SetAttributeFilter(sel_str)
sel = layer.GetNextFeature()
g = sel.GetGeometryRef()
g.GetEnvelope()

It is easier to use layer.ExecuteSQL

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.