コード例 #1
0
    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)
コード例 #2
0
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
コード例 #3
0
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
コード例 #4
0
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
コード例 #5
0
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
コード例 #6
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
コード例 #7
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
コード例 #8
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)
コード例 #9
0
ファイル: pipeline.py プロジェクト: jzisheng/mids_ei
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)
コード例 #10
0
import numpy as np
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")
コード例 #11
0
 def __init__(self, *args, **kwargs):
     if 'OSMPythonTools.api' not in sys.modules:
         from OSMPythonTools.api import Api
     if not SingletonApi.__instance:
         SingletonApi.__instance = Api(*args, **kwargs)
コード例 #12
0
ファイル: myoverpy.py プロジェクト: zyrgit/GreenRouteCode
import pprint
import requests
from xml.etree import ElementTree
try:
    import overpy
    overpy_api = overpy.Overpass()
except:
    print("\nRUN $HOME/anaconda2/bin/pip install overpy\n")
try:
    import overpass
    overpass_api = overpass.API()
except:
    print("\nRUN $HOME/anaconda2/bin/pip install overpass\n")
try:
    from OSMPythonTools.api import Api
    OSMPythonTools_api = Api()
    from OSMPythonTools.overpass import Overpass
    OSMPythonTools_overpass = Overpass()
    from OSMPythonTools.nominatim import Nominatim
    OSMPythonTools_nominatim = Nominatim()
except:
    print("\nCOPY ./OSMPythonTools\n")

mypydir = os.path.abspath(
    os.path.dirname(inspect.getfile(inspect.currentframe())))
addpath = mypydir + "/mytools"
if addpath not in sys.path: sys.path.append(addpath)
if mypydir not in sys.path: sys.path.append(mypydir)
from readconf import get_conf, get_conf_int, get_conf_float
iprint = 2
configfile = "conf.txt"
コード例 #13
0
ファイル: views.py プロジェクト: LukaszRemkowicz/GeoCastle
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)
コード例 #14
0
ファイル: views.py プロジェクト: LukaszRemkowicz/GeoCastle
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})
コード例 #15
0
class OSMQueryHelper:

    api = Api()
    nominatim = Nominatim()
    overpass = Overpass()

    # AREA ID
    BENIN_ID = 3600192784
    BORGOU_ID = 3602803880
    NIKKI_COM_ID = 3602859963
    NIKKI_ARR_ID = 3611176398

    # RELATION ID
    BENIN_ID_REL = 192784
    BORGOU_ID_REL = 2803880
    NIKKI_ID_REL = 2859963

    # CONSTANTS
    DEG_to_KM2 = 13000
    DEG_to_KM = 130

    def __init__(self):
        None

    def getAreaInOSM(self, osm_id, type: str) -> int:
        if type == 'way':
            return int(osm_id) + 2400000000
        if type == 'relation':
            return int(osm_id) + 3600000000
        return None

    def updateAreaIdOfDataframe(self, df: pd.DataFrame) -> None:
        for index, row in df.iterrows():
            if row['area_id'] == '' or int(row['area_id']) == 0:
                df.at[index,
                      'area_id'] = self.getAreaInOSM(row['osm_id'],
                                                     row['type'])

    def countBuildingsInAreaID(self, area_id: int) -> int:
        buildings_query = overpassQueryBuilder(area=area_id,
                                               elementType=['way'],
                                               selector="building",
                                               out='count')
        buildings_res = self.overpass.query(buildings_query)
        return buildings_res.countWays()

    def updateBuildingsInVillage(self, village_df: pd.DataFrame) -> None:
        for index, village_row in village_df.iterrows():
            nb_buildings = 0
            density = 0

            # Check if there have already been registered
            if int(village_row['nb_buildings']) < 0:
                nb_buildings = self.countBuildingsInAreaID(
                    village_row['area_id'])

            # Update the density
            if nb_buildings > 0:
                density = nb_buildings / \
                    (float(str(village_row['area']).replace(
                        ',', '.')) * 1000000.0)

                village_df.loc[index, 'nb_buildings'] = nb_buildings
                village_df.loc[index, 'build_dens'] = density
                village_df.loc[index, 'pop_est'] = nb_buildings * 5

    def isIdalready(self, df: pd.DataFrame, _id: int) -> bool:
        for row in df.iterrows():
            if int(row['osm_id']) == int(_id):
                return True
        return False

    def updateDistrictOfVillage(self, district_df: pd.DataFrame,
                                village_df: pd.DataFrame) -> None:
        # Loop throught all villages
        for i, village_row in village_df.iterrows():

            # Check if there is not already a district
            if village_row['district'] == '':

                # Loop throught all districts to see if the village is inside the polygon
                for district_row in district_df.iterrows():
                    point = village_row['point']
                    isin = district_row['polygon'].contains(point)
                    if isin == True:
                        village_df.at[i, 'district'] = district_row['name']
                        break

    def getVillagesFromOSM(self) -> pd.DataFrame:
        # Build the query
        village_query = overpassQueryBuilder(
            area=self.NIKKI_COM_ID,
            elementType='way',
            selector="'place'~'village|locality|town|city|hamlet'",
            out="body geom")
        residential_query = overpassQueryBuilder(
            area=self.NIKKI_COM_ID,
            elementType='way',
            selector="'landuse'~'residential'",
            out="body geom")

        # Make the query
        village_res = self.overpass.query(village_query)
        residential_res = self.overpass.query(residential_query)

        # Transform response into json
        village_json = village_res.toJSON()
        residential_res = residential_res.toJSON()

        village_headers = [
            'osm_id', 'area_id', 'type', 'place', 'district', 'name', 'link',
            'lat', 'lon', 'perim', 'area', 'boundary_lat', 'boundary_lon',
            'polygon', 'point', 'nb_buildings', 'building_density', 'pop_est'
        ]
        new_village_data = []

        # Loop throught the elements in the response
        for ele in village_json['elements']:
            geo_df = pd.DataFrame.from_records(ele['geometry'])
            poly = Polygon(zip(geo_df['lon'], geo_df['lat']))
            point = Point(poly.centroid.x, poly.centroid.y)
            boudary_lat = str(geo_df['lat'].tolist())
            boudary_lon = str(geo_df['lon'].tolist())
            ele_row = [
                ele.get('id', 0),                    # osm_id
                0,                                  # area_id
                ele.get('type', ''),                 # type
                ele['tags'].get('place', ''),        # place
                '',                                 # district
                ele['tags'].get('name', ''),         # name
                'https://www.openstreetmap.org/edit?' + \
                str(ele.get('type', '')) + '=' + \
                str(ele.get('id', '')),  # link
                str(poly.centroid.y),               # lat
                str(poly.centroid.x),               # lon
                str(poly.length * self.DEG_to_KM),       # perim
                str(poly.area * self.DEG_to_KM2),        # area
                boudary_lat,                        # boudary_lat
                boudary_lon,                        # boudary_lon
                poly,                               # polygon
                point,                              # point
                -1,                                 # nb_buildings
                0.0,                                # building_density
                0.0,                                # pop_est
            ]

            new_village_data.append(ele_row)

        # Loop throught the elements in the response
        for ele in residential_res['elements']:
            geo_df = pd.DataFrame.from_records(ele['geometry'])
            poly = Polygon(zip(geo_df['lon'], geo_df['lat']))
            point = Point(poly.centroid.x, poly.centroid.y)
            boudary_lat = str(geo_df['lat'].tolist())
            boudary_lon = str(geo_df['lon'].tolist())
            ele_row = [
                ele.get('id', 0),                    # osm_id
                0,                                  # area_id
                ele.get('type', ''),                 # type
                ele['tags'].get('landuse', ''),      # landuse
                '',                                 # district
                ele['tags'].get('name', ''),         # name
                'https://www.openstreetmap.org/edit?' + \
                str(ele.get('type', '')) + '=' + \
                str(ele.get('id', '')),  # link
                str(poly.centroid.y),               # lat
                str(poly.centroid.x),               # lon
                str(poly.length * self.DEG_to_KM),       # perim
                str(poly.area * self.DEG_to_KM2),        # area
                boudary_lat,                        # boudary_lat
                boudary_lon,                        # boudary_lon
                poly,                               # polygon
                point,                              # point
                -1,                                 # nb_buildings
                0.0,                                # building_density
                0.0,                                # population_est_building
            ]

            new_village_data.append(ele_row)

        # Create Dataframe from data
        new_village_df = pd.DataFrame(new_village_data,
                                      columns=village_headers)
        return new_village_df

    def getBuildingsInVillage(self, village_row_df: pd.DataFrame) -> any:

        # Build the query
        building_query = overpassQueryBuilder(
            area=village_row_df['area_id'].values[0],
            elementType='way',
            selector="building",
            out="geom")

        # Make the query
        building_res = self.overpass.query(building_query)

        # Transform response into json
        building_json = building_res.toJSON()
        new_building_data = []

        # Loop throught the elements in the response
        for ele in building_json['elements']:
            geo_df = pd.DataFrame.from_records(ele['geometry'])
            poly = Polygon(zip(geo_df['lon'], geo_df['lat']))
            point = Point(poly.centroid.x, poly.centroid.y)
            boudary_lat = str(geo_df['lat'].tolist())
            boudary_lon = str(geo_df['lon'].tolist())
            ele_row = [
                ele.get('id', 0),  # osm_id
                ele.get('type', ''),  # type
                village_row_df['district'].values[0],  # district
                village_row_df['name'].values[0],  # village
                str(poly.centroid.y),  # lat
                str(poly.centroid.x),  # lon
                str(poly.length * self.DEG_to_KM),  # perim
                str(poly.area * self.DEG_to_KM2),  # area
                boudary_lat,  # boudary_lat
                boudary_lon,  # boudary_lon
                poly,  # polygon
                point,  # point
            ]

            new_building_data.append(ele_row)
        return new_building_data
コード例 #16
0
def fetchFeatureVersion(id, version):
    api = Api()
    return (api.query('node/' + str(id) + '/1'))