python開源庫(kù)切割地圖影像
地圖影像數(shù)據(jù)的使用過程中,有時(shí)候需要根據(jù)一個(gè)坐標(biāo)范圍,或者設(shè)定好的矢量數(shù)據(jù),將影像數(shù)據(jù)進(jìn)行切割,常用的Arcmap、Envi、qgis等工具可進(jìn)行影像數(shù)據(jù)切割,下邊介紹一個(gè)使用開源的類庫(kù)進(jìn)行影像切割,開發(fā)的語言為python。開源庫(kù)的名稱為:rasterio,github的網(wǎng)址為:https://github.com/mapbox/rasterio,rasterio是一個(gè)專門的影像處理類庫(kù),有各種影像處理函數(shù),具體可參見開源網(wǎng)站上的說明。
發(fā)之前,準(zhǔn)備一個(gè)基礎(chǔ)影像,一個(gè)切割影像的面狀矢量數(shù)據(jù),這里準(zhǔn)備了一個(gè)shp文件,要保證矢量切割數(shù)據(jù)和影像數(shù)據(jù)的坐標(biāo)系保持一致。
具體的python代碼如下:
import fionaimport rasterio
import rasterio.mask
#fiona打開shp文件,使用conda或者pip進(jìn)行安裝
#以只讀的形式打開一個(gè)shp文件
with fiona.open("矢量文件路徑clipdata.shp","r") as shapfile:
? ?#循環(huán)遍歷shp文件的面狀坐標(biāo)信息
? ?features = [feature["geometry"] for feature in shapfile]
? ?#打開被切割的tif文件路徑
? ?with rasterio.open("被切割的tif文件路徑L15.tif") as src:
? ? ? ? #根據(jù)矢量的坐標(biāo)范圍,切割圖層
? ? ? ?out_image,out_transform = rasterio.mask.mask(src,features,
? ? ? ?crop=True)
? ? ? ?#拷貝源tif的數(shù)據(jù)
? ? ? ?out_meta = src.meta.copy()
? ? ? ?#根據(jù)切割的信息,更新復(fù)制的源tif數(shù)據(jù)
? ? ? ?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