On QGIS 2.16.2, I have a polygon layer of states and a line layer of streets. What I want is all streets that lie within a certain state, either as an additional attribute or as a sub-layer.

In the end, I only want to show the streets within a certain state.

Extension: Some polygons overlap with each other. In these cases, the lines should be shown for both polygons.

  • How can state polygons overlap? Use Vector/Geoprocessing tools/Intersect...
    – Zoltan
    Commented Sep 28, 2016 at 19:48
  • @ Zoltan: There is a version where I need the actual states and one where I need a buffer around the states. And the "buffered states" overlap, obviously. Commented Sep 28, 2016 at 21:35
  • 1
    Intersect the buffers with the roads, this should add the polygon attributes to the lines then using the feature subset dialog qgistutorials.com/en/docs/working_with_attributes.html restrict to only the state (buffer) in question. Commented Sep 28, 2016 at 22:11
  • @MichaelMiles-Stimson: This worked. Now I know the difference between "Clip" and "Intersect" Commented Sep 30, 2016 at 10:11

2 Answers 2


Here's a solution using a python script so you dont have to do any of your own pesky click and select mouse operations.

  1. Open the command console by clicking "Plugins/Python Console" or ctrl-alt-P
  2. Click the "show editor" icon (looks like a notepad with a pen)
  3. click the "New editor" icon (looks like a green plus)
  4. Copy and paste the code below and edit it to point to the path of your relevent files.
  5. Save your script by clicking the save-as button (blue disk with a pen)
  6. Run your script by clicking the "run script" icon (its a blue arrow)
  7. If you want this to run faster, this process is embarrasingly parallel, and can be parallelised by following the discussion at How can I parallelise embarrassingly parallel GIS operations in QGIS python?

Here's the code for your script (remember to edit the paths to your actual files)

import processing
import os
input_layer = QgsVectorLayer(input_file, "input file", "ogr")
tile_layer  = QgsVectorLayer(clip_polygons_file, "clip_polys", "ogr")
clipping_polygons = tile_layer.getFeatures()
for clipping_polygon in clipping_polygons:
    print "Tile no: "+str(tile_no)
    geom = clipping_polygon.geometry()
    "&field=id:integer&index=yes","clip_polygon", "memory")
    clip_layer_dp = clip_layer.dataProvider()
    clip_layer_feature = QgsFeature()
    (res, outFeats) = clip_layer_dp.addFeatures([clip_layer_feature])
    clip_file = os.path.join(output_folder,"tile_"+str(tile_no)+".shp")
    write_error = QgsVectorFileWriter.writeAsVectorFormat(clip_layer, \
    clip_file, "system", \
    QgsCoordinateReferenceSystem(EPSG_code), "ESRI Shapefile")
    output_file = os.path.join(output_folder,str(tile_no)+".shp")
    processing.runalg("qgis:clip", input_file, clip_file, output_file)
  • Select the state you want to interserct with the streets
  • Save the selection as a new layer with the right key of the mouse
  • After you could use the"extract by location" function of the QGIS geoalgorithms under the vector selection tools category. After just run function putting first position street.shp in the second pos. state.shp and check the "within" option. Run

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.