def fetch_osm_data(osm_id, osm_type): """ Fetch ways/nodes from the OSM API. """ osm_id = int(osm_id) osm = Api() if osm_type == "way": way = osm.query(f"way/{osm_id}") latlons = [(n.lat(), n.lon()) for n in way.nodes()] elif osm_type == "node": node = osm.query(f"node/{osm_id}") latlons = [(node.lat(), node.lon())] return latlons
def print_relation(relation_id, ref, route_start_point, operator): """Print relation on map using osm api""" api = Api() relation = api.query('relation/{}'.format(relation_id)) node_list_to_draw = [] map_setted = False folium_map = None print(relation.members()) for member in relation.members(): if len(member.nodes()) > 0: for node in member.nodes(): node_list_to_draw.append([node.lat(), node.lon()]) if not map_setted: map_setted = True folium_map = folium.Map(location=node_list_to_draw[0], zoom_start=14, tiles='openstreetmap') folium.PolyLine(node_list_to_draw, color='green', weight=2.5).add_to(folium_map) node_list_to_draw = [] folium.Marker(location=route_start_point, icon=folium.Icon(color='red')).add_to(folium_map) folium_map.save('./routes/{}/{}.html'.format(operator, ref)) return '<iframe src="./routes/{}/{}.html"></iframe>'.format( operator, ref)
def break_relation_into_ways(osm_id): """Takes a relation id and returns the way ids within.""" osm_id = int(osm_id) osm = Api() relation = osm.query(f"relation/{osm_id}") way_ids = [] for way in relation.members(): way_ids.append(way.id()) return way_ids
def fetch_osm_data(groups): """ Fetch ways/nodes from the OSM API. """ osm = Api() for i in groups.index: way_id = groups.loc[i, "objects"].split("/")[-1] way = osm.query(f"way/{way_id}") latlons = [(n.lat(), n.lon()) for n in way.nodes()] groups.loc[i, "lats"] = "|".join(map(str, [l[0] for l in latlons])) groups.loc[i, "lons"] = "|".join(map(str, [l[1] for l in latlons])) return groups
def test_relation(): api = Api() x = api.query('relation/1539714') assert x.isValid() assert x.id() == 1539714 assert x.type() == 'relation' assert len(x.members()) > 0 assert 108402486 in [n.id() for n in x.members()] assert abs(x.members()[0].nodes()[0].lat() - 40.866) < .01 assert abs(x.members()[0].nodes()[0].lon() - (-73.795)) < .01 assert abs(x.members(shallow=False)[1].nodes()[0].lat() - 40.866) < .01 assert abs(x.members(shallow=False)[1].nodes()[0].lon() - (-73.795)) < .01 assert int(x.version()) > 0
def test_node(): api = Api() x = api.query('node/42467507') assert x.isValid() assert x.id() == 42467507 assert x.type() == 'node' assert len(x.tags()) > 0 assert 'highway' in x.tags() assert x.tag('highway') is not None assert x.tag('abcde') is None assert abs(x.lat() - 40.7014417) < .00001 assert abs(x.lon() - (-73.9430797)) < .00001 assert int(x.version()) > 0
def test_way(): api = Api() x = api.query('way/108402486') assert x.isValid() assert x.id() == 108402486 assert x.type() == 'way' assert len(x.nodes()) > 0 assert 1243967857 in [n.id() for n in x.nodes()] assert abs(x.nodes()[0].lat() - 40.866) < .01 assert abs(x.nodes()[0].lon() - (-73.795)) < .01 assert abs(x.nodes(shallow=False)[1].lat() - 40.866) < .01 assert abs(x.nodes(shallow=False)[1].lon() - (-73.795)) < .01 assert int(x.version()) > 0
def get_coord(way_id): '''Given the id of one identified structure (*way*), from OCM, this function returns a list with its coordinates''' #Define empty list to store coordinates coord = [] #Execute query using OCM API #Requires command #from OSMPythonTools.api import Api api = Api() query_results = api.query("way/617488238") nodes = query_results.nodes() #Get the coordinates for each node and store in list for node in nodes: coord.append(node.geometry()['coordinates']) return(coord)
def get_coord(uniq_id): '''Given the id of one identified structure (*way*), from OCM, this function returns a list with its coordinates''' #Define empty list to store coordinates coord = [] #Execute query using OCM API #Requires command #from OSMPythonTools.api import Api api = Api() query_results = api.query(uniq_id) idType = uniq_id.split('/')[0] nodes = [] if (idType == 'way'): nodes = query_results.nodes() elif (idType == 'relation'): nodes = query_results.members() #Get the coordinates for each node and store in list for node in nodes: coord.append(node.geometry()['coordinates']) return (coord)
from matplotlib import image as mpimg from matplotlib import pyplot as plt from OSMPythonTools.api import Api api = Api() # load transform matrix geo2ind = np.load("data/geo2ind.npy") # load piste d'Argenton way_ids = [111415872, 235094917, 526991326, 235094922, 235094924, 235094919] track_lat = [] track_lon = [] for way_id in way_ids: way = api.query("way/" + str(way_id)) for node in way.nodes(): track_lat.append(node.lat()) track_lon.append(node.lon()) # get track as image indices track_col, track_row = geo2ind @ np.stack( (track_lat, track_lon, np.ones_like(track_lat))) # load image general_map = mpimg.imread("img/plan_general.jpg") # overlay OSM track over map image plt.imshow(np.mean(general_map, axis=2), cmap="gray") plt.plot(track_col, track_row, c="#ff7900cd") plt.axis("off")
osm_stops_list, columns=('osm_name', 'osm_merged_refs', 'osm_ref_bkv', 'osm_ref_bkk', 'osm_ref_bkktelebusz', 'osm_ref', 'osm_lat', 'osm_lon', 'osm_id', 'osm_tags', 'osm_version', 'osm_timestamp', 'osm_user', 'osm_uid', 'osm_changeset')) logging.info('Number of elements after all OSM queries: {0}'.format( len(df_osm_stops))) df_osm_stops.drop_duplicates(subset='osm_id', keep='first', inplace=True) logging.info( 'Number of elements after removing duplicates based on OSMID: {0}'. format(len(df_osm_stops))) api = Api() for index, osm_data in df_osm_stops.iterrows(): node = api.query('node/{}'.format(osm_data['osm_id'])) try: df_osm_stops.loc[[index], 'osm_timestamp'] = node.timestamp() if node.uid() != None and node.user != None: df_osm_stops.loc[[index], 'osm_uid'] = node.uid() df_osm_stops.loc[[index], 'osm_user'] = node.user() else: df_osm_stops.loc[[index], 'osm_uid'] = '4579407' df_osm_stops.loc[[index], 'osm_user'] = '******' df_osm_stops.loc[[index], 'osm_changeset'] = node.changeset() if node.version() != None: df_osm_stops.loc[[index], 'osm_version'] = node.version() else: df_osm_stops.loc[[index], 'osm_version'] = '55' except IOError as e: logging.error('File error: {0}'.format(e), exc_info=True)
def user_profile(request): if request.method == 'POST': review = request.POST.get('labelowski') loc_id = request.POST.get('loc_id') delete_but = request.POST.get('delete_but') if delete_but and 'deleting' in delete_but: loc_id = re.sub(r'(deleting)(\d+)', r'\2', delete_but) locations = Locations.objects.get(id=int(loc_id)) locations.review = '' locations.save(update_fields=['review']) if review and loc_id: locations = Locations.objects.get(id=int(loc_id)) locations.review = review locations.save(update_fields=['review']) api = Api() m = folium.Map() user = User.objects.get(id=request.user.id) locations = Locations.objects.filter(user=user) # locations_dict = [] # for location in locations: # locations_dict.append((location.name, location.review)) follow = Profile.followers.through.objects.filter( profile_id=user.id).count() follows = Profile.following.through.objects.filter( profile_id=user.id).count() review_count = 0 locations_dict = [] for location in locations: if location.review: review_count += 1 locations_dict.append((location.name, location.review)) castle_data = api.query(f'node/{location.ide}') print(location.state) if location.state == 'Wishlist': folium.Marker( [castle_data.lat(), castle_data.lon()], tooltip=location.name, icon=folium.Icon(color='lightblue', icon='fort-awesome', prefix='fa')).add_to(m) else: folium.Marker( [castle_data.lat(), castle_data.lon()], tooltip=location.name, icon=folium.Icon(color='orange', icon='fort-awesome', prefix='fa')).add_to(m) m = m._repr_html_() review_form = { 'map': m, 'review_count': review_count, 'location_count': locations.count(), 'follow': follow, 'follows': follows, 'review': locations, } return render(request, 'profiles/user_profile.html', review_form)
import overpy from OSMPythonTools.api import Api from OSMPythonTools.overpass import Overpass api = Api() op = Overpass() way = api.query('way/290083775') res = op.query('way(id:29008377);')
def calculate_distance_view(request): # initial values nominatim = Nominatim() global castle_ide #areaId = nominatim.query('poland').areaId() overpass = Overpass() api = Api() name = None review = None state = None country = nominatim.query('germany') locator = Photon() # (user_agent='martin_jr8') areaId = country.areaId() names_query = country.toJSON() country_details = names_query[0] country_coor_lat = country_details["lat"] country_coor_lon = country_details["lon"] # castles location query castle_query = overpassQueryBuilder(area=areaId, elementType='node', selector='castle_type', out='body') castle_objects = overpass.query(castle_query) castle_list = castle_objects.elements() # castle_infos = [] # for castle in castle_list: # castle_infos.append(castle.tags()) # initail folium map m = folium.Map(location=[country_coor_lat, country_coor_lon], zoom_start=6) for castle_loc in castle_list: info_dict = castle_loc.tags() castle_id = castle_loc.id() castle_name = info_dict.get('name') if castle_name == None: continue else: folium.Marker( [castle_loc.lat(), castle_loc.lon()], tooltip=castle_name, popup=folium.Popup(popup_box(info_dict.get('name'), castle_id), max_width=500), icon=folium.Icon(color='purple', icon='fort-awesome', prefix='fa')).add_to(m) if request.user.is_authenticated: user = Profile.objects.get(user=request.user) loc = user.location location = locator.geocode(loc) l_lat = location.latitude l_lon = location.longitude loc_point = (l_lat, l_lon) folium.Marker([l_lat, l_lon], tooltip='Your Location', popup=location, icon=folium.Icon(color='blue', icon='home', prefix='fa')).add_to(m) if request.method == 'GET' and request.is_ajax(): castle_ide = request.GET.get('castle_id') castle_data = api.query(f'node/{castle_ide}') castle_details = castle_data.tags() # if request.user.is_authenticated and user.location != None: # castle_lat = castle_data.lat() # castle_lon = castle_data.lon() # castle_point = (castle_lat, castle_lon) # distance = round(geodesic(loc_point, castle_point).km, 2) # castle_details["distance"] = distance # line = folium.PolyLine(locations=[loc_point, castle_point], weight=2, color='blue') # m.add_child(line) # m = m._repr_html_() return HttpResponse(json.dumps(castle_details), content_type="application/json") if request.user.is_authenticated and request.method == 'POST' and request.is_ajax( ): castleName = request.POST.get('castleName') review = request.POST.get('review') state = request.POST.get('state') user = request.user ide = castle_ide form = LocationsModelForm() form.save(user, castleName, review, state, ide) folium.Marker([52.352, 6.22], tooltip='Your Location', popup=location, icon=folium.Icon(color='green', icon='home', prefix='fa')).add_to(m) messages.info(request, 'success. Review saved') # m = m._repr_html_() # return redirect('/') # return HttpResponseRedirect('measurements/main.html') # return render(request, 'measurements/main.html', {'map': m}) # return render(request, 'measurements/main.html') # return HttpResponse(request) m = m._repr_html_() context = { # 'name': name, # 'review': review, # 'state': state, # 'map': m, # 'castles': country_coor_lat, # 'ops' : border_objects # 'name': requested_id } return render(request, 'measurements/main.html', {'map': m})
#!/usr/bin/env python3 print('=' * 6, 'Example 1', '=' * 6) from OSMPythonTools.api import Api api = Api() way = api.query('way/5887599') print('building: %s' % way.tag('building')) print('historic %s' % way.tag('historic')) print('architect: %s' % way.tag('architect')) print('website: %s' % way.tag('website')) print('=' * 6, 'Example 2', '=' * 6) from OSMPythonTools.overpass import Overpass overpass = Overpass() result = overpass.query('way["name"="Stephansdom"]; out body;') stephansdom = result.elements()[0] print('name:en: %s' % stephansdom.tag('name:en')) print('address: %s %s, %s %s' % (stephansdom.tag('addr:street'), stephansdom.tag('addr:housenumber'), stephansdom.tag('addr:postcode'), stephansdom.tag('addr:city'))) print('building: %s' % stephansdom.tag('building')) print('denomination: %s' % stephansdom.tag('denomination')) print('=' * 6, 'Example 3', '=' * 6) from OSMPythonTools.nominatim import Nominatim nominatim = Nominatim()
def fetchFeatureVersion(id, version): api = Api() return (api.query('node/' + str(id) + '/1'))