1

我有一个 shapefile 数据集。有的道路(线)名称相同,但分布在不同的地方,互不相连。

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

在此处输入图像描述

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

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

4

1 回答 1

4

在 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)

返回全零,因为它在索引gsgs对齐,这是所有相同的几何图形。

但:

gs.distance(gs.shift())

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

0         NaN
1    0.554700
2    0.948683
dtype: float64
于 2017-12-25T22:49:38.723 回答