| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- import sys
- import commute
- import config
- import db
- import geo_util
- import properties
- import ranking
- import util
- import datetime
- import visualize_map
- def _write_bboxes(f):
- f.write('''
- <Folder>
- <visibility>0</visibility>
- <name>Monitored bboxes</name>\n''')
- for (lat1, lon1), (lat2, lon2) in config.VISUALIZED_BBOXES.values():
- 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('</Folder>')
- def _write_styles(f):
- 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))
- def _write_commutes(f, commutes):
- f.write('<Folder><name>Commutes</name><visibility>0</visibility>\n')
- max_duration = min(4000, max([x['duration'] for x in commutes.values()]))
- for loc, commute_data in commutes.items():
- duration = commute_data['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>')
- def _write_properties(f, properties_dict, tmp_ids):
- f.write('<Folder><name>Properties</name><visibility>0</visibility>\n')
- properties_list = list(properties_dict.items())
- properties_list.sort(key=lambda x: x[1]['score'], reverse=True)
- min_score = max(0, min(x['score'] for x in properties_dict.values()))
- max_score = max(x['score'] for x in properties_dict.values())
- min_max_delta = max_score - min_score
- for pid, data in properties_list:
- value = ((data['score'] - min_score) / min_max_delta) if min_max_delta else 1
- value = min(1, value)
- color = 20 + value * 235
- title = '%.0f-%.0f-%s-%i' % (data['price'], data['size'], data.get('ber', ''), tmp_ids[pid])
- description = '<br>\n'.join([
- 'https://www.myhome.ie/_/brochure/_/%i' % pid,
- '',
- 'Price: %s (%.3f)' % (data.get('price', '---'), ranking.r_price(data.get('price'))),
- 'Size: %s (%.3f)' % (int(data.get('size', 0)), ranking.r_size(data.get('size'))),
- 'Bedrooms: %s (%.3f)' % (data.get('bedrooms') or '---', ranking.r_bedrooms(data.get('bedrooms'))),
- 'Bathrooms: %s (%.3f)' % (data.get('bathrooms') or '---', ranking.r_bathrooms(data.get('bathrooms'))),
- 'BER: %s (%.3f)' % (data.get('ber') or '---', ranking.r_ber(data.get('ber'))),
- 'Type: %s (%.3f)' % (data.get('type') or '---', ranking.r_type(data.get('type'))),
- ])
- total_duration = sum(x['duration'] for x in data.get('commute_path', []))
- description += '<br>\nCommute: %.1f (%.3f)<br>' % (total_duration/60.0, ranking.r_commute_duration(total_duration))
- description += '<br>\n'.join([(' %.1f: %s' % (x['duration']/60.0, (x.get('desc') or x['mode']).replace('>','>').replace('<', '<')))
- for x in reversed(data.get('commute_path', []))])
- f.write('''<Placemark>
- <Point><coordinates>%f,%f</coordinates></Point>
- <styleUrl>#color-%i</styleUrl>
- <name>%s</name>
- <description><![CDATA[ %s ]]></description>
- </Placemark>\n''' % (data['loc'][1], data['loc'][0], color, title, description))
- f.write('</Folder>\n')
- def _write_kml(commutes, properties_dict, fname, tmp_ids):
- 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>
- ''')
- _write_styles(f)
- _write_bboxes(f)
- if commutes:
- _write_commutes(f, commutes)
- _write_properties(f, properties_dict, tmp_ids)
- f.write('''
- </Document>
- </kml>
- ''')
- def _set_tmp_ids(properties_dict):
- print ('Setting tmp ids...')
- res = {}
- with db.connect() as conn:
- conn.execute('update properties set tmp_id=null')
- properties_list = list(properties_dict.items())
- properties_list.sort(key=lambda x: x[1]['score'], reverse=True)
- for i, (pid, _) in enumerate(properties_list):
- conn.execute('update properties set tmp_id=? where id=?', i, pid)
- res[pid] = i
- return res
- def run():
- days = int(sys.argv[1]) if len(sys.argv) > 1 else 31
- load_since = datetime.datetime.now() - datetime.timedelta(days=days)
- properties_dict = properties.load_properties(True, load_since)
- print('Loaded %i properties' % len(properties_dict))
- commutes = None
- # graph = commute.load_graph()
- # commutes = commute.determine_fastest_paths(graph, config.WORKPLACE)
- # commute.add_commute_data(properties_dict, commutes)
-
- properties_dict = ranking.add_scores(properties_dict)
- n1 = len(properties_dict)
- print('\nLoaded %i properties with score > 0' % len(properties_dict))
- util.print_distribution([v['score'] for v in properties_dict.values()])
- properties_dict = ranking.rerank(properties_dict)
- n2 = len(properties_dict)
- if n1 != n2:
- print('\nAfter reranking, %i properties with score > 0' % len(properties_dict))
- util.print_distribution([v['score'] for v in properties_dict.values()])
- tmp_ids = _set_tmp_ids(properties_dict)
- _write_kml(commutes, properties_dict, 'vis.kml', tmp_ids)
- visualize_map.visualize(properties_dict)
- if __name__ == '__main__':
- run()
|