我有這個非常簡單的代碼:
import numpy as np
from rasterio.transform import Affine
nx = 5
maxx = 4.0
minx = -4.0
res = (maxx - minx) / nx
maxy = 3.0
miny = -3.0
ny = int((maxy - miny) / res)
x = np.linspace(minx, maxx, nx)
y = np.linspace(miny, maxy, ny)
z = numpy.array([
[-1, 10, 15.1, 6.3, 50.4],
[26.7, -1, 15.7, 40.7, 5],
[5, -1, 9.0, 38, 40.3],
])
cmap = plt.get_cmap("nipy_spectral")
with rasterio.open(
os.path.join(os.path.dirname(__file__), "test.tiff"),
"w",
driver='GTiff',
height=z.shape[0],
width=z.shape[1],
count=1,
dtype=z.dtype,
crs=' proj=latlong',
transform=Affine.translation(x[0]-res/2, y[0]-res/2) * Affine.scale(res, res),
nodata=-1,
) as df:
df.colorinterp = [ColorInterp.palette]
# df.write_colormap(1, cmap)
df.write(z, 1)
它在 QGIS 中拖放時創建一個基本影像:

我想將此檔案拖放到 Qgis 中,它的 cmap 來自 matplotlib,名為nipy_spectral:

.
另一種解決方案是在 qgis 中手動添加一個預定義的 cmap,如下所示:

然后可以將樣式匯出為檔案夾。也許可以使用 qgis.core python 模塊自動將此樣式應用于 tiff 檔案?
uj5u.com熱心網友回復:
我終于完成了使用這些代碼行的技巧:
import os
import numpy
from qgis.core import (
QgsCoordinateReferenceSystem, QgsSingleBandPseudoColorRenderer, QgsColorRampShader, QgsStyle, QgsRasterBandStats,
QgsRasterShader, QgsApplication, QgsProject, QgsRasterLayer)
from calcul import conversion_to_geotiff
def create_project(path_qgz, tiffs=None, epsg=2154):
qgs = QgsApplication([], False)
qgs.initQgis()
project = QgsProject.instance()
project.setTitle('test')
project.setCrs(QgsCoordinateReferenceSystem(epsg))
for data in tiffs:
x, y, z, path_tif, colormap, precision = data
conversion_to_geotiff.create_tiff(path_tif, x, y, z)
layer = QgsRasterLayer(path_tif, os.path.splitext(os.path.basename(path_tif))[0])
stats = layer.dataProvider().bandStatistics(1, QgsRasterBandStats.All)
minimum = stats.minimumValue
maximum = stats.maximumValue
delta = maximum - minimum
nclass = max(2, int(delta / precision))
fractional_steps = [i / (nclass - 1) for i in range(nclass)]
ramp = QgsStyle().defaultStyle().colorRamp(colormap)
colors = [ramp.color(f) for f in fractional_steps]
steps = [minimum f * delta for f in fractional_steps]
ramp_items = [
QgsColorRampShader.ColorRampItem(step, color, str(step))
for step, color in zip(steps, colors)
]
shader_function = QgsColorRampShader()
shader_function.setClassificationMode(QgsColorRampShader.EqualInterval)
shader_function.setColorRampItemList(ramp_items)
raster_shader = QgsRasterShader()
raster_shader.setRasterShaderFunction(shader_function)
renderer = QgsSingleBandPseudoColorRenderer(layer.dataProvider(), 1, raster_shader)
layer.setRenderer(renderer)
layer.triggerRepaint()
project.addMapLayer(layer)
project.write(path_qgz)
qgs.exitQgis()
if __name__ == "__main__":
x = np.linspace(minx, maxx, nx)
y = np.linspace(miny, maxy, ny)
z = numpy.array([
[-1, 10, 15.1, 6.3, 50.4],
[26.7, -1, 15.7, 40.7, 5],
[5, -1, 9.0, 38, 40.3],
])
create_project(
"/home/vince/test.qgz",
tiffs=[
[x, y, z, "/home/vince/test.tif", "Turbo", 5]
]
)
困難的部分是將 qgis 模塊添加到您的 PYTHONPATH 中。由于 DLL,在 Windows 中非常困難,而在 linux 中則容易得多。(只要確保你的 qgis 的 python 版本和你的代碼的 python 版本是相同的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/406742.html
標籤:
上一篇:沒有日期的熊貓時間圖
