Monday, 7 November 2016

qgis - Is there a call in pyqgis that clips a layer similarly to the Geoprocessing Tool?


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?



Answer



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
OVERLAY
OUTPUT

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?


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...