如果您拥有所需格式的高程数据,那么这是最简单的选择:它可以正常工作。但是,如果您想要一个更加定制化的解决方案,以下是我为使用 Mapbox Terrain-RGB 格式的项目所做的:
我有从深蓝色到浅蓝色到白色的颜色比例。我希望能够指定从浅蓝色到白色使用多少步(默认为 10)。此代码使用 GDAL Python 绑定。以下代码片段用于测试。它只是将颜色映射输出到 GeoTIFF 文件。要获取 0 到 1 之间的值,只需使用value *= 1/num_steps
. 您可以在查找表中使用该值来获取 RGB 值。如果您只对输出颜色感兴趣,则可以忽略所有涉及gdal_translate
. 颜色将自动存储在单波段 GeoTIFF 中。如果您确实想重复使用这些颜色,请注意此版本会忽略 alpha 值(如果存在)。您可以使用gdal_translate
添加这些。该代码片段也可以在我的 gist 中找到。
import numpy as np
import gdal
from osgeo import gdal, osr
def get_color_map(num_steps):
colors = np.zeros((num_steps, 3), dtype=np.uint8)
colors[:, 0] = np.linspace(0, 255, num_steps, dtype=np.uint8)
colors[:, 1] = colors[::-1, 0]
return colors
ds = gdal.Open('/Users/myusername/Desktop/raster.tif')
band = ds.GetRasterBand(1) # Assuming single band raster
arr = band.ReadAsArray()
arr = arr.astype(np.float32)
arr *= 1/num_steps # Ensure values are between 0 and 1 (or use arr -= arr.min() / (arr.max() - arr.min()) to normalize to 0 to 1)
colors = get_color_map(num_steps) # Create color lookup table
colors[0] = [0, 0, 0] # Set black for no data so it doesn't show up as gray in the final product.
# Create new GeoTIFF with colors included (transparent alpha channel if possible). If you don't care about including the colors in the GeoTIFF, skip this.
cols = ds.RasterXSize
rows = ds.RasterYSize
out_ds = gdal.GetDriverByName('GTiff').Create('/Users/myusername/Desktop/raster_color.tif', cols, rows, 4)
out_ds.SetGeoTransform(ds.GetGeoTransform())
out_ds.SetProjection(ds.GetProjection())
band = out_ds.GetRasterBand(1)
band.WriteArray(colors[arr]) # This can be removed if you don't care about including the colors in the GeoTIFF
band = out_ds.GetRasterBand(2)
band.WriteArray(colors[arr]) # This can be removed if you don't care about including the colors in the GeoTIFF
band = out_ds.GetRasterBand(3)
band.WriteArray(colors[arr]) # This can be removed if you don't care about including the colors in the GeoTIFF
band = out_ds.GetRasterBand(4)
alpha = np.zeros((rows, cols), dtype=np.uint8) # Create alpha channel to simulate transparency of no data pixels (assuming 0 is "no data" and non-zero is data). You can remove this if your elevation values are not 0.
alpha[arr == 0] = 255
band.WriteArray(alpha) # This can be removed if you don't care about including the colors in the GeoTIFF
out_ds.FlushCache()
使用具有多个值的调色板时,Rasterio 中也存在此问题。这是一个例子。
但是,如果您的栅格具有 n 维或者是掩码数组,则翻转操作可能会很棘手。这是基于此 stackoverflow 问题中的一个答案的解决方案:如何垂直翻转 2D NumPy 数组?.