Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not completely sure what you want to accomplish, but here's how I interpret it.</p> <p>You have roads defined as coordinates along the road and you want to draw these coordinates as nodes and the road between them as edges. You also want the edge weight to be the distance between the two nodes.</p> <p>This can quite easily be accomplished by saving the previous node and using the Pythagorean theorem to calculate the distance. This is how I did it:</p> <pre class="lang-python prettyprint-override"><code>import networkx as nx from matplotlib import pyplot as plt import math G = nx.Graph() row = '643715.202,2499149.0506 643752.61523545,2499089.86084203 ' +\ '643773.6038,2499056.6558 643773.73878609,2499056.44011079 ' +\ '643793.20162482,2499025.34111554 643813.55943268,2498992.81212045 ' +\ '643826.6563,2498971.8852' a=row.split(" ") # Saving the previous node to be able to calculate the distance prev_point = None # Save the positions in a dictionary to be able to draw # the nodes at the correct positions pos = {} for i in a: cur_point = tuple([float(x) for x in i.split(',')]) assert len(cur_point) == 2 if prev_point is not None: # Calculate the distance between the nodes with the Pythagorean # theorem b = cur_point[1] - prev_point[1] c = cur_point[0] - prev_point[0] a = math.sqrt(b ** 2 + c ** 2) G.add_edge(cur_point, prev_point, weight=a) G.add_node(cur_point) pos[cur_point] = cur_point prev_point = cur_point nx.draw(G, pos=pos) plt.savefig('roads.png') </code></pre> <p>In this example I assume that a space separates the node positions and each position's x and y coordinate is comma separated, but this can easily be changed. The above code will output something like this:</p> <p><img src="https://i.stack.imgur.com/iWLVC.png" width="400" /></p> <p>This will put the nodes at their "correct" positions, although it can cause some issues if there are big differences in road length. In the above example you can see that two nodes are more or less on top of each other. But that's a different question.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload