【问题标题】:Python: how to save a geotiff file using rasterio with coordinates?Python:如何使用带坐标的光栅保存 geotiff 文件?
【发布时间】:2021-02-26 09:13:43
【问题描述】:

如下所述,我使用 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 文件。

【问题讨论】:

  • 你试过我下面的答案了吗?如果它不能回答您的问题,请告诉我您还需要什么。

标签: python rasterio


【解决方案1】:

您可以使用它写入新的 .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)

【讨论】:

    猜你喜欢
    • 2020-03-02
    • 2018-07-09
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    • 2019-02-10
    • 2020-05-07
    • 1970-01-01
    相关资源
    最近更新 更多