Browse Source

Geocoding for jcdecaux_vls !

Olivier Marty 8 years ago
parent
commit
a285cd75da
3 changed files with 19 additions and 33 deletions
  1. 0 10
      geocoding.py
  2. 18 9
      main.py
  3. 1 14
      source.py

+ 0 - 10
geocoding.py

@@ -24,18 +24,8 @@ def dist(posa, posb):
     else:
         return vincenty(posa, posb).km
 
-p1 = position_of_location("22 rue Henri Barbusse VILLEJUIF")
-#p2 = position_of_location("Université paris diderot")
-#print(p1, p2, dist(p1, p2))
-
-
 def k_neighbors(positions, fro, n):
   """returns a list of (dist, id) of the n nearest points from fro
   positions is a dictionary id -> (lat, long)"""
   distances = sorted([(dist(fro, pos), id) for (id, pos) in positions.items()])
   return distances[:n]
-
-res = k_neighbors(SourceProvider_jcdecaux_vls().dic_of_positions(), p1, 5)
-names = SourceProvider_jcdecaux_vls().dic_of_names()
-for (dist, id) in res:
-  print(names[id] + ' at ' + str(dist) + 'km')

+ 18 - 9
main.py

@@ -1,9 +1,10 @@
-import source
+from source import *
 import config
 from event import Event, HeapEvent
 from datetime import datetime, timedelta
 from time import sleep
 import notification
+from geocoding import position_of_location, k_neighbors
 from analyse_event import *
 
 def make_tz_aware(dt, tz='UTC', is_dst=None):
@@ -15,9 +16,21 @@ def make_tz_aware(dt, tz='UTC', is_dst=None):
         pass
     return tz.localize(dt, is_dst=is_dst)
 
-
+def gen_sources(sourceProviders, location):
+  position = position_of_location(location)
+  if not position:
+    print("Unable to find a position for " + location)
+  else:
+    for sp in sourceProviders:
+      # keep 2 nearest sources, if distance < 5 km
+      ids = [id for (dist, id) in k_neighbors(sp.dic_of_positions(), position, 2) if dist < 5]
+      for source in sp.sources_of_ids(ids):
+          yield source
 
 def main():
+    sourceProviders = [SourceProvider_ratp(),
+      SourceProvider_jcdecaux_vls(),
+      SourceProvider_transilien()]
     event_seen = set()
     heap = HeapEvent()
     manual = [Event('manual_1', datetime.now()+timedelta(minutes=30, seconds=10), "Villejuif", "descr Villejuif"),
@@ -55,16 +68,12 @@ def main():
             print("Check event:")
             print(str(event))
 
-            # get useful ids of sources for this location
-            ids_sources = source.from_location(event.location)
-            # flatten this dictionary
-            ids_sources_flat = [item for (key, sublist) in ids_sources.items() for item in sublist]
-            # grab info from internet for these sources
-            sources=source.gen_sources(ids_sources)
+            # get useful ids of sources for this location, and grab info from internet
+            sources=gen_sources(sourceProviders, event.location)
             if 'print' in config.notification['methods']:
                 print() # show an empty line
             for src in sources:
-              if src.id in ids_sources_flat and src.problem():
+              if src.problem():
                 # there is a problem ! We notify the user...
                 notification.notify(src.message)
 

+ 1 - 14
source.py

@@ -134,7 +134,7 @@ class SourceProvider_jcdecaux_vls(SourceProvider):
       xml = XML(url='https://api.jcdecaux.com/vls/v1/stations?apiKey=' + config.api_key['jcdecaux_vls'], lang='json')
       self.positions = {}
       for sta in xml.data.json.find_all("item", recursive=False):
-        self.positions[sta.contract_name.string.lower() + '_' + sta.number.string] =\
+        self.positions[sta.contract_name.string.lower() + '_' + sta.number.string + '_' + 'full'] =\
           (sta.lat.string, sta.lng.string)
         # we use find('name') because .name is the current tag name
     return self.positions
@@ -206,16 +206,3 @@ def from_location(location):
     """return a list of source ids useful for location
     TODO : for the moment returns the whole config.sources"""
     return config.sources
-
-sp_ratp = None
-sp_jcdecaux_vls = None
-sp_transilien = None
-
-def gen_sources(ids):
-  global sp_ratp, sp_jcdecaux_vls, sp_transilien
-  sp_ratp = sp_ratp or SourceProvider_ratp()
-  sp_jcdecaux_vls = sp_jcdecaux_vls or SourceProvider_jcdecaux_vls()
-  sp_transilien = sp_transilien or SourceProvider_transilien()
-  return chain(sp_ratp.sources_of_ids(ids.get('ratp_trafic', [])),\
-      sp_transilien.sources_of_ids(ids.get('transilien', [])),\
-      sp_jcdecaux_vls.sources_of_ids(ids.get('jcdecaux_vls', [])))