Wednesday 3 June 2015

gdal - Erase shapefile (polygon) area from GeoTIFF


I've been reading several posts to do so and one option would be to use gdalwarp -cutline, but I would need to keep what it is NOT inside the polygon area. GDAL or any command-line tool does that?



Answer



Draw the area(s) you want to hide from the image and save as vectors into shapefile or other format if you prefer. Then use the gdal_rasterize utility http://www.gdal.org/gdal_rasterize.html which burns fixed, non-transparent pixels into your image and removes permanently image data below the polygons.


Here is an example. The map is a RGB tiff image with RGB 0,0,0 defined as nodata value. Create two shapefiles red.shp and nodata.shp and save them on disk.


enter image description here


Run two gdal_rasterize commands. The first one is painting with red (RGB 255,0,0), the second one with totally black (RGB 0,0,0)


gdal_rasterize -b 1 -b 2 -b 3 -burn 255 -burn 0 -burn 0 red.shp burntest.tif

gdal_rasterize -b 1 -b 2 -b 3 -burn 0 -burn 0 -burn 0 nodata.shp burntest.tif

Open the image "burntest.tif" with a viewer that knows what to do with nodata


enter image description here


However, if the viewer does not know what to do with nodata the image will look different.


enter image description here


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...