3

I have a shapefile with a single geometry as the following:

enter image description here

I also have a GeoTIFF raster that I can put behind this vector layer... I'd like to find a way of programmatically clipping this shapefile feature from my GeoTIFF file, in a way that I get a png image as a result.

The closest solution I found to this problem is this answer. This solution solves the problem using gdalwarp with the following command line:

gdalwarp -of GTiff -cutline area_of_interest.shp -cl area_of_interest  -crop_to_cutline PCE_in_gw.asc  data_masked7.tiff 

In my case, I only have the Shapefile and the GeoTIFF file. I didn't get what I'm supposed to put on the .asc file and the area_of_interest parameter in this command line sample. What extra procedures do I have to do to clip my GeoTIFF with a Shapefile while using gdalwarp?

raylight
  • 1,241
  • 4
  • 19

2 Answers2

2

Look this example in Pyhton and Jupyter Notebook:

gdal.Warp(destNameOrDestDS = outputpath, 
          srcDSOrSrcDSTab  = intputpath, 
          cutlineDSName    = shapefile or geojson, 
          cropToCutline    = True,
          copyMetadata     = True,
          dstNodata        = 0)
Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
Helios
  • 427
  • 2
  • 8
2

From the command line you can do the following:

gdalwarp -of GTiff -cutline input.shp -crop_to_cutline input.tif input_clip.tif

Another alternative is with pkcrop from pktools

pkcrop -i input.tif -e input.shp -cut -o input_clip.tif

dmci
  • 4,882
  • 2
  • 19
  • 31