I have a buffer that intersects with two polygons.
How can displace the buffer to the polygon with the largest intersection?
In addition
So far my code determines how many polygons my buffer intersects with, counts them, and adds them to a field called "NUMPOLYGON"
layer1 = QgsProject.instance().mapLayersByName("MERGE_PARCELAS")[0]
layer2 = QgsProject.instance().mapLayersByName("Polygon_sv")[0]
caps = layer2.dataProvider().capabilities()
if caps & QgsVectorDataProvider.AddAttributes:
res = layer2.dataProvider().addAttributes([QgsField("NUMPOLYGON", QVariant.Int)])
layer2.updateFields()
layer2.startEditing()
for feat in layer2.getFeatures():
feat['NUMPOLYGON'] = 0
layer2.updateFeature(feat)
layer2.commitChanges()
layer2.startEditing()
for f in layer2.getFeatures():
for j in layer1.getFeatures():
if f.geometry().intersects(j.geometry()):
if f['NUMPOLYGON'] != NULL:
f['NUMPOLYGON'] += 1
layer2.updateFeature(f)
layer2.commitChanges()
once the values greater than one have been identified.