Shapely 几何与空间关系

在 Python 中处理地理空间数据

Dani Arribas-Bel

Geographic Data Science Lab (University of Liverpool)

标量几何值

cities = geopandas.read_file("ne_110m_populated_places.shp")
cities.head()
           name                                      geometry
0  Vatican City   POINT (12.45338654497177 41.90328217996012)
1    San Marino     POINT (12.44177015780014 43.936095834768)
2         Vaduz   POINT (9.516669472907267 47.13372377429357)
3       Lobamba  POINT (31.19999710971274 -26.46666746135247)
4    Luxembourg   POINT (6.130002806227083 49.61166037912108)
brussels = cities.loc[170, 'geometry']
print(brussels)
POINT (4.33137074969045 50.83526293533032)
在 Python 中处理地理空间数据

标量几何值

brussels = cities.loc[170, 'geometry']
print(brussels)
POINT (4.33137074969045 50.83526293533032)
type(brussels)
shapely.geometry.point.Point
在 Python 中处理地理空间数据

Shapely 包

type(brussels)
shapely.geometry.point.Point

Shapely

  • 用于几何对象操作与分析的 Python 包
  • 提供 PointLineStringPolygon 对象
  • GeoSeries(GeoDataFrame 的 geometry 列)由 Shapely 对象组成
在 Python 中处理地理空间数据

几何对象

从 GeoDataFrame 获取:

brussels = cities.loc[170, 'geometry']
paris = cities.loc[235, 'geometry']
belgium = countries.loc[countries['name'] == 'Belgium', 'geometry'].squeeze()
france = countries.loc[countries['name'] == 'France', 'geometry'].squeeze()
uk = countries.loc[countries['name'] == 'United Kingdom', 'geometry'].squeeze()

手动创建:

from shapely.geometry import Point
p = Point(1, 2)
print(p)
POINT (1 2)
在 Python 中处理地理空间数据

空间方法

几何的面积

belgium.area
3.8299974609075753

两几何间的距离

brussels.distance(paris)
2.8049127723186214

还有更多!(如 centroidsimplify 等)

在 Python 中处理地理空间数据

空间关系

geopandas.GeoSeries([belgium, france, uk, paris,  brussels, line]).plot()

在 Python 中处理地理空间数据

空间关系

belgium.contains(brussels)
True
france.contains(brussels)
False
brussels.within(belgium)
True
belgium.touches(france)
True
line.intersects(france)
True
line.intersects(uk)
False
在 Python 中处理地理空间数据

Vamos praticar!

在 Python 中处理地理空间数据

Preparing Video For Download...