Skip to content

Instantly share code, notes, and snippets.

@brookisme
Created July 19, 2018 22:56
Show Gist options
  • Save brookisme/af2d187a6d8010af3389f14f65a99010 to your computer and use it in GitHub Desktop.
Save brookisme/af2d187a6d8010af3389f14f65a99010 to your computer and use it in GitHub Desktop.
Add Colormap To GeoTiff
from __future__ import print_function
import re
import click
import rasterio
CLR_IDENT='clr'
CMAP={
1: (48, 124, 38),
2: (138, 216, 168),
3: (105, 224, 8),
4: (21, 80, 89),
5: (134, 232, 227),
6: (242, 209, 43),
7: (247, 236, 183),
8: (4, 219, 242)
}
@click.command(help='add colormap to geotiff')
@click.argument('src')
@click.argument('dst',default=None,required=False)
def add_colormap(src,dst=None):
if not dst:
dst=re.sub('.tif','.{}.tif'.format(CLR_IDENT),src)
print('add_cmap: read {}'.format(src))
with rasterio.open(src) as src:
im = src.read()
meta = src.meta
print('add_cmap: write {}'.format(dst))
with rasterio.open(dst, 'w', **meta) as dst:
dst.write(im)
dst.write_colormap(1, CMAP)
if __name__ == '__main__':
add_colormap()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment