Skip to content

Commit

Permalink
update notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
gboeing committed Feb 27, 2024
1 parent 7ffced4 commit f0c7cc7
Showing 1 changed file with 36 additions and 34 deletions.
70 changes: 36 additions & 34 deletions modules/08-urban-networks-ii/lecture.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"source": [
"# create a study site: geocode city hall, convert coords to shapely geometry,\n",
"# project geometry to UTM, buffer by 5km, project back to lat-lng\n",
"latlng_coords = ox.geocode(\"Los Angeles City Hall\")\n",
"latlng_coords = ox.geocoder.geocode(\"Los Angeles City Hall\")\n",
"latlng_point = Point(latlng_coords[1], latlng_coords[0])\n",
"latlng_point_proj, crs = ox.projection.project_geometry(latlng_point)\n",
"polygon_proj = latlng_point_proj.buffer(5000)\n",
Expand All @@ -71,8 +71,8 @@
"source": [
"# model the street network within study site\n",
"# your parameterization makes assumptions about your interests here\n",
"G = ox.graph_from_polygon(polygon, network_type=\"drive\", truncate_by_edge=True)\n",
"fig, ax = ox.plot_graph(G, node_size=0, edge_color=\"w\", edge_linewidth=0.3)"
"G = ox.graph.graph_from_polygon(polygon, network_type=\"drive\", truncate_by_edge=True)\n",
"fig, ax = ox.plot.plot_graph(G, node_size=0, edge_color=\"w\", edge_linewidth=0.3)"
]
},
{
Expand All @@ -82,8 +82,8 @@
"outputs": [],
"source": [
"# add speeds and travel times\n",
"G = ox.add_edge_speeds(G)\n",
"G = ox.add_edge_travel_times(G)"
"G = ox.speed.add_edge_speeds(G)\n",
"G = ox.speed.add_edge_travel_times(G)"
]
},
{
Expand Down Expand Up @@ -125,8 +125,8 @@
"outputs": [],
"source": [
"# now clean up the intersections and re-calculate\n",
"clean_intersects = ox.consolidate_intersections(\n",
" ox.project_graph(G), rebuild_graph=False, tolerance=10\n",
"clean_intersects = ox.simplification.consolidate_intersections(\n",
" ox.projection.project_graph(G), rebuild_graph=False, tolerance=10\n",
")\n",
"clean_intersect_count = len(clean_intersects)\n",
"clean_intersect_count"
Expand Down Expand Up @@ -177,8 +177,8 @@
"outputs": [],
"source": [
"# get home/work network nodes\n",
"home_nodes = ox.nearest_nodes(G, X=od[\"home_lng\"], Y=od[\"home_lat\"])\n",
"work_nodes = ox.nearest_nodes(G, X=od[\"work_lng\"], Y=od[\"work_lat\"])"
"home_nodes = ox.distance.nearest_nodes(G, X=od[\"home_lng\"], Y=od[\"home_lat\"])\n",
"work_nodes = ox.distance.nearest_nodes(G, X=od[\"work_lng\"], Y=od[\"work_lat\"])"
]
},
{
Expand All @@ -191,7 +191,7 @@
"source": [
"# calculate each shortest path\n",
"paths = [\n",
" ox.shortest_path(G, orig, dest, weight=\"travel_time\")\n",
" ox.routing.shortest_path(G, orig, dest, weight=\"travel_time\")\n",
" for orig, dest in zip(home_nodes, work_nodes)\n",
"]\n",
"len(paths)"
Expand All @@ -204,7 +204,7 @@
"outputs": [],
"source": [
"# filter out any nulls (ie, not successfully solved)\n",
"paths = [path for path in paths if path is not None]\n",
"paths = [path for path in paths if path is not None and len(path) > 1]\n",
"len(paths)"
]
},
Expand All @@ -215,7 +215,7 @@
"outputs": [],
"source": [
"# plot 100 routes\n",
"fig, ax = ox.plot_graph_routes(\n",
"fig, ax = ox.plot.plot_graph_routes(\n",
" G,\n",
" routes=paths[0:100],\n",
" node_size=0,\n",
Expand Down Expand Up @@ -255,13 +255,13 @@
"source": [
"def calc_efficiency(G, route, attr=\"length\"):\n",
" # sum the edge lengths in the route\n",
" trip_distance = sum(ox.utils_graph.get_route_edge_attributes(G, route=route, attribute=attr))\n",
" trip_distance = sum(ox.utils_graph.route_to_gdf(G, route=route)[attr])\n",
" # fast vectorized great-circle distance calculator\n",
" gc_distance = ox.distance.great_circle_vec(\n",
" gc_distance = ox.distance.great_circle(\n",
" lat1=G.nodes[route[0]][\"y\"],\n",
" lng1=G.nodes[route[0]][\"x\"],\n",
" lon1=G.nodes[route[0]][\"x\"],\n",
" lat2=G.nodes[route[-1]][\"y\"],\n",
" lng2=G.nodes[route[-1]][\"x\"],\n",
" lon2=G.nodes[route[-1]][\"x\"],\n",
" )\n",
" return gc_distance / trip_distance\n",
"\n",
Expand Down Expand Up @@ -331,13 +331,13 @@
"outputs": [],
"source": [
"# get home/work network nodes again, calculate routes, drop nulls\n",
"home_nodes_per = ox.nearest_nodes(G_per, X=od[\"home_lng\"], Y=od[\"home_lat\"])\n",
"work_nodes_per = ox.nearest_nodes(G_per, X=od[\"work_lng\"], Y=od[\"work_lat\"])\n",
"home_nodes_per = ox.distance.nearest_nodes(G_per, X=od[\"home_lng\"], Y=od[\"home_lat\"])\n",
"work_nodes_per = ox.distance.nearest_nodes(G_per, X=od[\"work_lng\"], Y=od[\"work_lat\"])\n",
"paths_per = [\n",
" ox.shortest_path(G_per, orig, dest, weight=\"travel_time\")\n",
" ox.routing.shortest_path(G_per, orig, dest, weight=\"travel_time\")\n",
" for orig, dest in zip(home_nodes_per, work_nodes_per)\n",
"]\n",
"paths_per = [path for path in paths_per if path is not None]\n",
"paths_per = [path for path in paths_per if path is not None and len(path) > 1]\n",
"len(paths_per)"
]
},
Expand Down Expand Up @@ -393,7 +393,7 @@
"# shuffle the order, so you don't just plot new atop old\n",
"paths_colors = pd.DataFrame({\"path\": all_paths, \"color\": colors}).sample(frac=1)\n",
"\n",
"fig, ax = ox.plot_graph_routes(\n",
"fig, ax = ox.plot.plot_graph_routes(\n",
" G,\n",
" routes=paths_colors[\"path\"],\n",
" node_size=0,\n",
Expand Down Expand Up @@ -445,7 +445,7 @@
"outputs": [],
"source": [
"# study area within 1/2 mile of SF Civic Center\n",
"latlng_coords = ox.geocode(\"Civic Center, San Francisco, CA, USA\")\n",
"latlng_coords = ox.geocoder.geocode(\"Civic Center, San Francisco, CA, USA\")\n",
"latlng_point = Point(latlng_coords[1], latlng_coords[0])\n",
"latlng_point_proj, crs = ox.projection.project_geometry(latlng_point)\n",
"polygon_proj = latlng_point_proj.buffer(800)\n",
Expand Down Expand Up @@ -505,7 +505,7 @@
"outputs": [],
"source": [
"# model the street network across all the study sub-sites\n",
"G_all = ox.graph_from_polygon(sf_tracts.unary_union, custom_filter=custom_filter)\n",
"G_all = ox.graph.graph_from_polygon(sf_tracts.unary_union, custom_filter=custom_filter)\n",
"len(G_all.nodes)"
]
},
Expand All @@ -519,8 +519,10 @@
"# calculate clean intersection counts per tract\n",
"intersect_counts = {}\n",
"for label, geom in zip(sf_tracts.index, sf_tracts[\"geometry\"]):\n",
" G_tmp = ox.graph_from_polygon(geom, custom_filter=custom_filter)\n",
" clean_intersects = ox.consolidate_intersections(ox.project_graph(G_tmp), rebuild_graph=False)\n",
" G_tmp = ox.graph.graph_from_polygon(geom, custom_filter=custom_filter)\n",
" clean_intersects = ox.simplification.consolidate_intersections(\n",
" ox.projection.project_graph(G_tmp), rebuild_graph=False\n",
" )\n",
" intersect_counts[label] = len(clean_intersects)"
]
},
Expand Down Expand Up @@ -550,7 +552,7 @@
"ax = sf_tracts.plot(\n",
" ax=ax, column=\"intersect_density\", cmap=\"Reds_r\", legend=True, legend_kwds={\"shrink\": 0.8}\n",
")\n",
"fig, ax = ox.plot_graph(G_all, ax=ax, node_size=0, edge_color=\"#111111\")\n",
"fig, ax = ox.plot.plot_graph(G_all, ax=ax, node_size=0, edge_color=\"#111111\")\n",
"fig.savefig(\"map.png\", dpi=300, facecolor=\"#111111\", bbox_inches=\"tight\")"
]
},
Expand Down Expand Up @@ -603,8 +605,8 @@
"outputs": [],
"source": [
"# model the walkable network within our original study site\n",
"G_walk = ox.graph_from_polygon(polygon, network_type=\"walk\")\n",
"fig, ax = ox.plot_graph(G_walk, node_size=0, edge_color=\"w\", edge_linewidth=0.3)"
"G_walk = ox.graph.graph_from_polygon(polygon, network_type=\"walk\")\n",
"fig, ax = ox.plot.plot_graph(G_walk, node_size=0, edge_color=\"w\", edge_linewidth=0.3)"
]
},
{
Expand All @@ -616,7 +618,7 @@
"# set a uniform walking speed on every edge\n",
"for u, v, data in G_walk.edges(data=True):\n",
" data[\"speed_kph\"] = walk_speed\n",
"G_walk = ox.add_edge_travel_times(G_walk)"
"G_walk = ox.speed.add_edge_travel_times(G_walk)"
]
},
{
Expand All @@ -626,8 +628,8 @@
"outputs": [],
"source": [
"# extract node/edge GeoDataFrames, retaining only necessary columns (for pandana)\n",
"nodes = ox.graph_to_gdfs(G_walk, edges=False)[[\"x\", \"y\"]]\n",
"edges = ox.graph_to_gdfs(G_walk, nodes=False).reset_index()[[\"u\", \"v\", \"travel_time\"]]"
"nodes = ox.utils_graph.graph_to_gdfs(G_walk, edges=False)[[\"x\", \"y\"]]\n",
"edges = ox.utils_graph.graph_to_gdfs(G_walk, nodes=False).reset_index()[[\"u\", \"v\", \"travel_time\"]]"
]
},
{
Expand All @@ -639,7 +641,7 @@
"# get all the \"fresh food\" stores on OSM within the study site\n",
"# you could load any amenities DataFrame, but we'll get ours from OSM\n",
"tags = {\"shop\": [\"grocery\", \"greengrocer\", \"supermarket\"]}\n",
"amenities = ox.geometries_from_bbox(\n",
"amenities = ox.features.features_from_bbox(\n",
" north=nodes[\"y\"].max(),\n",
" south=nodes[\"y\"].min(),\n",
" east=nodes[\"x\"].min(),\n",
Expand Down Expand Up @@ -707,7 +709,7 @@
"outputs": [],
"source": [
"# plot distance to nearest amenity\n",
"fig, ax = ox.plot_graph(\n",
"fig, ax = ox.plot.plot_graph(\n",
" G_walk, node_size=0, edge_linewidth=0.1, edge_color=\"gray\", show=False, close=False\n",
")\n",
"\n",
Expand Down Expand Up @@ -750,7 +752,7 @@
"outputs": [],
"source": [
"# plot amenity count within your walking horizon\n",
"fig, ax = ox.plot_graph(\n",
"fig, ax = ox.plot.plot_graph(\n",
" G_walk, node_size=0, edge_linewidth=0.1, edge_color=\"gray\", show=False, close=False\n",
")\n",
"\n",
Expand Down

0 comments on commit f0c7cc7

Please sign in to comment.