简体   繁体   English

如何使用先前栅格的投影保存新栅格

[英]How to save a new raster with the projections of the previous raster

I tried to make an algorithm in Python where I entered a georeferenced raster (known coordinate system), all its negative values were transformed to zero, and then a new image was saved with the georeference of the initial image.我试图在 Python 中创建一个算法,在其中我输入了一个地理参考栅格(已知坐标系),它的所有负值都被转换为零,然后使用初始图像的地理参考保存一个新图像。

import skimage.io
import pandas as pd
import numpy as np

pathhr = 'C:\\Users\\dataset\\S30W051.tif'
HR = skimage.io.imread(pathhr)
df1 = pd.DataFrame(HR)
HR_changed = df1[df1 < 0] = 0

#save function
savedata = df1.to_numpy()
skimage.io.imsave('C:\\Users\\dataset\\S30W051_TEST.tif', savedata)

But when I save my raster at the end of this script, I get a non-georeferenced TIFF raster.但是当我在这个脚本的末尾保存我的栅格时,我得到了一个非地理参考的 TIFF 栅格。

How do I keep the same coordinate system as the initial raster (without transforming the output raster into local coordinates)?如何保持与初始栅格相同的坐标系(不将 output 栅格转换为本地坐标)?

I ask for help in solving this problem.我寻求帮助来解决这个问题。 Thanks.谢谢。

You could use rasterio for opening and saving your tiff files, and copy the metadata of the initial raster to the new raster.您可以使用rasterio打开和保存 tiff 文件,并将初始栅格的元数据复制到新栅格。

import rasterio as rio

# Load the original image
with rio.open(pathhr, 'r') as r:
    HR = r.read()
    meta = r.meta

# Do any transformation you like (on the numpy array)
HR_changed = HR[HR < 0] = 0

# Save the changed raster
with rio.open('C:\\Users\\dataset\\S30W051_TEST.tif', 'w', **meta) as dst:
    dst.write(HR_change)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM