Python地学分析 — 矢量数据之间重叠分析 05
欢迎关注博主的微信公众号:“智能遥感”。
该公众号将为您奉上Python地学分析、爬虫、数据分析、Web开发、机器学习、深度学习等热门源代码。
Python的小伙伴们,你们好呀!天气转凉,记得加秋裤哦
前面的几节都是讲矢量数据的读写与创建,本节课开始讲几个实际应用的案列,希望对大家有所帮助。
本人的GitHub代码资料主页(持续更新中,多给Star,多Fork):
https://github.com/xbr2017
****也在同步更新:
https://blog.****.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好玩有趣儿的应用,赶紧关注吧!