I am trying to clip trees out of a raster layer using polygon I've traced around them. I've created one polygon that I've copied for each tree so that each clipped raster contains the same width and height in pixels. But for some reason, some pixels from the edge become 0 values after the clip, and the pixel will have shifted. I'm guessing it is because my mask layer doesn't cover some pixels completely around the edges. There is no way to snap a polygon to edges of raster pixels that I know of. So how could I avoid this issue.

2 Answers 2


I don't know of a method of snapping the polygon to the raster, but one solution may be to rasterize the polygon to the same extent and resolution of the raster you want to mask, and then use this as a mask.

This may still cause a shift if the image is resampled, like in this question: gdalwarp causing shift in pixels


Nathan Thomas's answer did not directly answer my question. But finding out how to use gdal warp to clip my raster worked well. using and Average resampling method, the No data values were resampled to an average of the pixels around them. Here is the code I ended using:

Clip = gdal.Warp(Outraster, Inraster, format = 'GTiff', cutlineDSName = polygon, cropToCutline = True, resampleAlg = gdal.GRA_Average)

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.