diff --git a/notebooks/00-osmnx-features-demo.ipynb b/notebooks/00-osmnx-features-demo.ipynb index a4e91c7..2e10ec1 100644 --- a/notebooks/00-osmnx-features-demo.ipynb +++ b/notebooks/00-osmnx-features-demo.ipynb @@ -54,8 +54,8 @@ "outputs": [], "source": [ "# download/model a street network for some city then visualize it\n", - "G = ox.graph_from_place(\"Piedmont, California, USA\", network_type=\"drive\")\n", - "fig, ax = ox.plot_graph(G)" + "G = ox.graph.graph_from_place(\"Piedmont, California, USA\", network_type=\"drive\")\n", + "fig, ax = ox.plot.plot_graph(G)" ] }, { @@ -67,7 +67,9 @@ "OSMnx models all networks as NetworkX `MultiDiGraph` objects. You can convert to:\n", " - undirected MultiGraphs\n", " - DiGraphs without (possible) parallel edges\n", - " - GeoPandas node/edge GeoDataFrames" + " - GeoPandas node/edge GeoDataFrames\n", + "\n", + "Note that converting to an undirected MultiGraph is really only meant for use cases where a function or algorithm only accepts a MultiGraph argument. If you just want a fully bidirectional graph (such as for a walking network), just configure the `settings` module’s `bidirectional_network_types` before creating your graph." ] }, { @@ -76,6 +78,10 @@ "metadata": {}, "outputs": [], "source": [ + "# get a fully bidirection network (as a MultiDiGraph)\n", + "ox.settings.bidirectional_network_types += \"drive\"\n", + "G = ox.graph.graph_from_place(\"Piedmont, California, USA\", network_type=\"drive\")\n", + "\n", "# convert your MultiDiGraph to an undirected MultiGraph\n", "M = ox.convert.to_undirected(G)\n", "\n", @@ -90,7 +96,7 @@ "outputs": [], "source": [ "# you can convert your graph to node and edge GeoPandas GeoDataFrames\n", - "gdf_nodes, gdf_edges = ox.graph_to_gdfs(G)\n", + "gdf_nodes, gdf_edges = ox.convert.graph_to_gdfs(G)\n", "gdf_nodes.head()" ] }, @@ -117,7 +123,7 @@ "outputs": [], "source": [ "# convert node/edge GeoPandas GeoDataFrames to a NetworkX MultiDiGraph\n", - "G2 = ox.graph_from_gdfs(gdf_nodes, gdf_edges, graph_attrs=G.graph)" + "G2 = ox.convert.graph_from_gdfs(gdf_nodes, gdf_edges, graph_attrs=G.graph)" ] }, { @@ -134,9 +140,9 @@ "outputs": [], "source": [ "# what sized area does our network cover in square meters?\n", - "G_proj = ox.project_graph(G)\n", - "nodes_proj = ox.graph_to_gdfs(G_proj, edges=False)\n", - "graph_area_m = nodes_proj.unary_union.convex_hull.area\n", + "G_proj = ox.projection.project_graph(G)\n", + "nodes_proj = ox.convert.graph_to_gdfs(G_proj, edges=False)\n", + "graph_area_m = nodes_proj.union_all().convex_hull.area\n", "graph_area_m" ] }, @@ -147,7 +153,7 @@ "outputs": [], "source": [ "# show some basic stats about the network\n", - "ox.basic_stats(G_proj, area=graph_area_m, clean_int_tol=15)" + "ox.stats.basic_stats(G_proj, area=graph_area_m, clean_int_tol=15)" ] }, { @@ -164,8 +170,8 @@ "outputs": [], "source": [ "# save graph to disk as geopackage (for GIS) or graphml file (for gephi etc)\n", - "ox.save_graph_geopackage(G, filepath=\"./data/mynetwork.gpkg\")\n", - "ox.save_graphml(G, filepath=\"./data/mynetwork.graphml\")" + "ox.io.save_graph_geopackage(G, filepath=\"./data/mynetwork.gpkg\")\n", + "ox.io.save_graphml(G, filepath=\"./data/mynetwork.graphml\")" ] }, { @@ -196,7 +202,7 @@ "source": [ "# color edges in original graph with closeness centralities from line graph\n", "ec = ox.plot.get_edge_colors_by_attr(G, \"edge_centrality\", cmap=\"inferno\")\n", - "fig, ax = ox.plot_graph(G, edge_color=ec, edge_linewidth=2, node_size=0)" + "fig, ax = ox.plot.plot_graph(G, edge_color=ec, edge_linewidth=2, node_size=0)" ] }, { @@ -235,8 +241,8 @@ "outputs": [], "source": [ "# find the shortest path between nodes, minimizing travel time, then plot it\n", - "route = ox.shortest_path(G, orig, dest, weight=\"travel_time\")\n", - "fig, ax = ox.plot_graph_route(G, route, node_size=0)" + "route = ox.routing.shortest_path(G, orig, dest, weight=\"travel_time\")\n", + "fig, ax = ox.plot.plot_graph_route(G, route, node_size=0)" ] }, { @@ -286,7 +292,9 @@ " G = ox.elevation.add_node_elevations_google(G, api_key=google_elevation_api_key)\n", " G = ox.elevation.add_edge_grades(G)\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=20, edge_linewidth=2, edge_color=\"#333\")\n", + " fig, ax = ox.plot.plot_graph(\n", + " G, node_color=nc, node_size=20, edge_linewidth=2, edge_color=\"#333\"\n", + " )\n", "except ImportError:\n", " print(\"You need a google_elevation_api_key to run this cell.\")" ] @@ -317,8 +325,8 @@ "source": [ "# you can make query an unambiguous dict to help the geocoder find it\n", "place = {\"city\": \"San Francisco\", \"state\": \"California\", \"country\": \"USA\"}\n", - "G = ox.graph_from_place(place, network_type=\"drive\", truncate_by_edge=True)\n", - "fig, ax = ox.plot_graph(G, figsize=(10, 10), node_size=0, edge_color=\"y\", edge_linewidth=0.2)" + "G = ox.graph.graph_from_place(place, network_type=\"drive\", truncate_by_edge=True)\n", + "fig, ax = ox.plot.plot_graph(G, figsize=(10, 10), node_size=0, edge_color=\"y\", edge_linewidth=0.2)" ] }, { @@ -328,8 +336,8 @@ "outputs": [], "source": [ "# you can get networks anywhere in the world\n", - "G = ox.graph_from_place(\"Sinalunga, Italy\", network_type=\"all\")\n", - "fig, ax = ox.plot_graph(G, node_size=0, edge_linewidth=0.5)" + "G = ox.graph.graph_from_place(\"Sinalunga, Italy\", network_type=\"all\")\n", + "fig, ax = ox.plot.plot_graph(G, node_size=0, edge_linewidth=0.5)" ] }, { @@ -342,8 +350,8 @@ "# ...useful when OSM just doesn't already have a polygon for the place you want\n", "wurster_hall = (37.870605, -122.254830)\n", "one_mile = 1609 # meters\n", - "G = ox.graph_from_point(wurster_hall, dist=one_mile, network_type=\"drive\")\n", - "fig, ax = ox.plot_graph(G, node_size=0)" + "G = ox.graph.graph_from_point(wurster_hall, dist=one_mile, network_type=\"drive\")\n", + "fig, ax = ox.plot.plot_graph(G, node_size=0)" ] }, { @@ -369,7 +377,7 @@ "outputs": [], "source": [ "# get NY subway rail network\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", @@ -377,7 +385,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)" ] }, { @@ -398,7 +406,7 @@ "# get all building footprints in some neighborhood\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" ] }, @@ -408,7 +416,7 @@ "metadata": {}, "outputs": [], "source": [ - "fig, ax = ox.plot_footprints(gdf, figsize=(3, 3))" + "fig, ax = ox.plot.plot_footprints(gdf, figsize=(3, 3))" ] }, { @@ -426,7 +434,7 @@ "source": [ "# get all parks and bus stops in some neighborhood\n", "tags = {\"leisure\": \"park\", \"highway\": \"bus_stop\"}\n", - "gdf = ox.features_from_place(place, tags)\n", + "gdf = ox.features.features_from_place(place, tags)\n", "gdf.shape" ] }, @@ -454,7 +462,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.3" + "version": "3.12.8" } }, "nbformat": 4, diff --git a/notebooks/01-overview-osmnx.ipynb b/notebooks/01-overview-osmnx.ipynb index 571353a..db43cf6 100644 --- a/notebooks/01-overview-osmnx.ipynb +++ b/notebooks/01-overview-osmnx.ipynb @@ -65,7 +65,7 @@ "outputs": [], "source": [ "# get the boundary polygon for manhattan, project it, and plot it\n", - "city = ox.geocode_to_gdf(\"Manhattan, New York, USA\")\n", + "city = ox.geocoder.geocode_to_gdf(\"Manhattan, New York, USA\")\n", "city_proj = ox.projection.project_gdf(city)\n", "ax = city_proj.plot(fc=\"gray\", ec=\"none\")\n", "_ = ax.axis(\"off\")" @@ -85,7 +85,7 @@ " \"Emeryville, California, USA\",\n", " \"Alameda, Alameda County, CA, USA\",\n", "]\n", - "east_bay = ox.geocode_to_gdf(place_names)\n", + "east_bay = ox.geocoder.geocode_to_gdf(place_names)\n", "east_bay.to_file(\"./data/east_bay.gpkg\", driver=\"GPKG\")\n", "east_bay = ox.projection.project_gdf(east_bay)\n", "ax = east_bay.plot(fc=\"gray\", ec=\"none\")\n", @@ -99,7 +99,7 @@ "outputs": [], "source": [ "# if you know the OSM ID of the place(s) you want, you can query it directly\n", - "ox.geocode_to_gdf([\"R357794\", \"N8170768521\", \"W427818536\"], by_osmid=True)" + "ox.geocoder.geocode_to_gdf([\"R357794\", \"N8170768521\", \"W427818536\"], by_osmid=True)" ] }, { @@ -119,12 +119,14 @@ " - a .osm formatted xml file\n", "\n", "You can also specify several different network types:\n", + " - 'all' - download all OSM streets and paths, including private-access ones (this is the default network type unless you specify a different one)\n", + " - 'all_public' - download all non-private OSM streets and paths\n", + " - 'bike' - get all streets and paths that cyclists can use\n", " - 'drive' - get drivable public streets (but not service roads)\n", " - 'drive_service' - get drivable streets, including service roads\n", - " - 'walk' - get all streets and paths that pedestrians can use (this network type ignores one-way directionality)\n", - " - 'bike' - get all streets and paths that cyclists can use\n", - " - 'all' - download all non-private OSM streets and paths (this is the default network type unless you specify a different one)\n", - " - 'all_private' - download all OSM streets and paths, including private-access ones" + " - 'walk' - get all streets and paths that pedestrians can use\n", + "\n", + "If you just want a fully bidirectional graph, just configure the `settings` module's `bidirectional_network_types` before creating your graph (it includes the \"walk\" network type by default)." ] }, { @@ -141,11 +143,11 @@ "metadata": {}, "outputs": [], "source": [ - "# define a bounding box in San Francisco\n", - "bbox = 37.79, 37.78, -122.41, -122.43\n", + "# define a bounding box in San Francisco as (left, bottom, right, top)\n", + "bbox = -122.43, 37.78, -122.41, 37.79\n", "\n", "# create network from that bounding box\n", - "G = ox.graph_from_bbox(bbox=bbox, network_type=\"drive_service\")" + "G = ox.graph.graph_from_bbox(bbox, network_type=\"drive_service\")" ] }, { @@ -166,7 +168,7 @@ "location_point = (37.791427, -122.410018)\n", "\n", "# create network from point, inside bounding box of N, S, E, W each 750m from point\n", - "G = ox.graph_from_point(location_point, dist=750, dist_type=\"bbox\", network_type=\"drive\")" + "G = ox.graph.graph_from_point(location_point, dist=750, dist_type=\"bbox\", network_type=\"drive\")" ] }, { @@ -185,8 +187,8 @@ "outputs": [], "source": [ "# same point again, but create network only of nodes within 500m along the network from point\n", - "G = ox.graph_from_point(location_point, dist=500, dist_type=\"network\")\n", - "fig, ax = ox.plot_graph(G, node_color=\"r\")" + "G = ox.graph.graph_from_point(location_point, dist=500, dist_type=\"network\")\n", + "fig, ax = ox.plot.plot_graph(G, node_color=\"r\")" ] }, { @@ -203,8 +205,8 @@ "outputs": [], "source": [ "# create network only of nodes within 500m walking along the network from point\n", - "G = ox.graph_from_point(location_point, dist=500, dist_type=\"network\", network_type=\"walk\")\n", - "fig, ax = ox.plot_graph(G, node_color=\"r\")" + "G = ox.graph.graph_from_point(location_point, dist=500, dist_type=\"network\", network_type=\"walk\")\n", + "fig, ax = ox.plot.plot_graph(G, node_color=\"r\")" ] }, { @@ -222,7 +224,7 @@ "outputs": [], "source": [ "# network from address, including only nodes within 1km along the network from the address\n", - "G = ox.graph_from_address(\n", + "G = ox.graph.graph_from_address(\n", " address=\"350 5th Ave, New York, NY\",\n", " dist=1000,\n", " dist_type=\"network\",\n", @@ -230,7 +232,7 @@ ")\n", "\n", "# you can project the network to UTM (zone calculated automatically)\n", - "G_projected = ox.project_graph(G)" + "G_projected = ox.projection.project_graph(G)" ] }, { @@ -249,7 +251,7 @@ "outputs": [], "source": [ "# create the street network within the city of Piedmont's borders\n", - "G = ox.graph_from_place(\"Piedmont, California, USA\", network_type=\"drive\")" + "G = ox.graph.graph_from_place(\"Piedmont, California, USA\", network_type=\"drive\")" ] }, { @@ -264,7 +266,7 @@ " {\"city\": \"Los Altos Hills\", \"state\": \"California\"},\n", " \"Loyola, California\",\n", "]\n", - "G = ox.graph_from_place(places, truncate_by_edge=True)" + "G = ox.graph.graph_from_place(places, truncate_by_edge=True)" ] }, { @@ -274,8 +276,8 @@ "outputs": [], "source": [ "# save to disk as GeoPackage file then plot\n", - "ox.save_graph_geopackage(G)\n", - "fig, ax = ox.plot_graph(G, node_size=0, edge_color=\"w\", edge_linewidth=0.2)" + "ox.io.save_graph_geopackage(G)\n", + "fig, ax = ox.plot.plot_graph(G, node_size=0, edge_color=\"w\", edge_linewidth=0.2)" ] }, { @@ -297,7 +299,7 @@ "mission_district = calif[(calif[\"CITY\"] == \"San Francisco\") & (calif[\"NAME\"] == \"Mission\")]\n", "polygon = mission_district[\"geometry\"].iloc[0]\n", "\n", - "G2 = ox.graph_from_polygon(polygon, network_type=\"drive_service\")" + "G2 = ox.graph.graph_from_polygon(polygon, network_type=\"drive_service\")" ] }, { @@ -314,7 +316,7 @@ "outputs": [], "source": [ "# create graph from .osm extract file\n", - "G = ox.graph_from_xml(\"./input_data/West-Oakland.osm.bz2\")" + "G = ox.graph.graph_from_xml(\"./input_data/West-Oakland.osm.bz2\")" ] }, { @@ -334,7 +336,9 @@ "source": [ "# create a network around some (lat, lng) point but do not simplify it yet\n", "location_point = (33.299896, -111.831638)\n", - "G = ox.graph_from_point(location_point, network_type=\"drive_service\", dist=500, simplify=False)" + "G = ox.graph.graph_from_point(\n", + " location_point, network_type=\"drive_service\", dist=500, simplify=False\n", + ")" ] }, { @@ -345,7 +349,7 @@ "source": [ "# turn off strict mode and see what nodes we'd remove, in 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)" ] }, { @@ -362,8 +366,8 @@ "outputs": [], "source": [ "# simplify the network\n", - "G = ox.simplify_graph(G)\n", - "fig, ax = ox.plot_graph(G, node_color=\"r\")" + "G = ox.simplification.simplify_graph(G)\n", + "fig, ax = ox.plot.plot_graph(G, node_color=\"r\")" ] }, { @@ -374,7 +378,7 @@ "source": [ "# show the simplified network with edges colored by length\n", "ec = ox.plot.get_edge_colors_by_attr(G, attr=\"length\", cmap=\"plasma_r\")\n", - "fig, ax = ox.plot_graph(\n", + "fig, ax = ox.plot.plot_graph(\n", " G, node_color=\"w\", node_edgecolor=\"k\", node_size=50, edge_color=ec, edge_linewidth=3\n", ")" ] @@ -387,7 +391,7 @@ "source": [ "# highlight all parallel (multiple) edges\n", "ec = [\"gray\" if k == 0 or u == v else \"r\" for u, v, k in G.edges(keys=True)]\n", - "fig, ax = ox.plot_graph(\n", + "fig, ax = ox.plot.plot_graph(\n", " G, node_color=\"w\", node_edgecolor=\"k\", node_size=50, edge_color=ec, edge_linewidth=3\n", ")" ] @@ -400,7 +404,7 @@ "source": [ "# highlight all one-way edges in the mission district network from earlier\n", "ec = [\"r\" if data[\"oneway\"] else \"w\" for u, v, key, data in G2.edges(keys=True, data=True)]\n", - "fig, ax = ox.plot_graph(G2, node_size=0, edge_color=ec, edge_linewidth=1.5, edge_alpha=0.7)" + "fig, ax = ox.plot.plot_graph(G2, node_size=0, edge_color=ec, edge_linewidth=1.5, edge_alpha=0.7)" ] }, { @@ -419,7 +423,7 @@ "outputs": [], "source": [ "# save street network as GeoPackage to work with in GIS\n", - "ox.save_graph_geopackage(G, filepath=\"./data/network.gpkg\")" + "ox.io.save_graph_geopackage(G, filepath=\"./data/network.gpkg\")" ] }, { @@ -429,7 +433,7 @@ "outputs": [], "source": [ "# save street network as GraphML file to work with later in OSMnx or networkx or gephi\n", - "ox.save_graphml(G, filepath=\"./data/network.graphml\")" + "ox.io.save_graphml(G, filepath=\"./data/network.graphml\")" ] }, { @@ -446,7 +450,7 @@ "outputs": [], "source": [ "# calculate basic street network metrics and display average circuity\n", - "stats = ox.basic_stats(G)\n", + "stats = ox.stats.basic_stats(G)\n", "stats[\"circuity_avg\"]" ] }, @@ -484,7 +488,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.3" + "version": "3.12.8" } }, "nbformat": 4, diff --git a/notebooks/02-routing-speed-time.ipynb b/notebooks/02-routing-speed-time.ipynb index 55f5c4c..19a89f9 100644 --- a/notebooks/02-routing-speed-time.ipynb +++ b/notebooks/02-routing-speed-time.ipynb @@ -38,8 +38,8 @@ "outputs": [], "source": [ "place = \"Piedmont, California, USA\"\n", - "G = ox.graph_from_place(place, network_type=\"drive\")\n", - "Gp = ox.project_graph(G)" + "G = ox.graph.graph_from_place(place, network_type=\"drive\")\n", + "Gp = ox.projection.project_graph(G)" ] }, { @@ -72,7 +72,7 @@ "outputs": [], "source": [ "# find each nearest node to several points, and optionally return distance\n", - "nodes, dists = ox.nearest_nodes(Gp, X, Y, return_dist=True)" + "nodes, dists = ox.distance.nearest_nodes(Gp, X, Y, return_dist=True)" ] }, { @@ -82,7 +82,7 @@ "outputs": [], "source": [ "# or, find the nearest node to a single point\n", - "node = ox.nearest_nodes(Gp, X0, Y0)\n", + "node = ox.distance.nearest_nodes(Gp, X0, Y0)\n", "node" ] }, @@ -93,7 +93,7 @@ "outputs": [], "source": [ "# find each nearest edge to several points, and optionally return distance\n", - "edges, dists = ox.nearest_edges(Gp, X, Y, return_dist=True)" + "edges, dists = ox.distance.nearest_edges(Gp, X, Y, return_dist=True)" ] }, { @@ -103,7 +103,7 @@ "outputs": [], "source": [ "# find the nearest edge to a single point\n", - "edge = ox.nearest_edges(Gp, X0, Y0)\n", + "edge = ox.distance.nearest_edges(Gp, X0, Y0)\n", "edge" ] }, @@ -125,8 +125,8 @@ "# find the shortest path (by distance) between these nodes then plot it\n", "orig = list(G)[0]\n", "dest = list(G)[120]\n", - "route = ox.shortest_path(G, orig, dest, weight=\"length\")\n", - "fig, ax = ox.plot_graph_route(G, route, route_color=\"y\", route_linewidth=6, node_size=0)" + "route = ox.routing.shortest_path(G, orig, dest, weight=\"length\")\n", + "fig, ax = ox.plot.plot_graph_route(G, route, route_color=\"y\", route_linewidth=6, node_size=0)" ] }, { @@ -142,8 +142,10 @@ "metadata": {}, "outputs": [], "source": [ - "routes = ox.k_shortest_paths(G, orig, dest, k=30, weight=\"length\")\n", - "fig, ax = ox.plot_graph_routes(G, list(routes), route_colors=\"y\", route_linewidth=4, node_size=0)" + "routes = ox.routing.k_shortest_paths(G, orig, dest, k=30, weight=\"length\")\n", + "fig, ax = ox.plot.plot_graph_routes(\n", + " G, list(routes), route_colors=\"y\", route_linewidth=4, node_size=0\n", + ")" ] }, { @@ -162,10 +164,10 @@ "outputs": [], "source": [ "# impute speed on all edges missing data\n", - "G = ox.add_edge_speeds(G)\n", + "G = ox.routing.add_edge_speeds(G)\n", "\n", "# calculate travel time (seconds) for all edges\n", - "G = ox.add_edge_travel_times(G)" + "G = ox.routing.add_edge_travel_times(G)" ] }, { @@ -175,7 +177,7 @@ "outputs": [], "source": [ "# see mean speed/time values by road type\n", - "edges = ox.graph_to_gdfs(G, nodes=False)\n", + "edges = ox.convert.graph_to_gdfs(G, nodes=False)\n", "edges[\"highway\"] = edges[\"highway\"].astype(str)\n", "edges.groupby(\"highway\")[[\"length\", \"speed_kph\", \"travel_time\"]].mean().round(1)" ] @@ -189,8 +191,8 @@ "# same thing again, but this time pass in a few default speed values (km/hour)\n", "# to fill in edges with missing `maxspeed` from OSM\n", "hwy_speeds = {\"residential\": 35, \"secondary\": 50, \"tertiary\": 60}\n", - "G = ox.add_edge_speeds(G, hwy_speeds=hwy_speeds)\n", - "G = ox.add_edge_travel_times(G)" + "G = ox.routing.add_edge_speeds(G, hwy_speeds=hwy_speeds)\n", + "G = ox.routing.add_edge_travel_times(G)" ] }, { @@ -202,8 +204,8 @@ "# calculate two routes by minimizing travel distance vs travel time\n", "orig = list(G)[1]\n", "dest = list(G)[120]\n", - "route1 = ox.shortest_path(G, orig, dest, weight=\"length\")\n", - "route2 = ox.shortest_path(G, orig, dest, weight=\"travel_time\")" + "route1 = ox.routing.shortest_path(G, orig, dest, weight=\"length\")\n", + "route2 = ox.routing.shortest_path(G, orig, dest, weight=\"travel_time\")" ] }, { @@ -213,7 +215,7 @@ "outputs": [], "source": [ "# plot the routes\n", - "fig, ax = ox.plot_graph_routes(\n", + "fig, ax = ox.plot.plot_graph_routes(\n", " G, routes=[route1, route2], route_colors=[\"r\", \"y\"], route_linewidth=6, node_size=0\n", ")" ] @@ -284,7 +286,7 @@ "# %%time\n", "# it takes 2.3 seconds to solve all the routes using all the cores on my computer\n", "# I have a 24-thread AMD 5900x: performance will depend on your specific CPU\n", - "# routes = ox.shortest_path(G, origs, dests, weight=\"travel_time\", cpus=None)" + "# routes = ox.routing.shortest_path(G, origs, dests, weight=\"travel_time\", cpus=None)" ] }, { @@ -295,7 +297,7 @@ "source": [ "%%time\n", "# it takes 29 seconds to solve all the routes using just 1 core on my computer\n", - "routes = ox.shortest_path(G, origs, dests, weight=\"travel_time\", cpus=1)" + "routes = ox.routing.shortest_path(G, origs, dests, weight=\"travel_time\", cpus=1)" ] }, { @@ -333,7 +335,7 @@ "metadata": {}, "outputs": [], "source": [ - "G2 = ox.graph_from_address(\n", + "G2 = ox.graph.graph_from_address(\n", " \"N. Sicily Pl., Chandler, Arizona\",\n", " dist=800,\n", " network_type=\"drive\",\n", @@ -343,8 +345,8 @@ "destination = (33.312994, -111.894998)\n", "origin_node = ox.distance.nearest_nodes(G2, origin[1], origin[0])\n", "destination_node = ox.distance.nearest_nodes(G2, destination[1], destination[0])\n", - "route = ox.shortest_path(G2, origin_node, destination_node)\n", - "fig, ax = ox.plot_graph_route(G2, route, route_color=\"c\", node_size=0)" + "route = ox.routing.shortest_path(G2, origin_node, destination_node)\n", + "fig, ax = ox.plot.plot_graph_route(G2, route, route_color=\"c\", node_size=0)" ] }, { @@ -361,13 +363,13 @@ "outputs": [], "source": [ "location_point = (33.299896, -111.831638)\n", - "G2 = ox.graph_from_point(location_point, dist=400, truncate_by_edge=True)\n", + "G2 = ox.graph.graph_from_point(location_point, dist=400, truncate_by_edge=True)\n", "origin = (33.301821, -111.829871)\n", "destination = (33.301402, -111.833108)\n", "origin_node = ox.distance.nearest_nodes(G2, origin[1], origin[0])\n", "destination_node = ox.distance.nearest_nodes(G2, destination[1], destination[0])\n", - "route = ox.shortest_path(G2, origin_node, destination_node)\n", - "fig, ax = ox.plot_graph_route(G2, route, route_color=\"c\", node_size=0)" + "route = ox.routing.shortest_path(G2, origin_node, destination_node)\n", + "fig, ax = ox.plot.plot_graph_route(G2, route, route_color=\"c\", node_size=0)" ] }, { @@ -394,7 +396,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.9" + "version": "3.12.8" } }, "nbformat": 4, diff --git a/notebooks/03-graph-place-queries.ipynb b/notebooks/03-graph-place-queries.ipynb index d29ed85..3822dda 100644 --- a/notebooks/03-graph-place-queries.ipynb +++ b/notebooks/03-graph-place-queries.ipynb @@ -62,13 +62,13 @@ "outputs": [], "source": [ "# neighborhoods or boroughs\n", - "gdf = ox.geocode_to_gdf(\"Manhattan, New York, New York, USA\")\n", + "gdf = ox.geocoder.geocode_to_gdf(\"Manhattan, New York, New York, USA\")\n", "\n", "# counties\n", - "gdf = ox.geocode_to_gdf(\"Cook County, Illinois, United States\")\n", + "gdf = ox.geocoder.geocode_to_gdf(\"Cook County, Illinois, United States\")\n", "\n", "# states\n", - "gdf = ox.geocode_to_gdf(\"Iowa\")" + "gdf = ox.geocoder.geocode_to_gdf(\"Iowa\")" ] }, { @@ -78,7 +78,7 @@ "outputs": [], "source": [ "# you can get multiple countries in a single query\n", - "gdf = ox.geocode_to_gdf([\"United Kingdom\", \"Ireland\"])\n", + "gdf = ox.geocoder.geocode_to_gdf([\"United Kingdom\", \"Ireland\"])\n", "\n", "# or multiple cities\n", "places = [\n", @@ -88,7 +88,7 @@ " \"Emeryville, California, USA\",\n", " \"Alameda, Alameda County, CA, USA\",\n", "]\n", - "gdf = ox.geocode_to_gdf(places)" + "gdf = ox.geocoder.geocode_to_gdf(places)" ] }, { @@ -107,10 +107,10 @@ "outputs": [], "source": [ "# oops, this gets the county of alameda rather than the city!\n", - "alameda1 = ox.geocode_to_gdf(\"Alameda, California, USA\")\n", + "alameda1 = ox.geocoder.geocode_to_gdf(\"Alameda, California, USA\")\n", "\n", "# this gets the city of alameda\n", - "alameda2 = ox.geocode_to_gdf(\n", + "alameda2 = ox.geocoder.geocode_to_gdf(\n", " {\n", " \"city\": \"Alameda\",\n", " \"county\": \"Alameda County\",\n", @@ -138,7 +138,7 @@ "metadata": {}, "outputs": [], "source": [ - "mexico = ox.geocode_to_gdf(\"Mexico\", which_result=2)\n", + "mexico = ox.geocoder.geocode_to_gdf(\"Mexico\", which_result=2)\n", "type(mexico[\"geometry\"].iloc[0])" ] }, @@ -149,7 +149,7 @@ "outputs": [], "source": [ "# let the geocoder find the first Polygon/MultiPolygon result\n", - "mexico = ox.geocode_to_gdf(\"Mexico\", which_result=None)\n", + "mexico = ox.geocoder.geocode_to_gdf(\"Mexico\", which_result=None)\n", "type(mexico[\"geometry\"].iloc[0])" ] }, @@ -160,7 +160,7 @@ "outputs": [], "source": [ "# instead of a string, you can pass a dict containing a structured query for better precision\n", - "mexico = ox.geocode_to_gdf({\"country\": \"Mexico\"})\n", + "mexico = ox.geocoder.geocode_to_gdf({\"country\": \"Mexico\"})\n", "type(mexico[\"geometry\"].iloc[0])" ] }, @@ -171,7 +171,7 @@ "outputs": [], "source": [ "# you can pass multiple queries with mixed types (dicts and strings)\n", - "mx_gt_tx = ox.geocode_to_gdf([{\"country\": \"Mexico\"}, \"Guatemala\", {\"state\": \"Texas\"}])\n", + "mx_gt_tx = ox.geocoder.geocode_to_gdf([{\"country\": \"Mexico\"}, \"Guatemala\", {\"state\": \"Texas\"}])\n", "mx_gt_tx = ox.projection.project_gdf(mx_gt_tx)\n", "ax = mx_gt_tx.plot(fc=\"gray\", ec=\"w\")\n", "_ = ax.axis(\"off\")" @@ -181,7 +181,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "If you query 'France', OSM returns the country with all its overseas territories as result 1 and Metropolitan France alone as later down the results. Passing `which_result` can help you specifically retrieve the desired geocoding result, or you can query the geospatial feature you want by its OSM ID." + "If you query 'France', OSM returns the country with all its overseas territories as result 1 and Metropolitan France alone as later down the results. Passing `which_result` can help you specifically retrieve the desired geocoding result." ] }, { @@ -190,19 +190,26 @@ "metadata": {}, "outputs": [], "source": [ - "france = ox.geocode_to_gdf(\"France\")\n", + "france = ox.geocoder.geocode_to_gdf(\"France\")\n", "france = ox.projection.project_gdf(france)\n", "ax = france.plot(fc=\"gray\", ec=\"none\")\n", "_ = ax.axis(\"off\")" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Finally, note that you can also retrieve an element by its OSM ID rather, than trying to geocode a place name, by passing `by_osmid=True` to the function. See documentation for usage details." + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "france = ox.projection.project_gdf(ox.geocode_to_gdf(\"R1403916\", by_osmid=True))\n", + "france = ox.projection.project_gdf(ox.geocoder.geocode_to_gdf(\"R1403916\", by_osmid=True))\n", "ax = france.plot(fc=\"gray\", ec=\"none\")\n", "_ = ax.axis(\"off\")" ] @@ -211,8 +218,6 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Finally, note that you can also query by OSM ID rather than place name by passing `by_osmid=True` to the function. See documentation for usage details.\n", - "\n", "## 2. Get street networks by place name\n", "\n", "This \"by place\" querying logic works the same as the place boundary querying we just saw above." @@ -225,7 +230,7 @@ "outputs": [], "source": [ "# get the walking network for piedmont\n", - "G = ox.graph_from_place(\"Piedmont, California, USA\", network_type=\"walk\")" + "G = ox.graph.graph_from_place(\"Piedmont, California, USA\", network_type=\"walk\")" ] }, { @@ -235,10 +240,9 @@ "outputs": [], "source": [ "# or get the walking network within a 500 meter buffer of piedmont\n", - "polygon = ox.geocode_to_gdf(\"Piedmont, California, USA\").loc[0, \"geometry\"]\n", - "poly_proj, crs_proj = ox.projection.project_geometry(polygon)\n", - "polygon, _ = ox.projection.project_geometry(poly_proj.buffer(500), crs=crs_proj, to_latlong=True)\n", - "G = ox.graph_from_polygon(polygon, network_type=\"walk\")" + "gdf = ox.geocoder.geocode_to_gdf(\"Piedmont, CA, USA\")\n", + "polygon = ox.utils_geo.buffer_geometry(gdf.iloc[0][\"geometry\"], 500)\n", + "G = ox.graph.graph_from_polygon(polygon, network_type=\"walk\")" ] }, { @@ -255,8 +259,8 @@ "]\n", "\n", "# use retain_all to keep all disconnected subgraphs (e.g. if your places aren't contiguous)\n", - "G = ox.graph_from_place(places, network_type=\"drive\", retain_all=True)\n", - "fig, ax = ox.plot_graph(G, node_size=0, edge_color=\"#FFFF5C\", edge_linewidth=0.25)" + "G = ox.graph.graph_from_place(places, network_type=\"drive\", retain_all=True)\n", + "fig, ax = ox.plot.plot_graph(G, node_size=0, edge_color=\"#FFFF5C\", edge_linewidth=0.25)" ] }, { @@ -270,7 +274,7 @@ " {\"city\": \"Daly City\", \"state\": \"California\"},\n", " {\"city\": \"South San Francisco\", \"state\": \"California\"},\n", "]\n", - "G = ox.graph_from_place(places, network_type=\"drive\")" + "G = ox.graph.graph_from_place(places, network_type=\"drive\")" ] }, { @@ -280,7 +284,7 @@ "outputs": [], "source": [ "# get the network for the borough of manhattan\n", - "G = ox.graph_from_place(\"Manhattan, New York, New York, USA\", network_type=\"drive\")" + "G = ox.graph.graph_from_place(\"Manhattan, New York, New York, USA\", network_type=\"drive\")" ] }, { @@ -291,7 +295,7 @@ "source": [ "# get the network for a neighborhood\n", "place = \"SoHo, New York, NY\"\n", - "G = ox.graph_from_place(place, network_type=\"drive\")" + "G = ox.graph.graph_from_place(place, network_type=\"drive\")" ] }, { @@ -303,8 +307,10 @@ "%%time\n", "# get the network for all of LA\n", "# takes a couple minutes to do all the downloading and processing\n", + "# retain_all=True means we'll keep all the disconnected graph components\n", + "# simplify=False means we won't simplify the graph topology\n", "place = \"Los Angeles, California, USA\"\n", - "G = ox.graph_from_place(place, network_type=\"drive\", simplify=False, retain_all=True)" + "G = ox.graph.graph_from_place(place, network_type=\"drive\", simplify=False, retain_all=True)" ] }, { @@ -314,7 +320,7 @@ "outputs": [], "source": [ "# create a network constrained to the shape of hong kong island\n", - "G = ox.graph_from_place(\"Hong Kong Island\", network_type=\"drive\")" + "G = ox.graph.graph_from_place(\"Hong Kong Island\", network_type=\"drive\")" ] }, { @@ -342,7 +348,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.9" + "version": "3.12.8" } }, "nbformat": 4,