0

I clipped a TIFF file using gdal warp, but it is not giving out any output clipped raster. I need help in finding why it does this and how to correct this.

import gdal

ras_in=gdal.Open('C:/Users/Lake.tif')
shp_in="C:/Users/mypo.shp"
ras_out='C:/Users/Lake_clip.tif'

result=gdal.Warp(ras_out,ras_in,cutlineDSName=shp_in,cropToCutline=True,dstNodata=0)
result=None

I updated the gdal version based on the idea given here at stack exchange. Now new problem - the output clipped raster has 0 in it completely, it is just blank.

3
  • Works for me with the data from your previous question from osgeo import gdal ras_in=gdal.Open('montreal_500m.tif') shp_in="Montreal_buffer.json" ras_out='out.tif' result=gdal.Warp(ras_out,ras_in,cutlineDSName=shp_in,cropToCutline=True,dstNodata=0). Does your shapefile intersect with your raster? You seem to use an old syntax in import gdal. Is your GDAL version old as well? The current version is 3.6.0.
    – user30184
    Dec 13, 2022 at 15:18
  • @user30184 Great I updated gdal my version and it works and gives an output. But a new error pops up where the output has no data; it is completely blank.
    – SVpk
    Dec 13, 2022 at 21:07
  • @user30184 As you suggested the shapefile didn't intersect with the raster hence the output was blank. Managed to get the proper output. Thanks!
    – SVpk
    Dec 14, 2022 at 16:06

2 Answers 2

0
from osgeo import gdal

ras_in=('C:/Users/Lake.tif')
shp_in="C:/Users/mypo.shp"
ras_out='C:/Users/Lake_clip.tif'

result = gdal.Warp(srcDSOrSrcDSTab  = ras_in, 
                   destNameOrDestDS = ras_out, 
                   cutlineDSName    = shp_in,
                   cropToCutline    = True,
                   dstNodata        = 0)
result = None

### You don't need to use  '''gdal.Open'''
1
  • Thanks, I corrected it.
    – SVpk
    Dec 14, 2022 at 16:07
0

Firstly, my gdal version was an older one and secondly, the shapefile and then the raster did not intersect, which is why the error occurred.

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.