<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    Hi,<br>
    Just by way of finality:<br>
    <br>
    The CRS of the polygon SHP was not what it was purported to be.<br>
    I found that the .prj file was invalid when opening it in QGIS -
    maybe next time I will first look at the maps I am given, rather
    than just dive into python scripting. <br>
    <br>
    All working as expected, now.<br>
    <br>
    Thanks to those who gave ideas to try.<br>
    <br>
    Kind regards,<br>
    Zoltan<br>
    <br>
    <div class="moz-cite-prefix">On 2024/08/25 19:44, Zoltan Szecsei via
      QGIS-User wrote:<br>
    </div>
    <blockquote type="cite"
      cite="mid:32bc6d4a-653c-4d08-b145-a367b60124de@geograph.co.za">
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      So as updated plan-of-attack as below.....<br>
      <blockquote>
        <blockquote type="cite">
          <div style="background-color:#1e1f22;color:#bcbec4">
            <pre
style="font-family:'JetBrains Mono',monospace;font-size:9,8pt;"><span
            style="color:#7a7e85;">lyr_poly = QgsVectorLayer(poly_shp, '', 'Ogr')
</span><span style="color:#7a7e85;">lyr_poly_si = QgsSpatialIndex(lyr_poly.getFeatures(), flags=QgsSpatialIndex.FlagStoreFeatureGeometries)
</span><span style="color:#7a7e85;"># EPSG:4326
</span>point = QgsPointXY(lon, lat)
point_rect = QgsGeometry.fromPointXY(point).buffer(<span
            style="color:#2aacb8;">0.001</span>, <span
            style="color:#2aacb8;">5</span>).boundingBox()
<span style="color:#7a7e85;">#
</span>feature_ids = lyr_poly_si.intersects(point_rect)  <span
            style="color:#7a7e85;">#  intersects wants rectangle
</span>requests = QgsFeatureRequest().setFilterFids(feature_ids)
<span style="color:#7a7e85;">#
</span>feature_idx = lyr_poly_si.nearestNeighbor(point, <span
            style="color:#2aacb8;">10</span>, <span
            style="color:#aa4926;">maxDistance</span>=<span
            style="color:#2aacb8;">0.001</span>)
requestx = QgsFeatureRequest().setFilterFids(feature_idx)
</pre>
          </div>
        </blockquote>
      </blockquote>
      For the above code the lists feature_ids and feature_idx are
      always empty.<br>
      The Longitude and Latitude coords are correct, also for the box
      created from the Point.<br>
      <br>
      I'd be grateful for any insight on why nothing is found.<br>
      <br>
      Thanks in advance,<br>
      Zoltan<br>
      <br>
      <br>
      <div class="moz-cite-prefix">On 2024/08/25 16:19, Zoltan Szecsei
        via QGIS-User wrote:<br>
      </div>
      <blockquote type="cite"
        cite="mid:4d547fda-c351-42fb-b54e-5a115c6b7222@geograph.co.za">
        <meta http-equiv="content-type"
          content="text/html; charset=UTF-8">
        Hi,<br>
        I'm doing my "once a year need to do something in python with
        QGIS" :-/<br>
        Using QGIS 3.38 and PyCharm.<br>
        <br>
        I have a SHP file with 28000 polygons.<br>
        <b><font color="#ff8040">A SPREADSHEET file with 22000 Points
            (for which I am making a SHP file and wanting attribute
            vaues from underlying polygons)</font></b><br>
        Both in EPSG:4326<br>
        <br>
        Without using QGIS processing, what is the quickest way to, for
        each point 1 by 1, find the underlying polygon and read the
        attribute field values of that polygon?<br>
        The code snippet below is far too inefficient - but I cannot
        find code that selects at XY and benefits from any spatial index
        on the polygons.<br>
        <blockquote>
          <pre>   <font face="Courier New, Courier, monospace">lyr_poly = QgsVectorLayer(polygon_shp, '', 'Ogr')
      point = QgsPointXY(lon, lat)
      request = QgsFeatureRequest().setFilterRect(lyr_poly.extent())
      for feature in lyr_poly.getFeatures(request):
          geom = feature.geometry()
          if geom.contains(QgsGeometry.fromPointXY(point)):
              lyr_poly.select(feature.id())
              print(f"Feature with ID {feature.id()} is selected.")
              break</font>
</pre>
        </blockquote>
        Thanks in advance,<br>
        Zoltan<br>
        <br>
        <br>
        <br>
        <fieldset class="moz-mime-attachment-header"></fieldset>
        <pre wrap="" class="moz-quote-pre">_______________________________________________
QGIS-User mailing list
<a class="moz-txt-link-abbreviated moz-txt-link-freetext"
        href="mailto:QGIS-User@lists.osgeo.org" moz-do-not-send="true">QGIS-User@lists.osgeo.org</a>
List info: <a class="moz-txt-link-freetext"
        href="https://lists.osgeo.org/mailman/listinfo/qgis-user"
        moz-do-not-send="true">https://lists.osgeo.org/mailman/listinfo/qgis-user</a>
Unsubscribe: <a class="moz-txt-link-freetext"
        href="https://lists.osgeo.org/mailman/listinfo/qgis-user"
        moz-do-not-send="true">https://lists.osgeo.org/mailman/listinfo/qgis-user</a>
</pre>
      </blockquote>
    </blockquote>
    <br>
  </body>
</html>