1

I am trying to crop an image using GDAL python. I have tried using both gdal.Warp and gdal.Translate, and both keep bringing the same result.

I have an image that I have orthorectified using gdal.Warp called imortho. I want to select a small window from this image:

window = [-76.845, 39.054, -76.805, 39.012]

This is what I have tried:

gdal.Translate(outfile, imortho, projWin = window)

The size of the square is accurate, but almost half of the image has no data. The original image has data on the entirety of the window (and beyond).

When I try to do the same, specifying the projWinSRS option the result is the same. When I try to use the gdal.Warp function instead:

gdal.Warp(outfile, imortho, outputBounds = window)

The result is similar (even though the nodata region is in a different place). See the image:

enter image description here Is this a bug or is there anything I'm not doing correctly to crop the data?

1 Answer 1

1

Look at this code:

  • Use a irregular geometry
clip = gdal.Warp(destNameOrDestDS = outputpath, 
                 srcDSOrSrcDSTab  = intputpath, 
                 cutlineDSName    = shapefile or geojson, 
                 cropToCutline    = True,
                 copyMetadata     = True,
                 dstNodata        = 0)      # Select the nodata value
clip = None


  • Use the image extent (bounds)

clip =  gdal.Warp(destNameOrDestDS = outputpath, 
                  srcDSOrSrcDSTab  = inputpath,
                  outputBounds     = (minx, miny, maxx, maxy),
                  cropToCutline    = True,
                  copyMetadata     = True)

clip = None

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.