1

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 Sep 28 '16 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. – KarlKlammer Sep 28 '16 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. – Michael Stimson Sep 28 '16 at 22:11
  • @MichaelMiles-Stimson: This worked. Now I know the difference between "Clip" and "Intersect" – KarlKlammer Sep 30 '16 at 10:11
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_file="/path/to/input_file.shp"
clip_polygons_file="/path/to/polygon_file.shp"
output_folder="/tmp/test/"
input_layer = QgsVectorLayer(input_file, "input file", "ogr")
QgsMapLayerRegistry.instance().addMapLayer(input_layer)
tile_layer  = QgsVectorLayer(clip_polygons_file, "clip_polys", "ogr")
QgsMapLayerRegistry.instance().addMapLayer(tile_layer)
tile_layer_dp=input_layer.dataProvider()
EPSG_code=int(tile_layer_dp.crs().authid().split(":")[1])
tile_no=0
clipping_polygons = tile_layer.getFeatures()
for clipping_polygon in clipping_polygons:
    print "Tile no: "+str(tile_no)
    tile_no+=1
    geom = clipping_polygon.geometry()
    clip_layer=QgsVectorLayer("Polygon?crs=epsg:"+str(EPSG_code)+\
    "&field=id:integer&index=yes","clip_polygon", "memory")
    clip_layer_dp = clip_layer.dataProvider()
    clip_layer.startEditing()
    clip_layer_feature = QgsFeature()
    clip_layer_feature.setGeometry(geom)
    (res, outFeats) = clip_layer_dp.addFeatures([clip_layer_feature])
    clip_layer.commitChanges()
    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")
    QgsMapLayerRegistry.instance().addMapLayer(clip_layer)
    output_file = os.path.join(output_folder,str(tile_no)+".shp")
    processing.runalg("qgis:clip", input_file, clip_file, output_file)
    QgsMapLayerRegistry.instance().removeMapLayer(clip_layer.id())
1
  • 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, privacy policy and cookie policy

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