<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black">Hello all,<o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black"><o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black">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.“<o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black"><a href="https://gdal.org/doxygen/classOGRLayer.html#ace902dfb44335fb1a877aa035ecff209">https://gdal.org/doxygen/classOGRLayer.html#ace902dfb44335fb1a877aa035ecff209</a><o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black"><o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black">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?<o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black"><o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black">I’m using GDAL 3.4.1 on Python 3.8.12<o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black"><o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black">Repro case:<o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black"><a href="https://gist.github.com/jontwo/01d8cbf1e047e36d2fb2f7786ad60e73">https://gist.github.com/jontwo/01d8cbf1e047e36d2fb2f7786ad60e73</a><o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black">Creates a layer with 4 features, areas 1, 64, 4, 25<o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black">Then sorts them with </span>
<span style="font-family:"Courier New";color:black">ds.ExecuteSQL(f"SELECT * FROM lname ORDER BY OGR_GEOM_AREA ASC")</span><span style="font-family:"Arial",sans-serif;color:black"><o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black">Gets a list of FIDs in the layer using GetFID<o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black">Calls GetFeature for each of these FIDs.<o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black"><o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black">Output:<o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black">Areas before: [(0, 1.0), (1, 64.0), (2, 4.0), (3, 25.0)]<o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black">Areas after: [(0, 1.0), (2, 4.0), (3, 25.0), (1, 64.0)]<o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black">Requested FID 0 (area 1.0) got 0 (area 1.0)<o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black">Requested FID 2 (area 4.0) got 3 (area 25.0)<o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black">Requested FID 3 (area 25.0) got 1 (area 64.0)<o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black">Requested FID 1 (area 64.0) got 2 (area 4.0)<o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black"><o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black">Thanks,<o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black"><o:p> </o:p></span></p>
<p class="MsoNormal" style="margin:0cm;font-size:11pt;font-family:Calibri, sans-serif">
<span style="font-family:"Arial",sans-serif;color:black">Jon<o:p> </o:p></span></p>
<br>
</div>
</body>
</html>