|
|
@@ -207,91 +207,6 @@ def log_graph(graph, commutes):
|
|
|
|
|
|
print('')
|
|
|
|
|
|
-def write_kml(commutes, properties, fname):
|
|
|
- print('Writing .kml visualization file...')
|
|
|
-
|
|
|
- with open(fname, 'w') as f:
|
|
|
- f.write('''<?xml version="1.0" encoding="UTF-8"?>
|
|
|
- <kml xmlns="http://www.opengis.net/kml/2.2">
|
|
|
- <Document>
|
|
|
- ''')
|
|
|
-
|
|
|
- for i in range(256):
|
|
|
- hexvalue = hex(255-i)[2:].lower()
|
|
|
- if len(hexvalue) == 1:
|
|
|
- hexvalue = '0' + hexvalue
|
|
|
- f.write('<Style id="color-%i"><IconStyle><color>ff%s%sff</color></IconStyle></Style>\n' % (i, hexvalue, hexvalue))
|
|
|
-
|
|
|
- max_duration = min(4000, max([x['duration'] for x in commutes.values()]))
|
|
|
-
|
|
|
- f.write('''
|
|
|
- <Folder>
|
|
|
- <visibility>0</visibility>
|
|
|
- <name>Monitored bboxes</name>\n''')
|
|
|
-
|
|
|
- for (lat1, lon1), (lat2, lon2) in config.MONITORED_BBOXES:
|
|
|
- f.write('<Placemark><LineString><coordinates>')
|
|
|
- f.write('%s,%s' % (lon1, lat1))
|
|
|
- f.write(' %s,%s' % (lon1, lat2))
|
|
|
- f.write(' %s,%s' % (lon2, lat2))
|
|
|
- f.write(' %s,%s' % (lon2, lat1))
|
|
|
- f.write(' %s,%s' % (lon1, lat1))
|
|
|
- f.write(' %s,%s' % (lon2, lat2))
|
|
|
- f.write(' %s,%s' % (lon1, lat2))
|
|
|
- f.write(' %s,%s' % (lon2, lat1))
|
|
|
-
|
|
|
- f.write('</coordinates></LineString></Placemark>\n')
|
|
|
-
|
|
|
- # f.write('''
|
|
|
- # <Placemark><Point><coordinates>%f,%f</coordinates></Point></Placemark>
|
|
|
- # <Placemark><Point><coordinates>%f,%f</coordinates></Point></Placemark>
|
|
|
- # ''' % (lon1, lat1, lon2, lat2))
|
|
|
- f.write('</Folder>')
|
|
|
-
|
|
|
- f.write('<Folder><name>Commutes</name><visibility>0</visibility>\n')
|
|
|
- for loc, commute in commutes.items():
|
|
|
- duration = commute['duration']
|
|
|
- color = 20 + int((duration/float(max_duration)) * 236)
|
|
|
- color = min(color, 255)
|
|
|
-
|
|
|
- f.write('''<Placemark>
|
|
|
- <Point><coordinates>%f,%f</coordinates></Point>
|
|
|
- <styleUrl>#color-%i</styleUrl>
|
|
|
- <name>%s</name>
|
|
|
- <description>%s</description>
|
|
|
- </Placemark>\n''' % (loc[1], loc[0], color, int(duration / 60), loc))
|
|
|
-
|
|
|
- f.write('</Folder>')
|
|
|
-
|
|
|
- f.write('<Folder><name>Properties</name><visibility>0</visibility>\n')
|
|
|
- max_score = max(x['score'] for x in properties.values())
|
|
|
- properties = list(properties.items())
|
|
|
- properties.sort(key=lambda x: x[1]['score'], reverse=True)
|
|
|
- for i, (pid, data) in enumerate(properties):
|
|
|
- color = 20 + int((data['score']/float(max_score)) * 236)
|
|
|
- color = min(color, 255)
|
|
|
-
|
|
|
- color = 20 + int(236*(float(i)/len(properties)))
|
|
|
-
|
|
|
- name = '%s/%s/%s' % (data['price'], data['size'], data['bedrooms'])
|
|
|
- loc = data['loc']
|
|
|
- description = pid
|
|
|
-
|
|
|
- print(color, data)
|
|
|
-
|
|
|
- f.write('''<Placemark>
|
|
|
- <Point><coordinates>%f,%f</coordinates></Point>
|
|
|
- <styleUrl>#color-%i</styleUrl>
|
|
|
- <name>%s</name>
|
|
|
- <description>%s</description>
|
|
|
- </Placemark>\n''' % (loc[1], loc[0], color, name, description))
|
|
|
- f.write('</Folder>\n')
|
|
|
-
|
|
|
- f.write('''
|
|
|
- </Document>
|
|
|
- </kml>
|
|
|
- ''')
|
|
|
-
|
|
|
def analyze_locations(commutes, num_locations=5):
|
|
|
print('Loading new directions data for %i locations...' % num_locations)
|
|
|
|
|
|
@@ -330,31 +245,11 @@ def analyze_locations(commutes, num_locations=5):
|
|
|
print('\tLoading %s...' % str(loc))
|
|
|
directions_call(loc, config.WORKPLACE, TransitMethod.TRANSIT)
|
|
|
|
|
|
-def load_properties():
|
|
|
- with db.connect() as conn:
|
|
|
- rows = conn.select('select id, lat, lon, size, price, bedrooms from properties where price<450000')
|
|
|
-
|
|
|
- res = {}
|
|
|
- for property_id, lat, lon, size, price, bedrooms in rows:
|
|
|
- if not price or not size or not lat or not lon:
|
|
|
- continue
|
|
|
-
|
|
|
- if not any(geo_util.inside((lat, lon), bbox) for bbox in config.MONITORED_BBOXES):
|
|
|
- continue
|
|
|
-
|
|
|
- price = price/1000
|
|
|
-
|
|
|
- res[property_id] = {
|
|
|
- 'loc': (lat, lon),
|
|
|
- 'size': size,
|
|
|
- 'price': price,
|
|
|
- 'bedrooms': bedrooms,
|
|
|
- 'score': float(size) / int(price),
|
|
|
- }
|
|
|
-
|
|
|
- # print(res[property_id])
|
|
|
-
|
|
|
- return res
|
|
|
+def add_commute_data(properties, commutes):
|
|
|
+ for p in properties.values():
|
|
|
+ p.update({
|
|
|
+ 'commute_duration': 1800,
|
|
|
+ })
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
@@ -366,10 +261,3 @@ if __name__ == '__main__':
|
|
|
commutes = determine_fastest_paths(graph, config.WORKPLACE)
|
|
|
|
|
|
# analyze_locations(commutes)
|
|
|
-
|
|
|
- properties = load_properties()
|
|
|
-
|
|
|
- write_kml(commutes, properties, 'vis.kml')
|
|
|
-
|
|
|
-
|
|
|
- # log_graph(graph, commutes)
|