-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocation.py
73 lines (58 loc) · 2.02 KB
/
location.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import heapq
import threading
import urllib.request
from io import BytesIO
from zipfile import ZipFile
import csv
from dataclasses import dataclass
@dataclass
class StopInfo:
name: str
id: str
lon: float
lat: float
def distance(self, lat, lon) -> float:
return ((self.lat -lat)**2 + (self.lon - lon)**2)
class OpenTransitThread:
def __init__(self, url, interval):
self.url = url #"http://www.titsa.com/Google_transit.zip"
self.interval = interval
def start(self):
self.__getTransitFile()
def __getTransitFile(self):
response = urllib.request.urlopen(self.url)
if response.getcode() == 200:
zipfile = ZipFile(BytesIO(response.read()))
StopsHandler.updateStops(zipfile.open("stops.txt"))
self.thread = threading.Timer(self.interval, self.__getTransitFile)
self.thread.start()
def stop(self):
if self.thread is not None:
self.thread.cancel()
class StopsHandler:
stops = {}
mutex = threading.Lock()
@staticmethod
def updateStops(stopsInfo):
stops = {}
csv_reader = csv.DictReader(stopsInfo.read().decode("utf-8-sig").splitlines())
for stop in csv_reader:
stops[int(stop["stop_id"])] = StopInfo(stop["stop_name"], stop["stop_id"], float(stop["stop_lon"]), float(stop["stop_lat"]))
with StopsHandler.mutex:
StopsHandler.stops = stops
@staticmethod
def nearestStops(k, lat, lon):
if len(StopsHandler.stops) == 0:
return None
with StopsHandler.mutex:
auxList = []
for stop in StopsHandler.stops.values():
auxList.append((stop.distance(lat,lon), stop))
return [aux[1] for aux in heapq.nsmallest(k, auxList, key=lambda item: item[0])]
@staticmethod
def stationName(id):
return StopsHandler.stops[int(id)].name
@staticmethod
def stopLocation(id):
stop = StopsHandler.stops[int(id)]
return stop.lat, stop.lon