Iterate through features in vector layer, using each as clip layer for separate point

المشرف العام

Administrator
طاقم الإدارة
I have a point layer (representing cities), and a set of vector layers whose features are hundreds of concentric discs, each of which I'd like to use as a clipping layer to select only the cities that lie within that disc, perform some operations on those cities, and then move to the next disc.

Here is a screenshot of my project - the blue-grey discs are the clipping polygons, and the red dots are the city features that I'm trying to select from:



I was trying to use the QGIS processing module to do this. However all of the documentation I've been able to find on the qgis:clip function shows its output going to a shapefile. For instance the answers to the question Is there a way to call the clip function in pyQGIS from the python console? suggest:

processing.runalg("qgis:clip",inputlayer,overlaylayer,"output_file.shp")processing.runandload("qgis:clip",inputlayer,overlaylayer,"output_file.shp")What I want to do instead is iterate through all of the discs in the clipping layer(s), and save the results from using each into a temporary in-memory vector layer instead of a shapefile.

Here's the (non-working) code that I tried to write to do this:

for disc in troopBuffers: # Clipping requires the cities layer and troop buffer disc to both be selected buffLayer.select(disc.id()) cityLayer.selectAll() clipLayer = QgsVectorLayer("Point", "temporary_points", "memory:tmpClipLayer") QgsMapLayerRegistry.instance().addMapLayer(clipLayer) processing.runandload("qgis:clip", cityLayer, disc, clipLayer) clippedCities = [f for f in clipLayer.getFeatures()] for c in clippedCities: # do something with each city that lies within this disc ... buffLayer.removeSelection() QgsMapLayerRegistry.instance().removeMapLayer(clipLayer.id())... however, when I run this clippedCities appears to always be empty, so something is obviously wrong.

How would I clip like this without actually creating shapefiles on disk for each iteration?

Can I just have processing output the results of each clip to an in-memory "scratch layer", or (even better) just get a list of QgsFeature objects from the cities layer for each disc?

Or am I going to have to use something else besides processing here?



أكثر...
 
أعلى