From e8b6a2e4cc8137676e536f0edf5d20ba8a28b83e Mon Sep 17 00:00:00 2001 From: Geoff Boeing Date: Wed, 18 Dec 2024 14:01:16 -0800 Subject: [PATCH] update examples --- .../04-simplify-graph-consolidate-nodes.ipynb | 34 +++++++++++-------- notebooks/05-save-load-networks.ipynb | 22 ++++++------ .../06-stats-indicators-centrality.ipynb | 18 +++++----- notebooks/07-plot-graph-over-shape.ipynb | 12 +++---- .../08-custom-filters-infrastructure.ipynb | 18 +++++----- 5 files changed, 54 insertions(+), 50 deletions(-) diff --git a/notebooks/04-simplify-graph-consolidate-nodes.ipynb b/notebooks/04-simplify-graph-consolidate-nodes.ipynb index 6dd5362..39cbc38 100644 --- a/notebooks/04-simplify-graph-consolidate-nodes.ipynb +++ b/notebooks/04-simplify-graph-consolidate-nodes.ipynb @@ -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\")" ] }, { @@ -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)" ] }, @@ -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)" ] }, @@ -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\")" ] }, { @@ -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\")" ] }, { @@ -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)" ] }, { @@ -160,7 +164,7 @@ "outputs": [], "source": [ "# simplify the network\n", - "G2 = ox.simplify_graph(G)" + "G2 = ox.simplification.simplify_graph(G)" ] }, { @@ -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)" ] }, { @@ -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)" ] }, { @@ -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\")" ] }, { @@ -235,7 +239,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.3" + "version": "3.12.8" } }, "nbformat": 4, diff --git a/notebooks/05-save-load-networks.ipynb b/notebooks/05-save-load-networks.ipynb index e7dd3a3..00afd1b 100644 --- a/notebooks/05-save-load-networks.ipynb +++ b/notebooks/05-save-load-networks.ipynb @@ -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\")" ] }, { @@ -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\")" ] }, { @@ -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)" ] }, { @@ -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)" ] }, { @@ -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\")" ] }, { @@ -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\")" ] @@ -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\")" ] @@ -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\")" ] }, { @@ -182,7 +182,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.9" + "version": "3.12.8" } }, "nbformat": 4, diff --git a/notebooks/06-stats-indicators-centrality.ipynb b/notebooks/06-stats-indicators-centrality.ipynb index 8d78c1e..9c30bcd 100644 --- a/notebooks/06-stats-indicators-centrality.ipynb +++ b/notebooks/06-stats-indicators-centrality.ipynb @@ -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\"]" ] }, @@ -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\")" ] }, { @@ -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)" ] }, @@ -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", @@ -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)" ] }, { @@ -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", @@ -192,7 +192,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.9" + "version": "3.12.8" } }, "nbformat": 4, diff --git a/notebooks/07-plot-graph-over-shape.ipynb b/notebooks/07-plot-graph-over-shape.ipynb index f296fdd..c8a93a1 100644 --- a/notebooks/07-plot-graph-over-shape.ipynb +++ b/notebooks/07-plot-graph-over-shape.ipynb @@ -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)" ] }, { @@ -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", @@ -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", @@ -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" ] }, @@ -114,7 +114,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.9" + "version": "3.12.8" } }, "nbformat": 4, diff --git a/notebooks/08-custom-filters-infrastructure.ipynb b/notebooks/08-custom-filters-infrastructure.ipynb index 652e544..a316a8c 100644 --- a/notebooks/08-custom-filters-infrastructure.ipynb +++ b/notebooks/08-custom-filters-infrastructure.ipynb @@ -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\")" ] }, @@ -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\"]')" ] }, { @@ -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)" ] }, { @@ -102,7 +102,7 @@ "# 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", @@ -110,7 +110,7 @@ " 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)" ] }, { @@ -137,7 +137,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.9" + "version": "3.12.8" } }, "nbformat": 4,