I can extract a rectangle from a raster using gdalwarp:

gdalwarp -te <x_min> <y_min> <x_max> <y_max> input.tif clipped_output.tif

Is there a way to do this in GDAL using a circle instead of a rectangle?

  • 1
    Rasters tend to be rectangles. However, you can clip a rectangle and burn pixels outside the circle to some fixed value with gdal_rasterize. See gis.stackexchange.com/questions/163371/… – user30184 Dec 12 '16 at 20:27
  • @user1186 With gdalwarp, you can do that by using one circular shapefile as 'cut line'. – xunilk Dec 12 '16 at 23:27

With gdalwarp, you can do that by using one circular shapefile as 'cut line'. One syntax of command can be:

gdalwarp -overwrite -dstnodata -999 -cutline your_circular_shape.shp original_raster.tif out.tif

In my particular case, I tried out it with raster DEM and shapefile of next image:

enter image description here

My complete command was:

gdalwarp -overwrite -dstnodata -999 -cutline /home/zeito/pyqgis_data/circle.shp /home/zeito/pyqgis_data/utah_demUTM2.tif /home/zeito/out.tif

and it worked; as it can be observed at the next image (where resulting raster was perfectly aligned with original raster).

enter image description here

  • thanks @xunilk, I do not have a circle shapefile though. I want to create it based on command line value. Similar to how I do not have a rectangle shapefile but can use gdalwarp to specify bounding box. Is there a command to create the circle shapefile? – user1186 Dec 12 '16 at 23:30
  • 1
    There are two parameters ( '-cwhere expression' and '-csql query'). You can try them (see documentation in: gdal.org/gdalwarp.html). – xunilk Dec 12 '16 at 23:46
  • thanks @xunilk, I would really appreciate if you have any example or could point me to one. – user1186 Dec 13 '16 at 0:19
  • 1
    There is no way around using a separate datasource file when using gdalwrap. Both -cwhere and -csql require a -cutline to be defined in the first place. However, it probably is very easy to simply generate a temporary circle.shp for this purpose that you discard afterwards. If you want more help with that, please edit your question and provide more details regarding data structure and scripting environment. – Senshi Dec 13 '16 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.