Closed utterances-bot closed 3 weeks ago
I received an error when try to use the script, ChatGpt did some corrections and worked well. Here is the revised script if somebody need to use or if you want to replace.
Thanks a lot for this tutorial and script
################################################################################
#
#
#
# ################################################################################ from qgis.utils import iface from PyQt5.QtCore import QVariant # Updated for PyQt5 from qgis.core import QgsField, QgsFeature, QgsSpatialIndex
_NAME_FIELD = 'NAME' _SUM_FIELD = 'POP_EST'
_NEW_NEIGHBORS_FIELD = 'NEIGHBORS' _NEW_SUM_FIELD = 'SUM'
layer = iface.activeLayer()
layer.startEditing() layer.dataProvider().addAttributes( [QgsField(_NEW_NEIGHBORS_FIELD, QVariant.String), QgsField(_NEW_SUM_FIELD, QVariant.Int)]) layer.updateFields()
feature_dict = {f.id(): f for f in layer.getFeatures()}
index = QgsSpatialIndex() for f in feature_dict.values(): index.insertFeature(f)
for f in feature_dict.values(): print(f'Working on {f[_NAME_FIELD]}') geom = f.geometry() intersecting_ids = index.intersects(geom.boundingBox())
neighbors = []
neighbors_sum = 0
for intersecting_id in intersecting_ids:
intersecting_f = feature_dict[intersecting_id]
if (f != intersecting_f and not intersecting_f.geometry().disjoint(geom)):
neighbors.append(intersecting_f[_NAME_FIELD])
neighbors_sum += intersecting_f[_SUM_FIELD]
f.setAttribute(_NEW_NEIGHBORS_FIELD, ','.join(neighbors))
f.setAttribute(_NEW_SUM_FIELD, neighbors_sum)
layer.updateFeature(f)
layer.commitChanges() print('Processing complete.')
Thanks for sharing. The code is very similar to what we teach in our PyQGIS class on how to compute neighbors using Actions. You can check it out https://courses.spatialthoughts.com/pyqgis-masterclass.html#selecting-neighbors
Find Neighbor Polygons in a Layer — QGIS Tutorials and Tips
https://www.qgistutorials.com/en/docs/find_neighbor_polygons.html