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 1cc4f76 commit e8b6a2e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 50 deletions.
34 changes: 19 additions & 15 deletions notebooks/04-simplify-graph-consolidate-nodes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
"source": [
"# get a street network and plot it with all edge intersections\n",
"point = 37.858495, -122.267468\n",
"G = ox.graph_from_point(point, network_type=\"drive\", dist=500)\n",
"fig, ax = ox.plot_graph(G, node_color=\"r\")"
"G = ox.graph.graph_from_point(point, network_type=\"drive\", dist=500)\n",
"fig, ax = ox.plot.plot_graph(G, node_color=\"r\")"
]
},
{
Expand All @@ -65,8 +65,10 @@
"outputs": [],
"source": [
"# get a GeoSeries of consolidated intersections\n",
"G_proj = ox.project_graph(G)\n",
"ints = ox.consolidate_intersections(G_proj, rebuild_graph=False, tolerance=15, dead_ends=False)\n",
"G_proj = ox.projection.project_graph(G)\n",
"ints = ox.simplification.consolidate_intersections(\n",
" G_proj, rebuild_graph=False, tolerance=15, dead_ends=False\n",
")\n",
"len(ints)"
]
},
Expand Down Expand Up @@ -97,7 +99,9 @@
"source": [
"# consolidate intersections and rebuild graph topology\n",
"# this reconnects edge geometries to the new consolidated nodes\n",
"G2 = ox.consolidate_intersections(G_proj, rebuild_graph=True, tolerance=15, dead_ends=False)\n",
"G2 = ox.simplification.consolidate_intersections(\n",
" G_proj, rebuild_graph=True, tolerance=15, dead_ends=False\n",
")\n",
"len(G2)"
]
},
Expand All @@ -107,7 +111,7 @@
"metadata": {},
"outputs": [],
"source": [
"fig, ax = ox.plot_graph(G2, node_color=\"r\")"
"fig, ax = ox.plot.plot_graph(G2, node_color=\"r\")"
]
},
{
Expand Down Expand Up @@ -138,8 +142,8 @@
"source": [
"# create a network around some (lat, lng) point and plot it\n",
"location_point = (33.299896, -111.831638)\n",
"G = ox.graph_from_point(location_point, dist=500, simplify=False)\n",
"fig, ax = ox.plot_graph(G, node_color=\"r\")"
"G = ox.graph.graph_from_point(location_point, dist=500, simplify=False)\n",
"fig, ax = ox.plot.plot_graph(G, node_color=\"r\")"
]
},
{
Expand All @@ -150,7 +154,7 @@
"source": [
"# show which nodes we'd remove if we simplify it (yellow)\n",
"nc = [\"r\" if ox.simplification._is_endpoint(G, node, None, None) else \"y\" for node in G.nodes()]\n",
"fig, ax = ox.plot_graph(G, node_color=nc)"
"fig, ax = ox.plot.plot_graph(G, node_color=nc)"
]
},
{
Expand All @@ -160,7 +164,7 @@
"outputs": [],
"source": [
"# simplify the network\n",
"G2 = ox.simplify_graph(G)"
"G2 = ox.simplification.simplify_graph(G)"
]
},
{
Expand All @@ -172,7 +176,7 @@
"# plot the simplified network and highlight any self-loop edges\n",
"loops = [edge[0] for edge in nx.selfloop_edges(G2)]\n",
"nc = [\"r\" if node in loops else \"y\" for node in G2.nodes()]\n",
"fig, ax = ox.plot_graph(G2, node_color=nc)"
"fig, ax = ox.plot.plot_graph(G2, node_color=nc)"
]
},
{
Expand All @@ -185,7 +189,7 @@
"nc = [\n",
" \"r\" if ox.simplification._is_endpoint(G, node, [\"osmid\"], None) else \"y\" for node in G.nodes()\n",
"]\n",
"fig, ax = ox.plot_graph(G, node_color=nc)"
"fig, ax = ox.plot.plot_graph(G, node_color=nc)"
]
},
{
Expand All @@ -195,8 +199,8 @@
"outputs": [],
"source": [
"# simplify network with strict mode turned off\n",
"G3 = ox.simplify_graph(G.copy(), edge_attrs_differ=[\"osmid\"])\n",
"fig, ax = ox.plot_graph(G3, node_color=\"r\")"
"G3 = ox.simplification.simplify_graph(G.copy(), edge_attrs_differ=[\"osmid\"])\n",
"fig, ax = ox.plot.plot_graph(G3, node_color=\"r\")"
]
},
{
Expand Down Expand Up @@ -235,7 +239,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
"version": "3.12.8"
}
},
"nbformat": 4,
Expand Down
22 changes: 11 additions & 11 deletions notebooks/05-save-load-networks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"source": [
"# get a network\n",
"place = \"Piedmont, California, USA\"\n",
"G = ox.graph_from_place(place, network_type=\"drive\")"
"G = ox.graph.graph_from_place(place, network_type=\"drive\")"
]
},
{
Expand All @@ -52,7 +52,7 @@
"outputs": [],
"source": [
"# save graph as a geopackage\n",
"ox.save_graph_geopackage(G, filepath=\"./data/piedmont.gpkg\")"
"ox.io.save_graph_geopackage(G, filepath=\"./data/piedmont.gpkg\")"
]
},
{
Expand All @@ -71,8 +71,8 @@
"# save/load graph as a graphml file: this is the best way to save your model\n",
"# for subsequent work later\n",
"filepath = \"./data/piedmont.graphml\"\n",
"ox.save_graphml(G, filepath)\n",
"G = ox.load_graphml(filepath)"
"ox.io.save_graphml(G, filepath)\n",
"G = ox.io.load_graphml(filepath)"
]
},
{
Expand All @@ -82,7 +82,7 @@
"outputs": [],
"source": [
"# if you want to work with your model in gephi, use gephi compatibility mode\n",
"ox.save_graphml(G, filepath=filepath, gephi=True)"
"ox.io.save_graphml(G, filepath=filepath, gephi=True)"
]
},
{
Expand All @@ -99,7 +99,7 @@
"outputs": [],
"source": [
"# save street network as SVG\n",
"fig, ax = ox.plot_graph(G, show=False, save=True, close=True, filepath=\"./images/piedmont.svg\")"
"fig, ax = ox.plot.plot_graph(G, show=False, save=True, close=True, filepath=\"./images/piedmont.svg\")"
]
},
{
Expand All @@ -116,7 +116,7 @@
"outputs": [],
"source": [
"# get all \"amenities\" and save as a geopackage via geopandas\n",
"gdf = ox.features_from_place(place, tags={\"amenity\": True})\n",
"gdf = ox.features.features_from_place(place, tags={\"amenity\": True})\n",
"gdf = gdf.apply(lambda c: c.astype(str) if c.name != \"geometry\" else c, axis=0)\n",
"gdf.to_file(\"./data/pois.gpkg\", driver=\"GPKG\")"
]
Expand All @@ -128,7 +128,7 @@
"outputs": [],
"source": [
"# get all building footprints and save as a geopackage via geopandas\n",
"gdf = ox.features_from_place(place, tags={\"building\": True})\n",
"gdf = ox.features.features_from_place(place, tags={\"building\": True})\n",
"gdf = gdf.apply(lambda c: c.astype(str) if c.name != \"geometry\" else c, axis=0)\n",
"gdf.to_file(\"./data/building_footprints.gpkg\", driver=\"GPKG\")"
]
Expand All @@ -153,8 +153,8 @@
"# save graph to disk as .osm xml file\n",
"ox.settings.all_oneway = True\n",
"ox.settings.log_console = True\n",
"G = ox.graph_from_place(\"Piedmont, California, USA\", network_type=\"drive\", simplify=False)\n",
"ox.save_graph_xml(G, filepath=\"./data/piedmont.osm\")"
"G = ox.graph.graph_from_place(\"Piedmont, California, USA\", network_type=\"drive\", simplify=False)\n",
"ox.io.save_graph_xml(G, filepath=\"./data/piedmont.osm\")"
]
},
{
Expand Down Expand Up @@ -182,7 +182,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/06-stats-indicators-centrality.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"outputs": [],
"source": [
"# get the network for Piedmont, calculate its basic stats, then show the average circuity\n",
"stats = ox.basic_stats(ox.graph_from_place(\"Piedmont, California, USA\"))\n",
"stats = ox.stats.basic_stats(ox.graph.graph_from_place(\"Piedmont, California, USA\"))\n",
"stats[\"circuity_avg\"]"
]
},
Expand All @@ -60,9 +60,9 @@
"source": [
"# get the street network for a place, and its area in square meters\n",
"place = \"Piedmont, California, USA\"\n",
"gdf = ox.geocode_to_gdf(place)\n",
"area = ox.projection.project_gdf(gdf).unary_union.area\n",
"G = ox.graph_from_place(place, network_type=\"drive\")"
"gdf = ox.geocoder.geocode_to_gdf(place)\n",
"area = ox.projection.project_gdf(gdf).union_all().area\n",
"G = ox.graph.graph_from_place(place, network_type=\"drive\")"
]
},
{
Expand All @@ -72,7 +72,7 @@
"outputs": [],
"source": [
"# calculate basic and extended network stats, merge them together, and display\n",
"stats = ox.basic_stats(G, area=area)\n",
"stats = ox.stats.basic_stats(G, area=area)\n",
"pd.Series(stats)"
]
},
Expand All @@ -90,7 +90,7 @@
"outputs": [],
"source": [
"# unpack dicts into individiual keys:values\n",
"stats = ox.basic_stats(G, area=area)\n",
"stats = ox.stats.basic_stats(G, area=area)\n",
"for k, count in stats[\"streets_per_node_counts\"].items():\n",
" stats[f\"{k}way_int_count\"] = count\n",
"for k, proportion in stats[\"streets_per_node_proportions\"].items():\n",
Expand Down Expand Up @@ -138,7 +138,7 @@
"source": [
"nc = [\"r\" if node == max_node else \"w\" for node in G.nodes]\n",
"ns = [80 if node == max_node else 15 for node in G.nodes]\n",
"fig, ax = ox.plot_graph(G, node_size=ns, node_color=nc, node_zorder=2)"
"fig, ax = ox.plot.plot_graph(G, node_size=ns, node_color=nc, node_zorder=2)"
]
},
{
Expand All @@ -157,7 +157,7 @@
"# add the betweenness centraliy values as new node attributes, then plot\n",
"nx.set_node_attributes(G, bc, \"bc\")\n",
"nc = ox.plot.get_node_colors_by_attr(G, \"bc\", cmap=\"plasma\")\n",
"fig, ax = ox.plot_graph(\n",
"fig, ax = ox.plot.plot_graph(\n",
" G,\n",
" node_color=nc,\n",
" node_size=30,\n",
Expand Down Expand Up @@ -192,7 +192,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.12.8"
}
},
"nbformat": 4,
Expand Down
12 changes: 6 additions & 6 deletions notebooks/07-plot-graph-over-shape.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
"source": [
"# get the place boundaries\n",
"place = \"Portland, Maine\"\n",
"gdf = ox.geocode_to_gdf(place)\n",
"gdf = ox.geocoder.geocode_to_gdf(place)\n",
"\n",
"# get the street network, with retain_all=True to retain all the disconnected islands' networks\n",
"G = ox.graph_from_place(place, network_type=\"drive\", retain_all=True)"
"G = ox.graph.graph_from_place(place, network_type=\"drive\", retain_all=True)"
]
},
{
Expand All @@ -49,7 +49,7 @@
"outputs": [],
"source": [
"# plot the network, but do not show it or close it yet\n",
"fig, ax = ox.plot_graph(\n",
"fig, ax = ox.plot.plot_graph(\n",
" G,\n",
" show=False,\n",
" close=False,\n",
Expand All @@ -64,7 +64,7 @@
"\n",
"# optionally set up the axes extents\n",
"margin = 0.02\n",
"west, south, east, north = gdf.unary_union.bounds\n",
"west, south, east, north = gdf.union_all().bounds\n",
"margin_ns = (north - south) * margin\n",
"margin_ew = (east - west) * margin\n",
"ax.set_ylim((south - margin_ns, north + margin_ns))\n",
Expand All @@ -85,7 +85,7 @@
"metadata": {},
"outputs": [],
"source": [
"islands = ox.features_from_place(place, tags={\"place\": [\"island\", \"islet\"]})\n",
"islands = ox.features.features_from_place(place, tags={\"place\": [\"island\", \"islet\"]})\n",
"islands.shape"
]
},
Expand Down Expand Up @@ -114,7 +114,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/08-custom-filters-infrastructure.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@
"\n",
"# only get motorway ways\n",
"cf = '[\"highway\"~\"motorway\"]'\n",
"G = ox.graph_from_place(place, network_type=\"drive\", custom_filter=cf)\n",
"G = ox.graph.graph_from_place(place, network_type=\"drive\", custom_filter=cf)\n",
"print(len(G), \"motorway\")\n",
"\n",
"# only get primary ways\n",
"cf = '[\"highway\"~\"primary\"]'\n",
"G = ox.graph_from_place(place, network_type=\"drive\", custom_filter=cf)\n",
"G = ox.graph.graph_from_place(place, network_type=\"drive\", custom_filter=cf)\n",
"print(len(G), \"primary\")\n",
"\n",
"# use the pipe (|) as 'or' operator\n",
"cf = '[\"highway\"~\"motorway|primary\"]'\n",
"G = ox.graph_from_place(place, network_type=\"drive\", custom_filter=cf)\n",
"G = ox.graph.graph_from_place(place, network_type=\"drive\", custom_filter=cf)\n",
"print(len(G), \"motorway + primary\")"
]
},
Expand All @@ -67,7 +67,7 @@
"source": [
"# network of the canals of amsterdam\n",
"place = \"Amsterdam, Netherlands\"\n",
"G = ox.graph_from_place(place, custom_filter='[\"waterway\"~\"canal\"]')"
"G = ox.graph.graph_from_place(place, custom_filter='[\"waterway\"~\"canal\"]')"
]
},
{
Expand All @@ -88,8 +88,8 @@
"# takes a couple minutes to do all the downloading and processing\n",
"# OSMnx automatically divides up the query into multiple requests to not overload server\n",
"cf = '[\"highway\"~\"motorway|motorway_link|trunk|trunk_link\"]'\n",
"G = ox.graph_from_place(\"Belgium\", network_type=\"drive\", custom_filter=cf)\n",
"fig, ax = ox.plot_graph(G, node_size=0)"
"G = ox.graph.graph_from_place(\"Belgium\", network_type=\"drive\", custom_filter=cf)\n",
"fig, ax = ox.plot.plot_graph(G, node_size=0)"
]
},
{
Expand All @@ -102,15 +102,15 @@
"# note this is rail *infrastructure* and thus includes crossovers, sidings, spurs, yards, etc\n",
"# for station-based rail network, you should download a station adjacency matrix elsewhere\n",
"ox.settings.useful_tags_way += [\"railway\"]\n",
"G = ox.graph_from_place(\n",
"G = ox.graph.graph_from_place(\n",
" \"New York, New York, USA\",\n",
" retain_all=False,\n",
" truncate_by_edge=True,\n",
" simplify=True,\n",
" custom_filter='[\"railway\"~\"subway\"]',\n",
")\n",
"\n",
"fig, ax = ox.plot_graph(G, node_size=0, edge_color=\"w\", edge_linewidth=0.2)"
"fig, ax = ox.plot.plot_graph(G, node_size=0, edge_color=\"w\", edge_linewidth=0.2)"
]
},
{
Expand All @@ -137,7 +137,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.12.8"
}
},
"nbformat": 4,
Expand Down

0 comments on commit e8b6a2e

Please sign in to comment.