线串 Geopandas 之间的距离
Distance Between Linestring Geopandas
我有一个 shapefile 数据集。部分道路(线)名称相同,但位置不同,互不相连。
这是我的 geopandas 数据文件中同名道路的图片:
我希望能够测量道路块(线串)之间的距离,以便能够在距离高于阈值时重命名道路,这样每条道路都有自己独特的名称。
所以,你知道如何求线串之间的距离吗?
在 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
如果你有一只地理熊猫GeoSeries/GeoDataFrame,你需要更聪明一些。
gs = geopandas.GeoSeries([line1, line2, line3])
gs.distance(gs)
Returns全为零,因为它在索引上排列 gs
到 gs
,它们都是相同的几何形状。
但是:
gs.distance(gs.shift())
为您提供从第 1 行到第 2 行以及第 2 行到第 3 行的距离:
0 NaN
1 0.554700
2 0.948683
dtype: float64
我有一个 shapefile 数据集。部分道路(线)名称相同,但位置不同,互不相连。
这是我的 geopandas 数据文件中同名道路的图片:
我希望能够测量道路块(线串)之间的距离,以便能够在距离高于阈值时重命名道路,这样每条道路都有自己独特的名称。
所以,你知道如何求线串之间的距离吗?
在 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
如果你有一只地理熊猫GeoSeries/GeoDataFrame,你需要更聪明一些。
gs = geopandas.GeoSeries([line1, line2, line3])
gs.distance(gs)
Returns全为零,因为它在索引上排列 gs
到 gs
,它们都是相同的几何形状。
但是:
gs.distance(gs.shift())
为您提供从第 1 行到第 2 行以及第 2 行到第 3 行的距离:
0 NaN
1 0.554700
2 0.948683
dtype: float64