Distance Between Linestring Geopandas

前端 未结 1 713
忘掉有多难
忘掉有多难 2021-01-24 18:48

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 na

1条回答
  •  无人共我
    2021-01-24 19:08

    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
    

    0 讨论(0)
提交回复
热议问题