繁体   English   中英

如何找到离点最近的LINESTRING?

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

如何为某个点附近最近的 LINESTRING 提供资金?

首先,我有一个 LINESTRING 和点值的列表。 我如何获得最接近点 (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)

到目前为止,我认为我通过为第一个 LINESTRING 执行 line_string [0].distance(point) 获得了距离值,但我只是想确保我以正确的方式进行。

这是一个 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))
  • 您的示例几何图形对线串无效,已修改
  • 使用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)
几何_x index_right 几何_y
0 点(5.41 3.9) 0 线串(-1.15 9.9,-1.15 9.93)

暂无
暂无

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

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