线串 Geopandas 之间的距离

jam*_*mes 1 python multilinestring python-3.x geopandas

我有一个 shapefile 数据集。有些道路(线)名称相同,但位置不同,互不相连。

这是我的 geopandas 数据文件中同名道路的图片:

在此处输入图片说明

如果距离高于阈值,我希望能够测量道​​路块(线串)之间的距离,以便能够重命名道路,这样每条道路都有自己唯一的名称。

因此,您知道如何找到线串之间的距离吗?

Pau*_*l H 5

在 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
Run Code Online (Sandbox Code Playgroud)

如果你有一个 geopandas GeoSeries/GeoDataFrame,你需要更聪明一点。

gs = geopandas.GeoSeries([line1, line2, line3])
gs.distance(gs)
Run Code Online (Sandbox Code Playgroud)

返回全部为零,因为排队gsgs该索引,这是所有相同的几何形状。

但:

gs.distance(gs.shift())
Run Code Online (Sandbox Code Playgroud)

为您提供从 line1 到 line2 以及 line2 到 line3 的距离:

0         NaN
1    0.554700
2    0.948683
dtype: float64
Run Code Online (Sandbox Code Playgroud)