1. 程式人生 > >Python地學分析 — 向量資料之間交叉重疊分析 05

Python地學分析 — 向量資料之間交叉重疊分析 05

歡迎關注博主的微信公眾號:“智慧遙感”。

該公眾號將為您奉上Python地學分析、爬蟲、資料分析、Web開發、機器學習、深度學習等熱門原始碼。

Python的小夥伴們,你們好呀!天氣轉涼,記得加秋褲哦

前面的幾節都是講向量資料的讀寫與建立,本節課開始講幾個實際應用的案列,希望對大家有所幫助。

本人的GitHub程式碼資料主頁(持續更新中,多給Star,多Fork):

https://github.com/xbr2017

CSDN也在同步更新:

https://blog.csdn.net/XBR_2014

~~~~~~~~~~~~~~~~~~~~~~~~~~

程式設計環境:

作業系統:windows

Python版本:2.7

IDE版本:PyCharm 2018.2.4專業版

~~~~~~~~~~~~~~~~~~~~~~~~~~

首先,展示一下向量資料01號選手:

# _*_ coding: utf-8 _*_
__author__ = 'xbr'
__date__ = '2018/11/5 11:45'

from osgeo import ogr
import matplotlib.pyplot as plt

from ospybook.vectorplotter import VectorPlotter

water_ds = ogr.Open(r'D:\osgeopy-data\US\wtrbdyp010.shp')
water_lyr = water_ds.GetLayer(0)
water_lyr.SetAttributeFilter('WaterbdyID = 1011327')
marsh_feat = water_lyr.GetNextFeature()
marsh_geom = marsh_feat.geometry().Clone()
# 呼叫VectorPlotter類
vp = VectorPlotter(True)
vp.plot(marsh_geom, 'b')
plt.show()   # 少了這句話則影象不顯示

向量資料01號選手:

展示向量資料02號選手與01號選手的重疊程式碼(接著上面的程式碼):

nola_ds = ogr.Open(r'D:\osgeopy-data\Louisiana\NOLA.shp')
nola_lyr = nola_ds.GetLayer(0)
nola_feat = nola_lyr.GetNextFeature()
nola_geom = nola_feat.geometry().Clone()
vp.plot(nola_geom, fill=False, ec='red', ls='dashed', lw=3)

intersection = marsh_geom.Intersection (nola_geom)
vp.plot(intersection, 'yellow', hatch='x')

兩位選手的重疊部分用黃色填充,紅色虛線為02號選手:

那麼黃色區域所佔01號選手的面積比是多少呢?答案就在下方:

water_lyr.SetAttributeFilter("Feature != 'Lake'")
water_lyr.SetSpatialFilter(nola_geom)
wetlands_area = 0
for feat in water_lyr:
    intersect = feat.geometry().Intersection (nola_geom)
    wetlands_area += intersect.GetArea()

pcnt = wetlands_area / nola_geom.GetArea()

print('{:.1%} of New Orleans is wetland'.format(pcnt))

具體所佔面積比:

重疊面積所佔比為28.7%

與前面的基礎知識相比,是不是覺得應用很方便,後面還有一大波Python好玩有趣兒的應用,趕緊關注吧!