import xml etree ElementTree as ET import networkx as nx from scipy sp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import xml.etree.ElementTree as ET
import networkx as nx
from scipy.spatial import distance
import numpy as np
G = nx.DiGraph()
tree = ET.parse('bt.osm')
root = tree.getroot()
ways = root.findall('way')
nodes = root.findall('node')
for node in nodes:
G.add_node(node.get('id'), x = (float(node.get('lon'))), y = float(node.get('lat')))
print ("Загрузка вершин")
p = {node.get('id'): [float(node.get('lon')), float(node.get('lat'))] for node in nodes}
for way in ways:
nodes = [nd.attrib.get('ref') for nd in way.findall('nd')]
for n in range(len(nodes)-1):
G.add_edge(nodes[n], nodes[n+1], weight = distance.euclidean(p[nodes[n]], p[nodes[n+1]]), capacity=np.random.randint(10) + 1, flow=set())
print ("Загрузка ребер")
nx.draw_networkx(G,p, with_labels = False,node_size=0)