欢迎光临散文网 会员登陆 & 注册

python开源库切割地图影像

2022-07-26 12:04 作者:地理信息技术杂谈  | 我要投稿

地图影像数据的使用过程中,有时候需要根据一个坐标范围,或者设定好的矢量数据,将影像数据进行切割,常用的Arcmap、Envi、qgis等工具可进行影像数据切割,下边介绍一个使用开源的类库进行影像切割,开发的语言为python。开源库的名称为:rasterio,github的网址为:https://github.com/mapbox/rasterio,rasterio是一个专门的影像处理类库,有各种影像处理函数,具体可参见开源网站上的说明。

发之前,准备一个基础影像,一个切割影像的面状矢量数据,这里准备了一个shp文件,要保证矢量切割数据和影像数据的坐标系保持一致。

具体的python代码如下:

import fionaimport rasterio
import rasterio.mask
#fiona打开shp文件,使用conda或者pip进行安装
#以只读的形式打开一个shp文件
with fiona.open("矢量文件路径clipdata.shp","r") as shapfile:
   #循环遍历shp文件的面状坐标信息
   features = [feature["geometry"] for feature in shapfile]
   #打开被切割的tif文件路径
   with rasterio.open("被切割的tif文件路径L15.tif") as src:
        #根据矢量的坐标范围,切割图层
       out_image,out_transform = rasterio.mask.mask(src,features,
       crop=True)
       #拷贝源tif的数据
       out_meta = src.meta.copy()
       #根据切割的信息,更新复制的源tif数据
       out_meta.update({"driver": "GTiff",
                        "height": out_image.shape[1],
                        "width": out_image.shape[2],
                        "transform": out_transform})
       #保存tif到新文件中,并写入到磁盘上
       with rasterio.open("保存路径clip.tif", "w", **out_meta) as dest:
           dest.write(out_image)

https://mp.weixin.qq.com/s?__biz=MzU2ODYzNzc4OQ==&mid=2247485575&idx=1&sn=d3ccb97afd2aead5565e5c2484aadb5d&chksm=fc8ba8b5cbfc21a364f264c4757562abd20fa0aa7e91e8e92c65b0e3cdff2d54e5917474b96d&token=446413984&lang=zh_CN#rd

python开源库切割地图影像的评论 (共 条)

分享到微博请遵守国家法律