简体   繁体   English

如何找到离点最近的LINESTRING?

[英]how to find the nearest LINESTRING to a POINT?

How do I fund the nearest LINESTRING near a point?如何为某个点附近最近的 LINESTRING 提供资金?

First I have a list of LINESTRING and point value.首先,我有一个 LINESTRING 和点值的列表。 How do I have the nearest LINESTRING to the POINT (5.41 3.9) and maybee the distance?我如何获得最接近点 (5.41 3.9) 的 LINESTRING 以及距离?

from shapely.geometry import Point, LineString

line_string = [LINESTRING (-1.15.12 9.9, -1.15.13 9.93), LINESTRING (-2.15.12 8.9, -2.15.13 8.93)]
point = POINT (5.41 3.9)

#distance 
line_string [0].distance(point)

So far I think I got the distance value by doing line_string [0].distance(point) for the first LINESTRING so far but I just want to make sure I am going about it the right way.到目前为止,我认为我通过为第一个 LINESTRING 执行 line_string [0].distance(point) 获得了距离值,但我只是想确保我以正确的方式进行。

Here's a function that takes a list of LineString s and a point , and returns the LineString closest to the point , as well as the distance.这是一个 function ,它接受一个LineString s 和一个point的列表,并返回最接近该pointLineString以及距离。

from shapely.geometry import Point, LineString

# set up lines and points
line_string_list = [LineString([(-1,1),(1,.5)]), LineString([(-1,-.5),(.5,-1)]), LineString([(-1,0),(.5,-.5)])]
point = Point(.25,-.75)

def closest_line(lines, point):
    # get distances
    distance_list = [line.distance(point) for line in line_string_list]
    shortest_distance = min(distance_list) # find the line closest to the point
    return(lines[distance_list.index(shortest_distance)], # return the closest line
           shortest_distance) # return the distance to that line
    
print(closest_line(line_string_list, point))
  • your sample geometry is invalid for line strings, have modified您的示例几何图形对线串无效,已修改
  • it's simple to achieve with sjoin_nearest()使用sjoin_nearest()很容易实现
import geopandas as gpd
import shapely.wkt
import shapely.geometry

line_string = ["LINESTRING (-1.15.12 9.9, -1.15.13 9.93)", "LINESTRING (-2.15.12 8.9, -2.15.13 8.93)"]
# fix invalid wkt string...
line_string = ["LINESTRING (-1.15 9.9, -1.15 9.93)", "LINESTRING (-2.15 8.9, -2.15 8.93)"]
point = "POINT (5.41 3.9)"

gdf_p = gpd.GeoDataFrame(geometry=[shapely.wkt.loads(point)])
gdf_l = gpd.GeoDataFrame(geometry=pd.Series(line_string).apply(shapely.wkt.loads))

gpd.sjoin_nearest(gdf_p, gdf_l).merge(gdf_l, left_on="index_right", right_index=True)
geometry_x几何_x index_right index_right geometry_y几何_y
0 0 POINT (5.41 3.9)点(5.41 3.9) 0 0 LINESTRING (-1.15 9.9, -1.15 9.93)线串(-1.15 9.9,-1.15 9.93)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM