Переглянути джерело

visualization of properties, very simple scoring

Martin 8 роки тому
батько
коміт
a63578d57b
2 змінених файлів з 104 додано та 27 видалено
  1. 96 23
      commute.py
  2. 8 4
      config.py

+ 96 - 23
commute.py

@@ -92,6 +92,7 @@ def directions_call(source, destination, mode):
     _record_api_response(resp_data)
 
 def load_graph():
+    print('Loading stored transit data...')
 
     graph = {
         config.WORKPLACE: {}
@@ -120,6 +121,8 @@ def load_graph():
 
     locs = list(graph.keys())
 
+    print('Creating additional commute links...')
+
     for i, loc in enumerate(locs):
         for secondary_loc in locs[i+1:]:
 
@@ -150,6 +153,8 @@ def load_graph():
 
 
 def determine_fastest_paths(graph, source):
+    print('Determining the fastest paths from transit locations to %s...' % str(source))
+
     indices = {}
     for loc in graph:
         if loc not in indices:
@@ -202,7 +207,9 @@ def log_graph(graph, commutes):
 
         print('')
 
-def write_kml(commutes, fname):
+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">
@@ -220,16 +227,28 @@ def write_kml(commutes, fname):
         f.write('''
         <Folder>
             <visibility>0</visibility>
-            <name>bbox</name>
-            <Placemark><Point><coordinates>%f,%f</coordinates></Point></Placemark>
-            <Placemark><Point><coordinates>%f,%f</coordinates></Point></Placemark>
-        </Folder>
-        ''' % (config.MONITORED_BBOX[0][1], config.MONITORED_BBOX[0][0], config.MONITORED_BBOX[1][1], config.MONITORED_BBOX[1][0]))
-
+            <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>\n')
-        f.write('<name>Commutes</name>\n')
-        
+        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)
@@ -244,20 +263,43 @@ def write_kml(commutes, fname):
 
         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):
+def analyze_locations(commutes, num_locations=5):
+    print('Loading new directions data for %i locations...' % num_locations)
+
     with db.connect() as conn:
-        locs = config.MONITORED_LOCALITIES
         rows = conn.select('''select p.id, p.lat, p.lon from properties p
                                 where lat is not null and lon is not null''')
 
         random.shuffle(rows)
-
         to_load = []
 
         for property_id, lat, lon in rows:
@@ -273,30 +315,61 @@ def analyze_locations(commutes):
             target_loc = (lat, lon)
 
             if (geo_util.ggcd(target_loc, closest) > 200
-                and geo_util.inside(target_loc, config.MONITORED_BBOX)):
-
+                and any(geo_util.inside(target_loc, bbox) for bbox in config.MONITORED_BBOXES)):
                 to_load.append(target_loc)
         
-        print('Remaining to load: %i' % len(to_load))
+        print('\tRemaining to load: %i' % len(to_load))
 
         # target = (53.699593,-6.259772)
         # scores = {loc: geo_util.ggcd(target, loc) for loc in to_load}
         # to_load.sort(key=lambda loc: scores[loc])
 
-        for loc in to_load[:1]:
-            print('Loading %s...' % list(loc))
+        return
+
+        for loc in to_load[:num_locations]:
+            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
 
 if __name__ == '__main__':
 
     # _record_api_response(data.x)
     # exit()
 
-    graph = load_graph()
-    commutes = determine_fastest_paths(graph, config.WORKPLACE)
-    write_kml(commutes, 'vis.kml')
+    for _ in range(1):
+        graph = load_graph()
+        commutes = determine_fastest_paths(graph, config.WORKPLACE)
+
+        # analyze_locations(commutes)
+
+    properties = load_properties()
+
+    write_kml(commutes, properties, 'vis.kml')
 
-    # analyze_locations(commutes)
 
-    log_graph(graph, commutes)
+    # log_graph(graph, commutes)

+ 8 - 4
config.py

@@ -17,10 +17,14 @@ MONITORED_LOCALITIES = [
     2712, # Co Meath
 ]
 
-MONITORED_BBOX = (
-    (53, -6.9),
-    (53.75, -5.9),
-)
+MONITORED_BBOXES = [
+    ((53, -6.9), (53.85, -5.9)), # Overall
+    ((53.358687,-6.219396), (53.371389,-6.168778)), # Clontarf
+    ((53.355553,-6.108301), (53.397744,-6.031430)), # Howth
+    ((53.459195,-6.173369), (53.588187,-6.060123)), # NE
+    ((53.450280,-6.190388), (53.456924,-6.158301)), # Malahide estuary
+    ((53.265768,-6.127407), (53.290505,-6.081422)), # Dalkey, Killiney
+]
 
 WORKPLACE = (53.353361,-6.247984)
 WORKPLACE_TARGET_TIME = 1540893600 # tue 10:00