Python – Clipping a Raster Using an Irregular Polygon with Python

clippyqgisqgis-processingraster

I want to clip a raster (GeoTIFF) using a polygon shapefile. Both are in the same reference system. The polygon is not a regular polygon but has many vertices.

I used this Python script below in QGIS 2.18.4

    import processing
    processing.runandload("gdalogr:cliprasterbymasklayer", \
    "/home/giacomo/Desktop/RSM/bordi.tif", \
    "/home/giacomo/Desktop/RSM/mask_vec.shp", \
    "none", \
    False, \
    True, \
    True, \
    "", \
    "", \
    0, \
    0, \
    0, \
    False, \
    "", \
    False, \
    "", \
    "/home/giacomo/Desktop/RSM/bordi_clip.tif")

But I get Error: Wrong parameter value:

The parameters required as input are

    INPUT <ParameterRaster>
    MASK <ParameterVector>
    NO_DATA <ParameterString>
    ALPHA_BAND <ParameterBoolean>
    CROP_TO_CUTLINE <ParameterBoolean>
    KEEP_RESOLUTION <ParameterBoolean>
    RTYPE <ParameterSelection>
    COMPRESS <ParameterSelection>
    JPEGCOMPRESSION <ParameterNumber>
    ZLEVEL <ParameterNumber>
    PREDICTOR <ParameterNumber>
    TILED <ParameterBoolean>
    BIGTIFF <ParameterSelection>
    TFW <ParameterBoolean>
    EXTRA <ParameterString>
    OUTPUT <OutputRaster>

Best Answer

There are several errors in your code.

Firstly, you need to specify your input layers as objects (and not as strings):

raster_filepath = "C:/Users/path_to_the_raster/bordi.tif"
mask_filepath = "C:/Users/path_to_the_shapefile/mask_vec.shp"

raster_layer = QgsRasterLayer(raster_filepath, 'raster')
mask_layer = QgsVectorLayer(mask_filepath, 'mask', 'ogr')

Then, you didn't specify the RTYPE parameter:

RTYPE(Output raster type)
    0 - Byte
    1 - Int16
    2 - UInt16
    3 - UInt32
    4 - Int32
    5 - Float32
    6 - Float64

You can't set "" as value (as you did), but a number is required.

The same with the COMPRESS parameter:

COMPRESS(GeoTIFF options. Compression type:)
    0 - NONE
    1 - JPEG
    2 - LZW
    3 - PACKBITS
    4 - DEFLATE

You can't set "" as value (as you did), but a number is required.

Therefore, you set 0 as value for JPEGCOMPRESSION, ZLEVEL and PREDICTOR parameters. They can't be equal to 0 so, if you don't know what they mean, leave the default values (i.e. 75, 6 and 1, respectively).

Then, you didn't specify the BIGTIFF parameter:

BIGTIFF(Control whether the created file is a BigTIFF or a classic TIFF)
    0 - 
    1 - YES
    2 - NO
    3 - IF_NEEDED
    4 - IF_SAFER

Finally, you can't set "" as value, but a number is required.

A working solution for you should be this:

import processing
from qgis.core import *

raster_filepath = "C:/Users/path_to_the_raster/bordi.tif"
mask_filepath = "C:/Users/path_to_the_shapefile/mask_vec.shp"

raster_layer = QgsRasterLayer(raster_filepath, 'raster')
mask_layer = QgsVectorLayer(mask_filepath, 'mask', 'ogr')

processing.runandload("gdalogr:cliprasterbymasklayer", \
raster_layer, \
mask_layer, \
"none", \
False, \
True, \
True, \
5, \
4, \
75, \
6, \
1, \
False, \
0, \
False, \
"", \
"C:/Users/path_to_the_result/output.tif")
Related Question