4

The overall aim of this is to clip a raster (rlayer), feature by feature of a shapefile (vlayer). Then run univariate stats on each clipped DEM. Passing the answer to a table for output.

However I have fallen at the first hurdle and cannot get clip to work. Example error:

Unable to execute algorithm
Wrong parameter value: <qgis._core.QgsRasterLayer object at 0x000000002455D9D8>

My code as of the moment is:

#Load the required modules
from qgis.core import*
import qgis.utils
import processing
import os

#Load administrative polygons
vlayer = (iface.addVectorLayer
("C:/Users/geol-tdi/Documents/UK_NUTS.shp",
"nutspoly",
"ogr"))
if not vlayer:
    print "Vector layer failed to load!"

#Load raster for analysis
rlayer = (iface.addRasterLayer
("C:/Users/geol-tdi/Documents/Rasters/UKwheatPEC80div50.tif", 
"WheatD"))
if not rlayer:
    print "Raster layer failed to load!"

#Iteratively analyse admin polys. 
vlayer.selectAll()
features = vlayer.getFeatures()

for feature in features:
    vlayer.setSelectedFeatures([feature.id()])
    print "Feature ID %d: " % feature.id()
    processing.runalg('qgis:clip',rlayer, feature, None )  
    vlayer.removeSelection()

vlayer.removeSelection()

I have tried variations such as naming the selected feature before passing it to the algorithm but that fails as well. Output is currently set to NONE to allow testing/pass the result to the univaraite stats alg when I get there.

I am aware this is likely a simple problem/mistake, but I am relatively new to this.

Note that I have already tried v.rast.stats from the processing toolbox, it fails to write any output to the shapefile, for reasons unknown.

2

The input layer and clip layer for that processing tool only clips vector data, not raster data. You are trying to clip a raster layer.

Input layer [vector: any] Clip layer [vector: any]

https://docs.qgis.org/2.8/en/docs/user_manual/processing_algs/qgis/vector_overlay_tools.html

To clip a raster by vector see:

Clipping a raster using an irregular polygon with Python

How to clip a raster by a shapefile in pyqgis?

  • I knew it was something silly... many thanks indeed for your time and excellent links. – Tom Dowling Jun 7 '17 at 15:52
  • However, I am still failing to pass the selected polygon to the now correct clip algorithm sorry. – Tom Dowling Jun 7 '17 at 17:10
  • To close this question, I changed plan as still could not get clip to accept the passed/selected polygon. Therefore have split the polygons and am running stats from there. – Tom Dowling Jun 8 '17 at 14:54

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.