首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python:如何使用带坐标的rasterio保存geotiff文件?

Python:如何使用带坐标的rasterio保存geotiff文件?
EN

Stack Overflow用户
提问于 2021-02-26 17:13:44
回答 1查看 589关注 0票数 0

我使用shapefile掩蔽了geotiff栅格,如下所述

代码语言:javascript
运行
复制
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文件。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-27 05:35:39

你可以使用它来写一个新的.tif。因为rasterio需要一些meta来写入,所以使用输入光栅是很常见的,例如在本例中使用调整后的属性。

代码语言:javascript
运行
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66383026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档