def nearest_stations(bot, update, count=5):
with open('allstations.csv', newline='', encoding='utf-8') as infile:
csv_reader = csv.reader(infile, delimiter=';')
stations = [(int(row[0]), float(row[1]), float(row[2]), row[3]) for row in csv_reader]
# distance sorting based on http://stackoverflow.com/a/28368926 by Sergey Ivanov
coord = (float(update.message.location.latitude), float(update.message.location.longitude))
pts = [geopy.Point(p[1], p[2], p[0]) for p in stations]
sts = [p[3] for p in stations]
onept = geopy.Point(coord[0], coord[1])
alldist = [(p, geopy.distance.distance(p, onept).m) for p in pts]
nearest = sorted(alldist, key=lambda x: (x[1]))[:count]
nearest_points = [n[0] for n in nearest]
nearest_distances = [n[1] for n in nearest]
nearest_sts = [sts[int(n.altitude)] for n in nearest_points]
msg = 'Nächstgelegene Stationen:'
for s, d, p in zip(nearest_sts, nearest_distances, nearest_points):
msg += '\n{} (<a href="https://www.google.de/maps?q={},{}">{:.0f}m</a>)'.format(s, p.latitude,
p.longitude, d)
reply_keyboard = [[telegram.KeyboardButton(text='/Abfahrten {}'.format(n))] for n in nearest_sts]
bot.sendMessage(chat_id=update.message.chat_id, text=msg, parse_mode='HTML',
reply_markup=telegram.ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))
评论列表
文章目录