rasterio实用教程(3)——图像掩膜提取

文章目录

背景

掩膜提取是指基于矢量面范围内的栅格像素值,并输出为新图像的操作。

因为涉及矢量面数据获取,所以需要引入fiona包,未安装的读者请自行安装。

实战

import fiona
import rasterio
import rasterio.mask

mask = 'D:/A_2021寒假/城市群相关/Data/0Slab中国基础地理数据/China/China_single.shp'
src_img = 'input.tif'
dst_img = 'output.tif'

# 读取矢量面
with fiona.open(mask, "r") as shapefile:
	shapes = [feature["geometry"] for feature in shapefile]

# 读取输入图像
with rasterio.open(src_img) as src:
	out_image, out_transform = rasterio.mask.mask(src, # 输入数据
	shapes, # 掩膜数据
	crop=True, # 是否裁剪
	nodata=0) # 缺省值
	out_meta = src.meta # 元数据

# 更新元数据
out_meta.update({"driver": "GTiff",
	"height": out_image.shape[1],
	"width": out_image.shape[2],
	"transform": out_transform}) # 转换函数

# 输出掩膜提取图像
with rasterio.open(dst_img, "w", **out_meta) as dest:
	dest.write(out_image)
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HouGISer

HouGiser需要你的鼓励~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值