I have a point layer and a polygon layer (representing buildings). I created a buffer around the points and extracted those polygons which intersect the buffer.
The points contain an attribute with a certain value (e.g.
100). The polygons also contain an attribute with varying values (e.g.
What I would like to do is to iterate the intersected polygons in terms of distance to the point (i.e. the polygon closest to the point would be first, the polygon second closest would be second etc.). And each time, the polygon value would subtract the point value until it can no longer be subtracted (it cannot be less than
0) in which case it should stop iterating.
If possible, it would also be ideal where if a polygon has been iterated over, a new field would contain a boolean value
1 and all those which have not been iterated over contain
I'm hoping the result would look similar to this:
How could this be achieved by either SQL (virtual layer) or PyQGIS?
The following are pieces of code which only extracts the intersected polygons at 200m from the points:
SELECT * FROM Buildings d, point c WHERE ST_INTERSECTS(d.geometry, ST_BUFFER(c.geometry,200))
def func(point_layer, polygon_layer, spacing): # Create buffer buffer_layer = QgsVectorLayer("Polygon?crs=epsg:27700", 'Buffers' , "memory") with edit(buffer_layer): buffer_layer.addAttribute(QgsField('ID', QVariant.Int)) feat_list =  for f in point_layer.getFeatures(): poly = QgsFeature() f_buffer = f.geometry().buffer((spacing), 99) poly.setAttributes() poly.setGeometry(f_buffer) feat_list.append(poly) buffer_layer.addFeatures(feat_list) buffer_layer.updateExtents() buffer_layer.updateFields() QgsMapLayerRegistry.instance().addMapLayers(buffer_layer) # Intersect with polygon features = [feat for feat in polygon_layer.getFeatures()] ids =  for buffers in buffer_layer.getFeatures(): for polygons in polygon_layer.getFeatures(): if buffers.geometry().intersects(polygons.geometry()): ids.append(polygons) intersect_layer = QgsVectorLayer("Polygon?crs=epsg:27700", 'Intersect', "memory") intersect_layer.dataProvider().addAttributes(polygon_layer.fields()) with edit(intersect_layer): for feat in ids: feature = QgsFeature() feature.setGeometry(feat.geometry()) feature.setAttributes(feat.attributes()) intersect_layer.addFeature(feature) QgsMapLayerRegistry.instance().addMapLayer(intersect_layer) func(point_layer, polygon_layer, 200)