visualize.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import commute
  2. import config
  3. import db
  4. import geo_util
  5. import properties
  6. import ranking
  7. def _write_bboxes(f):
  8. f.write('''
  9. <Folder>
  10. <visibility>0</visibility>
  11. <name>Monitored bboxes</name>\n''')
  12. for (lat1, lon1), (lat2, lon2) in config.MONITORED_BBOXES:
  13. f.write('<Placemark><LineString><coordinates>')
  14. f.write('%s,%s' % (lon1, lat1))
  15. f.write(' %s,%s' % (lon1, lat2))
  16. f.write(' %s,%s' % (lon2, lat2))
  17. f.write(' %s,%s' % (lon2, lat1))
  18. f.write(' %s,%s' % (lon1, lat1))
  19. f.write(' %s,%s' % (lon2, lat2))
  20. f.write(' %s,%s' % (lon1, lat2))
  21. f.write(' %s,%s' % (lon2, lat1))
  22. f.write('</coordinates></LineString></Placemark>\n')
  23. f.write('</Folder>')
  24. def _write_styles(f):
  25. for i in range(256):
  26. hexvalue = hex(255-i)[2:].lower()
  27. if len(hexvalue) == 1:
  28. hexvalue = '0' + hexvalue
  29. f.write('<Style id="color-%i"><IconStyle><color>ff%s%sff</color></IconStyle></Style>\n' % (i, hexvalue, hexvalue))
  30. def _write_commutes(f, commutes):
  31. f.write('<Folder><name>Commutes</name><visibility>0</visibility>\n')
  32. max_duration = min(4000, max([x['duration'] for x in commutes.values()]))
  33. for loc, commute_data in commutes.items():
  34. duration = commute_data['duration']
  35. color = 20 + int((duration/float(max_duration)) * 236)
  36. color = min(color, 255)
  37. f.write('''<Placemark>
  38. <Point><coordinates>%f,%f</coordinates></Point>
  39. <styleUrl>#color-%i</styleUrl>
  40. <name>%s</name>
  41. <description>%s</description>
  42. </Placemark>\n''' % (loc[1], loc[0], color, int(duration / 60), loc))
  43. f.write('</Folder>')
  44. def _write_properties(f, properties_dict):
  45. f.write('<Folder><name>Properties</name><visibility>0</visibility>\n')
  46. properties_list = list(properties_dict.items())
  47. properties_list.sort(key=lambda x: x[1]['score'], reverse=True)
  48. # properties_list = properties_list[:1000]
  49. min_score = max(0, min(x['score'] for x in properties_dict.values()))
  50. max_score = max(x['score'] for x in properties_dict.values())
  51. min_max_delta = max_score - min_score
  52. for pid, data in properties_list:
  53. value = (data['score'] - min_score) / min_max_delta
  54. value = min(1, value)
  55. color = 20 + value * 235
  56. title = '%.0f-%.0f' % (data['price'], data['size'])
  57. description = '<br>\n'.join([
  58. 'https://www.myhome.ie/_/brochure/_/%i' % pid,
  59. '',
  60. 'Price: %s (%.3f)' % (data.get('price', '---'), ranking.r_price(data.get('price'))),
  61. 'Size: %s (%.3f)' % (int(data.get('size', 0)), ranking.r_size(data.get('size'))),
  62. 'Bedrooms: %s (%.3f)' % (data.get('bedrooms') or '---', ranking.r_bedrooms(data.get('bedrooms'))),
  63. 'BER: %s (%.3f)' % (data.get('ber') or '---', ranking.r_ber(data.get('ber'))),
  64. ])
  65. total_duration = sum(x['duration'] for x in data.get('commute_path', []))
  66. description += '<br>\nCommute: %.1f (%.3f)<br>' % (total_duration/60.0, ranking.r_commute_duration(total_duration))
  67. description += '<br>\n'.join([(' %.1f: %s' % (x['duration']/60.0, (x.get('desc') or x['mode']).replace('>','&gt;').replace('<', '&lt;')))
  68. for x in reversed(data.get('commute_path', []))])
  69. f.write('''<Placemark>
  70. <Point><coordinates>%f,%f</coordinates></Point>
  71. <styleUrl>#color-%i</styleUrl>
  72. <name>%s</name>
  73. <description><![CDATA[ %s ]]></description>
  74. </Placemark>\n''' % (data['loc'][1], data['loc'][0], color, title, description))
  75. f.write('</Folder>\n')
  76. def _write_kml(commutes, properties_dict, fname):
  77. print('Writing .kml visualization file...')
  78. with open(fname, 'w') as f:
  79. f.write('''<?xml version="1.0" encoding="UTF-8"?>
  80. <kml xmlns="http://www.opengis.net/kml/2.2">
  81. <Document>
  82. ''')
  83. _write_styles(f)
  84. _write_bboxes(f)
  85. # _write_commutes(f, commutes)
  86. _write_properties(f, properties_dict)
  87. f.write('''
  88. </Document>
  89. </kml>
  90. ''')
  91. def run():
  92. graph = commute.load_graph()
  93. commutes = commute.determine_fastest_paths(graph, config.WORKPLACE)
  94. # import json
  95. # print(json.dumps({str(k): v for k, v in commutes.items()}))
  96. properties_dict = properties.load_properties()
  97. commute.add_commute_data(properties_dict, commutes)
  98. ranking.add_scores(properties_dict)
  99. properties_dict = {k: v for k, v in properties_dict.items() if v['score'] > 0}
  100. print('Loaded %i locations with score>0' % len(properties_dict))
  101. _write_kml(commutes, properties_dict, 'vis.kml')
  102. if __name__ == '__main__':
  103. run()