I have a GeoTiff raster in which half of its area is nodata. The problem is that the raster extent includes the nodata area.

Is there a way with GDal to recalculate the extent of the raster excluding the nodata areas?


Thanx to @user30184 here is how to get the raster extent of an image without nodata border.

  1. Replace all nodata pixels with 0 and all other pixels with 1

    gdal_calc.py -A source_image.tif --outfile out.tif --NoDataValue 0 --calc="1*(A>0)"

  2. Get the shape file for the meaningful area.

    gdal_polygonize.py -8 -b 1 -f "ESRI Shapefile" out.tif pols

  3. Clip the source_image.tif with the shape file to create a new tif with updated extent.

    gdalwarp -of GTiff -cutline pols/out.shp -crop_to_cutline -dstalpha 3559007_2014-05-08_RE5_3A_181809processed.tif OUTPUT.tif

Source Image

  • I do not know any direct way for that but if you have only a few images you could perhaps have a try by vectorizing them with gdal_polygonize.py and deleting the no-data polygons from the resulting vector layer. – user30184 Nov 5 '14 at 8:56
  • It is too slow, my file is 240MB and after the polygonization the result shp files becomes bigger then 500MB. – bozhidarc Nov 5 '14 at 9:13
  • 1
    How about opening image into QGIS and digitizing extents manually as a polygon? Or before running gdal_polygonize.py use gdal_calc.py and convert image to 1-bit image: nodata pixels=0 and other pixels=1. – user30184 Nov 5 '14 at 9:29
  • Replacing all meaningful pixels and all nodata pixels with 1 and 0 works perfect! But my new problem is how to recalculate the extent for the result shape file according to the old extent. – bozhidarc Nov 5 '14 at 11:37
  • this is essentially the same question as gis.stackexchange.com/questions/45159/…. The embedded answer should be extracted and placed as an answer and fetch it's due up-votes. (tip: to speed up processing time and reduce redundant disk storage, use a "virtual raster table" (vrt) for intermediate steps). – matt wilkie Jun 15 '15 at 21:41

Browse other questions tagged or ask your own question.