I am new in QGis python plugins dev and I have a "maybe stupid" question (in the title :-) ).

I try to retrieve pixels (from a raster layer) which are "contained" in a polygon defined in a vector layer. How can I achieve that via a python plugin ? The user has to select the vector layer and the raster layer using the GUI of the plugin (which is already coded).

Thanks in advance !

1 Answer 1


Do you need to do it by plugin? The QGIS GUI has the functionality already built in (v.1.8.0) at Raster > Extraction > Clipper.

Set "Input file (raster)" to your raster.

Then set "Clipping mode" to "Mask Layer". The picklist will show layers already in the GUI, or you can use the Select... button to select an unloaded mask.

EDIT after comment below

You might try agoodle. I haven't used it myself, but it looks like a closer fit to your comments below. You can use polygons to clip a raster, then access the raster cell values in a numpy array.

... or gdallocationinfo, as mentioned here and documented here.

  • Your answer is very interesting and do you think that I can access this clipped area via my plugin ? So, my plugin has to get these pixels contained in a shape in order to compute several complex metrics over these pixels. So, my main question is how can I get these pixels (in an array, in a list or something else) in order to compute complex function over. NB : the raster layer displays a fully calibrated picture in which the pixel value is very important and will be the input data of complex metrics process. Oct 22, 2012 at 20:18
  • I have tried to clip in the way you have described. The result is not the one that I expect, in fact, clipping tool just clips following the smallest rectangle containing all the polygons drawn in the vector layer :-( Oct 22, 2012 at 20:32
  • @Lambdacrash, okay, I think I understand your question better now. You'd like to access those pixel values after you've clipped them; I've extended the answer with a couple possibilities for that. I admit that I haven't used those myself. I'd like to try them out now, though!
    – andytilia
    Oct 23, 2012 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.