From 9d0a9781d19d4c34e2cbeb6eb8e1006033158452 Mon Sep 17 00:00:00 2001 From: Geoff Boeing Date: Wed, 18 Dec 2024 15:30:59 -0800 Subject: [PATCH] update examples --- notebooks/09-example-figure-ground.ipynb | 4 +- notebooks/10-building-footprints.ipynb | 18 +++---- notebooks/11-interactive-web-mapping.ipynb | 32 ++++++------ .../12-node-elevations-edge-grades.ipynb | 52 ++++++++++--------- notebooks/13-isolines-isochrones.ipynb | 20 +++---- notebooks/14-osmnx-to-igraph.ipynb | 4 +- notebooks/15-advanced-plotting.ipynb | 48 ++++++++--------- .../16-download-osm-geospatial-features.ipynb | 47 +++++++++++++++-- .../17-street-network-orientations.ipynb | 10 ++-- 9 files changed, 138 insertions(+), 97 deletions(-) diff --git a/notebooks/09-example-figure-ground.ipynb b/notebooks/09-example-figure-ground.ipynb index dfee892..d339489 100644 --- a/notebooks/09-example-figure-ground.ipynb +++ b/notebooks/09-example-figure-ground.ipynb @@ -345,7 +345,7 @@ "point = (37.793897, -122.402189)\n", "fp = \"./images/sf_custom.png\"\n", "G = ox.graph.graph_from_point(point, dist=1000, network_type=\"all\", truncate_by_edge=True)\n", - "fig, ax = ox.plot_figure_ground(\n", + "fig, ax = ox.plot.plot_figure_ground(\n", " G=G,\n", " filepath=fp,\n", " street_widths=street_widths,\n", @@ -383,7 +383,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.9" + "version": "3.12.8" } }, "nbformat": 4, diff --git a/notebooks/10-building-footprints.ipynb b/notebooks/10-building-footprints.ipynb index 1a16032..7fbc3a7 100644 --- a/notebooks/10-building-footprints.ipynb +++ b/notebooks/10-building-footprints.ipynb @@ -56,10 +56,10 @@ "metadata": {}, "outputs": [], "source": [ - "gdf = ox.features_from_place(\"Piedmont, California, USA\", tags)\n", + "gdf = ox.features.features_from_place(\"Piedmont, California, USA\", tags)\n", "gdf_proj = ox.projection.project_gdf(gdf)\n", "fp = f\"./{img_folder}/piedmont_bldgs.{extension}\"\n", - "fig, ax = ox.plot_footprints(gdf_proj, filepath=fp, dpi=400, save=True, show=False, close=True)\n", + "fig, ax = ox.plot.plot_footprints(gdf_proj, filepath=fp, dpi=400, save=True, show=False, close=True)\n", "Image(fp, height=size, width=size)" ] }, @@ -109,7 +109,7 @@ "outputs": [], "source": [ "# get the total area within Piedmont's admin boundary in sq meters\n", - "place = ox.geocode_to_gdf(\"Piedmont, California, USA\")\n", + "place = ox.geocoder.geocode_to_gdf(\"Piedmont, California, USA\")\n", "place_proj = ox.projection.project_gdf(place)\n", "place_proj.area.iloc[0]" ] @@ -139,11 +139,11 @@ "source": [ "point = (48.873446, 2.294255)\n", "dist = 612\n", - "gdf = ox.features_from_point(point, tags, dist=dist)\n", + "gdf = ox.features.features_from_point(point, tags, dist=dist)\n", "gdf_proj = ox.projection.project_gdf(gdf)\n", "bbox = ox.utils_geo.bbox_from_point(point=point, dist=dist, project_utm=True)\n", "fp = f\"./{img_folder}/paris_bldgs.{extension}\"\n", - "fig, ax = ox.plot_footprints(\n", + "fig, ax = ox.plot.plot_footprints(\n", " gdf_proj,\n", " bbox=bbox,\n", " color=\"w\",\n", @@ -188,8 +188,8 @@ " G = ox.graph.graph_from_point(\n", " point, dist=dist, network_type=network_type, truncate_by_edge=True\n", " )\n", - " gdf = ox.features_from_point(point, tags, dist=dist)\n", - " fig, ax = ox.plot_figure_ground(\n", + " gdf = ox.features.features_from_point(point, tags, dist=dist)\n", + " fig, ax = ox.plot.plot_figure_ground(\n", " G=G,\n", " dist=dist,\n", " default_width=default_width,\n", @@ -198,7 +198,7 @@ " show=False,\n", " close=True,\n", " )\n", - " fig, ax = ox.plot_footprints(\n", + " fig, ax = ox.plot.plot_footprints(\n", " gdf, ax=ax, filepath=fp, dpi=dpi, save=True, show=False, close=True\n", " )\n", "\n", @@ -276,7 +276,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.3" + "version": "3.12.8" } }, "nbformat": 4, diff --git a/notebooks/11-interactive-web-mapping.ipynb b/notebooks/11-interactive-web-mapping.ipynb index a88df0e..2e68f28 100644 --- a/notebooks/11-interactive-web-mapping.ipynb +++ b/notebooks/11-interactive-web-mapping.ipynb @@ -40,10 +40,10 @@ "source": [ "# download a street network then solve a shortest-path route on it\n", "weight = \"length\"\n", - "G = ox.graph_from_place(\"Piedmont, CA, USA\", network_type=\"drive\")\n", + "G = ox.graph.graph_from_place(\"Piedmont, CA, USA\", network_type=\"drive\")\n", "orig = list(G.nodes)[0]\n", "dest = list(G.nodes)[-1]\n", - "route = ox.shortest_path(G, orig, dest, weight=weight)" + "route = ox.routing.shortest_path(G, orig, dest, weight=weight)" ] }, { @@ -62,7 +62,7 @@ "outputs": [], "source": [ "# explore graph edges interactively, with a simple one-liner\n", - "ox.graph_to_gdfs(G, nodes=False).explore()" + "ox.convert.graph_to_gdfs(G, nodes=False).explore()" ] }, { @@ -81,7 +81,7 @@ "outputs": [], "source": [ "# explore graph nodes interactively, with different basemap tiles\n", - "nodes = ox.graph_to_gdfs(G, edges=False)\n", + "nodes = ox.convert.graph_to_gdfs(G, edges=False)\n", "nodes.explore(tiles=\"cartodbpositron\", marker_kwds={\"radius\": 8})" ] }, @@ -94,7 +94,7 @@ "outputs": [], "source": [ "# explore nodes and edges together in a single map\n", - "nodes, edges = ox.graph_to_gdfs(G)\n", + "nodes, edges = ox.convert.graph_to_gdfs(G)\n", "m = edges.explore(color=\"skyblue\", tiles=\"cartodbdarkmatter\")\n", "nodes.explore(m=m, color=\"pink\", marker_kwds={\"radius\": 6})" ] @@ -124,7 +124,7 @@ "source": [ "# explore graph nodes interactively, colored by betweenness centrality\n", "nx.set_node_attributes(G, nx.betweenness_centrality(G, weight=\"length\"), name=\"bc\")\n", - "nodes = ox.graph_to_gdfs(G, edges=False)\n", + "nodes = ox.convert.graph_to_gdfs(G, edges=False)\n", "nodes.explore(tiles=\"cartodbdarkmatter\", column=\"bc\", marker_kwds={\"radius\": 8})" ] }, @@ -169,7 +169,7 @@ "outputs": [], "source": [ "# or explore multiple routes together in a single map\n", - "routes = ox.k_shortest_paths(G, orig, dest, k=200, weight=weight)\n", + "routes = ox.routing.k_shortest_paths(G, orig, dest, k=200, weight=weight)\n", "gdfs = (ox.routing.route_to_gdf(G, route, weight=weight) for route in routes)\n", "m = edges.explore(color=\"#222222\", tiles=\"cartodbdarkmatter\")\n", "for route_edges in gdfs:\n", @@ -203,11 +203,11 @@ "outputs": [], "source": [ "# explore a city's bus stops and rail transit interactively\n", - "bus = ox.features_from_place(place, tags={\"highway\": \"bus_stop\"})\n", + "bus = ox.features.features_from_place(place, tags={\"highway\": \"bus_stop\"})\n", "m = bus.explore(tiles=tiles, color=\"red\", tooltip=\"name\", marker_kwds=mk)\n", - "rail = ox.features_from_place(place, tags={\"railway\": \"light_rail\"})\n", + "rail = ox.features.features_from_place(place, tags={\"railway\": \"light_rail\"})\n", "m = rail.explore(m=m, tiles=tiles, color=\"yellow\", tooltip=\"name\")\n", - "stations = ox.features_from_place(place, tags={\"railway\": \"station\"})\n", + "stations = ox.features.features_from_place(place, tags={\"railway\": \"station\"})\n", "stations.explore(m=m, tiles=tiles, color=\"yellow\", tooltip=\"name\", marker_kwds=mk)" ] }, @@ -218,7 +218,7 @@ "outputs": [], "source": [ "# explore a city's parks interactively\n", - "parks = ox.features_from_place(place, tags={\"leisure\": \"park\"})\n", + "parks = ox.features.features_from_place(place, tags={\"leisure\": \"park\"})\n", "parks.explore(tiles=tiles, color=\"lime\", tooltip=\"name\")" ] }, @@ -229,7 +229,7 @@ "outputs": [], "source": [ "# explore a neighborhood's buildings interactively\n", - "gdf = ox.features_from_place(\"SoHo, New York, NY\", tags={\"building\": True})\n", + "gdf = ox.features.features_from_place(\"SoHo, New York, NY\", tags={\"building\": True})\n", "cols = [\"height\", \"addr:housenumber\", \"addr:street\", \"addr:postcode\"]\n", "gdf.explore(tiles=\"cartodbdarkmatter\", tooltip=cols)" ] @@ -243,10 +243,10 @@ "# explore a neighborhood's buildings + street network interactively\n", "place = \"SoHo, New York, NY\"\n", "cols = [\"height\", \"addr:housenumber\", \"addr:street\", \"addr:postcode\"]\n", - "G = ox.graph_from_place(place, network_type=\"drive\", truncate_by_edge=True)\n", - "gdf = ox.features_from_place(place, tags={\"building\": True})\n", + "G = ox.graph.graph_from_place(place, network_type=\"drive\", truncate_by_edge=True)\n", + "gdf = ox.features.features_from_place(place, tags={\"building\": True})\n", "m = gdf.explore(tiles=tiles, tooltip=cols)\n", - "ox.graph_to_gdfs(G, nodes=False).explore(m=m, color=\"yellow\")" + "ox.convert.graph_to_gdfs(G, nodes=False).explore(m=m, color=\"yellow\")" ] }, { @@ -286,7 +286,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.3" + "version": "3.12.8" } }, "nbformat": 4, diff --git a/notebooks/12-node-elevations-edge-grades.ipynb b/notebooks/12-node-elevations-edge-grades.ipynb index 3671b49..9c428e1 100644 --- a/notebooks/12-node-elevations-edge-grades.ipynb +++ b/notebooks/12-node-elevations-edge-grades.ipynb @@ -47,7 +47,7 @@ "outputs": [], "source": [ "address = \"600 Montgomery St, San Francisco, California, USA\"\n", - "G = ox.graph_from_address(address=address, dist=500, dist_type=\"bbox\", network_type=\"bike\")" + "G = ox.graph.graph_from_address(address=address, dist=500, dist_type=\"bbox\", network_type=\"bike\")" ] }, { @@ -91,7 +91,7 @@ "source": [ "## Elevation from Google Maps Elevation API\n", "\n", - "You will need a Google Maps Elevation [API key](https://developers.google.com/maps/documentation/elevation/start). Consider your API usage limits. OSMnx rounds coordinates to 5 decimal places (approx 1 meter) to fit 350 locations in a batch. Note that there is some spatial inaccuracy given Google's dataset's resolution. For example, in San Francisco (where the resolution is 19 meters) a couple of edges in hilly parks have a 50+ percent grade because Google assigns one of their nodes the elevation of a hill adjacent to the street." + "You will need a Google Maps Elevation [API key](https://developers.google.com/maps/documentation/elevation/start). Remember to track your API usage and costs. If you don't want to set up a Google Maps API key, you could use a free alternative web service that provides the same interface, such as [Open Topo Data](https://www.opentopodata.org/) which doesn't require an API key. Note that there is some spatial inaccuracy in elevation data resolution. For example, in San Francisco (where Google's resolution is ~19 meters) a couple of edges in hilly parks have a 50+ percent grade because Google assigns one of their nodes the elevation of a hill adjacent to the street." ] }, { @@ -100,11 +100,15 @@ "metadata": {}, "outputs": [], "source": [ - "# replace this with your own API key!\n", - "try:\n", - " from keys import google_elevation_api_key\n", - "except ImportError:\n", - " sys.exit() # you need an API key to proceed" + "# add elevation to each of the nodes, using the Open Topo Data, then calculate edge grades\n", + "G = ox.graph.graph_from_place(\"Piedmont, California, USA\", network_type=\"drive\")\n", + "original_elevation_url = ox.settings.elevation_url_template\n", + "ox.settings.elevation_url_template = (\n", + " \"https://api.opentopodata.org/v1/aster30m?locations={locations}\"\n", + ")\n", + "G = ox.elevation.add_node_elevations_google(G, batch_size=100, pause=1)\n", + "G = ox.elevation.add_edge_grades(G)\n", + "ox.settings.elevation_url_template = original_elevation_url" ] }, { @@ -113,19 +117,19 @@ "metadata": {}, "outputs": [], "source": [ + "# or use the Google Maps Elevation API\n", + "# replace this with your own API key!\n", + "try:\n", + " from keys import google_elevation_api_key\n", + "except ImportError:\n", + " sys.exit() # you need an API key to proceed\n", + "\n", "# get the street network for san francisco\n", "place = \"San Francisco\"\n", "place_query = {\"city\": \"San Francisco\", \"state\": \"California\", \"country\": \"USA\"}\n", - "G = ox.graph_from_place(place_query, network_type=\"drive\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# add elevation to each of the nodes, using the google elevation API, then calculate edge grades\n", + "G = ox.graph_from_place(place_query, network_type=\"drive\")\n", + "\n", + "# add elevation to each of the nodes then calculate edge grades\n", "G = ox.elevation.add_node_elevations_google(G, api_key=google_elevation_api_key)\n", "G = ox.elevation.add_edge_grades(G)" ] @@ -180,7 +184,7 @@ "source": [ "# get one color for each node, by elevation, then plot the network\n", "nc = ox.plot.get_node_colors_by_attr(G, \"elevation\", cmap=\"plasma\")\n", - "fig, ax = ox.plot_graph(G, node_color=nc, node_size=5, edge_color=\"#333333\", bgcolor=\"k\")" + "fig, ax = ox.plot.plot_graph(G, node_color=nc, node_size=5, edge_color=\"#333333\", bgcolor=\"k\")" ] }, { @@ -200,7 +204,7 @@ "source": [ "# get a color for each edge, by grade, then plot the network\n", "ec = ox.plot.get_edge_colors_by_attr(G, \"grade_abs\", cmap=\"plasma\", num_bins=5, equal_size=True)\n", - "fig, ax = ox.plot_graph(G, edge_color=ec, edge_linewidth=0.5, node_size=0, bgcolor=\"k\")" + "fig, ax = ox.plot.plot_graph(G, edge_color=ec, edge_linewidth=0.5, node_size=0, bgcolor=\"k\")" ] }, { @@ -258,8 +262,8 @@ "metadata": {}, "outputs": [], "source": [ - "route_by_length = ox.shortest_path(G, origin, destination, weight=\"length\")\n", - "fig, ax = ox.plot_graph_route(G, route_by_length, bbox=bbox, node_size=0)" + "route_by_length = ox.routing.shortest_path(G, origin, destination, weight=\"length\")\n", + "fig, ax = ox.plot.plot_graph_route(G, route_by_length, bbox=bbox, node_size=0)" ] }, { @@ -275,8 +279,8 @@ "metadata": {}, "outputs": [], "source": [ - "route_by_impedance = ox.shortest_path(G, origin, destination, weight=\"impedance\")\n", - "fig, ax = ox.plot_graph_route(G, route_by_impedance, bbox=bbox, node_size=0)" + "route_by_impedance = ox.routing.shortest_path(G, origin, destination, weight=\"impedance\")\n", + "fig, ax = ox.plot.plot_graph_route(G, route_by_impedance, bbox=bbox, node_size=0)" ] }, { @@ -358,7 +362,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.3" + "version": "3.12.8" } }, "nbformat": 4, diff --git a/notebooks/13-isolines-isochrones.ipynb b/notebooks/13-isolines-isochrones.ipynb index 87d0a00..ff62880 100644 --- a/notebooks/13-isolines-isochrones.ipynb +++ b/notebooks/13-isolines-isochrones.ipynb @@ -60,7 +60,7 @@ "outputs": [], "source": [ "# download the street network\n", - "G = ox.graph_from_place(place, network_type=network_type)" + "G = ox.graph.graph_from_place(place, network_type=network_type)" ] }, { @@ -70,10 +70,10 @@ "outputs": [], "source": [ "# find the centermost node and then project the graph to UTM\n", - "gdf_nodes = ox.graph_to_gdfs(G, edges=False)\n", - "x, y = gdf_nodes[\"geometry\"].unary_union.centroid.xy\n", + "gdf_nodes = ox.convert.graph_to_gdfs(G, edges=False)\n", + "x, y = gdf_nodes[\"geometry\"].union_all().centroid.xy\n", "center_node = ox.distance.nearest_nodes(G, x[0], y[0])\n", - "G = ox.project_graph(G)" + "G = ox.projection.project_graph(G)" ] }, { @@ -121,7 +121,7 @@ " node_colors[node] = color\n", "nc = [node_colors[node] if node in node_colors else \"none\" for node in G.nodes()]\n", "ns = [15 if node in node_colors else 0 for node in G.nodes()]\n", - "fig, ax = ox.plot_graph(\n", + "fig, ax = ox.plot.plot_graph(\n", " G,\n", " node_color=nc,\n", " node_size=ns,\n", @@ -151,7 +151,7 @@ "for trip_time in sorted(trip_times, reverse=True):\n", " subgraph = nx.ego_graph(G, center_node, radius=trip_time, distance=\"time\")\n", " node_points = [Point((data[\"x\"], data[\"y\"])) for node, data in subgraph.nodes(data=True)]\n", - " bounding_poly = gpd.GeoSeries(node_points).unary_union.convex_hull\n", + " bounding_poly = gpd.GeoSeries(node_points).union_all().convex_hull\n", " isochrone_polys.append(bounding_poly)\n", "gdf = gpd.GeoDataFrame(geometry=isochrone_polys)" ] @@ -163,7 +163,7 @@ "outputs": [], "source": [ "# plot the network then add isochrones as colored polygon patches\n", - "fig, ax = ox.plot_graph(\n", + "fig, ax = ox.plot.plot_graph(\n", " G, show=False, close=False, edge_color=\"#999999\", edge_alpha=0.2, node_size=0\n", ")\n", "gdf.plot(ax=ax, color=iso_colors, ec=\"none\", alpha=0.6, zorder=-1)\n", @@ -202,7 +202,7 @@ " n = nodes_gdf.buffer(node_buff).geometry\n", " e = gpd.GeoSeries(edge_lines).buffer(edge_buff).geometry\n", " all_gs = list(n) + list(e)\n", - " new_iso = gpd.GeoSeries(all_gs).unary_union\n", + " new_iso = gpd.GeoSeries(all_gs).union_all()\n", "\n", " # try to fill in surrounded areas so shapes will appear solid and\n", " # blocks without white space inside them\n", @@ -217,7 +217,7 @@ "gdf = gpd.GeoDataFrame(geometry=isochrone_polys)\n", "\n", "# plot the network then add isochrones as colored polygon patches\n", - "fig, ax = ox.plot_graph(\n", + "fig, ax = ox.plot.plot_graph(\n", " G, show=False, close=False, edge_color=\"#999999\", edge_alpha=0.2, node_size=0\n", ")\n", "gdf.plot(ax=ax, color=iso_colors, ec=\"none\", alpha=0.6, zorder=-1)\n", @@ -248,7 +248,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.3" + "version": "3.12.8" } }, "nbformat": 4, diff --git a/notebooks/14-osmnx-to-igraph.ipynb b/notebooks/14-osmnx-to-igraph.ipynb index 27a483d..d2be5ab 100644 --- a/notebooks/14-osmnx-to-igraph.ipynb +++ b/notebooks/14-osmnx-to-igraph.ipynb @@ -51,7 +51,7 @@ "outputs": [], "source": [ "# create networkx graph\n", - "G_nx = ox.graph_from_place(\"Piedmont, CA, USA\", network_type=\"drive\")\n", + "G_nx = ox.graph.graph_from_place(\"Piedmont, CA, USA\", network_type=\"drive\")\n", "osmids = list(G_nx.nodes)\n", "G_nx = nx.relabel.convert_node_labels_to_integers(G_nx)\n", "\n", @@ -223,7 +223,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.3" + "version": "3.12.8" } }, "nbformat": 4, diff --git a/notebooks/15-advanced-plotting.ipynb b/notebooks/15-advanced-plotting.ipynb index d1fb7cb..d244177 100644 --- a/notebooks/15-advanced-plotting.ipynb +++ b/notebooks/15-advanced-plotting.ipynb @@ -33,7 +33,7 @@ "outputs": [], "source": [ "place = \"Piedmont, California, USA\"\n", - "G = ox.graph_from_place(place, network_type=\"drive\")" + "G = ox.graph.graph_from_place(place, network_type=\"drive\")" ] }, { @@ -63,7 +63,7 @@ "source": [ "# get node colors by linearly mapping an attribute's values to a colormap\n", "nc = ox.plot.get_node_colors_by_attr(G, attr=\"y\", cmap=\"plasma\")\n", - "fig, ax = ox.plot_graph(G, node_color=nc, edge_linewidth=0.3)" + "fig, ax = ox.plot.plot_graph(G, node_color=nc, edge_linewidth=0.3)" ] }, { @@ -80,7 +80,7 @@ "ec = ox.plot.get_edge_colors_by_attr(G, attr=\"length\")\n", "\n", "# plot the graph with colored edges\n", - "fig, ax = ox.plot_graph(G, node_size=5, edge_color=ec, bgcolor=\"k\")" + "fig, ax = ox.plot.plot_graph(G, node_size=5, edge_color=ec, bgcolor=\"k\")" ] }, { @@ -98,7 +98,7 @@ "metadata": {}, "outputs": [], "source": [ - "fig, ax = ox.plot_graph(\n", + "fig, ax = ox.plot.plot_graph(\n", " G,\n", " ax=None, # optionally draw on pre-existing axis\n", " figsize=(8, 8), # figure size to create if ax is None\n", @@ -135,10 +135,10 @@ "metadata": {}, "outputs": [], "source": [ - "Gc = ox.consolidate_intersections(ox.project_graph(G), dead_ends=True)\n", - "c = ox.graph_to_gdfs(G, edges=False).unary_union.centroid\n", + "Gc = ox.simplification.consolidate_intersections(ox.projection.project_graph(G), dead_ends=True)\n", + "c = ox.convert.graph_to_gdfs(G, edges=False).union_all().centroid\n", "bbox = ox.utils_geo.bbox_from_point(point=(c.y, c.x), dist=200, project_utm=True)\n", - "fig, ax = ox.plot_graph(\n", + "fig, ax = ox.plot.plot_graph(\n", " Gc,\n", " figsize=(5, 5),\n", " bbox=bbox,\n", @@ -157,7 +157,7 @@ "outputs": [], "source": [ "# or save a figure to disk instead of showing it\n", - "fig, ax = ox.plot_graph(G, filepath=\"./images/image.png\", save=True, show=False, close=True)" + "fig, ax = ox.plot.plot_graph(G, filepath=\"./images/image.png\", save=True, show=False, close=True)" ] }, { @@ -174,17 +174,17 @@ "outputs": [], "source": [ "# impute missing edge speeds and calculate free-flow travel times\n", - "G = ox.add_edge_speeds(G)\n", - "G = ox.add_edge_travel_times(G)\n", + "G = ox.routing.add_edge_speeds(G)\n", + "G = ox.routing.add_edge_travel_times(G)\n", "\n", "# calculate 3 shortest paths, minimizing travel time\n", "w = \"travel_time\"\n", "orig, dest = list(G)[10], list(G)[-10]\n", - "route1 = ox.shortest_path(G, orig, dest, weight=w)\n", + "route1 = ox.routing.shortest_path(G, orig, dest, weight=w)\n", "orig, dest = list(G)[0], list(G)[-1]\n", - "route2 = ox.shortest_path(G, orig, dest, weight=w)\n", + "route2 = ox.routing.shortest_path(G, orig, dest, weight=w)\n", "orig, dest = list(G)[-100], list(G)[100]\n", - "route3 = ox.shortest_path(G, orig, dest, weight=w)" + "route3 = ox.routing.shortest_path(G, orig, dest, weight=w)" ] }, { @@ -200,7 +200,7 @@ "metadata": {}, "outputs": [], "source": [ - "fig, ax = ox.plot_graph_route(G, route1, orig_dest_size=0, node_size=0)" + "fig, ax = ox.plot.plot_graph_route(G, route1, orig_dest_size=0, node_size=0)" ] }, { @@ -210,7 +210,7 @@ "outputs": [], "source": [ "# you can also pass any ox.plot_graph parameters as additional keyword args\n", - "fig, ax = ox.plot_graph_route(G, route1, save=True, show=False, close=True)" + "fig, ax = ox.plot.plot_graph_route(G, route1, save=True, show=False, close=True)" ] }, { @@ -230,7 +230,7 @@ "source": [ "routes = [route1, route2, route3]\n", "rc = [\"r\", \"y\", \"c\"]\n", - "fig, ax = ox.plot_graph_routes(G, routes, route_colors=rc, route_linewidth=6, node_size=0)" + "fig, ax = ox.plot.plot_graph_routes(G, routes, route_colors=rc, route_linewidth=6, node_size=0)" ] }, { @@ -248,7 +248,7 @@ "metadata": {}, "outputs": [], "source": [ - "G2 = ox.graph_from_address(\"Piedmont, CA, USA\", dist=200, network_type=\"drive\")\n", + "G2 = ox.graph.graph_from_address(\"Piedmont, CA, USA\", dist=200, network_type=\"drive\")\n", "G2 = ox.convert.to_undirected(G2)" ] }, @@ -258,8 +258,8 @@ "metadata": {}, "outputs": [], "source": [ - "fig, ax = ox.plot_graph(G2, edge_linewidth=3, node_size=0, show=False, close=False)\n", - "for _, edge in ox.graph_to_gdfs(G2, nodes=False).fillna(\"\").iterrows():\n", + "fig, ax = ox.plot.plot_graph(G2, edge_linewidth=3, node_size=0, show=False, close=False)\n", + "for _, edge in ox.convert.graph_to_gdfs(G2, nodes=False).fillna(\"\").iterrows():\n", " text = edge[\"name\"]\n", " c = edge[\"geometry\"].centroid\n", " ax.annotate(text, (c.x, c.y), c=\"y\")\n", @@ -282,7 +282,7 @@ "outputs": [], "source": [ "# get all the building footprints in a city\n", - "gdf = ox.features_from_place(\"Piedmont, California, USA\", {\"building\": True})\n", + "gdf = ox.features.features_from_place(\"Piedmont, California, USA\", {\"building\": True})\n", "gdf.shape" ] }, @@ -292,7 +292,7 @@ "metadata": {}, "outputs": [], "source": [ - "fig, ax = ox.plot_footprints(gdf)" + "fig, ax = ox.plot.plot_footprints(gdf)" ] }, { @@ -302,8 +302,8 @@ "outputs": [], "source": [ "# or plot street network and the geospatial features' footprints together\n", - "fig, ax = ox.plot_footprints(gdf, alpha=0.4, show=False)\n", - "fig, ax = ox.plot_graph(G, ax=ax, node_size=0, edge_color=\"w\", edge_linewidth=0.7)" + "fig, ax = ox.plot.plot_footprints(gdf, alpha=0.4, show=False)\n", + "fig, ax = ox.plot.plot_graph(G, ax=ax, node_size=0, edge_color=\"w\", edge_linewidth=0.7)" ] }, { @@ -330,7 +330,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.3" + "version": "3.12.8" } }, "nbformat": 4, diff --git a/notebooks/16-download-osm-geospatial-features.ipynb b/notebooks/16-download-osm-geospatial-features.ipynb index cbae85b..aa16ea4 100644 --- a/notebooks/16-download-osm-geospatial-features.ipynb +++ b/notebooks/16-download-osm-geospatial-features.ipynb @@ -25,6 +25,7 @@ "outputs": [], "source": [ "import osmnx as ox\n", + "import pandas as pd\n", "\n", "ox.__version__" ] @@ -53,7 +54,7 @@ "# `True` means retrieve any object with this tag, regardless of value\n", "place = \"SoHo, New York, NY\"\n", "tags = {\"building\": True}\n", - "gdf = ox.features_from_place(place, tags)\n", + "gdf = ox.features.features_from_place(place, tags)\n", "gdf.shape" ] }, @@ -63,7 +64,7 @@ "metadata": {}, "outputs": [], "source": [ - "fig, ax = ox.plot_footprints(gdf, figsize=(3, 3))" + "fig, ax = ox.plot.plot_footprints(gdf, figsize=(3, 3))" ] }, { @@ -75,7 +76,7 @@ "# get all the parks in some neighborhood\n", "# constrain acceptable `leisure` tag values to `park`\n", "tags = {\"leisure\": \"park\"}\n", - "gdf = ox.features_from_place(place, tags)\n", + "gdf = ox.features.features_from_place(place, tags)\n", "gdf.shape" ] }, @@ -89,7 +90,7 @@ "# and everything tagged landuse = retail or commercial,\n", "# and everything tagged highway = bus_stop\n", "tags = {\"amenity\": True, \"landuse\": [\"retail\", \"commercial\"], \"highway\": \"bus_stop\"}\n", - "gdf = ox.features_from_place(\"Piedmont, California, USA\", tags)\n", + "gdf = ox.features.features_from_place(\"Piedmont, California, USA\", tags)\n", "gdf.shape" ] }, @@ -113,6 +114,42 @@ "gdf[gdf[\"highway\"] == \"bus_stop\"].dropna(axis=1, how=\"any\").head()" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Attach features to nearest nodes\n", + "\n", + "For example, attach parking information to nearest network nodes." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# first create the graph and the features\n", + "place = \"Piedmont, CA, USA\"\n", + "G = ox.graph.graph_from_place(place, network_type=\"drive\")\n", + "features = ox.features.features_from_place(place, {\"amenity\": \"parking\"})" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# then attach your features to your graph's nearest nodes as attributes\n", + "feature_points = features.representative_point()\n", + "nn = ox.distance.nearest_nodes(G, feature_points.x, feature_points.y)\n", + "useful_tags = [\"access\", \"parking\", \"surface\", \"capacity\", \"fee\"]\n", + "for node, feature in zip(nn, features[useful_tags].to_dict(orient=\"records\")):\n", + " feature = {k: v for k, v in feature.items() if pd.notna(v)}\n", + " G.nodes[node].update({\"parking\": feature})" + ] + }, { "cell_type": "code", "execution_count": null, @@ -137,7 +174,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.3" + "version": "3.12.8" } }, "nbformat": 4, diff --git a/notebooks/17-street-network-orientations.ipynb b/notebooks/17-street-network-orientations.ipynb index 318f315..7d2e201 100644 --- a/notebooks/17-street-network-orientations.ipynb +++ b/notebooks/17-street-network-orientations.ipynb @@ -74,7 +74,7 @@ "outputs": [], "source": [ "# verify OSMnx geocodes each query to what you expect (i.e., a [multi]polygon geometry)\n", - "gdf = ox.geocode_to_gdf(list(places.values()))\n", + "gdf = ox.geocoder.geocode_to_gdf(list(places.values()))\n", "gdf" ] }, @@ -96,9 +96,9 @@ " print(ox.utils.ts(), place)\n", "\n", " # get undirected graphs with edge bearing attributes\n", - " G = ox.graph_from_place(place, network_type=\"drive\")\n", - " Gu = ox.add_edge_bearings(ox.convert.to_undirected(G))\n", - " fig, ax = ox.plot_orientation(Gu, ax=ax, title=place, area=True)\n", + " G = ox.graph.graph_from_place(place, network_type=\"drive\")\n", + " Gu = ox.bearing.add_edge_bearings(ox.convert.to_undirected(G))\n", + " fig, ax = ox.plot.plot_orientation(Gu, ax=ax, title=place, area=True)\n", "\n", "# add figure title and save image\n", "suptitle_font = {\n", @@ -145,7 +145,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.3" + "version": "3.12.8" } }, "nbformat": 4,