Distance Between Linestring Geopandas

依然范特西╮ 提交于 2019-12-02 09:53:12

问题


I have a shapefile dataset. Some roads (line) have the same name but are located at different places and are not connected.

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 ?


回答1:


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

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)

Returns all zeros, because it lines up gs to gs on the index, which is all the same geometries.

But:

gs.distance(gs.shift())

Gives you the distances from line1 to line2, and line2 to line3:

0         NaN
1    0.554700
2    0.948683
dtype: float64


来源:https://stackoverflow.com/questions/47972049/distance-between-linestring-geopandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!