示例#1
0
def test_queryAreaIDFormatRelation():
    q = overpassQueryBuilder(area=3600415473, elementType='node')
    assert q == overpassQueryBuilder(area='relation/415473',
                                     elementType='node')
    assert q == overpassQueryBuilder(area='relation 415473',
                                     elementType='node')
    assert q == overpassQueryBuilder(area='r415473', elementType='node')
def query_construction(search_area):
        nominatim_query=Nominatim().query(search_area)
        query_01=overpassQueryBuilder(area=nominatim_query.areaId(),elementType=['node','way','relation'], selector=["military"],includeGeometry=True)
        query_02=overpassQueryBuilder(area=nominatim_query.areaId(),elementType=['node','way','relation'], selector='"landuse"="military"',includeGeometry=True)
        result_01=Overpass().query(query_01)
        result_02=Overpass().query(query_02)
        print("Pronađeno ukupno "+str(result_01.countElements())+" 'military' objekta")
        print("Pronađeno ukupno "+str(result_02.countElements())+" 'landuse=military' objekta")
        results=[result_01,result_02]
        return results
示例#3
0
def get_OSM():
    nominatim = Nominatim()
    overpass = Overpass()
    areaId = nominatim.query('Montreal, Canada').areaId()
    query = overpassQueryBuilder(
        area=areaId, elementType='node',
        selector=['shop']) + overpassQueryBuilder(
            area=areaId, elementType='node', selector='amenity')
    result = overpass.query(query)
    db = db_from_OSM(result)
    db_tags = list_tags(db)
    return db, db_tags
def fetch(year, city, typeOfRoad):
    areaId = nominatim.query(city).areaId()
    query = overpassQueryBuilder(area=areaId,
                                 elementType='way',
                                 selector='"highway"="' + typeOfRoad + '"',
                                 out='count')
    return overpass.query(query, date=year, timeout=60).countElements()
示例#5
0
def query_overpass(area_id, query_statement, element_type='node'):
    # Query Overpass based on area
    overpass = Overpass()
    query = overpassQueryBuilder(area=area_id,
                                 elementType=element_type,
                                 selector=query_statement)
    return overpass.query(query)
示例#6
0
def main():
    """Main function to create the output file"""
    nominatim = Nominatim()
    overpass = Overpass()
    config = configparser.ConfigParser()
    config.read('configuration')
    area_id = nominatim.query('Italia').areaId()

    html_string = ""

    for operator in ast.literal_eval(config.get("MAPPING", "operators")):
        #create folders for needed saving operations later
        if not os.path.exists("./home_to_destination/{}".format(operator)):
            os.mkdir("./home_to_destination/{}".format(operator))
        if not os.path.exists("./routes/{}".format(operator)):
            os.mkdir("./routes/{}".format(operator))
        operator_parameter = '"operator" = "{}"'.format(operator)
        query = overpassQueryBuilder(area=area_id,
                                     elementType='rel',
                                     selector=operator_parameter,
                                     out='body')
        result = overpass.query(query, timeout=600)
        html_string += HTML.main_output_generation(
            config['MAPPING']['address'], result, operator)

    html_file = open("output.html", "w")
    html_file.write(html_string)
    html_file.close()
示例#7
0
 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()
示例#8
0
def fetch(year, city):
    areaId = nominatim.query(city).areaId()
    query = overpassQueryBuilder(area=areaId,
                                 elementType='node',
                                 selector='"natural"="tree"',
                                 out='count')
    return overpass.query(query, date=year, timeout=60).countElements()
示例#9
0
def fetch(typeOfRoad):
    areaId = nominatim.query("Lyon").areaId()
    query = overpassQueryBuilder(area=areaId,
                                 elementType='way',
                                 selector=typeOfRoad,
                                 out='body',
                                 includeGeometry=True)
    return overpass.query(query)
示例#10
0
def get_OSM():
    nominatim = Nominatim()
    overpass = Overpass()
    areaId = nominatim.query('Le Plateau Mont Royal').areaId()
    query = overpassQueryBuilder(area=areaId, elementType=['way'])
    result = overpass.query(query)
    db = db_from_OSM(result)
    return db
示例#11
0
 def fetch(year, city, typeOfShop):
     areaId = nominatim.query(city).areaId()
     query = overpassQueryBuilder(area=areaId,
                                  elementType='way',
                                  selector='"shop"="' + typeOfShop + '"',
                                  out='count')
     x = overpass.query(query, date=year, timeout=30).countElements()
     assert x >= 0
     return x
示例#12
0
def segment(lat, long):
    """
    Main function defining the segment in wich the point is
    :param long: coords
    :param lat: coords
    :return: two way-objects such as our point is in between these two ways, and the road between them, and the main road
    (for later uses)
    """
    long, lat = lat, long
    point = np.array([long, lat])
    bbox, id, name = geo_to_way_info(long, lat)
    query = overpassQueryBuilder(bbox=bbox,
                                 elementType=['way'],
                                 selector=['highway', 'name'],
                                 includeGeometry=True)
    result = Overpass().query(query)

    for x in result.elements():
        if x.id() == id:
            closet_road = x

    IWW_list = []
    for x in result.elements():
        if x.id() != id and x.tags()['name'] != name:
            cc = closest_point_on_way(closet_road.geometry()["coordinates"],
                                      x.geometry()["coordinates"])
            dtm = np.linalg.norm(point - cc)
            IWW_list.append(IWW(x, dtm, cc))

    IWW_list.sort(
        key=lambda x: x.dtm
    )  #sorting the crossing road by distance crossing-point to center of interest
    result1 = IWW_list[
        0]  #first segmentation is done by the closest crossing road

    for iww in IWW_list:  #second one is done by the closest crossing road having the dot product negative (please refer to the presentation)
        if iww.obj != result1.obj:
            if np.dot(result1.cc - point, iww.cc - point) < 0:
                result2 = iww
                break

    new_geometry = []
    bool_add = False
    for point in closet_road.geometry(
    )["coordinates"]:  #we compute the geometry of the segmented road, usefull for next objectives
        point = np.array([point[1], point[0]])
        if np.array_equal(point, result1.cc) or np.array_equal(
                point, result2.cc):
            bool_add = not (bool_add)
        if bool_add:
            new_geometry.append(point)
    closet_road_geom = []
    for x in closet_road.geometry()["coordinates"]:
        closet_road_geom.append(np.array([x[1], x[0]]))

    return result1, result2, new_geometry, closet_road_geom
示例#13
0
    def getOsmGeoObjects(self, areaId, selector, elementType:OsmType):
        """
        sends overpass-query and return the elements from the json response
        """
        overpass = Overpass()
        # out='geom' also leads to geometry key (list of coordinates for each object)

        query = overpassQueryBuilder(
            area=areaId, elementType=elementType.value, selector=selector, out='geom')
        return overpass.query(query).toJSON()["elements"]
示例#14
0
def fetchFeatures(areaId, osmkey, osmtype):
    overpass = Overpass()

    query = overpassQueryBuilder(area=areaId,
                                 elementType=['node', 'way', 'relation'],
                                 selector='"' + osmkey + '"="' + osmtype + '"',
                                 includeGeometry=True,
                                 out='center meta')
    logging.debug("OSM query: %s", query)
    return overpass.query(query, timeout=60)
示例#15
0
def obtain_square_portion(corner1, corner3, corner2, corner4):
    overpass = Overpass()
    query = overpassQueryBuilder(bbox=[corner1, corner2, corner3, corner4],
                                 elementType='way',
                                 selector='"junction"',
                                 out="body geom")
    result = overpass.query(query)
    road_coords = []
    for element in result.elements():
        road_coords.append(element.geometry()['coordinates'])
    return road_coords
示例#16
0
def create_query(line_number, city_name):
    line_num = ('"ref" = "' + line_number + '"')
    query = overpassQueryBuilder(area=nominatim.query(city_name).areaId(),
                                 elementType=['node', 'way', 'relation'],
                                 selector=['"route"="subway"', line_num],
                                 out='body;>;out skel qt')

    print("        +++query for line ", line_number, " in ", city_name)
    print(query)
    print("        +++\n")
    return query
示例#17
0
def boundbox(Key, Value, box):
    #nominatim = Nominatim()
    print(box)
    overpass = Overpass()
    query = overpassQueryBuilder(bbox=box,
                                 elementType=['node', 'way', 'relation'],
                                 selector=f'"{Key}"="{Value}"',
                                 includeGeometry=True)
    print(query)
    result = overpass.query(query, timeout=250)
    result = result.toJSON()
    return result
示例#18
0
def query(Key, Value, Location):
    nominatim = Nominatim()
    areaId = nominatim.query(f'{Location}').areaId()
    overpass = Overpass()
    query = overpassQueryBuilder(area=areaId,
                                 elementType=['node', 'way', 'relation'],
                                 selector=f'"{Key}"="{Value}"',
                                 includeGeometry=True)
    print("querying OSM")
    result = overpass.query(query, timeout=250)
    result = result.toJSON()
    return result
def segment(long, lat):
    """
    Main function defining the segment in wich the point is
    :param long: coords
    :param lat: coords
    :return: two way-objects such as our point is in between these two ways
    """
    point = np.array([long, lat])
    bbox, id, name = geo_to_way_info(long, lat)
    query = overpassQueryBuilder(bbox=bbox,
                                 elementType=['way'],
                                 selector=['highway', 'name'],
                                 includeGeometry=True)
    result = Overpass().query(query)

    for x in result.elements():
        if x.id() == id:
            closet_road = x

    IWW_list = []
    for x in result.elements():
        if x.id() != id and x.tags()['name'] != name:
            cc = closest_point_on_way(closet_road.geometry()["coordinates"],
                                      x.geometry()["coordinates"])
            dtm = np.linalg.norm(point - cc)
            IWW_list.append(IWW(x, dtm, cc))

    IWW_list.sort(key=lambda x: x.dtm)
    result1 = IWW_list[0]

    for iww in IWW_list:
        if iww.obj != result1.obj:
            if np.dot(result1.cc - point, iww.cc - point) < 0:
                result2 = iww
                break

    new_geometry = []
    bool_add = False
    for point in closet_road.geometry()["coordinates"]:
        point = np.array([point[1], point[0]])
        if np.array_equal(point, result1.cc) or np.array_equal(
                point, result2.cc):
            bool_add = not (bool_add)
        if bool_add:
            new_geometry.append(point)
    closet_road_geom = []
    for x in closet_road.geometry()["coordinates"]:
        closet_road_geom.append(np.array([x[1], x[0]]))

    return result1, result2, new_geometry, closet_road_geom
示例#20
0
def get_pharmacy(overpass, bbox):
    query = overpassQueryBuilder(bbox=bbox,
                                 elementType='node',
                                 selector='"amenity"="pharmacy"',
                                 includeGeometry=True,
                                 out='body')
    result = overpass.query(query)
    output = {}
    for x in result.elements():
        nodeID = x.id()
        shopName = x.tag('name')
        coord = [x.lat(), x.lon()]
        output[nodeID] = [shopName, coord]

    return output
示例#21
0
def get_BART(overpass, bbox):
    query = overpassQueryBuilder(
        bbox=bbox,
        elementType='node',
        selector=['"network" = "BART"', '"railway"="station"'],
        includeGeometry=True,
        out='body')
    result = overpass.query(query)
    output = {}
    for x in result.elements():
        nodeID = x.id()
        stopName = x.tag('name')
        coord = [x.lat(), x.lon()]
        output[nodeID] = [stopName, coord]

    return output
示例#22
0
def get_bus_stops(overpass, bbox):
    query = overpassQueryBuilder(bbox=bbox,
                                 elementType='node',
                                 selector='"highway"="bus_stop"',
                                 includeGeometry=True,
                                 out='body')
    result = overpass.query(query)
    output = {}
    for x in result.elements():
        nodeID = x.id()
        stopName = x.tag('name')
        routes = x.tag('route_ref')
        coord = [x.lat(), x.lon()]
        output[nodeID] = [stopName, routes, coord]

    return output
def download_osm(area_name):
    """ Given an area name, download corresponding OSM elements """

    # Get area id
    nomanatim = Nominatim()
    area_id = nomanatim.query(area_name).areaId()

    # Form and ask query
    overpass = Overpass()
    query = overpassQueryBuilder(area=area_id, elementType=['way', 'node'], out='body')
    osm_data = overpass.query(query, timeout=600)

    # Display results
    utils.display_results(osm_data)

    # Keep elements (ways and nodes) in JSON format
    elements = osm_data.toJSON()['elements']
    return elements
示例#24
0
    def _queryOSM(self, **kwargs) -> None:
        """Query OSM and load data into self._elements_dict

        Keyword args:
            name (str): Element type.
            tag (str): Element tag.
            topology (str): Element topology.
        """

        if not self._bbox:
            raise ValueError("Bounding Box not loaded.")

        self._elements_dict[kwargs["name"]] = []

        for t in kwargs["tag"]:
            query = overpassQueryBuilder(
                bbox=self._bbox,
                selector=t,
                elementType=kwargs["topology"],
                includeGeometry=True,
            )

            while True:
                try:
                    results = self._overpass.query(
                        query,
                        timeout=self._timeout,
                    )
                    break
                except Exception as _:
                    # OFC they couldn't raise proper exceptions.
                    # this exceptions is a "generic" exception.
                    logging.error(
                        f"Trying again in {self._try_again} seconds...")
                    sleep(self._try_again)

            if not results.elements():
                continue

            if "node" in kwargs["topology"]:
                self._elements_dict[kwargs["name"]].extend(results.nodes())
            elif any(t in ["way", "area"] for t in kwargs["topology"]):
                self._elements_dict[kwargs["name"]].extend(results.ways())
示例#25
0
def fetch_osm(bbox, osm_value="motorway", osm_key="highway"):
    """
    Fetches OSM road data from the OverpassAPI.
    :param bbox: array-like of four coordinates: miny, minx, maxy, maxx.
    :param osm_value: str specifies the OSM value to be retrieved.
    :param osm_key: str specifies the OSM key to be retrieved.
    :return: gpd.GeoDataFrame
    """
    element_type = ["way", "relation"]
    bbox_osm = buffer_bbox(bbox)
    quot = '"'
    select = quot + osm_key + quot + "=" + quot + osm_value + quot
    select_link = select.replace(osm_value,
                                 osm_value + "_link")  # also get road links
    select_junction = select.replace(osm_value, osm_value + "_junction")
    geoms = []
    for selector in [select, select_link, select_junction]:
        query = overpassQueryBuilder(bbox=bbox_osm,
                                     elementType=element_type,
                                     selector=selector,
                                     out="body",
                                     includeGeometry=True)
        try:
            elements = Overpass().query(query, timeout=120).elements()
        except Exception:  # type?
            elements = []
            Warning("Could not download OSM data")
        # create multiline of all elements
        if len(elements) > 0:
            for i in range(len(elements)):
                elem = elements[i]
                try:
                    geoms.append(elem.geometry())
                except Exception:
                    continue
        else:
            Warning("Could not retrieve " + select)
    if len(geoms) > 0:
        lines = gpd.GeoDataFrame(crs="EPSG:4326", geometry=geoms)
        n = len(geoms)
        lines["osm_value"] = [osm_value] * n  # add road type
        return lines
示例#26
0
def GetOSMWaysData(box):
    overpass = Overpass()
    query = overpassQueryBuilder(
        bbox=[box.latMin, box.lonMin, box.latMax, box.lonMax],
        elementType='way',
        out='body')
    try:
        ways = overpass.query(query)
    except:
        Log.logging.error("In OSMPythonToolsHandler.py, GetOSMWaysData",
                          exc_info=True)
        return False
    if ways._json['elements'] == []:
        box.lonMin -= 0.5
        box.latMin -= 0.5
        box.lonMax += 0.5
        box.latMax += 0.5
        GetOSMWaysData(box)
    else:
        StoreWaysData(ways._json['elements'])
    return True
示例#27
0
    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
示例#28
0
    def __init__(self,
                 areaName: str,
                 elementsToUse: OsmObjectType = OsmObjectType.NODE):
        assert (self.osmSelector)

        areaId = Nominatim().query(areaName).areaId()
        query = overpassQueryBuilder(area=areaId,
                                     elementType=elementsToUse.value,
                                     selector=self.osmSelector,
                                     out='geom')
        osmObjects = Overpass().query(query).toJSON()["elements"]

        self.dataSource = osmObjectsToGeoJSON(osmObjects,
                                              polygonize=True)["features"]

        shapeGeoms = []
        for loc in self.dataSource:
            shapeGeom = shape(loc["geometry"])
            shapeGeom.properties = loc["properties"]
            shapeGeoms.append(shapeGeom)

        self.shapeIndex = STRtree(shapeGeoms)
示例#29
0
def get_osm(bbox,
            osm_value="motorway",
            osm_key="highway",
            ref_arr=None):
    element_type = ["way", "relation"]
    bbox_osm = buffer_bbox(bbox, ref_arr)
    quot = '"'
    select = quot + osm_key + quot + "=" + quot + osm_value + quot
    select_link = select.replace(osm_value, osm_value + "_link")  # also get road links
    select_junction = select.replace(osm_value, osm_value + "_junction")
    geoms = []
    for selector in [select, select_link, select_junction]:
        query = overpassQueryBuilder(bbox=bbox_osm,
                                     elementType=element_type,
                                     selector=selector,
                                     out="body",
                                     includeGeometry=True)
        try:
            elements = Overpass().query(query, timeout=120).elements()
        except Exception:
            elements = []
            Warning("Could not download OSM data")
        # create multiline of all elements
        if len(elements) > 0:
            for i in range(len(elements)):
                elem = elements[i]
                try:
                    geoms.append(elem.geometry())
                except Exception:
                    continue
        Warning("Could not retrieve " + select)
    if len(geoms) > 0:
        lines = gpd.GeoDataFrame(crs="EPSG:4326", geometry=geoms)
        n = len(geoms)
        lines["osm_value"] = [osm_value] * n  # add road type
        return lines
    else:
        return []
示例#30
0
def assertForQueryResult(minElements=100, overpassKwargs={}, **kwargs):
    print(kwargs)
    overpass = Overpass()
    y = overpass.query(overpassQueryBuilder(**kwargs), **overpassKwargs)
    assert y.isValid()
    assert len(y.elements()) > minElements
    assert len(y.elements()) == y.countElements()
    assert len(y.nodes()) >= 0
    assert len(y.nodes()) == y.countNodes()
    assert len(y.ways()) >= 0
    assert len(y.ways()) == y.countWays()
    assert len(y.relations()) >= 0
    assert len(y.relations()) == y.countRelations()
    assert len(y.areas()) >= 0
    assert len(y.areas()) == y.countAreas()
    assert y.countNodes() + y.countWays() + y.countRelations() + y.countAreas(
    ) == y.countElements()
    assert y.toJSON()
    assert y.version() > 0
    assert y.generator()
    assert y.timestamp_osm_base()
    assert y.copyright()
    return y