Add function to find nearest PT stop from coordinates, relying on OSM...

This commit is contained in:
Klaus-Uwe Mitterer 2017-09-21 21:55:32 +02:00
parent 8d09794c9f
commit 494dc01554
3 changed files with 78 additions and 4 deletions

View file

@ -3,13 +3,19 @@ class Station:
self.name = name
self.sttype = sttype
self.extid = extid
self.xcoord = xcoord
self.ycoord = ycoord
self.xcoord = float(xcoord)/1000000
self.ycoord = float(ycoord)/1000000
self.prodclass = prodclass
def useId(self):
return self.extid or self.name
def lat(self):
return self.ycoord
def lon(self):
return self.xcoord
class Service:
def __init__(self, name, svtype, depst, deptime, arrst, arrtime, deppf = None, currdep = None, arrpf = None, curarr = None, img = None, url = None):
self.name = name

60
main.py
View file

@ -1,7 +1,9 @@
import cgi
import datetime
import workers.conn
import workers.val
import workers.closest
def application(env, re):
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")
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:
outtime = datetime.datetime.strptime("%s %s" % (date, time), "%d.%m.%Y %H:%M")
except:
@ -75,7 +87,7 @@ def application(env, re):
try:
output = workers.val.worker(name)
except:
except Exception as e:
re("500 Internal Server Error", [])
yield "<h1>500 Internal Server Error</h1>".encode()
if "debug" in args:
@ -84,3 +96,49 @@ def application(env, re):
re("200 OK", [])
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

View file

@ -22,4 +22,14 @@ def validateName(name):
yield Station(name = name, sttype = sttype, extid = extid, xcoord = xcoord, ycoord = ycoord, prodclass = prodclass)
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