Hello all, I’m having trouble getting features in a layer after sorting with SQL query. It seems that the FIDs change when the layer is sorted. I’m ok with that, but if you take the id returned by GetFID and call GetFeature on the layer, you get a different feature. I would have thought GetFID/GetFeature should remain in sync even after sorting. The docs for GetFeature say “If this method returns a non-NULL feature, it is guaranteed that its feature id (OGRFeature::GetFID()) will be the same as nFID.“ https://gdal.org/doxygen/classOGRLayer.html#ace902dfb44335fb1a877aa035ecff209
I’m aware I could just iterate through the layer, but I want to add/delete features so prefer to iterate through a list of ids instead. Should I just ignore the FIDs and iterate through range(layer.GetFeatureCount()) instead? Is that guaranteed to give me every feature in size order? I’m using GDAL 3.4.1 on Python 3.8.12 Repro case: https://gist.github.com/jontwo/01d8cbf1e047e36d2fb2f7786ad60e73 Creates a layer with 4 features, areas 1, 64, 4, 25 Then sorts them with ds.ExecuteSQL(f"SELECT * FROM lname ORDER BY OGR_GEOM_AREA ASC") Gets a list of FIDs in the layer using GetFID Calls GetFeature for each of these FIDs. Output: Areas before: [(0, 1.0), (1, 64.0), (2, 4.0), (3, 25.0)] Areas after: [(0, 1.0), (2, 4.0), (3, 25.0), (1, 64.0)] Requested FID 0 (area 1.0) got 0 (area 1.0) Requested FID 2 (area 4.0) got 3 (area 25.0) Requested FID 3 (area 25.0) got 1 (area 64.0) Requested FID 1 (area 64.0) got 2 (area 4.0) Thanks, Jon
_______________________________________________ gdal-dev mailing list [email protected] https://lists.osgeo.org/mailman/listinfo/gdal-dev
