Add function to find nearest PT stop from coordinates, relying on OSM...
This commit is contained in:
parent
8d09794c9f
commit
494dc01554
|
@ -3,13 +3,19 @@ class Station:
|
||||||
self.name = name
|
self.name = name
|
||||||
self.sttype = sttype
|
self.sttype = sttype
|
||||||
self.extid = extid
|
self.extid = extid
|
||||||
self.xcoord = xcoord
|
self.xcoord = float(xcoord)/1000000
|
||||||
self.ycoord = ycoord
|
self.ycoord = float(ycoord)/1000000
|
||||||
self.prodclass = prodclass
|
self.prodclass = prodclass
|
||||||
|
|
||||||
def useId(self):
|
def useId(self):
|
||||||
return self.extid or self.name
|
return self.extid or self.name
|
||||||
|
|
||||||
|
def lat(self):
|
||||||
|
return self.ycoord
|
||||||
|
|
||||||
|
def lon(self):
|
||||||
|
return self.xcoord
|
||||||
|
|
||||||
class Service:
|
class Service:
|
||||||
def __init__(self, name, svtype, depst, deptime, arrst, arrtime, deppf = None, currdep = None, arrpf = None, curarr = None, img = None, url = None):
|
def __init__(self, name, svtype, depst, deptime, arrst, arrtime, deppf = None, currdep = None, arrpf = None, curarr = None, img = None, url = None):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
60
main.py
60
main.py
|
@ -1,7 +1,9 @@
|
||||||
import cgi
|
import cgi
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
import workers.conn
|
import workers.conn
|
||||||
import workers.val
|
import workers.val
|
||||||
|
import workers.closest
|
||||||
|
|
||||||
def application(env, re):
|
def application(env, re):
|
||||||
if env["REQUEST_METHOD"] == "POST":
|
if env["REQUEST_METHOD"] == "POST":
|
||||||
|
@ -39,6 +41,16 @@ def application(env, re):
|
||||||
time = args["time"][0] if "time" in args and args["time"] else datetime.datetime.strftime(datetime.datetime.now(),"%H:%M")
|
time = args["time"][0] if "time" in args and args["time"] else datetime.datetime.strftime(datetime.datetime.now(),"%H:%M")
|
||||||
mode = True if "mode" in args and args["mode"] and args["mode"][0].lower() == "arr" else False
|
mode = True if "mode" in args and args["mode"] and args["mode"][0].lower() == "arr" else False
|
||||||
|
|
||||||
|
try:
|
||||||
|
count = int(count)
|
||||||
|
if count < 0 or count > 10:
|
||||||
|
raise ValueError()
|
||||||
|
except:
|
||||||
|
re("400 Bad Request", [])
|
||||||
|
yield "<h1>400 Bad Request</h1>".encode()
|
||||||
|
yield "The \"count\" value must be a value between 0 and 10."
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
outtime = datetime.datetime.strptime("%s %s" % (date, time), "%d.%m.%Y %H:%M")
|
outtime = datetime.datetime.strptime("%s %s" % (date, time), "%d.%m.%Y %H:%M")
|
||||||
except:
|
except:
|
||||||
|
@ -75,7 +87,7 @@ def application(env, re):
|
||||||
try:
|
try:
|
||||||
output = workers.val.worker(name)
|
output = workers.val.worker(name)
|
||||||
|
|
||||||
except:
|
except Exception as e:
|
||||||
re("500 Internal Server Error", [])
|
re("500 Internal Server Error", [])
|
||||||
yield "<h1>500 Internal Server Error</h1>".encode()
|
yield "<h1>500 Internal Server Error</h1>".encode()
|
||||||
if "debug" in args:
|
if "debug" in args:
|
||||||
|
@ -84,3 +96,49 @@ def application(env, re):
|
||||||
|
|
||||||
re("200 OK", [])
|
re("200 OK", [])
|
||||||
return output.encode()
|
return output.encode()
|
||||||
|
|
||||||
|
elif rtype.lower() in ["closest", "close", "near", "nearby"]:
|
||||||
|
try:
|
||||||
|
lat = float(args["lat"][0])
|
||||||
|
lon = float(args["lon"][0])
|
||||||
|
|
||||||
|
if (not lat and not lat == float(0)) or (not lon and not lon == float(0)):
|
||||||
|
raise ValueError()
|
||||||
|
|
||||||
|
except:
|
||||||
|
re("400 Bad Request", [])
|
||||||
|
yield "<h1>400 Bad Request</h1>".encode()
|
||||||
|
yield "\"lat\" and \"lon\" values are required for this type of request.".encode()
|
||||||
|
return
|
||||||
|
|
||||||
|
distance = args["distance"][0] if "distance" in args and args["distance"] else 1000
|
||||||
|
|
||||||
|
try:
|
||||||
|
distance = int(distance)
|
||||||
|
if distance < 0 or distance > 10000:
|
||||||
|
raise ValueError()
|
||||||
|
except:
|
||||||
|
re("400 Bad Request", [])
|
||||||
|
yield "<h1>400 Bad Request</h1>"
|
||||||
|
yield "\"distance\" must be a value between 0 and 10000."
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
output = workers.closest.worker(lat, lon, distance)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
re("500 Internal Server Error", [])
|
||||||
|
yield "<h1>500 Internal Server Error</h1>".encode()
|
||||||
|
if "debug" in args:
|
||||||
|
yield e.encode()
|
||||||
|
return
|
||||||
|
|
||||||
|
re("200 OK", [])
|
||||||
|
yield output.encode()
|
||||||
|
return
|
||||||
|
|
||||||
|
else:
|
||||||
|
re("400 Bad Request", [])
|
||||||
|
yield "<h1>400 Bad Request</h1>".encode()
|
||||||
|
yield "The request type you submitted is invalid.".encode()
|
||||||
|
return
|
||||||
|
|
|
@ -22,4 +22,14 @@ def validateName(name):
|
||||||
yield Station(name = name, sttype = sttype, extid = extid, xcoord = xcoord, ycoord = ycoord, prodclass = prodclass)
|
yield Station(name = name, sttype = sttype, extid = extid, xcoord = xcoord, ycoord = ycoord, prodclass = prodclass)
|
||||||
|
|
||||||
def worker(name):
|
def worker(name):
|
||||||
return "Validate."
|
outtext = """<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<stations>
|
||||||
|
"""
|
||||||
|
|
||||||
|
for station in validateName(name):
|
||||||
|
outtext += "<station><name>%s</name><id>%s</id></station>\n" % (station.name, station.useId())
|
||||||
|
|
||||||
|
outtext += "</stations>"
|
||||||
|
|
||||||
|
return outtext
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue