visualize.py 5.8 KB

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