<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
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>
Another SHP file with 22000 Points <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>
</body>
</html>