在 geopandas 中,几何图形是有形状的物体。要获得任意两个匀称物体之间的距离,您可以使用巧妙命名的 distance 方法:
from shapely.geometry import Point, LineString
import geopandas
line1 = LineString([
Point(0, 0),
Point(0, 1),
Point(1, 1),
Point(1, 2),
Point(3, 3),
Point(5, 6),
])
line2 = LineString([
Point(5, 3),
Point(5, 5),
Point(9, 5),
Point(10, 7),
Point(11, 8),
Point(12, 12),
])
line3 = LineString([
Point(9, 10),
Point(10, 14),
Point(11, 12),
Point(12, 15),
])
print(line1.distance(line2))
> 0.5547001962252291
如果你有一个 geopandas GeoSeries/GeoDataFrame,你需要更聪明一点。
gs = geopandas.GeoSeries([line1, line2, line3])
gs.distance(gs)
返回全零,因为它在索引上排列 gs 到 gs,这是所有相同的几何图形。
但是:
gs.distance(gs.shift())
为您提供从 line1 到 line2 以及 line2 到 line3 的距离:
0 NaN
1 0.554700
2 0.948683
dtype: float64