问题描述
我有一个shapefile数据集.某些道路(线)具有相同的名称,但位于不同的位置,并且没有连接.
I have a shapefile dataset. Some roads (line) have the same name but are located at different places and are not connected.
这是我的geopandas数据文件中同名公路的照片:
Here is a picture of the roads with a same name in my geopandas datafile:
我希望能够测量路段(线串)之间的距离,以便能够在距离大于阈值的情况下重命名道路,从而使每条道路都有其自己的唯一名称.
I would like to be able to measure the distance between road chunks (linestrings) to be able to rename the roads if the distance is higher than a threshold, such that each road has its own unique name.
因此,您知道如何找到线串之间的距离吗?
Hence, do you know how to find the distance between linestrings ?
推荐答案
在geopandas中,几何形状是匀称的对象.要获得任意两个匀称对象之间的距离,请使用巧妙命名的distance
方法:
In geopandas, the geometries are shapely objects. To get a distance between any two shapely objects, you use the cleverly named distance
method:
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,则需要对此有所了解.
If you have a geopandas GeoSeries/GeoDataFrame, you need to be a little smarter about it.
gs = geopandas.GeoSeries([line1, line2, line3])
gs.distance(gs)
返回全零,因为它在索引上将gs
排列为gs
,它们都是相同的几何图形.
Returns all zeros, because it lines up gs
to gs
on the index, which is all the same geometries.
但是:
gs.distance(gs.shift())
为您提供从第1行到第2行以及从第2行到第3行的距离:
Gives you the distances from line1 to line2, and line2 to line3:
0 NaN
1 0.554700
2 0.948683
dtype: float64
这篇关于线串大 pandas 之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!