У меня есть фрейм геоданных, состоящий из набора строк, которые географически представляют дорожная сеть (фиолетовый). У меня есть две точки (красная и синяя), обозначающие A и B. Я хотел бы найти кратчайший маршрут между A и B, проходящий по дорожной сети.

Я преобразовал фрейм геоданных в сеть с помощью библиотеки Momepy:
Код: Выделить всё
road = gpd.read_file(input_road.shp)
#need to explode roads, as is multiline
exploded = road.explode(index_parts=True)
G = momepy.gdf_to_nx(exploded, approach="primal",length='mm_len',multigraph=True)
Затем я добавляю точки A и B к сеть, получив ближайшие узлы дорожной сети к этим точкам:
Код: Выделить всё
my_points = gpd.read_file(my_points)
#Firstly, need to convert the coordinates to a tuple of x,y coordinates
for my_point in my_points .geometry.values:
coords_values = re.findall('[0-9.]+ [0-9.]+', str(my_point ))
#convert list of strings separated by spaces, to a list of coords as list.
coords = [sub.split(' ') for sub in coords_values ][0]
coords_tuple= tuple(map(float, coords ))
coords_list.append(coords_tuple)
#add this coord tuple as a column in the dataframe
my_points["coords_syntax"] = coords_list
#this dictionary will be used to select the network nodes. The key is the coordinate tuple, and the attribute will be the 'Name' of the point
dict_df_mypoints = home.set_index('coords_syntax')['Name'].to_dict()
#select the nearest node points on the road network to these coordinate tuples
A = list(G.nodes())
#iterate through dict of coords: my_points
for point in dict_df_mypoints :
#get closest node (using KDTree) between point and the array of cords A generated above
closest_node = A[scipy.spatial.KDTree(A).query(point)[1]]
#make these above closest nodes have attribute my_point, with other attributes (dict_df[point])
G.nodes[closest_node]['my_point'] = dict_df[point]
Однако, когда я запускаю алгоритм кратчайшего_пути между A и B, я получаю эту ошибку:
Код: Выделить всё
nx.shortest_path(G,source = (455529.02164326626, 206374.9504608615),target = (454340.3426543578, 207204.53480888018))
>>NetworkXNoPath: No path between (455529.02164326626, 206374.9504608615) and (454340.3426543578, 207204.53480888018)
Код: Выделить всё
nx.single_source_shortest_path_length(G,source = (455529.02164326626, 206374.9504608615))
>>{(455529.02164326626, 206374.9504608615): 0,
>>(455582.6204962559, 206424.4603359318): 1,
>>(455596.5948359391, 206455.62556823122): 2}

Подробнее здесь: https://stackoverflow.com/questions/784 ... tring-to-a