I have a raster of North America and I'd like to remove the larger lakes from it, replacing them with the same no-data value as the original raster.
So I downloaded Natural Earth's 10 meter lakes vector data, which has lakes conveniently ranked by scale rank: 0 for large lakes like the Great Lakes, and larger scale ranks for more minor lakes.
The closest I've come with
$ gdalwarp -cutline Data/ne_10m_lakes/ne_10m_lakes.shp \ -csql "SELECT * FROM ne_10m_lakes WHERE scalerank=0" \ INPUT.tif lakes-only.tif
This actually does the opposite of what I want, it keeps only the Great Lakes portions of my raster, replacing all other non-lake pixels with the no-data value.
My question: how can I invert the cutline, so instead of getting the area inside the lake boundary, I get whatever is outside it? Can I invert the original Natural Earth .shp file, perhaps?
In How to invert polygons (or other options for exterior styling)?, GRASS'
r.mask was suggested, which has an inverse mode. Is there something similar with OGR/GDAL?
In How to get an ocean shapefile from a very detailed land area shapefile?, the .shp file was converted to a raster, then inverted, and used as a mask. I'd prefer not to generate a raster of non-lakes because my original TIFF raster is very large (high resolution) and I'd prefer not to generate a raster of the same size. As a stopgap though, I was able to do this:
# Create a not-lakes raster of the same spacing and extent as INPUT $ gdal_rasterize -i -burn 255 -a_nodata 0 -ot Byte \ -te `gdalinfo INPUT.tif | egrep "Lower Left|Upper Right"|cut -d"(" -f2 | cut -d")" -f1 | sed 's/,//'| tr "\n" " "` \ -sql "SELECT * FROM ne_10m_lakes WHERE scalerank=0" \ -tr `gdalinfo INPUT.tif |grep "Pixel Size"|cut -d"(" -f2|sed 's/[,)]/ /g'` \ Data/ne_10m_lakes/ne_10m_lakes.shp lakes.tif # Apply the lakes raster to input as some kind of mask $ gdal_calc.py -A INPUT.tif -B lakes.tif --outfile OUTPUT.tif --calc='A' \ --NoDataValue=0
Can something similar be done without creating a gigantic non-lake raster?