Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
gboeing committed Dec 18, 2024
1 parent e80128c commit 9d0a978
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 97 deletions.
4 changes: 2 additions & 2 deletions notebooks/09-example-figure-ground.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -383,7 +383,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.12.8"
}
},
"nbformat": 4,
Expand Down
18 changes: 9 additions & 9 deletions notebooks/10-building-footprints.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
]
},
Expand Down Expand Up @@ -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]"
]
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -276,7 +276,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
"version": "3.12.8"
}
},
"nbformat": 4,
Expand Down
32 changes: 16 additions & 16 deletions notebooks/11-interactive-web-mapping.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
]
},
{
Expand All @@ -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()"
]
},
{
Expand All @@ -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})"
]
},
Expand All @@ -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})"
]
Expand Down Expand Up @@ -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})"
]
},
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)"
]
},
Expand All @@ -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\")"
]
},
Expand All @@ -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)"
]
Expand All @@ -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\")"
]
},
{
Expand Down Expand Up @@ -286,7 +286,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
"version": "3.12.8"
}
},
"nbformat": 4,
Expand Down
52 changes: 28 additions & 24 deletions notebooks/12-node-elevations-edge-grades.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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\")"
]
},
{
Expand Down Expand Up @@ -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."
]
},
{
Expand All @@ -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"
]
},
{
Expand All @@ -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)"
]
Expand Down Expand Up @@ -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\")"
]
},
{
Expand All @@ -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\")"
]
},
{
Expand Down Expand Up @@ -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)"
]
},
{
Expand All @@ -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)"
]
},
{
Expand Down Expand Up @@ -358,7 +362,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
"version": "3.12.8"
}
},
"nbformat": 4,
Expand Down
20 changes: 10 additions & 10 deletions notebooks/13-isolines-isochrones.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
]
},
{
Expand All @@ -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)"
]
},
{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)"
]
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -248,7 +248,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
"version": "3.12.8"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 9d0a978

Please sign in to comment.