[gdal-dev] GetFID incorrect after sorting layer

Jon Morris jon.morris.dev at outlook.com
Wed Jun 22 11:14:27 PDT 2022


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 <even.rouault at spatialys.com>
Sent: 22 June 2022 16:09
To: Jon Morris <jon.morris.dev at outlook.com>; gdal-dev at lists.osgeo.org <gdal-dev at lists.osgeo.org>
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
gdal-dev at lists.osgeo.org<mailto:gdal-dev at lists.osgeo.org>
https://lists.osgeo.org/mailman/listinfo/gdal-dev


--
http://www.spatialys.com
My software is free, but my time generally not.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20220622/7a203c7d/attachment-0001.htm>


More information about the gdal-dev mailing list