5

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.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jun 6, 2017 at 12:27
0

1 Answer 1

3

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?

answered Jun 6, 2017 at 12:44
3
  • I knew it was something silly... many thanks indeed for your time and excellent links. Commented Jun 7, 2017 at 15:52
  • However, I am still failing to pass the selected polygon to the now correct clip algorithm sorry. Commented Jun 7, 2017 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. Commented Jun 8, 2017 at 14:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.