问题
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