blob: e7c10186a618e22c870852d1e788a67b1284a733 (
plain) (
blame)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
import time
from osm_parser import get_default_parser
class Node:
def __init__(self, id, lat, lng):
self.id = id
self.lat = float(lat)
self.lng = float(lng)
self.neighbours = []
def coord_tuple(self):
return self.lat, self.lng
parser = None # Have a global reusable parser object
def add_neighbours(nodes):
for way in parser.iter_ways():
if 'highway' not in way['tags']:
continue
road = way['road']
for i in range(len(road) - 1):
node1 = road[i]
node2 = road[i + 1]
nodes[node1].neighbours.append(nodes[node2])
nodes[node2].neighbours.append(nodes[node1])
return nodes
def extract_osm_nodes(f_name):
global parser
parser = get_default_parser(f_name)
nodes = dict()
start = time.monotonic()
for node in parser.iter_nodes():
nodes[node['id']] = Node(node['id'], node['lat'], node['lon'])
add_neighbours(nodes)
# remove nodes without neighbours
for node_id, node in nodes.copy().items():
if not node.neighbours:
del nodes[node_id]
print(f"Extracted {len(nodes)} nodes in {time.monotonic() - start:.3} s")
return nodes
def select_nodes_in_rectangle(nodes, min_lat, max_lat, min_long, max_long):
return [node for node in nodes.values()
if min_lat <= node.lat <= max_lat
and min_long <= node.lng <= max_long]
|