visualize.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. max_score = max(x['score'] for x in properties_dict.values())
  47. properties_list = list(properties_dict.items())
  48. # properties_list.sort(key=lambda x: x[1]['score'], reverse=True)
  49. import random
  50. random.shuffle(properties_list)
  51. for i, (pid, data) in enumerate(properties_list[:500]):
  52. color = 20 + (int((data['score']/float(max_score)) * 236) if max_score else 0)
  53. color = min(color, 255)
  54. # color = 20 + int(236*(float(i)/len(properties_list)))
  55. title = ','.join([str(x) for x in [
  56. int(data['price']),
  57. data['size'],
  58. ]])
  59. description = '<br>\n'.join([
  60. 'https://www.myhome.ie/_/brochure/_/%i' % pid,
  61. '',
  62. 'Price: %s (%.3f)' % (data.get('price', '---'), ranking.r_price(data.get('price'))),
  63. 'Size: %s (%.3f)' % (int(data.get('size', 0)), ranking.r_size(data.get('size'))),
  64. 'Bedrooms: %s' % data.get('bedrooms', '---'),
  65. ])
  66. total_duration = sum(x['duration'] for x in data.get('commute_path', []))
  67. description += '<br>\nCommute: %i (%.3f)<br>' % (total_duration, ranking.r_commute_duration(total_duration))
  68. description += '<br>\n'.join([(' %i: %s' % (x['duration'], (x.get('desc') or x['mode']).replace('>','&gt;').replace('<', '&lt;')))
  69. for x in reversed(data.get('commute_path', []))])
  70. f.write('''<Placemark>
  71. <Point><coordinates>%f,%f</coordinates></Point>
  72. <styleUrl>#color-%i</styleUrl>
  73. <name>%s</name>
  74. <description><![CDATA[ %s ]]></description>
  75. </Placemark>\n''' % (data['loc'][1], data['loc'][0], color, title, description))
  76. f.write('</Folder>\n')
  77. def _write_kml(commutes, properties_dict, fname):
  78. print('Writing .kml visualization file...')
  79. with open(fname, 'w') as f:
  80. f.write('''<?xml version="1.0" encoding="UTF-8"?>
  81. <kml xmlns="http://www.opengis.net/kml/2.2">
  82. <Document>
  83. ''')
  84. _write_styles(f)
  85. _write_bboxes(f)
  86. # _write_commutes(f, commutes)
  87. _write_properties(f, properties_dict)
  88. f.write('''
  89. </Document>
  90. </kml>
  91. ''')
  92. def run():
  93. graph = commute.load_graph()
  94. commutes = commute.determine_fastest_paths(graph, config.WORKPLACE)
  95. # import json
  96. # print(json.dumps({str(k): v for k, v in commutes.items()}))
  97. properties_dict = properties.load_properties()
  98. commute.add_commute_data(properties_dict, commutes)
  99. ranking.add_scores(properties_dict)
  100. properties_dict = {k: v for k, v in properties_dict.items() if v['score'] > 0}
  101. _write_kml(commutes, properties_dict, 'vis.kml')
  102. if __name__ == '__main__':
  103. run()