Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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 !

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
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. –  Lambdacrash Oct 22 '12 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 :-( –  Lambdacrash Oct 22 '12 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 '12 at 0:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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