基於Python的緩衝區分析

前段時間有朋友問破解後的ArcGIS做緩衝區時總是失敗,想到Python做緩衝區分析應該並不複雜,之前就看到過Shapely這個工具在GIS分析方面的例子,所以查下相關資料,寫一個入門的用shapely進行緩衝區分析的小例子。

基本概念

緩衝區分析是根據指定的距離,在點、線、面幾何對象周圍建立一定寬度的區域的分析方法。

shapely

shapely是在笛卡爾平面對幾何對象進行操作和分析的Python工具包。它基於應用廣泛的GEOS和JTS庫。

Shapely is a BSD-licensed Python package for manipulation and analysis of planar geometric objects. It is based on the widely deployed GEOS (the engine of PostGIS) and JTS (from which GEOS is ported) libraries.

用shapely生成緩衝區時,主要使用object.buffer(distance,…)語法。

在開始示例之前,先引入點線面類型。

from shapely.geometry import Pointnfrom shapely.geometry import LineStringnfrom shapely.geometry import MultiPolygonn

點緩衝區

# 定義兩個點npoint_1 = Point(1, 1)npoint_2 = Point(2, 1.2)n# 兩個點以指定的緩衝距離為半徑生成圓形區域na = point_1.buffer(2)nb = point_2.buffer(1.5)n

n相交與合併操作n

inter = a.intersection(b)nunion = a.union(b)nnbufferPlt(inter,union,intersection,union)n

n裁剪操作n

diff1 = a.difference(b)ndiff2 = b.difference(a)nnbufferPlt(diff1,diff2,difference(a-b),difference(b-a))n

線緩衝區

線的緩衝區是沿線對象的法線方向,分別向線對象的兩側平移一定的距離而得到兩條線,並與在線端點處形成的光滑曲線(或平頭)接合形成的封閉區域。

# 定義兩條線段nline_1 = LineString([(0.1, 0.1), (2, 3)])nline_2 = LineString([(0.2, 0.4), (3.5, 2.5)])n# 生成緩衝區na = line_1.buffer(0.5)nb = line_2.buffer(0.5)n

n相交與合併操作n

inter = a.intersection(b)nunion = a.union(b)nnbufferPlt(inter,union,intersection,union)n

n裁剪操作n

diff = a.difference(b)nnfig = pyplot.figure(1, figsize=(5,5),dpi=90)nax = fig.add_subplot(111)nfor polygon in diff:n patch = PolygonPatch(polygon, alpha=0.5, zorder=2)n ax.add_patch(patch)nnx_a,y_a = a.boundary.xynx_b,y_b = b.boundary.xynax.plot(x_a,y_a,b)nax.plot(x_b,y_b,g)nnax.set_title(difference)n

面緩衝區

用shapely生成面緩衝區的語法和點線類似,其實生成的點線緩衝區對象本身就是Polygon類型。

推薦閱讀:

深度學習中的Python語言3:SciPy和Matplotlib庫介紹
看看黃哥是怎麼解決問題的,網友答疑對話錄
《Django By Example》第八章 中文翻譯
請問安裝完anaconda後在開始的菜單中沒有Anaconda文件夾怎麼辦?
Python · 神經網路(五)· Cost & Optimizer

TAG:Python | GIS地理信息系统 |