I have a problem with clipping a raster with a polygon(shapefile). I have a polygon of a small river, and a sentinel 2 tif file. I want to clip the river off the sentinel image, but keep only the pixels that cut my polygon line an are inside above 50%.

Is there a way to do it in QGIS?

  • 1
    You have tried to clip, what is the problem? You get the whole pixels if they touch your boundary?
    – BERA
    Sep 6, 2021 at 12:23
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Sep 6, 2021 at 12:24
  • Ok, sorry for misinformation. I tried to clip and I did it. I did it with two ways that don't solve my problem. First i clipped and it kept all the pixels that their center is on the inside of the polygon and Secondly, every pixel that get in touch with the polygon with gdal library. I am wondering if there is a way to make a choice of which pixels to be clipped.I would like to keep only the pixels that the 50% of them is in the inside of the polygon. Thanks. I hope i made it more clear. Sep 6, 2021 at 12:51
  • See here how to get the percentage each pixel overlaps with a polygon: gis.stackexchange.com/a/397101/88814
    – Babel
    Sep 7, 2021 at 7:33

1 Answer 1


"Clipping" a raster with a polygon internally converts the polygons to raster before masking the pixels. In order to have full control on the clipping, you should therefore split your processing in these two steps 1) convert polygon to raster (=rasterize) 2) mask the pixels.

So the hard part is about getting labelling your pixels as river if they are covered by more than 50 %. gdal_rasterize (and QGIS, which directly uses gdal) only includes to methods (center of the pixel inside the polygon OR pixel touched by the boundary of the polygon.

The workaround is to rasterize your polygons withat a higher resolution (e.g. 2 m) with a constant value of 1 (based on a field). Then gdalwarp the result with "-r sum" at the very same resolution (e.g. -tr 10 10 ) and extent (with -te ) than your Sentinel image.

You can then use your favorite tool to mask your Sentinel image when the sum is larger than your threshold (in ùmy example, sum > 12 because you have 25 pixels, but you can change the number of pixels depending on your compromise between processing time and precision )

Your Answer

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

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