Exemple #1
0
def project_and_plot(g,
                     fig_height,
                     node_size,
                     point=None,
                     text='test',
                     color=None,
                     save=False):
    if point:  # Don't show plot until point is added to graph
        fig, ax = ox.plot_graph(g,
                                fig_height=fig_height,
                                node_size=node_size,
                                edge_color=color,
                                node_color=color,
                                show=False,
                                close=False)
        ax.scatter(x=point[1], y=point[0], c=['#00688B'])

        if save:
            ox.save_and_show(fig=fig,
                             ax=ax,
                             save=True,
                             show=True,
                             close=False,
                             filename=text,
                             file_format='svg',
                             dpi=None,
                             axis_off=True)
        else:
            plt.show()
    else:
        fig, ax = ox.plot_graph(g,
                                fig_height=fig_height,
                                node_size=node_size,
                                edge_color=color,
                                node_color=color)
Exemple #2
0
def plot(g, fig_height, node_size, point, text, color, save):
    # Don't show plot until point is added to graph
    if point:
        fig, ax = ox.plot_graph(g,
                                fig_height=fig_height,
                                node_size=node_size,
                                edge_color=color,
                                node_color=color,
                                show=False,
                                close=False)
        ax.scatter(x=point[1], y=point[0], c=[color])

        if save:
            ox.save_and_show(fig=fig,
                             ax=ax,
                             save=True,
                             show=False,
                             close=False,
                             filename=text,
                             file_format='jpg',
                             dpi=None,
                             axis_off=True)
        else:
            return plt
    else:
        fig, ax = ox.plot_graph(g,
                                fig_height=fig_height,
                                node_size=node_size,
                                edge_color=color,
                                node_color=color)
Exemple #3
0
def plot_graph_mls(G,
                   bbox=None,
                   fig_height=6,
                   fig_width=None,
                   margin=0.02,
                   axis_off=True,
                   equal_aspect=False,
                   bgcolor='w',
                   show=True,
                   save=False,
                   close=True,
                   file_format='png',
                   filename='temp',
                   dpi=300,
                   annotate=False,
                   node_color='#66ccff',
                   node_size=15,
                   node_alpha=1,
                   node_edgecolor='none',
                   node_zorder=1,
                   edge_color='#999999',
                   edge_linewidth=1,
                   edge_alpha=1,
                   use_geom=True):
    """
    Plot a networkx spatial graph. Modified to accept MultiLineString.

    Parameters
    ----------
    G : networkx multidigraph
    bbox : tuple
        bounding box as north,south,east,west - if None will calculate from
        spatial extents of data. if passing a bbox, you probably also want to
        pass margin=0 to constrain it.
    fig_height : int
        matplotlib figure height in inches
    fig_width : int
        matplotlib figure width in inches
    margin : float
        relative margin around the figure
    axis_off : bool
        if True turn off the matplotlib axis
    equal_aspect : bool
        if True set the axis aspect ratio equal
    bgcolor : string
        the background color of the figure and axis
    show : bool
        if True, show the figure
    save : bool
        if True, save the figure as an image file to disk
    close : bool
        close the figure (only if show equals False) to prevent display
    file_format : string
        the format of the file to save (e.g., 'jpg', 'png', 'svg')
    filename : string
        the name of the file if saving
    dpi : int
        the resolution of the image file if saving
    annotate : bool
        if True, annotate the nodes in the figure
    node_color : string
        the color of the nodes
    node_size : int
        the size of the nodes
    node_alpha : float
        the opacity of the nodes
    node_edgecolor : string
        the color of the node's marker's border
    node_zorder : int
        zorder to plot nodes, edges are always 2, so make node_zorder 1 to plot
        nodes beneath them or 3 to plot nodes atop them
    edge_color : string
        the color of the edges' lines
    edge_linewidth : float
        the width of the edges' lines
    edge_alpha : float
        the opacity of the edges' lines
    use_geom : bool
        if True, use the spatial geometry attribute of the edges to draw
        geographically accurate edges, rather than just lines straight from node
        to node

    Returns
    -------
    fig, ax : tuple
    """

    node_Xs = [float(x) for _, x in G.nodes(data='x')]
    node_Ys = [float(y) for _, y in G.nodes(data='y')]

    # get north, south, east, west values either from bbox parameter or from the
    # spatial extent of the edges' geometries
    if bbox is None:
        edges = graph_to_gdfs(G, nodes=False, fill_edge_geometry=True)
        west, south, east, north = edges.total_bounds
    else:
        north, south, east, west = bbox

    # if caller did not pass in a fig_width, calculate it proportionately from
    # the fig_height and bounding box aspect ratio
    bbox_aspect_ratio = (north - south) / (east - west)
    if fig_width is None:
        fig_width = fig_height / bbox_aspect_ratio

    # create the figure and axis
    fig, ax = plt.subplots(figsize=(fig_width, fig_height), facecolor=bgcolor)
    ax.set_facecolor(bgcolor)

    # draw the edges as lines from node to node
    # start_time = time.time()
    lines = []
    for u, v, data in G.edges(keys=False, data=True):
        if 'geometry' in data and use_geom:
            # if it has a geometry attribute (a list of line segments), add them
            # to the list of lines to plot
            if isinstance(data['geometry'], MultiLineString):
                lines += [
                    list(t) for t in mapping(data['geometry'])['coordinates']
                ]
            else:
                lines += [list(mapping(data['geometry'])['coordinates'])]

        else:
            # if it doesn't have a geometry attribute, the edge is a straight
            # line from node to node
            x1 = G.nodes[u]['x']
            y1 = G.nodes[u]['y']
            x2 = G.nodes[v]['x']
            y2 = G.nodes[v]['y']
            line = [(x1, y1), (x2, y2)]
            lines.append(line)

    # add the lines to the axis as a linecollection
    lc = LineCollection(lines,
                        colors=edge_color,
                        linewidths=edge_linewidth,
                        alpha=edge_alpha,
                        zorder=2)
    ax.add_collection(lc)

    # scatter plot the nodes
    ax.scatter(node_Xs,
               node_Ys,
               s=node_size,
               c=node_color,
               alpha=node_alpha,
               edgecolor=node_edgecolor,
               zorder=node_zorder)

    # set the extent of the figure
    margin_ns = (north - south) * margin
    margin_ew = (east - west) * margin
    ax.set_ylim((south - margin_ns, north + margin_ns))
    ax.set_xlim((west - margin_ew, east + margin_ew))

    # configure axis appearance
    xaxis = ax.get_xaxis()
    yaxis = ax.get_yaxis()

    xaxis.get_major_formatter().set_useOffset(False)
    yaxis.get_major_formatter().set_useOffset(False)

    # if axis_off, turn off the axis display set the margins to zero and point
    # the ticks in so there's no space around the plot
    if axis_off:
        ax.axis('off')
        ax.margins(0)
        ax.tick_params(which='both', direction='in')
        xaxis.set_visible(False)
        yaxis.set_visible(False)
        fig.canvas.draw()

    if equal_aspect:
        # make everything square
        ax.set_aspect('equal')
        fig.canvas.draw()

    # annotate the axis with node IDs if annotate=True
    if annotate:
        for node, data in G.nodes(data=True):
            ax.annotate(node, xy=(data['x'], data['y']))

    # save and show the figure as specified
    fig, ax = save_and_show(fig, ax, save, show, close, filename, file_format,
                            dpi, axis_off)
    return fig, ax
Exemple #4
0
def double_expansion_one_queue_callback(g,
                                        origin,
                                        dest,
                                        bss,
                                        edge_status,
                                        edge_labels,
                                        i,
                                        prefix="double_expansion_one_queue"):
    # bbox = [north, south, east, west]
    o_lat, o_lon = g.nodes[origin]['y'], g.nodes[origin]['x']
    d_lat, d_lon = g.nodes[dest]['y'], g.nodes[dest]['x']
    lat_diff = abs(o_lat - d_lat)
    lon_diff = abs(o_lon - d_lon)

    coeff = lat_diff / lon_diff

    # bbox = [north, south, east, west]
    bbox = (max(o_lat, d_lat) + 0.8 * lat_diff / coeff, min(o_lat, d_lat) -
            0.8 * lat_diff / coeff, max(o_lon, d_lon) + 0.8 * lon_diff,
            min(o_lon, d_lon) - 0.8 * lon_diff)

    dpi = 200
    fig, ax = osmnx.plot_graph(g,
                               bbox=bbox,
                               fig_height=6,
                               margin=0.02,
                               bgcolor='w',
                               axis_off=True,
                               show=False,
                               save=False,
                               close=False,
                               file_format='png',
                               filename='temp',
                               dpi=dpi,
                               annotate=False,
                               node_color='#999999',
                               node_size=0,
                               node_alpha=0,
                               node_edgecolor='none',
                               node_zorder=1,
                               edge_color='#999999',
                               edge_linewidth=1,
                               edge_alpha=1,
                               use_geom=True)

    origin_destination_lats = (o_lat, d_lat)
    origin_destination_lons = (o_lon, d_lon)

    ax.scatter(origin_destination_lons,
               origin_destination_lats,
               s=50,
               c='#c942ff',
               alpha=1,
               edgecolor='none',
               zorder=6)

    bss_lats = []
    bss_lons = []

    for node in bss:
        bss_lats.append(g.nodes[node]['y'])
        bss_lons.append(g.nodes[node]['x'])

    ax.scatter(bss_lons,
               bss_lats,
               s=8,
               c='#0046ff',
               alpha=1,
               edgecolor='none',
               zorder=6)

    walking_nodes = set([
        edge_labels[status.edge_label_index].end_node
        for _, status in edge_status.items()
        if edge_labels[status.edge_label_index].edge_id.mode.value == 0
    ])

    walking_lats = []
    walking_lons = []

    for node in walking_nodes:
        walking_lats.append(g.nodes[node]['y'])
        walking_lons.append(g.nodes[node]['x'])

    ax.scatter(walking_lons,
               walking_lats,
               s=7,
               c='#ff1b00',
               alpha=0.4,
               edgecolor='none',
               zorder=3)

    bike_nodes = set([
        edge_labels[status.edge_label_index].end_node
        for _, status in edge_status.items()
        if edge_labels[status.edge_label_index].edge_id.mode.value == 1
    ])

    bike_lats = []
    bike_lons = []

    for node in bike_nodes:
        bike_lats.append(g.nodes[node]['y'])
        bike_lons.append(g.nodes[node]['x'])

    ax.scatter(bike_lons,
               bike_lats,
               s=6,
               c='#22ff36',
               alpha=0.4,
               edgecolor='none',
               zorder=4)

    bss_lats = []
    bss_lons = []

    for node in bss:
        bss_lats.append(g.nodes[node]['y'])
        bss_lons.append(g.nodes[node]['x'])

    ax.scatter(bss_lons,
               bss_lats,
               s=8,
               c='#0046ff',
               alpha=1,
               edgecolor='none',
               zorder=6)

    filename = '{}_{}'.format(prefix, i)
    show = False
    save = True
    axis_off = True
    file_format = 'png'
    close = True
    osmnx.save_and_show(fig, ax, save, show, close, filename, file_format, dpi,
                        axis_off)
Exemple #5
0
def double_expansion_isochrone_callback(g,
                                        origin,
                                        dest_nodes,
                                        bss,
                                        edges_status_walking,
                                        edge_labels_walking,
                                        edges_status_bike,
                                        edge_labels_bike,
                                        i,
                                        prefix="double_expansion_isochrone"):

    o_lat, o_lon = g.nodes[origin]['y'], g.nodes[origin]['x']

    # bbox = [north, south, east, west]
    bbox = (o_lat + 0.042, o_lat - 0.042, o_lon + 0.06, o_lon - 0.06)

    dpi = 200
    fig, ax = osmnx.plot_graph(g,
                               bbox=bbox,
                               fig_height=6,
                               margin=0.02,
                               bgcolor='w',
                               axis_off=True,
                               show=False,
                               save=False,
                               close=False,
                               file_format='png',
                               filename='temp',
                               dpi=dpi,
                               annotate=False,
                               node_color='#999999',
                               node_size=0,
                               node_alpha=0,
                               node_edgecolor='none',
                               node_zorder=1,
                               edge_color='#999999',
                               edge_linewidth=1,
                               edge_alpha=1,
                               use_geom=True)

    ax.scatter((o_lon), (o_lat),
               s=50,
               c='#c942ff',
               alpha=1,
               edgecolor='none',
               zorder=6)

    walking_first_nodes = set([
        edge_labels_walking[status.edge_label_index].end_node
        for _, status in edges_status_walking.items()
        if edge_labels_walking[status.edge_label_index].can_change_mode
    ])

    walking_first_lats = []
    walking_first_lons = []

    for node in walking_first_nodes:
        walking_first_lats.append(g.nodes[node]['y'])
        walking_first_lons.append(g.nodes[node]['x'])

    ax.scatter(walking_first_lons,
               walking_first_lats,
               s=7,
               c='#ff1b00',
               alpha=0.3,
               edgecolor='none',
               zorder=3)

    walking_second_nodes = set([
        edge_labels_walking[status.edge_label_index].end_node
        for _, status in edges_status_walking.items()
        if not edge_labels_walking[status.edge_label_index].can_change_mode
    ])

    walking_second_lats = []
    walking_second_lons = []

    for node in walking_second_nodes:
        walking_second_lats.append(g.nodes[node]['y'])
        walking_second_lons.append(g.nodes[node]['x'])

    ax.scatter(walking_second_lons,
               walking_second_lats,
               s=5,
               c='#22ff36',
               alpha=0.7,
               edgecolor='none',
               zorder=5)

    bike_nodes = set([
        edge_labels_bike[status.edge_label_index].end_node
        for _, status in edges_status_bike.items()
    ])

    bike_lats = []
    bike_lons = []

    for node in bike_nodes:
        bike_lats.append(g.nodes[node]['y'])
        bike_lons.append(g.nodes[node]['x'])

    ax.scatter(bike_lons,
               bike_lats,
               s=6,
               c='#cb6aff',
               alpha=0.2,
               edgecolor='none',
               zorder=4)

    bss_lats = []
    bss_lons = []

    for node in bss:
        bss_lats.append(g.nodes[node]['y'])
        bss_lons.append(g.nodes[node]['x'])

    ax.scatter(bss_lons,
               bss_lats,
               s=8,
               c='#0046ff',
               alpha=1,
               edgecolor='none',
               zorder=6)

    filename = '{}_{}'.format(prefix, i)
    show = False
    save = True
    axis_off = True
    file_format = 'png'
    close = True
    osmnx.save_and_show(fig, ax, save, show, close, filename, file_format, dpi,
                        axis_off)
Exemple #6
0
def double_expansion_call_back(g,
                               origin,
                               dest,
                               bss,
                               edge_status_w_f,
                               edge_labels_w_f,
                               edge_status_w_b,
                               edge_labels_w_b,
                               edge_status_b_f,
                               edge_labels_b_f,
                               edge_status_b_b,
                               edge_labels_b_b,
                               i,
                               prefix='double_expansion'):
    # bbox = [north, south, east, west]
    o_lat, o_lon = g.nodes[origin]['y'], g.nodes[origin]['x']
    d_lat, d_lon = g.nodes[dest]['y'], g.nodes[dest]['x']
    lat_diff = abs(o_lat - d_lat)
    lon_diff = abs(o_lon - d_lon)

    coeff = lat_diff / lon_diff

    # bbox = [north, south, east, west]
    bbox = (max(o_lat, d_lat) + 0.2 * lat_diff / coeff, min(o_lat, d_lat) -
            0.2 * lat_diff / coeff, max(o_lon, d_lon) + 0.2 * lon_diff,
            min(o_lon, d_lon) - 0.2 * lon_diff)

    dpi = 200
    fig, ax = osmnx.plot_graph(g,
                               bbox=bbox,
                               fig_height=6,
                               margin=0.02,
                               bgcolor='w',
                               axis_off=True,
                               show=False,
                               save=False,
                               close=False,
                               file_format='png',
                               filename='temp',
                               dpi=dpi,
                               annotate=False,
                               node_color='#999999',
                               node_size=0,
                               node_alpha=0,
                               node_edgecolor='none',
                               node_zorder=1,
                               edge_color='#999999',
                               edge_linewidth=1,
                               edge_alpha=1,
                               use_geom=True)

    origin_destination_lats = (o_lat, d_lat)
    origin_destination_lons = (o_lon, d_lon)

    ax.scatter(origin_destination_lons,
               origin_destination_lats,
               s=50,
               c='#c942ff',
               alpha=1,
               edgecolor='none',
               zorder=6)

    w_nodes = set()
    for _, status in edge_status_w_f.items():
        node = edge_labels_w_f[status.edge_label_index].end_node
        w_nodes.add(node)
    for _, status in edge_status_w_b.items():
        node = edge_labels_w_b[status.edge_label_index].end_node
        w_nodes.add(node)

    w_lats = []
    w_lons = []

    for node in w_nodes:
        w_lats.append(g.nodes[node]['y'])
        w_lons.append(g.nodes[node]['x'])

    ax.scatter(w_lons,
               w_lats,
               s=7,
               c='#ff1b00',
               alpha=0.5,
               edgecolor='none',
               zorder=3)

    b_nodes = set()
    for _, status in edge_status_b_f.items():
        node = edge_labels_b_f[status.edge_label_index].end_node
        b_nodes.add(node)
    for _, status in edge_status_b_b.items():
        node = edge_labels_b_b[status.edge_label_index].end_node
        b_nodes.add(node)

    b_lats = []
    b_lons = []

    for node in b_nodes:
        b_lats.append(g.nodes[node]['y'])
        b_lons.append(g.nodes[node]['x'])

    ax.scatter(b_lons,
               b_lats,
               s=5,
               c='#42ff00',
               alpha=0.5,
               edgecolor='none',
               zorder=4)

    bss_lats = []
    bss_lons = []

    for node in bss:
        bss_lats.append(g.nodes[node]['y'])
        bss_lons.append(g.nodes[node]['x'])

    ax.scatter(bss_lons,
               bss_lats,
               s=10,
               c='#0046ff',
               alpha=1,
               edgecolor='none',
               zorder=5)

    filename = '{}_{}'.format(prefix, i)
    show = False
    save = True
    axis_off = True
    file_format = 'png'
    close = True

    osmnx.save_and_show(fig, ax, save, show, close, filename, file_format, dpi,
                        axis_off)
Exemple #7
0
        print(polygon)
        patch = PolygonPatch(polygon,
                             fc=color,
                             ec='#555555',
                             linewidth=0,
                             alpha=0.9)
        ax.add_patch(patch)

    # adjust the axis margins and limits around the image and make axes
    # equal-aspect
    margin = 0.02
    axis_off = True
    west, south, east, north = placedf.unary_union.bounds
    margin_ns = (north - south) * margin
    margin_ew = (east - west) * margin
    ax.set_ylim((south - margin_ns, north + margin_ns))
    ax.set_xlim((west - margin_ew, east + margin_ew))
    ax.set_aspect(aspect='equal', adjustable='box')
    if axis_off:
        ax.axis('off')
    # plt.show()

    ox.save_and_show(fig,
                     ax,
                     save=True,
                     filename='city_point_pic/' + str(i),
                     show=False,
                     close=True,
                     file_format='png',
                     dpi=300,
                     axis_off=True)