Python (GIS): How to convert geometric lines (in the form of a LineString) to a Complete Graph network using Networkx

TL;DR How to convert a geodataframe of LineStrings to a Complete Graph with NetworkX?

I have a geodataframe of a collection of linestrings that geographically represent a road network (purple). I have 2 points (red and blue) representing A and B. I’d like to find the shortest route between A and B travelling along the road network.
enter image description here

I have converted the geodataframe to a network using momepy library:

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)

This successfully converts my road network to a networkx multigraph, whilst preserving distance (mm_len) between points.

Then I add points A and B to the network by getting the closest nodes on the road network to these points:

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]

So now within all the nodes of my road network, I have 2 representing points A and B respectively. Their node key is the coordinate pair.

However, when I run the shortest_path algorithm between A and B I get this error:

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)

And when I check the edges of these nodes, they are only connected to their 2 neighbours, and no other node:

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}

So these nodes need to all be connected to one another with edges, otherwise the shortest_path won’t work. I need to make my network a Complete Graph. However, I have no idea how to do that- it can’t be done with Momepy. However I am unsure how to create a NetworkX Graph from scratch whilst preserving the geographical distances between the nodes of the roadnetwork – this is what drew me to using Momepy in the first place. Advice would be appreciated, thanks 🙂

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật