2

I've loaded two shape files using pyqgis like so:

data_layer = QgsVectorLayer("lines.shp", "highways", "ogr")
clip_layer = QgsVectorLayer("polygon.shp", "highways", "ogr")

As the names of the source files suggest one of the files consists of line geometry data and the other of polygon data. I'd like to do the GUI operation Vector->Geoprocessing Tools->Clip operation in code. Something like: new_layer = QgsClip(clip_layer, data_layer)

I notice QgsClipper in the API docs but that only does rectangular clipping as far as I can see.

Is there a function that does what I want?

2 Answers 2

1

If you want to use the QGIS algorithms you can use de processing module (Using processing algorithms from the console)

import processing
processing.alglist("clip")
Clip------------------------------------------------->qgis:clip
Clip Multiple Rasters-------------------------------->script:clipmultiplerasters
Clip grid with polygon------------------------------->saga:clipgridwithpolygon
Clip points with polygons---------------------------->saga:clippointswithpolygons
Clip raster by extent-------------------------------->gdalogr:cliprasterbyextent
Clip raster by mask layer---------------------------->gdalogr:cliprasterbymasklayer
Clip vectors by extent------------------------------->gdalogr:clipvectorsbyextent
Clip vectors by polygon------------------------------>gdalogr:clipvectorsbypolygon

The one you were looking for is qgis:clip

processing.alghelp("qgis:clip")
ALGORITHM: Clip
   INPUT <ParameterVector>
   OVERLAY <ParameterVector>
   OUTPUT <OutputVector>

This command is illustrated in many answers in GIS SE -> Is there a way to call the clip function in pyQGIS from the python console?

2
  • This is the correct answer and works fine when I do this within the QGIS console. I should have added that I want to do this in a standalone script. When I do so it fails with Unable to execute algorithm
    – lovelyzoo
    Commented Oct 30, 2015 at 15:28
  • This is the correct answer and works fine when I do this within the QGIS console. I should have added that I want to do this in a standalone script. When I do so it fails with "Unable to execute algorithm. Wrong parameter value: path/to/lines.shp". Is there an obvious answer here or should I ask a new question?
    – lovelyzoo
    Commented Oct 30, 2015 at 15:34
0

Next code:

mapcanvas = iface.mapCanvas()

layers = mapcanvas.layers()

feats1 = [ feature for feature in layers[0].getFeatures() ]

feats2 = [ feature for feature in layers[1].getFeatures() ]

n_feats1 = len(feats1)
n_feats2 = len(feats2)

#comprehension list
geom3 = [ feats1[i].geometry().intersection(feats2[j].geometry()).exportToWkt()
          for i in range(n_feats1)
          for j in range(n_feats2)
          if feats1[i].geometry().intersects(feats2[j].geometry()) ]

for item in geom3:
    print item

it was ran at the Python Console for the two vector layers (line and polygon6 equivalents to your data_layer and clip_layer; respectively) of following image:

enter image description here

and I got this:

enter image description here

Geometry geom3 (lines that intersect the polygon) is in WKT notation and it was visualized with the QuickWKT plugin.

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.