I recently posted an answer to Zonal stats for complete pixels qgis that required clipping a raster in a way that excludes pixels that fall partially outside the mask layer. Here's an illustration: enter image description here

The best method I could figure out is as follows:

  1. Polygonize the raster.
  2. Use Select by location tool to select features in Vectorized layer that fall within the polygon layer.
  3. Clip raster by mask layer using the selected features of the Vectorized layer as the mask layer. Don't crop the extent.

But this method seems like a lot of steps, and polygonizing a large raster can take a long time.

Is there a better method of clipping a raster and excluding cells that overlap the edge of the mask?

By "better" I mean fewer steps and/or requiring less processing time.


1 Answer 1


I'm not aware of a better way to do this using QGIS. I wrote the exactextractr package for R out of a need to solve problems like this, where the nature of polygon/raster cell intersections is important. It is much, much faster than creating a polygon for each cell and doing an intersection calculation. If you can use R, maybe it will be of use to you. Here is an example:


elev <- aggregate(raster(elevation_global(res = 10, path = tempdir())), 3)
mexico <- st_as_sf(gadm(country = 'MEX', level = 0, path = tempdir()))

# Get a list with one mask for each input feature.
# Each cell of the mask will have a value from 0 to 1
# indicating the fraction of the cell that is within the
# feature.
# We have only one input feature, so grab the first
# element.
mask <- coverage_fraction(elev, mexico)[[1]]
# Set values in the mask to NA where less than 100% of
# the cell area is within the polygon
mask[mask < 1] <- NA

plot(mask*elev, add=TRUE)

This produces the following output:

map of mexico showing only raster cells 100% within the boundary

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.