I am using QGIS.

Is it possible to clip a raster when the shapefile overlaps with it by 10%?

Clip raster by mask layer tool is fine for Ontario (left) but makes me lose a lot of data from smaller locations (right). Need a rule to ensure consistency but don't know how to go about it.

Does anybody know of a function / have a code which could allow me to only cut the raster if 10% of the shapefile overlaps it?

enter image description here

  • I'm confused... Where is the shapefile in your screenshot(s)? And I can't picture the data loss you mention... – Stu Smith Jul 3 '19 at 5:08

Here's a workflow.

  • Use 'Zonal Statistics' algorithm with 'Count' statistic for the raster. You will have an attribute in the polygon layer with number of raster pixels within each polygon
  • The total expected pixels in the polygon are area / (raster resolution)^2. If raster resolution is 10m x 10m and the polygon area is 100000 sq m, the polygon should have ~1000 pixels for 100% overlap. You can compare that with the actual pixels counted from zonal statistics and get an idea of the overlap. Note that this is not perfect, but an approximation, as the shape of the polygon will have an impact on how many pixels should be within in. Add a new column 'overlap' using the formula such as below

    100*"Count"/ ($area/(res*res)) (Replace res with raster resolution)

  • The 'overlap' column will now have a percentage overlap with the polygon. Use that to select polygons with expression such as "overlap" >10 and run the Clip by mask layer algorithm with 'selected features only' enabled.

| improve this answer | |

I can't provide a working code at the moment but here are the steps which can be followed by standards GIS operations as well:

  1. convert your raster to vector
  2. select only your desired polygons based on the pixel value and discard the rest
  3. Use an intersect operation to get the "common" ground between your vectorized raster layer and your mask layer
  4. compare the area of the intersection with your vectorized raster layer. if they differ more than 10% then discard this feature from the mask layer
  5. clip the original raster with the updated mask layer.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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