Done: https://github.com/OSGeo/gdal/issues/5967
Many thanks, Jon [https://opengraph.githubassets.com/594c877faa442856292a9b4df982ba26051c434a7b1e2c82ab21fe444a6f95dc/OSGeo/gdal/issues/5967]<https://github.com/OSGeo/gdal/issues/5967> GetFID incorrect after sorting layer · Issue #5967 · OSGeo/gdal<https://github.com/OSGeo/gdal/issues/5967> Expected behavior and actual behavior. When a layer is sorted using ExecuteSQL with ORDER BY, the FIDs in the layer are changed such GetFeature returns the wrong feature when using an fid obtained ... github.com ________________________________ From: Even Rouault <[email protected]> Sent: 22 June 2022 16:09 To: Jon Morris <[email protected]>; [email protected] <[email protected]> Subject: Re: [gdal-dev] GetFID incorrect after sorting layer Jon, this is a bug on the result SQL layer implementation of GetFeature(). Can you file a ticket in https://github.com/OSGeo/gdal/issues/new with the below elements? Thanks Even Le 22/06/2022 à 15:37, Jon Morris a écrit : 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]<mailto:[email protected]> https://lists.osgeo.org/mailman/listinfo/gdal-dev -- http://www.spatialys.com My software is free, but my time generally not.
_______________________________________________ gdal-dev mailing list [email protected] https://lists.osgeo.org/mailman/listinfo/gdal-dev
