如何解决Python:如何使用带坐标的光栅保存geotiff文件?
我用如下所述的 shapefile 屏蔽了 geotiff 栅格
import Rasterio
from Rasterio.plot import show
import geopandas as gpd
population = Rasterio.open('myData.tif')
gdf = gpd.read_file('myFile.shp')
clipped_array,clipped_transform =
Rasterio.mask.mask(population,[mapping(ps.iloc[0].geometry)],crop=True)
f,ax=plt.subplots(figsize=(10,10))
gdf.boundary.plot(ax=ax,lw=3,color='red')
show(clipped_array,transform=clipped_transform,ax=ax)
ax.set_xlim([1.82,2.74])
ax.set_ylim([48.51,49.14])
现在我想将新数据保存为带有坐标和信息的 .tif
文件。
解决方法
您可以使用它写入新的 .tif。由于 rasterio 需要一些元来进行写入,因此通常使用输入栅格,例如在这种情况下具有调整的属性。
import rasterio
import os
import fiona
from rasterio import mask
with fiona.open('myFile.shp',"r") as shapefile:
shapes = [feature["geometry"] for feature in shapefile]
with rasterio.open('myData.tif') as src:
out_meta = src.meta
out_image,out_transform = rasterio.mask.mask(src,shapes=shapes,crop=True)
profile = src.profile
profile["height"] = out_image.shape[1]
profile["width"] = out_image.shape[2]
profile["transform"] = out_transform
out_meta.update({"driver": "GTiff","height": out_image.shape[1],"width": out_image.shape[2],"transform": out_transform})
with rasterio.open("masked.tif","w",**out_meta) as dest:
dest.write(out_image)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。