2

I'm fairly new to GDAL and I have some GeoTIFF files that I would like to highlight some bodies of water on. I have gotten pretty far, but I have one problem. The only way I am able to highlight a body of water is with gdal.warp and cutlineDSName (I'm using osgeo gdal in Python), which is great, but it cuts out the rest of the image completely. What I would like to do is just draw a line, rather than cut the surrounding area out.

Can anyone help me out?

I'm hoping it's a flag that I am missing somewhere.

code below.

tifFile = 'C:\\merged.tif'
shapeFile = 'C:\shapefile.shp'
outFile = 'C:\\merged_and_cropped.tif'

input_raster = tifFile
output_raster = outFile
input_kml = shapeFile

ds = gdal.Warp(output_raster,
              input_raster,
              format = 'GTiff',
              cutlineDSName = shapeFile,
              cropToCutline=True,
              dstNodata = 0)
ds = None
3

you can use gdal_rasterize to do this.

It will let you 'burn' values into a raster from lines or polygons. In some cases it'll create a new raster but it might update your existing raster, so take a backup of your raster before experimenting ;-)

An example (3 bands, for aerial imagery for example) from that site which draws a line/polygon from outline.shp into work.tif . It'll use Red (255 0 0).

gdal_rasterize -b 1 -b 2 -b 3 -burn 255 -burn 0 -burn 0 -l mask outline.shp work.tif

If your raster is a polygon the interior will be filled, if you're after an outline you need to convert the polygon to a line first.

Depending on output resolution, you could also get a thicker outline by buffering the cut line.

  • Thanks! I got the interior filled, as you stated. Now I just need to get the outline. Sorry, I am very new to this - when you say to convert the polygon to a line, do you mean to do that in the shapefile? – jonesy19 Jul 17 '18 at 19:56
  • see this answer. It's seven years old old, but this hasn't changed much since then and should still work. – Steven Kay Jul 17 '18 at 20:37
  • Another question - I am trying to get a thicker outline, as you suggested above. You mentioned I could do that by buffering the cut line. How would I do that? I have tried -l buffer_lines, but that doesn't work. I also tried to modify the .shp file by putting a thicker line in, but that also doesn't work. Any thoughts? – jonesy19 Aug 23 '18 at 16:58

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.