Python 如何对光栅单元重新采样以进行合并(马赛克)?
我正在使用光栅文件创建DEM,但我想对像素值进行降采样(重新采样到较低分辨率/较大的单元大小),以减小生成文件的大小。我的想法是使用尽可能少的内存来完成这项工作。 我的代码在马赛克上运行良好,没有下采样。我发现了这个,但我没有弄清楚如何在马赛克代码中插入代码示例Python 如何对光栅单元重新采样以进行合并(马赛克)?,python,rasterio,Python,Rasterio,我正在使用光栅文件创建DEM,但我想对像素值进行降采样(重新采样到较低分辨率/较大的单元大小),以减小生成文件的大小。我的想法是使用尽可能少的内存来完成这项工作。 我的代码在马赛克上运行良好,没有下采样。我发现了这个,但我没有弄清楚如何在马赛克代码中插入代码示例 import os import rasterio from rasterio.merge import merge from rasterio.plot import show from rasterio.enums import R
import os
import rasterio
from rasterio.merge import merge
from rasterio.plot import show
from rasterio.enums import Resampling
layers = ['A', 'B', 'C', 'D', 'E']
for layer in layers:
# files to merge into the DEM.
files = ['example1.zip', 'example2.zip', 'example3.zip', 'example4.zip'] # In my real problem, each layer has different files.
files_path = '/content/raster'
src_files_to_mosaic = []
if not os.path.exists('/content/'+layer+'.tif'):
for grade in list(set(files)):
q = os.path.join(files_path, grade) #path of each grid
raster_path = 'zip://'+q+'!'+grade[:8]+'.tif'# extract zip
print(raster_path)
src = rasterio.open(raster_path) # open each raster
src_files_to_mosaic.append(src) #joins the paths to do the mosaic
mosaic, out_trans = merge(src_files_to_mosaic)
out_meta = src.meta.copy() # Copy the metadata
# Update the metadata
out_meta.update({"driver": "GTiff",
"height": mosaic.shape[1],
"width": mosaic.shape[2],
"transform": out_trans,
"crs": "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs",
}
)
# Plot the result
show(mosaic, cmap='terrain')
# Write the mosaic raster to disk
raster_name = '/content/'+layer+'.tif'
with rasterio.open(raster_name, "w", **out_meta) as dest:
dest.write(mosaic)