From: zharkovstas Date: Sun, 19 May 2019 03:57:48 +0000 (+0500) Subject: Add object directions X-Git-Url: https://git.xn--bdkaa.com/?a=commitdiff_plain;h=91332fc1235d2decdd641c1319b1367f9a91972d;p=where-are-you.py.git Add object directions --- diff --git a/app.py b/app.py index b1839fa..2e67f91 100644 --- a/app.py +++ b/app.py @@ -23,7 +23,7 @@ cities = { "Казань": (55.770257, 55.830138, 49.088112, 49.181250), "Самара": (53.171396, 53.299662, 50.066118, 50.288368), "Санкт-Петербург": (59.896114, 59.993548, 30.231423, 30.413881), - "Лондон": (51.464854, 51.575864,-0.181617, 0.012276) + "Лондон": (51.464854, 51.575864, -0.181617, 0.012276) } move_distance = 300 @@ -61,7 +61,8 @@ def add_tips(game): coordinate[1] + radius) for o in near_objects['amenities']: - game.tips.append(f'Рядом с вами находится {o["name"]}') + direction = get_direction(coordinate, (o['lat'], o['lon'])) + game.tips.append(f'На {convert_direction(direction)}е от вас находится {o["name"]}') for o in near_objects['rivers']: game.tips.append(f'Рядом с вами протекает {o["name"]} 🌊') @@ -76,8 +77,10 @@ def add_tips(game): success, summary = parse_summary( s['name'].replace('улица', '').replace('проспект', '').replace('переулок', '').strip()) - if success and summary and 'улица' not in summary and not re.search(stemming(s['name']), stemming(summary), re.IGNORECASE): - game.tips.append(f'{summary[0].capitalize() + summary[1:]}. Это как-то связано с названием ближайшей улицы 🤔') + if success and summary and 'улица' not in summary and not re.search(stemming(s['name']), stemming(summary), + re.IGNORECASE): + game.tips.append( + f'{summary[0].capitalize() + summary[1:]}. Это как-то связано с названием ближайшей улицы 🤔') buildings = near_objects['buildings'] @@ -94,12 +97,23 @@ def add_tips(game): for s in near_objects['sightseeings']: type = convert_sightseeing_type(s['type']) + direction = get_direction(coordinate, (s['lat'], s['lon'])) game.tips.append( - f'Кстати, недалеко ' + (type if type else 'интересный туристический объект') + ': ' + s["name"]) + f'Кстати, неподалеку на {convert_direction(direction)}е есть ' + (type if type else 'интересный туристический объект') + ': ' + s["name"]) shuffle(game.tips) +def get_direction(my_coordinates, object_coordinates): + dlat = object_coordinates[0] - my_coordinates[0] + dlon = object_coordinates[1] - my_coordinates[1] + + if abs(dlat) < abs(dlon): + return 'east' if dlon > 0 else 'west' + + return 'north' if dlat > 0 else 'south' + + def convert_building_type(building_type): if building_type == 'dormitory': return 'с общежитием' @@ -300,7 +314,7 @@ def move(game_id, direction): add_tips(game) - game.shown_tips.append(f'Вы переместились на {move_distance} м на {convert_direction(direction)}') + game.shown_tips.append(f'Вы переместились на {move_distance}м на {convert_direction(direction)}') show_tips(game, 1) diff --git a/osm/osm.py b/osm/osm.py index 3a7d440..8c29ed9 100644 --- a/osm/osm.py +++ b/osm/osm.py @@ -5,58 +5,66 @@ import re Api = OsmApi() pp = pprint.PrettyPrinter(indent=4) -min_lon, min_lat, max_lon, max_lat = 30.3830262851,59.9141077326,30.4079171848,59.9222065257 +min_lon, min_lat, max_lon, max_lat = 30.3830262851, 59.9141077326, 30.4079171848, 59.9222065257 + def remove_duplicates(l): return [dict(t) for t in {tuple(d.items()) for d in l}] + def get_objects_from_square(min_lon, min_lat, max_lon, max_lat): return Api.Map(min_lon, min_lat, max_lon, max_lat) - + + def filter_streets(objects): - return list(filter(lambda o: - o['data']['tag'] != {} - and 'highway' in o['data']['tag'] - and 'name' in o['data']['tag'], objects)) + return list(filter(lambda o: + o['data']['tag'] != {} + and 'highway' in o['data']['tag'] + and 'name' in o['data']['tag'], objects)) + def filter_waterways(objects): - return list(filter(lambda o: - o['data']['tag'] != {} - and 'waterway' in o['data']['tag'] - and 'river' in o['data']['tag']['waterway'] - and 'name' in o['data']['tag'], objects)) + return list(filter(lambda o: + o['data']['tag'] != {} + and 'waterway' in o['data']['tag'] + and 'river' in o['data']['tag']['waterway'] + and 'name' in o['data']['tag'], objects)) + def filter_sightseeings(objects): - return list(filter(lambda o: - o['data']['tag'] != {} - and - (('tourism' in o['data']['tag'] - and ('attraction' in o['data']['tag']['tourism'] - or 'artwork' in o['data']['tag']['tourism'] - or 'resort' in o['data']['tag']['tourism'] - or 'viewpoint' in o['data']['tag']['tourism'] - or 'museum' in o['data']['tag']['tourism'])) - or - (('historic' in o['data']['tag'] - and ('memorial' in o['data']['tag']['historic'] - or 'monument' in o['data']['tag']['historic'] - or 'yes' in o['data']['tag']['historic'] - or 'building' in o['data']['tag']['historic'])))) - and 'name' in o['data']['tag'], objects)) + return list(filter(lambda o: + o['data']['tag'] != {} + and + (('tourism' in o['data']['tag'] + and ('attraction' in o['data']['tag']['tourism'] + or 'artwork' in o['data']['tag']['tourism'] + or 'resort' in o['data']['tag']['tourism'] + or 'viewpoint' in o['data']['tag']['tourism'] + or 'museum' in o['data']['tag']['tourism'])) + or + (('historic' in o['data']['tag'] + and ('memorial' in o['data']['tag']['historic'] + or 'monument' in o['data']['tag']['historic'] + or 'yes' in o['data']['tag']['historic'] + or 'building' in o['data']['tag']['historic'])))) + and 'name' in o['data']['tag'], objects)) + def filter_buildings(objects): - return list(filter(lambda o: - o['data']['tag'] != {} - and 'building' in o['data']['tag'] - and 'building:levels' in o['data']['tag'], objects)) + return list(filter(lambda o: + o['data']['tag'] != {} + and 'building' in o['data']['tag'] + and 'building:levels' in o['data']['tag'], objects)) + def filter_amenities(objects): - return list(filter(lambda o: - o['data']['tag'] != {} - and 'lat' in o['data'] - and 'lon' in o['data'] - and 'amenity' in o['data']['tag'] - and 'name' in o['data']['tag'], objects)) + return list(filter(lambda o: + o['data']['tag'] != {} + and 'lat' in o['data'] + and 'lon' in o['data'] + and 'amenity' in o['data']['tag'] + and 'name' in o['data']['tag'], objects)) + def convert_building_type(building_type): if building_type == 'dormitory': @@ -68,12 +76,14 @@ def convert_building_type(building_type): else: return 'building' + def filter_vehicles(objects): - return list(filter(lambda o: - o['data']['tag'] != {} - and 'vehicle' in o['data']['tag'] - and 'route' in o['data']['tag'] - and 'name' in o['data']['tag'], objects)) + return list(filter(lambda o: + o['data']['tag'] != {} + and 'vehicle' in o['data']['tag'] + and 'route' in o['data']['tag'] + and 'name' in o['data']['tag'], objects)) + def convert_vehicle_type(vehicle_type): if vehicle_type == 'bus': @@ -85,43 +95,52 @@ def convert_vehicle_type(vehicle_type): if vehicle_type == 'train': return 'train' else: - return 'unknown' + return 'unknown' + def filter_districts(objects): - return list(filter(lambda o: - o['data']['tag'] != {} - and 'type' in o['data']['tag'] - and (o['data']['tag']['type'] == 'boundary' or 'boundary' in o['data']['tag']) - and 'name' in o['data']['tag'] - and 'район' in o['data']['tag']['name'], objects)) + return list(filter(lambda o: + o['data']['tag'] != {} + and 'type' in o['data']['tag'] + and (o['data']['tag']['type'] == 'boundary' or 'boundary' in o['data']['tag']) + and 'name' in o['data']['tag'] + and 'район' in o['data']['tag']['name'], objects)) + def describe_objects(min_lat, min_lon, max_lat, max_lon): objects = get_objects_from_square(min_lon, min_lat, max_lon, max_lat) + streets = [ - {'name': x['data']['tag']['name']} for x in filter_streets(objects) + {'name': x['data']['tag']['name']} for x in filter_streets(objects) ] + buildings = [ - {'building_type': convert_building_type(x['data']['tag']['building']), - 'levels': x['data']['tag']['building:levels']} for x in filter_buildings(objects) + {'building_type': convert_building_type(x['data']['tag']['building']), + 'levels': x['data']['tag']['building:levels']} for x in filter_buildings(objects) ] vehicles = [ - {'vehicle_type': convert_vehicle_type(x['data']['tag']['route']), - 'name': re.split(r'\.', x['data']['tag']['name'].replace(':', '.'))[0]} for x in filter_vehicles(objects) + {'vehicle_type': convert_vehicle_type(x['data']['tag']['route']), + 'name': re.split(r'\.', x['data']['tag']['name'].replace(':', '.'))[0]} for x in filter_vehicles(objects) ] sightseeings = [ - {'name': x['data']['tag']['name'], 'type': x['data']['tag']['tourism'] if 'tourism' in x['data']['tag'] else x['data']['tag']['historic']} for x in filter_sightseeings(objects) + {'name': x['data']['tag']['name'], + 'type': x['data']['tag']['tourism'] if 'tourism' in x['data']['tag'] else x['data']['tag']['historic'], + 'lat': x['data']['lat'], + 'lon': x['data']['lon'] + } for x + in filter_sightseeings(objects) ] districts = [ - {'name': x['data']['tag']['name']} for x in filter_districts(objects) + {'name': x['data']['tag']['name']} for x in filter_districts(objects) ] rivers = [ - {'name': x['data']['tag']['name']} for x in filter_waterways(objects) + {'name': x['data']['tag']['name']} for x in filter_waterways(objects) ] amenities = [ - {'name': x['data']['tag']['name'], - 'type': x['data']['tag']['amenity'], - 'lat': x['data']['lat'], - 'lon': x['data']['lon']} for x in filter_amenities(objects)] + {'name': x['data']['tag']['name'], + 'type': x['data']['tag']['amenity'], + 'lat': x['data']['lat'], + 'lon': x['data']['lon']} for x in filter_amenities(objects)] return {'center': {'lat': (min_lat + max_lat) / 2, 'lon': (min_lon + max_lon) / 2}, 'streets': remove_duplicates(streets), 'vehicles': remove_duplicates(vehicles), @@ -134,4 +153,4 @@ def describe_objects(min_lat, min_lon, max_lat, max_lon): if __name__ == '__main__': objects = describe_objects(min_lat, min_lon, max_lat, max_lon) - pp.pprint(objects) \ No newline at end of file + pp.pprint(objects) diff --git a/src/createMap.js b/src/createMap.js index 2969268..068a557 100644 --- a/src/createMap.js +++ b/src/createMap.js @@ -19,7 +19,7 @@ export function createMap(elementId, onMapClick) { 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: 'Map data © OpenStreetMap contributors, CC-BY-SA', - maxZoom: 13, + maxZoom: 17, id: 'mapbox.streets' } );