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 are some screenshots of the project. The concentric blue-grey discs are the clipping polygons - 25/50/100/150 mile buffers around military base locations(represented as green diamonds). The red dots are the city features (displayed in graduated sizes by violent crime rate) that I'm trying to use as the input layer :
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:
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