oebb_py/workers/closest.py

77 lines
1.9 KiB
Python
Raw Normal View History

2017-09-22 15:23:37 +00:00
import overpy
import math
import workers.val
def getOSM(lat, lon, distance = 1000):
query = """
(
node
(around:%i,%f,%f)
["railway"~"tram_stop|station|subway"]
[name];
>;
node
(around:%i,%f,%f)
["highway"="bus_stop"]
[name];
>;);
out;
""" % (distance, lat, lon, distance, lat, lon)
api = overpy.Overpass()
res = api.query(query)
return res.nodes
def cDistance(lat1, lon1, lat2, lon2):
lat1, lon1, lat2, lon2 = map(math.radians, [lat1, lon1, lat2, lon2])
dila = lat2 - lat1
dilo = lon2 - lon1
diff = 2 * math.asin(math.sqrt(math.sin(dila/2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(dilo/2) ** 2))
dis = diff * 6371000
return dis
def getStations(nodes, distance = 1000, lat = None, lon = None):
unodes = list(set([node.tags["alt_name"].split(",")[0] if "alt_name" in node.tags and len(node.tags["alt_name"].split(",")[0]) > len(node.tags["name"]) else node.tags["name"] for node in nodes]))
for node in unodes:
for station in workers.val.validateName(node):
if lat and lon:
idistance = cDistance(station.lat(), station.lon(), lat, lon)
if idistance < distance:
station.distance = idistance
yield station
break
else:
yield station
break
def findStations(lat, lon, distance = 1000, validate = True):
stations = list(getStations(getOSM(lat, lon, distance), distance, lat if validate else None, lon if validate else None))
if validate:
return sorted(stations, key = lambda station: station.distance)
return stations
def worker(lat, lon, distance = 1000):
outtext = """<?xml version="1.0" encoding="UTF-8"?>
<stations>
"""
for station in findStations(lat, lon, distance):
try:
idistance = int(station.distance)
except:
idistance = int(cDistance(station.lat(), station.lon(), lat, lon))
outtext += "<station><name>%s</name><id>%s</id><distance>%i</distance></station>\n" % (station.name, station.useId(), idistance)
outtext += "</stations>"
return outtext