<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Jon,</p>
    <p>this is a bug on the result SQL layer implementation of
      GetFeature(). Can you file a ticket in
      <a class="moz-txt-link-freetext" href="https://github.com/OSGeo/gdal/issues/new">https://github.com/OSGeo/gdal/issues/new</a> with the below elements?
      Thanks</p>
    <p>Even<br>
    </p>
    <div class="moz-cite-prefix">Le 22/06/2022 à 15:37, Jon Morris a
      écrit :<br>
    </div>
    <blockquote type="cite"
cite="mid:DB8PR10MB2844EC4F2DE8419AFC9CB186AFB29@DB8PR10MB2844.EURPRD10.PROD.OUTLOOK.COM">
      <div class="elementToProof">
        <p class="MsoNormal">
          <span>Hello all, </span></p>
        <p class="MsoNormal">
          <span> </span></p>
        <p class="MsoNormal">
          <span>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.“ </span></p>
        <p class="MsoNormal">
          <span><a
href="https://gdal.org/doxygen/classOGRLayer.html#ace902dfb44335fb1a877aa035ecff209"
              moz-do-not-send="true" class="moz-txt-link-freetext">https://gdal.org/doxygen/classOGRLayer.html#ace902dfb44335fb1a877aa035ecff209</a> </span></p>
        <p class="MsoNormal">
          <span> </span></p>
        <p class="MsoNormal">
          <span>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? </span></p>
        <p class="MsoNormal">
          <span> </span></p>
        <p class="MsoNormal">
          <span>I’m using GDAL 3.4.1 on Python 3.8.12 </span></p>
        <p class="MsoNormal">
          <span> </span></p>
        <p class="MsoNormal">
          <span>Repro case: </span></p>
        <p class="MsoNormal">
          <span><a
              href="https://gist.github.com/jontwo/01d8cbf1e047e36d2fb2f7786ad60e73"
              moz-do-not-send="true" class="moz-txt-link-freetext">https://gist.github.com/jontwo/01d8cbf1e047e36d2fb2f7786ad60e73</a> </span></p>
        <p class="MsoNormal">
          <span>Creates a layer with 4 features, areas 1, 64, 4, 25 </span></p>
        <p class="MsoNormal">
          <span>Then sorts them with </span>
          <span>ds.ExecuteSQL(f"SELECT * FROM lname ORDER BY
            OGR_GEOM_AREA ASC")</span><span> </span></p>
        <p class="MsoNormal">
          <span>Gets a list of FIDs in the layer using GetFID </span></p>
        <p class="MsoNormal">
          <span>Calls GetFeature for each of these FIDs. </span></p>
        <p class="MsoNormal">
          <span> </span></p>
        <p class="MsoNormal">
          <span>Output: </span></p>
        <p class="MsoNormal">
          <span>Areas before: [(0, 1.0), (1, 64.0), (2, 4.0), (3,
            25.0)] </span></p>
        <p class="MsoNormal">
          <span>Areas after: [(0, 1.0), (2, 4.0), (3, 25.0), (1, 64.0)] </span></p>
        <p class="MsoNormal">
          <span>Requested FID 0 (area 1.0) got 0 (area 1.0) </span></p>
        <p class="MsoNormal">
          <span>Requested FID 2 (area 4.0) got 3 (area 25.0) </span></p>
        <p class="MsoNormal">
          <span>Requested FID 3 (area 25.0) got 1 (area 64.0) </span></p>
        <p class="MsoNormal">
          <span>Requested FID 1 (area 64.0) got 2 (area 4.0) </span></p>
        <p class="MsoNormal">
          <span> </span></p>
        <p class="MsoNormal">
          <span>Thanks, </span></p>
        <p class="MsoNormal">
          <span> </span></p>
        <p class="MsoNormal">
          <span>Jon </span></p>
        <br>
      </div>
      <br>
      <fieldset class="moz-mime-attachment-header"></fieldset>
      <pre class="moz-quote-pre" wrap="">_______________________________________________
gdal-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:gdal-dev@lists.osgeo.org">gdal-dev@lists.osgeo.org</a>
<a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/gdal-dev">https://lists.osgeo.org/mailman/listinfo/gdal-dev</a>
</pre>
    </blockquote>
    <pre class="moz-signature" cols="72">-- 
<a class="moz-txt-link-freetext" href="http://www.spatialys.com">http://www.spatialys.com</a>
My software is free, but my time generally not.</pre>
  </body>
</html>