def translate(src, dest, of): """Run gdal_translate to convert raster format.""" cmd = 'gdal_translate %s %s -of %s' % ( src, dest, of ) try: run(cmd) except CommandError, e: log.error(e.message) sys.exit(1)
def warp(src, dest, s_srs, t_srs='WGS84', t_res=None, t_extent=None, of='VRT'): """Run GDAL warp to modify spatial reference of raster.""" cmd = 'gdalwarp %s %s -overwrite' % (src, dest) if t_srs is not None: cmd += ' -s_srs \"%s\" -t_srs \"%s\"' % (s_srs, t_srs) if t_res is not None: cmd += ' -tr %f %f' % (t_res, t_res) if t_extent is not None: cmd += ' -te %f %f %f %f' % ( t_extent[0][0], t_extent[0][1], t_extent[1][0], t_extent[1][1] ) cmd += ' -of %s' % of try: run(cmd) except CommandError, e: log.error(e.message) sys.exit(1)
def color_relief(src, dest, colormap, of='VRT', nearest=True): """Run gdaldem color-relief to produce rgba image from raster.""" tmp_colormap = NamedTemporaryFile() for rec in colormap: tmp_colormap.write('%f %i %i %i %i\n' % rec) tmp_colormap.flush() if path.exists(dest): remove(dest) cmd = 'gdaldem color-relief %s %s %s -of %s -alpha' % ( src, tmp_colormap.name, dest, of ) if nearest: cmd += ' -nearest_color_entry' try: run(cmd) except CommandError, e: log.error(e.message) sys.exit(1)