0

正如标题所说,我需要编写一个 python 代码来输出某种类型的所有节点,其中两个值之间的步行距离。我猜想它需要对某个点周围的所有节点进行立交桥查询,然后用立交桥构建最短路线并对所有路径进行排序。

因此,我的问题是 - 是否有更好/更短的方法来做到这一点,如果没有,如何按长度对 openrouteservice 查询给出的路线进行排序?

最后一个问题是,我将如何使用 openrouteservice 在 2 个点之间找到 2 条不同的路线,从而有效地形成一个包含两个点的“循环”。

我当前非常垃圾的代码只返回到给定半径内给定类型的随机点的路线:

client = openrouteservice.Client(key="my_ORS_key")

def findNodeNearLoc(lat, lon, rad):
    """
    Given a location finds a node of given type within rad meters.

        Parameters:
                lat (double): Latitude
                lon (double): Longitude
                rad (double): radius

        Returns:
                way (Way): the Way corresponding to a node.
    """
    api = overpy.Overpass()
    # warning! this uses lat,lon format.
    query = f"""way[amenity={type}](around:10000,{lat},{lon});
    out body;"""
    result = api.query(query)

    # Picks the first way in the result
    if not result.ways:
        print("\n\tError: No results found")
        return
    else:
        return result.ways[0]

def findRoute(start_lat, start_lon, end_lat, end_lon):
    """
    Given a start and end location find a route between them

        Parameters:
                start_lat (double): Start Latitude
                start_lon (double): Start Longitude
                end_lat (double): End Latitude
                end_lon (double): End Longitude

        Returns:
                Dictionary: A route from the start to the finish
    """
    geometry =  client.directions(((start_lon, start_lat),(end_lon, end_lat)), profile = "foot-hiking")['routes'][0]['geometry']
    decoded = convert.decode_polyline(geometry)
    return decoded

node_way = osm.findNodeNearLoc(lat, lon, rad, type)
end_node = node_way.get_nodes(resolve_missing=True)[0]
route = osm.findRoute(lat, lon, float(end_node.lat), float(end_node.lon))
4

0 回答 0