使用rasterio从栅格数据中编写带有颜色映射的TIFF。

huangapple go评论102阅读模式
英文:

Writing a tiff with colormap from rasterio

问题

以下是您要翻译的代码部分:

  1. import xarray as xr
  2. import rioxarray
  3. import cftime
  4. import os
  5. import datetime
  6. from rasterio.enums import Resampling
  7. import matplotlib.pyplot as plt
  8. ds_input = "inputfile.nc"
  9. operatingdir = '\\'
  10. filename = ds_input[:-3]
  11. # determine the maximum number of bands in the raster using the band name in position 0 of the common_key list
  12. bandcount = 7
  13. # create a colormap for the output raster, with 5 colour bands
  14. cmap = plt.cm.get_cmap('viridis', 5)
  15. # use a for loop to output each day of the forecast
  16. for i in range(0, bandcount):
  17. # Convert ds_netcdf_interp to raster using the max fbi values
  18. ds_input.var_name.rio.to_raster('{}\\Output\\{}_number{}.tif'.format(operatingdir, filename, i),
  19. windowed=True,
  20. compress='lzw',
  21. dtype='int16',
  22. nodata=-9999,
  23. overwrite=True,
  24. tiled=True,
  25. cloud_optimized=True,
  26. driver='COG',
  27. colormap=cmap
  28. )

希望这对您有所帮助。

英文:

I'm writing a single band tif using the code below. What I'm most interested in doing is writing a colormap for band 0 along with the tiff.

The code below is iterating through some other files and is successfully writing out each band of each input. I would like to be able to apply a colormap to each output. Below I'm trying to ask for viridis to be limited to 5 categories.

I would like to avoid explicitly avoid importing GDAL - and this seems entirely possible.

So my question is:
How do I write out the file with the viridis colormap applied to it? If necessary, apply on band[0]

  1. import xarray as xr
  2. import rioxarray
  3. import cftime
  4. import os
  5. import datetime
  6. from rasterio.enums import Resampling
  7. import matplotlib.pyplot as plt
  8. ds_input = "inputfile.nc"
  9. operatingdir = '\\'
  10. filename = ds_input[:-3]
  11. #determine the maximum number of bands in the raster using the band name in position 0 of the common_key list
  12. bandcount = 7
  13. #create a colormap for the output raster, with 5 colour bands
  14. cmap = plt.cm.get_cmap('viridis', 5)
  15. #use a for loop to output each day of the forecast
  16. for i in range(0, bandcount):
  17. #Convert ds_netcdf_interp to raster using the max fbi values
  18. ds_input.var_name.rio.to_raster('{}\\Output\\{}_number{}.tif'.format(operatingdir, filename, i),
  19. windowed=True,
  20. compress='lzw',
  21. dtype='int16',
  22. nodata=-9999,
  23. overwrite=True,
  24. tiled=True,
  25. cloud_optimized=True,
  26. driver='COG',
  27. colormap=cmap
  28. )

答案1

得分: 1

你可以使用rasterio的write_colormap()方法将色彩映射写入TIFF文件。以下是如何从文档中使用的简短示例:

  1. import rasterio
  2. with rasterio.Env():
  3. with rasterio.open('tests/data/shade.tif') as src:
  4. shade = src.read(1)
  5. meta = src.meta
  6. with rasterio.open('/tmp/colormap.tif', 'w', **meta) as dst:
  7. dst.write(shade, indexes=1)
  8. dst.write_colormap(
  9. 1, {
  10. 0: (255, 0, 0, 255),
  11. 255: (0, 0, 255, 255) })
  12. cmap = dst.colormap(1)
  13. # True
  14. assert cmap[0] == (255, 0, 0, 255)
  15. # True
  16. assert cmap[255] == (0, 0, 255, 255)

基本上,write_colormap方法接受带号作为第一个参数,然后是以栅格值为键、其对应的RGBA值为值的字典。

英文:

You can write a colormap to a tiff file using rasterios write_colormap(). Here's a short example on how to use it from the documentation:

  1. import rasterio
  2. with rasterio.Env():
  3. with rasterio.open('tests/data/shade.tif') as src:
  4. shade = src.read(1)
  5. meta = src.meta
  6. with rasterio.open('/tmp/colormap.tif', 'w', **meta) as dst:
  7. dst.write(shade, indexes=1)
  8. dst.write_colormap(
  9. 1, {
  10. 0: (255, 0, 0, 255),
  11. 255: (0, 0, 255, 255) })
  12. cmap = dst.colormap(1)
  13. # True
  14. assert cmap[0] == (255, 0, 0, 255)
  15. # True
  16. assert cmap[255] == (0, 0, 255, 255)

Basically write_colormap takes the band number as the first argument and then a dictionary of raster value as keys and their corresponding RGBA values as values.

huangapple
  • 本文由 发表于 2023年6月8日 13:14:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定