-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathos3e_weighted.py
64 lines (53 loc) · 1.95 KB
/
os3e_weighted.py
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
63
64
#!/usr/bin/env python
'''Find geocodes for each node name in the OS3e graph using PlaceFinder.'''
import os
# From http://hoegners.de/Maxi/geo/
import geo
# To test, go to http://www.daftlogic.com/projects-google-maps-distance-calculator.htm
# and run this code - see if the Portland - SanFran distance is ~533 miles.
# portland = geo.xyz(45.5, -122.67)
# sanfran = geo.xyz(37.777, -122.419)
METERS_TO_MILES = 0.000621371192
# print geo.distance(portland, sanfran) * METERS_TO_MILES
from file_libs import write_json_file, read_json_file
from geocode import get_lat_long
from topo.os3e import OS3EGraph
import networkx as nx
LATLONG_FILE = "geo/os3e_latlong.json"
def lat_long_pair(node):
return (float(node["Latitude"]), float(node["Longitude"]))
def dist_in_miles(data, src, dst):
'''Given a dict of names and location data, compute mileage between.'''
src_pair = lat_long_pair(data[src])
src_loc = geo.xyz(src_pair[0], src_pair[1])
dst_pair = lat_long_pair(data[dst])
dst_loc = geo.xyz(dst_pair[0], dst_pair[1])
return geo.distance(src_loc, dst_loc) * METERS_TO_MILES
def OS3EWeightedGraph():
data = {}
g = OS3EGraph()
longit = {}
lat = {}
# Get locations
if os.path.isfile(LATLONG_FILE):
print "Using existing lat/long file"
data = read_json_file(LATLONG_FILE)
else:
print "Generating new lat/long file"
for n in g.nodes():
data[n] = get_lat_long(n)
write_json_file(LATLONG_FILE, data)
for node in g.nodes():
latit = float(data[node]["Latitude"])
lon = float(data[node]["Longitude"])
lat[node] = latit
longit[node] = lon
nx.set_node_attributes(g,'Latitude',lat)
nx.set_node_attributes(g,'Longitude',longit)
# Append weights
for src, dst in g.edges():
g[src][dst]["weight"] = dist_in_miles(data, src, dst)
#print "%s to %s: %s" % (src, dst, g[src][dst]["weight"])
return g
if __name__ == '__main__':
g = OS3EWeightedGraph()