Python geographic data processing eight: using GR for vector analysis

1. Overlay analysis

  Overlay analysis operation:
Insert picture description here
  plot colors: 'r' red,'g' green,'b' blue,'c' cyan,'y' yellow,'m' magenta,'k' black,'w' white.

  A simple map of New Orleans city boundaries, water bodies, and wetlands:

Insert picture description here
  1. New Orleans urban swamp area analysis:

import os
from osgeo import ogr
from ospybook.vectorplotter import VectorPlotter

data_dir = r'E:\Google chrome\Download\gis with python\osgeopy data'

# 得到新奥尔良附近的一个特定的沼泽特征
vp = VectorPlotter(True)
water_ds = ogr.Open(os.path.join(data_dir, '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()
vp.plot(marsh_geom, 'c')


# 获得新奥尔良边城市边界
nola_ds = ogr.Open(os.path.join(data_dir, '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')
vp.draw()

Insert picture description here
  2. Calculate the wetland area of ​​the city:

# 获得城市内的湿地多边形
# 将多边形的面积进行累加
# 除以城市面积
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% of New Orleans is wetland

  Note : Filtering unnecessary elements through spatial filtering and attribute filtering can significantly reduce processing time.

  3. Intersection of two layers:

# 将湖泊数据排除
# 在内存中创建一个临时图层
# 将图层相交,将结果储存在临时图层中
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))


water_lyr.SetSpatialFilter(None)
water_lyr.SetAttributeFilter("Feature != 'Lake'")

memory_driver = ogr.GetDriverByName('Memory')
temp_ds = memory_driver.CreateDataSource('temp')
temp_lyr = temp_ds.CreateLayer('temp')

nola_lyr.Intersection(water_lyr, temp_lyr)

sql = 'SELECT SUM(OGR_GEOM_AREA) AS area FROM temp'
lyr = temp_ds.ExecuteSQL(sql)
pcnt = lyr.GetFeature(0).GetField('area') / nola_geom.GetArea()
print('{:.1%} of New Orleans is wetland'.format(pcnt))

28.7% of New Orleans is wetland

2. Proximity analysis (determine the distance between elements)

  OGR contains two proximity analysis tools: measuring the distance of geometric elements; creating a buffer zone.

  1. Determine how many cities in the United States are within 10 miles (1 mile = 1609.3 meters) of the volcano. Methods to determine the number of cities near the volcano are problematic:

from osgeo import ogr

shp_ds = ogr.Open(r'E:\Google chrome\Download\gis with python\osgeopy data\US')
volcano_lyr = shp_ds.GetLayer('us_volcanos_albers')
cities_lyr = shp_ds.GetLayer('cities_albers')

# 在内存中创建一个临时层来存储缓冲区
memory_driver = ogr.GetDriverByName('memory')
memory_ds = memory_driver.CreateDataSource('temp')
buff_lyr = memory_ds.CreateLayer('buffer')
buff_feat = ogr.Feature(buff_lyr.GetLayerDefn())

# 缓缓冲每一个火山点,将结果添加到缓冲图层中
for volcano_feat in volcano_lyr:
    buff_geom = volcano_feat.geometry().Buffer(16000)
    tmp = buff_feat.SetGeometry(buff_geom)
    tmp = buff_lyr.CreateFeature(buff_feat)

# 将城市图层与火山缓冲区图层相交
result_lyr = memory_ds.CreateLayer('result')
buff_lyr.Intersection(cities_lyr, result_lyr)
print('Cities: {}'.format(result_lyr.GetFeatureCount()))

Cities: 83

  2. A better way to determine the number of cities near the volcano:

from osgeo import ogr

shp_ds = ogr.Open(r'E:\Google chrome\Download\gis with python\osgeopy data\US')
volcano_lyr = shp_ds.GetLayer('us_volcanos_albers')
cities_lyr = shp_ds.GetLayer('cities_albers')

# 将缓冲区添加到一个复合多边形,而不是一个临时图层
multipoly = ogr.Geometry(ogr.wkbMultiPolygon)
for volcano_feat in volcano_lyr:
    buff_geom = volcano_feat.geometry().Buffer(16000)
    multipoly.AddGeometry(buff_geom)

# 将所有的缓冲区联合在一起得到一个可以使用的多边形作为空间过滤器
cities_lyr.SetSpatialFilter(multipoly.UnionCascaded())
print('Cities: {}'.format(cities_lyr.GetFeatureCount()))
Cities: 78

Note : UnionCascaded(): effectively merges all polygons into a compound polygon
  . In the first example, whenever a city is located in the volcano buffer, it will be copied to the output result. It means that a city is located in multiple 16,000-meter buffer zones and will be listed more than once.

  3. Calculate the distance between a specific city and the volcano:

import os
from osgeo import ogr
from ospybook.vectorplotter import VectorPlotter

data_dir = r'E:\Google chrome\Download\gis with python\osgeopy data'

shp_ds = ogr.Open(os.path.join(data_dir, 'US'))
volcano_lyr = shp_ds.GetLayer('us_volcanos_albers')
cities_lyr = shp_ds.GetLayer('cities_albers')

# 西雅图到雷尼尔山的距离
volcano_lyr.SetAttributeFilter("NAME = 'Rainier'")
feat = volcano_lyr.GetNextFeature()
rainier = feat.geometry().Clone()

cities_lyr.SetSpatialFilter(None)
cities_lyr.SetAttributeFilter("NAME = 'Seattle'")
feat = cities_lyr.GetNextFeature()
seattle = feat.geometry().Clone()

meters = round(rainier.Distance(seattle))
miles = meters / 1600
print('{} meters ({} miles)'.format(meters, miles))

92656 meters (57.91 miles)

  3. Use 2.5D geometric objects to indicate the distance between two points:

# 2D
pt1_2d = ogr.Geometry(ogr.wkbPoint)
pt1_2d.AddPoint(15, 15)
pt2_2d = ogr.Geometry(ogr.wkbPoint)
pt2_2d.AddPoint(15, 19)
print(pt1_2d.Distance(pt2_2d))

4.0
# 2.5D
pt1_25d = ogr.Geometry(ogr.wkbPoint25D)
pt1_25d.AddPoint(15, 15, 0)
pt2_25d = ogr.Geometry(ogr.wkbPoint25D)
pt2_25d.AddPoint(15, 19, 3)
print(pt1_25d.Distance(pt2_25d))

4.0

  Taking into account the elevation Z value, the true distance is 5.

# 用2D计算面积
ring = ogr.Geometry(ogr.wkbLinearRing)
ring.AddPoint(10, 10)
ring.AddPoint(10, 20)
ring.AddPoint(20, 20)
ring.AddPoint(20, 10)
poly_2d = ogr.Geometry(ogr.wkbPolygon)
poly_2d.AddGeometry(ring)
poly_2d.CloseRings()
print(poly_2d.GetArea())
100.0
# 用2.5D计算面积
ring = ogr.Geometry(ogr.wkbLinearRing)
ring.AddPoint(10, 10, 0)
ring.AddPoint(10, 20, 0)
ring.AddPoint(20, 20, 10)
ring.AddPoint(20, 10, 10)
poly_25d = ogr.Geometry(ogr.wkbPolygon25D)
poly_25d.AddGeometry(ring)
poly_25d.CloseRings()
print(poly_25d.GetArea())
100.0

  The area of ​​2.5D is actually 141.

# 叠加操作同样忽略了高程值Z
print(poly_2d.Contains(pt1_2d))
print(poly_25d.Contains(pt1_2d))
True
True

Guess you like

Origin blog.csdn.net/amyniez/article/details/113661992