Exemple #1
0
    def parse_osm_nodes_paths(self, osm_data):
        """
        Edited Original OSMNX function to include singapore bus stop code
        Construct dicts of nodes and paths with key=osmid and value=dict of
        attributes.
        Parameters
        ----------
        osm_data : dict
            JSON response from from the Overpass API
        Returns
        -------
        nodes, paths : tuple
        """

        nodes = {}
        paths = {}
        for element in osm_data['elements']:
            if element['type'] == 'node':
                key = element['id']
                nodes[key] = self.get_node(element)
            elif element['type'] == 'way':  # osm calls network paths 'ways'
                key = element['id']
                paths[key] = ox.get_path(element)

        return nodes, paths
def parse_osm_nodes_paths(osm_data):
    """
    Construct dicts of nodes and paths with key=osmid and value=dict of
    attributes.

    Parameters
    ----------
    osm_data : dict
        JSON response from from the Overpass API

    Returns
    -------
    nodes, paths : tuple
    """

    nodes = {}
    paths = {}
    for element in osm_data['elements']:
        if element['type'] == 'node':
            key = element['id']
            nodes[key] = get_node(element)

        elif element['type'] == 'way':  #osm calls network paths 'ways'
            key = element['id']
            paths[key] = ox.get_path(element)

    return nodes, paths
Exemple #3
0
def parse_osm_nodes_paths(osm_data):

    nodes = {}
    paths = {}
    for element in osm_data['elements']:
        if element['type'] == 'node':
            key = element['id']
            nodes[key] = get_node(element)
        elif element['type'] == 'way':  # osm calls network paths 'ways'
            key = element['id']
            paths[key] = ox.get_path(element)

    return nodes, paths
Exemple #4
0
def routes_from_polygon(polygon, route_type):
    """
    Create a network graph corresponding to a selected route type from
    OSM data within the spatial boundaries of the passed-in shapely
    polygon.

    This function mostly reproduces that is already done in
    :py:func:`osmnx.graph_from_polygon`, but it preserves the route
    structure imposed by relations.  The graph is always truncated to
    the polygon and simplified.

    :param shapely.Polygon polygon: the shpae to get network data
        within.  Coordinates should be in units of latitude-longiute.
    :param str route_type: what type of route to get.

    :returns: a dictionary mapping route name to the corresponding
              multigraph.
    """

    for tag in ("name", "public_transport"):  # might be expanded later
        if tag not in osmnx.settings.useful_tags_node:
            osmnx.settings.useful_tags_node.append(tag)

    response_jsons = osmnx.osm_net_download(
        polygon=polygon,
        infrastructure='rel["route"]',
        custom_filter='["route"="{}"]'.format(route_type))

    # Collect all the elements from the response.
    elements = []
    for osm_data in response_jsons:
        elements.extend(osm_data["elements"])

    # Sort them into paths, nodes and relations building a global
    # lookup dictionary.
    nodes = {}
    paths = {}
    relations = {}
    for element in elements:
        key = element["id"]
        if element["type"] == "node":
            nodes[key] = osmnx.get_node(element)
        if element["type"] == "way":
            paths[key] = osmnx.get_path(element)
        if element["type"] == "relation":
            relations[key] = element

    # Build a graph for every relation.
    routes = {}

    for _, relation in relations.items():
        try:
            line, route = route_from_relation(relation, nodes, paths)
            route = osmnx.truncate_graph_polygon(route,
                                                 polygon,
                                                 retain_all=True)
        except Exception:
            # route might be empty or the line might be outside of
            # region
            continue

        if route.edges:
            routes[line] = route

    return routes