Beispiel #1
0
    def _get_cycleways(self):
        """
        Creates all cycleways in a city (networkx.MultiDiGraph).
        """
        useful_tags = ox.settings.useful_tags_way + ['cycleway']
        ox.config(use_cache=True,
                  log_console=True,
                  useful_tags_way=useful_tags)

        if self.query_type == 'city':
            if self.city_limits is True:
                cycleways = ox.graph_from_place(query=self.city_name,
                                                network_type='bike',
                                                simplify=False)
            else:
                cycleways = ox.graph_from_bbox(self.north,
                                               self.south,
                                               self.east,
                                               self.west,
                                               network_type='bike',
                                               simplify=False)

        elif self.query_type == 'address':
            cycleways = ox.graph_from_address(self.address,
                                              network_type='bike',
                                              dist=self.distance)

        non_cycleways = [
            (u, v, k) for u, v, k, d in cycleways.edges(keys=True, data=True)
            if not ('cycleway' in d or d['highway'] == 'cycleway')
        ]
        cycleways.remove_edges_from(non_cycleways)
        cycleways = ox.utils_graph.remove_isolated_nodes(cycleways)

        return cycleways
Beispiel #2
0
def get_data(G, district_name):
    # print(nx.info(G))
    print(f'File Path = {PATH}')

    # save as graphml
    path = f'{PATH}/{district_name}.xml'
    ox.save_graphml(G, path)

    # save as .osm
    path = f'{PATH}/{district_name}.osm'
    ox.config(all_oneway=True)
    if not os.path.exists(path):
        ox.save_graph_xml(G, filepath=path)

    # save as folium html
    path = f'{PATH}/{district_name}_folium.html'
    if not os.path.exists(path):
        map_folium = ox.folium.plot_graph_folium(G)
        map_folium.save(path)

    # save as SVG
    path = f'{PATH}/{district_name}_image.svg'
    fig, ax = ox.plot_graph(G,
                            show=False,
                            save=True,
                            close=True,
                            filepath=path)

    # save graph as a shapefile and .csv
    path = f'{PATH}/{district_name}_shape'
    ox.save_graph_shapefile(G, filepath=path)

    make_adjacency_matrix(district_name)
    clean_csv(district_name)
    make_adjacency_required_matrix(district_name)
Beispiel #3
0
 def __init__(self):
     # uncomment for log console
     ox.config(use_cache=True, log_console=True)
     self.italy_gdf = ox.gdf_from_place('Italy')
     self.map = None
     self.route = None
     self.route_cache = []
     self.load_Italy()
Beispiel #4
0
def setup():
    """Sets up OSMnx"""

    logger = logging.getLogger()
    ox.config(log_console=False,
              log_file=os.devnull,
              log_name=logger.name,
              use_cache=True)
Beispiel #5
0
def test_overpass():
    import pytest

    # Test changing the endpoint. This should fail because we didn't provide a valid endpoint
    ox.config(overpass_endpoint="http://NOT_A_VALID_ENDPOINT/api/")
    with pytest.raises(Exception):
        G = ox.graph_from_place('Piedmont, California, USA')

    ox.config(overpass_endpoint="http://overpass-api.de/api")
Beispiel #6
0
def get_greece_graph():
    place_name = "Greece"
    ox.config(use_cache=True, log_console=True)
    #cf = '["highway"~"motorway|motorway_link|trunk|secondary|primary"]'
    cf = '["highway"~"motorway|motorway_link"]'
    greece_graph = ox.graph_from_place(place_name,
                                       network_type='drive',
                                       custom_filter=cf)
    return greece_graph
Beispiel #7
0
def find_area():
    request = flask.request.args
    lat = float(request.get('lat'))
    lng = float(request.get('lng'))
    type = 'drive'

    trip_times = request.get('trip_times').split(',')
    ox.config(log_console=True, use_cache=True)
    G = ox.load_graphml('network.graphml')
    f = open("network_type.yml", "r")
    network_type = f.read()
    f.close()
    travel_speed = 60.0 if network_type == 'drive' else 4.5
    center_node = ox.get_nearest_node(G, (lat, lng))
    G = ox.load_graphml('project.graphml')
    meters_per_minute = travel_speed * 1000 / 60
    for u, v, k, data in G.edges(data=True, keys=True):
        data['time'] = data['length'] / meters_per_minute
    iso_colors = ox.get_colors(n=len(trip_times),
                               cmap='Reds',
                               start=0.3,
                               return_hex=True)

    isochrone_polys = []
    for trip_time in sorted(trip_times, reverse=True):
        subgraph = nx.ego_graph(G,
                                center_node,
                                radius=float(trip_time),
                                distance='time')

        node_points = [
            Point((float(data['lat']), float(data['lon'])))
            for node, data in subgraph.nodes(data=True)
        ]
        bounding_poly = gpd.GeoSeries(node_points).unary_union.convex_hull
        isochrone_polys.append(bounding_poly)
    polygons_array = []
    for polygon, fc in zip(isochrone_polys, iso_colors):
        patch = PolygonPatch(polygon, fc=fc, ec='none', alpha=0.6, zorder=-1)
        geojson = {
            "type": "Feature",
            "properties": {
                "timeline": trip_time
            },
            "geometry": {
                "type":
                "Polygon",
                "coordinates": [
                    list(([list(x)[0], list(x)[1]]
                          for x in list(patch._path.vertices)))
                ]
            }
        }
        polygons_array.append(geojson)
    return flask.jsonify(polygons_array)
Beispiel #8
0
    def _osm_net_from_osmnx(self, boundary, osm_file=None):
        """
        Submits an Overpass API query and returns a geodataframe of results

        Parameters
        ----------
        boundary : shapely geometry object
            shapely geometry representing the boundary for pulling the network
        osm_file : str, optional
            an OSM XML file to use instead of downloading data from the network
        """
        # https://osmnx.readthedocs.io/en/stable/osmnx.html#osmnx.save_load.graph_to_gdfs
        node_tags = [
            "access", "amenity", "bicycle", "bridge", "button_operated",
            "crossing", "flashing_lights", "foot", "highway", "junction",
            "leisure", "motorcar", "name", "oneway", "oneway:bicycle",
            "operator", "public_transport", "railway", "segregated", "shop",
            "stop", "surface", "traffic_sign", "traffic_signals", "tunnel",
            "width"
        ]
        way_tags = [
            "access", "bridge", "bicycle", "button_operated", "crossing",
            "cycleway", "cycleway:left", "cycleway:right", "cycleway:both",
            "cycleway:buffer", "cycleway:left:buffer", "cycleway:right:buffer",
            "cycleway:both:buffer", "cycleway:width", "cycleway:left:width",
            "cycleway:right:width", "cycleway:both:width", "flashing_lights",
            "foot", "footway", "highway", "junction", "landuse", "lanes",
            "lanes:forward", "lanes:backward", "lanes:both_ways", "leisure",
            "maxspeed", "motorcar", "name", "oneway", "oneway:bicycle",
            "operator", "parking", "parking:lane", "parking:lane:right",
            "parking:lane:left", "parking:lane:both", "parking:lane:width",
            "parking:lane:right:width", "parking:lane:left:width",
            "parking:lane:both:width", "public_transport", "railway",
            "segregated", "service", "shop", "stop", "surface", "tracktype",
            "traffic_sign", "traffic_signals:direction", "tunnel",
            "turn:lanes", "turn:lanes:both_ways", "turn:lanes:backward",
            "turn:lanes:forward", "width", "width:lanes",
            "width:lanes:forward", "width:lanes:backward"
        ]

        ox.config(useful_tags_node=node_tags, useful_tags_path=way_tags)
        if osm_file:
            G = ox.graph_from_file(osm_file, simplify=True, retain_all=False)
        else:
            G = ox.graph_from_polygon(boundary,
                                      network_type='all',
                                      simplify=True,
                                      retain_all=False,
                                      truncate_by_edge=False,
                                      timeout=180,
                                      clean_periphery=True,
                                      custom_filter=None)
        G = ox.get_undirected(G)
        gdfs = ox.graph_to_gdfs(G)
        return gdfs[1], gdfs[0]
Beispiel #9
0
def create_knots_and_edges_from_boundary(
        district_voronoi_gdf: gpd.geodataframe.GeoDataFrame,
        cs: str) -> KnotsEdges:
    district_poly = district_voronoi_gdf.unary_union
    ox.config(overpass_settings=cs)
    district_graph = ox.graph_from_polygon(
        district_poly,
        truncate_by_edge=False,
        network_type='all',
        retain_all=True,
    )
    return (district_poly, district_graph)
Beispiel #10
0
def get_network_graph(place_name, net_type, custom_filter):
    """ Method to get OSM data and to convert it to network graph.
    
    place_name -- the name of a place.
    net_type -- network type (for instance 'drive')
    custom_filter -- a custom filter for selecting particular parts of the network.
    
    Returns a graph of the selected network.
    """
    ox.config(use_cache=True, log_console=True)
    graph = ox.graph_from_place(place_name,
                                network_type=net_type,
                                custom_filter=custom_filter)
    return graph
Beispiel #11
0
def get_data_drone(G, district_name):
    # Graph for Drones
    district_name += '_drone'
    R = 6371e3
    nodes = list(G.nodes.data())

    for i in range(len(nodes)):
        for j in range(len(nodes)):
            if i == j or (G.has_edge(nodes[i][1]['osmid'],
                                     nodes[j][1]['osmid'])):
                continue
            else:
                lon1 = float(nodes[i][1]['x'] * np.pi / 180)
                lat1 = float(nodes[i][1]['y'] * np.pi / 180)
                lon2 = float(nodes[j][1]['x'] * np.pi / 180)
                lat2 = float(nodes[j][1]['y'] * np.pi / 180)
                d_lat = lat2 - lat1
                d_lon = lon2 - lon1
                a = np.sin(d_lat/2) ** 2 + np.cos(lat1) * \
                    np.cos(lat2) * np.sin(d_lon/2) ** 2
                c = 2 * np.arctan2(a**0.5, (1 - a)**0.5)
                d = R * c
                d = round(d, 3)
                G.add_edge(nodes[i][1]['osmid'],
                           nodes[j][1]['osmid'],
                           length=d)

    # save as graphml
    path = f'{PATH}/{district_name}.xml'
    ox.save_graphml(G, path)

    # save as .osm
    path = f'{PATH}/{district_name}.osm'
    ox.config(all_oneway=True)
    if not os.path.exists(path):
        ox.save_graph_xml(G, filepath=path)

    # save as folium html
    path = f'{PATH}/{district_name}_folium.html'
    if not os.path.exists(path):
        map_folium = ox.folium.plot_graph_folium(G)
        map_folium.save(path)

    # save as SVG
    path = f'{PATH}/{district_name}_image.svg'
    fig, ax = ox.plot_graph(G,
                            show=False,
                            save=True,
                            close=True,
                            filepath=path)
Beispiel #12
0
def heatfromdoc(doc):
    import networkx as nx
    import osmnx as ox
    from folium import Map
    import folium.plugins as plugins

    ox.config(use_cache=True, log_console=True)
    G = ox.graph_from_place('Cologne', network_type='bike')
    gdf_nodes, gdf_edges = ox.graph_to_gdfs(G)

    timestamps = []
    for row in doc:
        timestamps.append(row["starttime"])
    timestamps = list(sorted(timestamps))

    datapoints = []
    cnt = 0
    timeindex = []
    points = []
    for time in timestamps:
        for route in doc:
            try:
                if route["starttime"] == time:
                    for node in route["route"]:
                        point = []
                        nodepoint = gdf_nodes.loc[node]
                        point = [nodepoint["y"], nodepoint["x"], 1]
                        points.append(point)

            except:
                continue
        if cnt == 6: cnt = 0
        if points != [] and cnt == 0:
            datapoints.append(points)
            timeindex.append(str(time))
            points = []
        cnt += 1
    m = Map([50.9287107, 6.9459497], tiles="cartodbpositron", zoom_start=13)

    hm = plugins.HeatMapWithTime(datapoints,
                                 index=timeindex,
                                 auto_play=True,
                                 max_opacity=0.5,
                                 radius=8,
                                 use_local_extrema=True)

    hm.add_to(m)
    m.save('index.html')
Beispiel #13
0
def init_osmnx():
    """
    Configure osmnx's settings to match anprx's settings.
    """

    osmnx_folder = os.path.join(settings["app_folder"], "osmnx")
    if not os.path.exists(osmnx_folder):
        os.makedirs(osmnx_folder)

    ox.config(data_folder=os.path.join(osmnx_folder, "data"),
              logs_folder=os.path.join(osmnx_folder, "logs"),
              imgs_folder=os.path.join(osmnx_folder, "images"),
              cache_folder=os.path.join(osmnx_folder, "cache"),
              use_cache=True,
              log_file=True,
              log_console=False)
Beispiel #14
0
def manhattan_data():

    ## Use OSMNX library to download and save a street map of Manhattan from Open Street Map
    # Library config
    ox.config(use_cache=True, log_console=True)

    # Get the drive-able roads on Manhattan using OpenStreetMap api's to request data
    G = ox.graph_from_place('Manhattan Island, New York City, New York, USA',
                            network_type='drive')

    # Save file to disk as graphML format
    ox.save_load.save_graphml(
        G,
        filename='ManhattanDrive.graphml',
        folder='/Users/kevincrossley/Documents/DevUp/Capstone Data',
        gephi=False)
 def __init__(self,
              bounding_box,
              network_type='drive_service',
              crs='epsg:4326',
              tags_nodes=None,
              tags_edges=None,
              simplify_strict=False,
              custom_filter=None,
              truncate_by_edge=False):
     # researched area (bounding box)
     self.bounding_box = bounding_box
     self.network_type = network_type
     self.custom_filter = custom_filter
     self.strict = simplify_strict
     self.tags_nodes = tags_nodes
     self.tags_edges = tags_edges
     self.crs = crs
     if tags_edges is None:
         self.tags_edges = [
             'bridge', 'tunnel', 'oneway', 'lanes', 'name', 'highway',
             'busway', 'busway:both', 'busway:left', 'busway:right',
             'maxspeed', 'service', 'access', 'area', 'landuse', 'width',
             'est_width', 'junction', 'surface', 'turn'
         ]
     if tags_nodes is None:
         self.tags_nodes = [
             'highway', 'public_transport', 'traffic_signals', 'crossing'
         ]
     # download the road network from OSM
     ox.config(useful_tags_way=self.tags_edges,
               useful_tags_node=self.tags_nodes)
     self.graph_latlon = ox.graph_from_bbox(
         self.bounding_box[0],
         self.bounding_box[1],
         self.bounding_box[2],
         self.bounding_box[3],
         network_type=self.network_type,
         custom_filter=self.custom_filter,
         simplify=self.strict,
         truncate_by_edge=truncate_by_edge)
     self.graph_xy = ox.project_graph(self.graph_latlon)
     self.graph_raw = self.graph_latlon
     self.network_edges = pd.DataFrame()
     self.network_nodes = pd.DataFrame()
     self.used_network = pd.DataFrame()
     self.mm_id = {}
     self.node_tags = node_tags(self.graph_raw, tag='highway')
def make_updated_graph_model():
    ox.config(log_console=True, use_cache=True)

    G_2 = ox.load_graphml('osmnx_graph.graphml')

    # plot the street network with folium
    graph_map = ox.plot_graph_folium(G_2, edge_width=2)

    filepath = 'templates/graph.html'
    graph_map.save(filepath)
    IFrame(filepath, width=600, height=500)

    soup = BeautifulSoup(open('templates/graph.html'), 'html.parser')

    js_tag = soup.find_all("script")
    js_tag[5].append('{% block script %} {% endblock %}')

    with open("templates/graph.html", "w") as file:
        file.write(str(soup))

    soup.find("div")
    div = soup.find("div")
    div = str(div).split()
    map_id = div[2][4:-8]

    fin = open("templates/graph.html", "rt")
    # output file to write the result to
    fout = open("templates/final_graph.html", "wt")
    # for each line in the input file
    for line in fin:
        # read replace the string and write to output file
        fout.write(line.replace(map_id, 'map_id'))
    # close input and output files
    fin.close()
    fout.close()

    soup = BeautifulSoup(open('templates/final_graph.html'), 'html.parser')

    data_body = '{% block body %} {% endblock %}'
    data_script = '{% block script %} {% endblock %}'
    data_style = '{% block style %} {% endblock %}'

    soup.body.append(data_body)

    soup.style.append(data_style)
    with open("templates/final_graph.html", "w") as file:
        file.write(str(soup))
Beispiel #17
0
def planRoute(bnb, amenities_list, choice):
    '''
    Reference: https://stackoverflow.com/questions/60578408/is-it-possible-to-draw-paths-in-folium
    '''
    ox.config(log_console=True, use_cache=True)
    if choice == 1:
        G_walk = ox.graph_from_place('Vancouver, British Columbia, Canada',
                                     network_type='walk')
    elif choice == 2:
        G_walk = ox.graph_from_place('Vancouver, British Columbia, Canada',
                                     network_type='bike')
    else:
        G_walk = ox.graph_from_place('Vancouver, British Columbia, Canada',
                                     network_type='drive')

    orig_node = ox.get_nearest_node(G_walk,
                                    (bnb.iloc[0]['lat'], bnb.iloc[0]['lon']))
    dest_node = orig_node

    nodes = []
    routes = []

    for index, row in amenities_list.iterrows():
        nodes.append(ox.get_nearest_node(G_walk, (row['lat'], row['lon'])))
        if index == 0:
            routes.append(
                nx.shortest_path(G_walk,
                                 orig_node,
                                 nodes[index],
                                 weight='length'))
        elif (index == len(amenities_list.index) - 1):
            routes.append(
                nx.shortest_path(G_walk,
                                 nodes[index],
                                 dest_node,
                                 weight='length'))
        else:
            routes.append(
                nx.shortest_path(G_walk,
                                 nodes[index - 1],
                                 nodes[index],
                                 weight='length'))

    for route in routes:
        route_map = ox.plot_route_folium(G_walk, route)

    return route_map
Beispiel #18
0
    def form_valid(self, form):
        # obj = self.get_object()
        ox.config(log_console=True, use_cache=True)

        G = ox.graph_from_place('Sievierodonetsk, UA', network_type='drive')

        a = (form.instance.point_a, form.instance.point_b)
        b = (form.instance.point_c, form.instance.point_d)

        orig_node = ox.get_nearest_node(G, a, method='euclidean')
        dest_node = ox.get_nearest_node(G, b, method='euclidean')

        route_direct = nx.shortest_path(G,
                                        orig_node,
                                        dest_node,
                                        weight='length')
        route_return = nx.shortest_path(G,
                                        dest_node,
                                        orig_node,
                                        weight='length')

        route_map = ox.plot_route_folium(G,
                                         route_direct,
                                         route_color='green',
                                         route_width=5,
                                         route_opacity=0.5,
                                         tiles='openstreetmap',
                                         popup_attribute='length')
        route_map = ox.plot_route_folium(G,
                                         route_return,
                                         route_color='blue',
                                         route_width=5,
                                         route_opacity=0.5,
                                         route_map=route_map,
                                         popup_attribute='length')
        folium.Marker(location=(G.node[orig_node]['y'],
                                G.node[orig_node]['x']),
                      icon=folium.Icon(color='green', prefix='fa',
                                       icon='flag')).add_to(route_map)
        folium.Marker(
            location=(G.node[dest_node]['y'], G.node[dest_node]['x']),
            icon=folium.Icon(color='blue', prefix='fa',
                             icon='flag-checkered')).add_to(route_map)
        generated_route = route_map.get_root().render()
        form.instance.route_view = generated_route
        return super().form_valid(form)
Beispiel #19
0
    def plot_route(self, dest, orig=None):
        if orig is None:
            orig = self.home_loc
        ox.config(log_console=True, use_cache=True)

        origin = self.client.geocode(orig)[0]
        orig_loc = (origin["geometry"]["location"]["lat"],
                    origin["geometry"]["location"]["lng"])

        destination = self.client.geocode(dest)[0]
        dest_loc = (destination["geometry"]["location"]["lat"],
                    destination["geometry"]["location"]["lng"])

        route = self.get_gmaps_path(orig_loc, dest_loc)

        route_map = folium.Map(location=orig_loc, tiles="Stamen Terrain")

        self.make_polyline(route).add_to(route_map)

        self.map_exposures(route_map)

        folium.Marker(list(orig_loc),
                      popup=self.make_popup("<i>" + orig + "</i>"),
                      tooltip="Origin").add_to(route_map)

        dest_risk_score = calculate_risk_score(self, *dest_loc)

        if dest_risk_score > 0.5:
            self.waypoint_lower_risk_locs(route_map, dest_risk_score,
                                          destination, dest)

        destination_html = "<strong> Location: </strong><i>" + dest + \
                           "</i> <br> <strong> Risk Score: " + str(dest_risk_score * 100)[:4] + "%" \
                                                                                                "</strong> <br> <strong> Recommendation: </strong>" + \
                           give_recommendation(destination, dest, dest_risk_score)
        folium.Marker(list(dest_loc),
                      popup=self.make_popup(destination_html),
                      tooltip="Destination").add_to(route_map)

        density_choropleth = generate_density_choropleth()
        density_choropleth.add_to(route_map)

        folium.LayerControl().add_to(route_map)

        route_map.save(str(ROOT_PATH / self.settings["save_path"]))
Beispiel #20
0
def get_town_data(town_params):
    """Query OSM and process its traffic graph."""
    ox.config(use_cache=True, log_console=True)
    G_town = ox.graph_from_place(town_params.town_name,
                                 network_type="drive",
                                 simplify=False)
    G_town = ox.simplify.simplify_graph(G_town, strict=False)
    nodes, edges = ox.graph_to_gdfs(G_town)
    town_node_ids = set(nodes)

    node_ids = nodes[["osmid"]].values
    processed_nodes = []
    for node_id in node_ids:
        if node_id == town_params.dest:
            processed_nodes.append("T")
        if node_id == town_params.source:
            processed_nodes.append("S")
        processed_nodes.append(node_id[0])
    # change from meters to miles
    edges["length"] = edges["length"].apply(meters_to_miles)
    edges["speed_limit"] = edges.apply(
        lambda x: _set_speed(x.highway, x.length), axis=1)

    processed_edges = {}
    for u, v, length, speed in edges[["u", "v", "length",
                                      "speed_limit"]].values:
        u = int(u)
        v = int(v)
        if u == town_params.source:
            u = "S"
        if v == town_params.source:
            v = "S"
        if u == town_params.dest:
            u = "T"
        if v == town_params.dest:
            v = "T"

        if u in processed_nodes and v in processed_nodes:
            e = EdgeData(speed_lim=speed, length=length)
            processed_edges[u, v] = e
    G = ox.gdfs_to_graph(nodes, edges)
    return (G, town_params.source, town_params.dest), (processed_nodes,
                                                       processed_edges)
Beispiel #21
0
def osm_gdf_from_place(query, tags, which_result=None, buffer_dist=None):
    """Create GeoDataFrame of OSM entities within boundaries of geocodable place(s).

    Args:
        query (str | dict | list): Query string(s) or structured dict(s) to geocode.
        tags (dict): Dict of tags used for finding objects in the selected area. Results returned are the union, not intersection of each individual tag. Each result matches at least one given tag. The dict keys should be OSM tags, (e.g., building, landuse, highway, etc) and the dict values should be either True to retrieve all items with the given tag, or a string to get a single tag-value combination, or a list of strings to get multiple values for the given tag. For example, tags = {‘building’: True} would return all building footprints in the area. tags = {‘amenity’:True, ‘landuse’:[‘retail’,’commercial’], ‘highway’:’bus_stop’} would return all amenities, landuse=retail, landuse=commercial, and highway=bus_stop.
        which_result (int, optional): Which geocoding result to use. if None, auto-select the first (Multi)Polygon or raise an error if OSM doesn't return one. to get the top match regardless of geometry type, set which_result=1. Defaults to None.
        buffer_dist (float, optional): Distance to buffer around the place geometry, in meters. Defaults to None.

    Returns:
        GeoDataFrame: A GeoDataFrame of OSM entities.
    """
    check_package("osmnx",
                  "https://osmnx.readthedocs.io/en/stable/#installation")
    import osmnx as ox

    ox.config(use_cache=True, log_console=True)

    gdf = ox.geometries_from_place(query, tags, which_result, buffer_dist)
    return gdf
Beispiel #22
0
def pltfromdoc(doc):
    import networkx as nx
    import osmnx as ox
    ox.config(use_cache=True, log_console=True)
    listofroutes = []
    for row in doc:
        listofroutes.append(row["route"])
    G = ox.graph_from_place('Cologne', network_type='bike')
    ox.plot_graph_routes(G,
                         listofroutes,
                         fig_height=15,
                         node_size=1,
                         route_linewidth=1,
                         route_alpha=0.4,
                         orig_dest_node_size=10,
                         orig_dest_node_color='r',
                         node_alpha=0.2,
                         save=True,
                         filename="graphbikes",
                         file_format="png")
Beispiel #23
0
    def __init__(self, jsonData):
        QAbstractTableModel.__init__(self)

        self.json = jsonData
        self.nodes = []
        self.data = []
        for i in jsonData["elements"]:
            if i["type"] == "way":
                self.data.append(i)
            elif i["type"] == "node":
                self.nodes.append(i)

        self.headerItems = []
        self.alt = []

        self.allKeys = frozenset([])
        for i in self.data:
            self.allKeys |= frozenset(i["tags"].keys())
        self.allKeys -= frozenset(["osmid", "length"])
        ox.config(useful_tags_path=list(self.allKeys))
Beispiel #24
0
    def get_trail_pois_for_polygon(self, polygon: Polygon):
        useful_tags_node = ox.settings.useful_tags_node
        useful_tags_node.extend(
            ['historic', 'wikipedia', 'tourism', 'backcountry'])
        ox.config(useful_tags_node=useful_tags_node)

        tags = {
            'amenity': [
                'shelter',
                'shower',
                'toilets',
            ],
            'natural': [
                'peak',
                'saddle',
            ],
            'toilets:disposal': [
                'pitlatrine',
                'flush',
            ],
            'tourism': [
                'alpine_hut',
                'camp_site',
                'picnic_site',
                'viewpoint',
                'wilderness_hut',
            ],
        }  # yapf: ignore

        gdf = ox.create_poi_gdf(polygon=polygon, tags=tags)

        # Some geometries are polygons, so replace geometry with its centroid.
        # For Points this makes no difference; for Polygons, this takes the
        # centroid.
        gdf.geometry = gdf.geometry.centroid

        # TODO: keep only useful columns?
        return gdf
Beispiel #25
0
def osmnx_config(node_tags: List[str],
                 edge_tags: List[str]) -> Generator[None, None, None]:
    """
    Prepare osmnx config for downloading data.
    """
    # aggregate default tags
    useful_node_tags = set(osmnx.settings.useful_tags_node +
                           osmnx.settings.osm_xml_node_attrs +
                           osmnx.settings.osm_xml_node_tags +
                           ["junction"]  # additional useful tags
                           + ["street_count"]  # custom enhancements
                           + node_tags)
    useful_edge_tags = set(
        osmnx.settings.useful_tags_way + osmnx.settings.osm_xml_way_attrs +
        osmnx.settings.osm_xml_way_tags +
        ["junction"]  # additional useful tags
        + ["bearing", "speed_kph", "travel_time"]  # custom enhancements
        + edge_tags)

    original_settings = {
        "all_oneway": osmnx.settings.all_oneway,
        "useful_tags_node": osmnx.settings.useful_tags_node,
        "useful_tags_way": osmnx.settings.useful_tags_node,
    }
    new_settings = {
        "all_oneway": False,  # we need digraph, an edge for each direction
        "useful_tags_node": useful_node_tags,
        "useful_tags_way": useful_edge_tags,
    }
    try:
        logger.debug("Update osmnx configuration", extra=new_settings)
        osmnx.config(**new_settings)
        yield
    finally:
        logger.debug("Resetting osmnx configuration", extra=original_settings)
        osmnx.config(**original_settings)
Beispiel #26
0
def test_nominatim():

    import pytest
    from collections import OrderedDict

    params = OrderedDict()
    params['format'] = "json"
    params['address_details'] = 0

    # Bad Address - should return an empty response
    params['q'] = "AAAAAAAAAAA"
    response_json = ox.nominatim_request(params = params,
                                         type = "search")

    # Good Address - should return a valid response with a valid osm_id
    params['q'] = "Newcastle A186 Westgate Rd"
    response_json = ox.nominatim_request(params = params,
                                         type = "search")

    # Lookup
    params = OrderedDict()
    params['format'] = "json"
    params['address_details'] = 0
    params['osm_ids'] = "W68876073"

    response_json = ox.nominatim_request(params = params,
                                         type = "lookup")

    # Invalid nominatim query type
    with pytest.raises(ValueError):
        response_json = ox.nominatim_request(
                            params = params,
                            type = "transfer")

    # Searching on public nominatim should work even if a key was provided
    ox.config(
        nominatim_key="NOT_A_KEY"
    )
    response_json = ox.nominatim_request(params = params,
                                         type = "search")

    # Test changing the endpoint. It should fail because we didn't provide a valid key
    ox.config(
        nominatim_endpoint="http://open.mapquestapi.com/nominatim/v1/"
    )
    with pytest.raises(Exception):
        response_json = ox.nominatim_request(params=params,
                                             type="search")

    ox.config(log_console=True, log_file=True, use_cache=True,
              data_folder='.temp/data', logs_folder='.temp/logs',
              imgs_folder='.temp/imgs', cache_folder='.temp/cache')
import time
import osmnx as ox
'''
Script to connect and analyze the different connected components on the bicycle layer of the cities.
This iteration of the algorithm randomly takes on of the connected commponents, looks for the distance to all other commponents and create a link with the closest one.
'''
#Script configs:
output_path = '../Data/bike_streets/filter/outputs/'
data_path = '../Data/bike_streets/filter/'

# Confg osmnx
ox.config(data_folder=data_path,
          logs_folder='../logs',
          imgs_folder='../imgs',
          cache_folder='../cache',
          use_cache=True,
          log_console=False,
          log_name='osmnx',
          log_file=True,
          log_filename='osmnx')
now = datetime.datetime.now()


# Auxiliar functions
def assure_path_exists(path):
    dir = os.path.dirname(path)
    if not os.path.exists(dir):
        os.makedirs(dir)


def load_graph(name, layer):
Beispiel #28
0
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 18 18:17:22 2021

@author: focke
"""
from __future__ import print_function
from ortools.graph import pywrapgraph
import numpy as np
import osmnx as ox
ox.config(use_cache=True, log_console=False)
# ox.__version__
import networkx as nx


# def network_solver(carriers, transportables, weight_list):
#     ALI_G = nx.DiGraph()
#     counter = 0
#
#     for j, trans in enumerate(transportables):
#         ALI_G.add_node(trans, demand = 1)
#
#     for i, car in enumerate(carriers):
#         ALI_G.add_node(car, demand = -1)
#
#     for i, car in enumerate(carriers):
#         for j, trans in enumerate(transportables):
#             ALI_G.add_edge(car, trans, weight=int(weight_list[counter]), capacity=1)
#             counter += 1
#
#     flowDict_alt = nx.min_cost_flow(G)
Beispiel #29
0
import osmnx as ox
from osmnx import distance as distance
import plotly.graph_objects as go
import plotly.express as px
import numpy as np
import csv
import random
import operator
import math
import pandas
import matplotlib.pyplot as plt
from collections import deque
from copy import deepcopy

## -- Set up the initial map area and save it as a networkx graph
ox.config(log_console=True, use_cache=True)
G = ox.graph_from_address(
    '1276 Gilbreath Drive, Johnson City, Washington County, Tennessee, 37614, United States',
    dist=8000,
    network_type='drive')

### Use this code to display a plot of the graph if desired. Note: You need to import matplotlib.pyplot as plt
# fig, ax = ox.plot_graph(G, edge_linewidth=3, node_size=0, show=False, close=False)
# plt.show()

## -- Genetic Algorithm Parameters
GENERATIONS = 100
POPULATION_SIZE = 200
MUTATION_RATE = 0.1
DISPLAY_RATE = 20
#! /bin/bash/env python

import osmnx as ox, matplotlib.pyplot as plt
from descartes import PolygonPatch
from shapely.geometry import Polygon, MultiPolygon
import geopandas as gpd
import json
import os, sys, glob

ox.config(log_console=True, use_cache=True, data_folder='data/vector/city_networks/')

scriptpath = os.path.dirname(__file__)
cities = glob.glob("data/vector/city_boundaries/*.shp")


#indir = "data/vector/city_boundaries/"
for city in cities:
    name = os.path.basename(city).split('.')[0]
    
    place = gpd.read_file(city)
    place_simple = place.unary_union # disolving boundaries based on attributes

    # Use retain_all if you want to keep all disconnected subgraphs (e.g. when your places aren't adjacent)
    G = ox.graph_from_polygon(place_simple, network_type='drive', retain_all=True)
    G_projected = ox.project_graph(G)

    # save the shapefile to disk
    #name = os.path.basename("beijing.shp")).split(".")[0]  # make better place_names
    ox.save_graph_shapefile(G_projected, filename=name)

    area = ox.project_gdf(place).unary_union.area
                # Meters to Miles conversion
                edge_length = edge[0]['length'] * 0.00062137   
            except:
                edge_length = 0
            total_length = total_length + edge_length
        i = i + 1
    return(total_length)
    
print('Getting road network')

if os.path.isfile(root+'data/'+filename):
    # Read in the file
    G = ox.load_graphml(root+'data/'+filename)
else:
    # Create file since it doesn't exist
    ox.config(use_cache=True, log_console=False)
    G = ox.graph_from_place('Dutchess County, New York, USA', network_type='drive')
    ox.save_graphml(G, filename=filename)
    
stations = pd.read_excel(root+'Drive Distance Data.xlsx', 'Stations')
events = pd.read_excel(root+'Drive Distance Data.xlsx', 'Events')
length_data = list()

for index, row in stations.iterrows():
    station_name = row['Station']
    station = (row['Lat'], row['Lon'])
    station_node= ox.get_nearest_node(G, station)
    for index, e in events.iterrows():
        percent_complete = station_name + ' ' + str(int(index / (events.shape[0]-1) * 100)) + '% complete'
        print(percent_complete, end='')
        print('\r', end='')
#! /bin/bash/env python

import osmnx as ox, matplotlib.pyplot as plt
from descartes import PolygonPatch
from shapely.geometry import Polygon, MultiPolygon
import json
import os, sys
from places import *

data_path = os.path.dirname('../../data/vector/city_networks/')

ox.config(log_console=True, use_cache=True, data_folder=data_path)

def ensure_dir(file_path):
    directory = os.path.dirname(file_path)
    if not os.path.exists(directory):
        os.makedirs(directory)
        

for place in places:
    
    name = (place.replace(",","").replace(" ","")) # make better place_names
    print('working on: ', name)

    #make a geodataframe of the street network (outline) from openstreetmap place names
    # use retain_all if you want to keep all disconnected subgraphs (e.g. when your places aren't adjacent)
    G = ox.graph_from_place(place, network_type='drive', retain_all=True)
    G = ox.project_graph(G)
    
    #make a geodataframe of the shape (outline) from openstreetmap place names
    gdf = ox.gdf_from_place(place)
import osmnx as ox, matplotlib.pyplot as plt
from descartes import PolygonPatch
from shapely.geometry import Polygon, MultiPolygon
ox.config(log_console=True, use_cache=True)

#Beijing, Jakarta, London, Los Angeles, Manila, Mexico, New Delhi, New York,Sao Paulo, Seoul, Singapore, Tokyo, Lagos,
#Nairobi, Bangalore, Ho Chi Minh

place_names = ['Ho Chi Minh City, Vietnam',
               #'Beijing, China', 
               #'Jakarta, Indonesia',
               'London, UK',
               'Los Angeles, California, USA',
               'Manila, Philippines',
               #'Mexico City, Mexico',
               'New Delhi, India',
               'Sao Paulo, Brazil',
               'New York, New York, USA',
               'Seoul',
               'Singapore',
               #'Tokyo, Japan',
               #'Nairobi, Kenya',
               #'Bangalore, India'
              ]
              
# In this for-loop, we save all the shapefiles for the valid cities.
for city in place_names:  
    city_admin_20kmbuff = ox.gdf_from_place(city, gdf_name = 'global_cities', buffer_dist = 20000)
    fig, ax = ox.plot_shape(city_admin_20kmbuff)
    ox.save_gdf_shapefile(city_admin_20kmbuff, filename = city)
    
Beispiel #34
0
################################################################################
# Module: Data downloader
# saves the original network in the folder ../data/raw/{type}_{city}.{format}
# name of the city as key.
# developed by: Luis Natera @natera
# 			  [email protected]
# updated: 25/08/2020
################################################################################
import osmnx as ox
from .utils import *
from shapely.geometry import Polygon
import json

ox.config(data_folder='../data',
          cache_folder='../data/raw/cache',
          use_cache=True,
          log_console=True)


def create_polygon(bbox, city, save=True):
    """Create a polygon from a bounding box and save it to a file
	
	Arguments:
		bbox {list} -- list containing the coordinates of the bounding box [north, south, east, west]
	
	Keyword Arguments:
		save {bool} -- boolean to save or not the polygon to a file as a GeoJSON (default: {True})
	
	Returns:
		polygon -- GoeDataFrame with the geometry of the polygon to be used to download the data
	"""
Beispiel #35
0
# Web: https://github.com/gboeing/osmnx
################################################################################

import matplotlib as mpl
mpl.use('Agg') #use agg backend so you don't need a display on travis-ci

# remove the .temp folder if it already exists so we start fresh with tests
import os, shutil
if os.path.exists('.temp'):
    shutil.rmtree('.temp')

import osmnx as ox

# configure OSMnx
ox.config(log_console=True, log_file=True, use_cache=True,
          data_folder='.temp/data', logs_folder='.temp/logs',
          imgs_folder='.temp/imgs', cache_folder='.temp/cache')


def test_imports():

    # test all of OSMnx's module imports
    import ast
    import datetime
    import geopandas
    import hashlib
    import io
    import json
    import logging
    import math
    import matplotlib.cm