From 64b1aedea4cca137c61bb59c82100dd3f3e0cbd2 Mon Sep 17 00:00:00 2001 From: Renata Imai Date: Tue, 10 Sep 2024 11:13:31 -0300 Subject: [PATCH] removes geopandas from some examples --- .../plot_route_choice_basics.py | 3 +- .../plot_route_choice_set.py | 5 ++- .../plot_subarea_analysis.py | 11 +++---- .../creating_models/plot_create_from_layer.py | 31 ++--------------- .../creating_models/plot_create_from_osm.py | 33 +++---------------- .../examples/visualization/plot_display.py | 2 +- 6 files changed, 17 insertions(+), 68 deletions(-) diff --git a/docs/source/examples/assignment_workflows/plot_route_choice_basics.py b/docs/source/examples/assignment_workflows/plot_route_choice_basics.py index f1b4e3eab..49eeee784 100644 --- a/docs/source/examples/assignment_workflows/plot_route_choice_basics.py +++ b/docs/source/examples/assignment_workflows/plot_route_choice_basics.py @@ -169,11 +169,10 @@ # let's define a function to plot assignment results def plot_results(link_loads): import folium - import geopandas as gpd link_loads = link_loads[link_loads.tot > 0] max_load = link_loads["tot"].max() - links = gpd.GeoDataFrame(project.network.links.data, crs=4326) + links = project.network.links.data loaded_links = links.merge(link_loads, on="link_id", how="inner") loads_lyr = folium.FeatureGroup("link_loads") diff --git a/docs/source/examples/assignment_workflows/plot_route_choice_set.py b/docs/source/examples/assignment_workflows/plot_route_choice_set.py index 98ff23c07..1a7f06235 100644 --- a/docs/source/examples/assignment_workflows/plot_route_choice_set.py +++ b/docs/source/examples/assignment_workflows/plot_route_choice_set.py @@ -101,7 +101,6 @@ # %% # Now we will plot the paths we just created for the second OD pair import folium -import geopandas as gpd # %% # Let's create a separate for each route so we can visualize one at a time @@ -115,8 +114,8 @@ # %% # We get the data we will use for the plot: Links, Nodes and the route choice set -links = gpd.GeoDataFrame(project.network.links.data, crs=4326) -nodes = gpd.GeoDataFrame(project.network.nodes.data, crs=4326) +links = project.network.links.data +nodes = project.network.nodes.data plot_routes = choice_set[(choice_set["origin id"] == 77011)]["route set"].values diff --git a/docs/source/examples/assignment_workflows/plot_subarea_analysis.py b/docs/source/examples/assignment_workflows/plot_subarea_analysis.py index e92397fbe..579319960 100644 --- a/docs/source/examples/assignment_workflows/plot_subarea_analysis.py +++ b/docs/source/examples/assignment_workflows/plot_subarea_analysis.py @@ -17,7 +17,6 @@ import itertools import pandas as pd -import geopandas as gpd import numpy as np import folium @@ -110,7 +109,7 @@ # # the union of their geometry. It's best to choose a polygon that avoids any unnecessary intersections with links as # the resource requirements of this approach grow quadratically with the number of links cut. zones_of_interest = [29, 30, 31, 32, 33, 34, 37, 38, 39, 40, 49, 50, 51, 52, 57, 58, 59, 60] -zones = gpd.GeoDataFrame(project.zoning.data).set_index("zone_id") +zones = project.zoning.data.set_index("zone_id") zones = zones.loc[zones_of_interest] zones.head() @@ -164,7 +163,7 @@ def plot_results(link_loads): link_loads = link_loads[link_loads.tot > 0] max_load = link_loads["tot"].max() - links = gpd.GeoDataFrame(project.network.links.data, crs=4326) + links = project.network.links.data loaded_links = links.merge(link_loads, on="link_id", how="inner") loads_lyr = folium.FeatureGroup("link_loads") @@ -202,18 +201,18 @@ def plot_results(link_loads): # %% # We take the union of this GeoDataFrame as our polygon. -poly = zones.unary_union +poly = zones.union_all() poly # %% # It's useful later on to know which links from the network cross our polygon. -links = gpd.GeoDataFrame(project.network.links.data) +links = project.network.links.data inner_links = links[links.crosses(poly.boundary)].sort_index() inner_links.head() # %% # As well as which nodes are interior. -nodes = gpd.GeoDataFrame(project.network.nodes.data).set_index("node_id") +nodes = project.network.nodes.data.set_index("node_id") inside_nodes = nodes.sjoin(zones, how="inner").sort_index() inside_nodes.head() diff --git a/docs/source/examples/creating_models/plot_create_from_layer.py b/docs/source/examples/creating_models/plot_create_from_layer.py index 97ddbe2de..c0abe4bc0 100644 --- a/docs/source/examples/creating_models/plot_create_from_layer.py +++ b/docs/source/examples/creating_models/plot_create_from_layer.py @@ -132,37 +132,12 @@ new_link.save() # %% -# We grab all the links data as a Pandas DataFrame so we can process it easier +# We grab all the links data as a geopandas GeoDataFrame so we can process it easier links = project.network.links.data # %% -# We create a Folium layer -network_links = folium.FeatureGroup("links") - -#%% -# We do some Python magic to transform this dataset into the format required by Folium. -# We are only getting `link_id` and `link_type` into the map, but we could get other pieces of info as well - -# %% -for i, row in links.iterrows(): - points = row.geometry.wkt.replace("LINESTRING ", "").replace("(", "").replace(")", "").split(", ") - points = "[[" + "],[".join([p.replace(" ", ", ") for p in points]) + "]]" - # We need to take from x/y to lat/long - points = [[x[1], x[0]] for x in eval(points)] - - line = folium.vector_layers.PolyLine( - points, popup=f"link_id: {row.link_id}", tooltip=f"{row.link_type}", color="blue", weight=10 - ).add_to(network_links) - -# %% -# We get the center of the region we are working with some SQL magic -curr = project.conn.cursor() -curr.execute("select avg(xmin), avg(ymin) from idx_links_geometry") -long, lat = curr.fetchone() - -# %% -map_osm = folium.Map(location=[lat, long], zoom_start=15) -network_links.add_to(map_osm) +# Let's plot our network! +map_osm = links.explore(color="blue", weight=10, tooltip="link_type", popup="link_id", name="links") folium.LayerControl().add_to(map_osm) map_osm diff --git a/docs/source/examples/creating_models/plot_create_from_osm.py b/docs/source/examples/creating_models/plot_create_from_osm.py index 5a5d84dae..162cf5058 100644 --- a/docs/source/examples/creating_models/plot_create_from_osm.py +++ b/docs/source/examples/creating_models/plot_create_from_osm.py @@ -37,41 +37,18 @@ project.network.create_from_osm(place_name="Nauru") # %% -# We can also choose to create a model from a polygon (which must be in `EPSG:4326`) -# Or from a Polygon defined by a bounding box, for example. +# We can also choose to create a model from a polygon (which must be in ``EPSG:4326``) +# or from a Polygon defined by a bounding box, for example. # project.network.create_from_osm(model_area=box(-112.185, 36.59, -112.179, 36.60)) # %% -# We grab all the links data as a Pandas DataFrame so we can process it easier +# We grab all the links data as a geopandas GeoDataFrame so we can process it easier links = project.network.links.data # %% -# We create a Folium layer -network_links = folium.FeatureGroup("links") - -# %% -# We do some Python magic to transform this dataset into the format required by Folium. -# We are only getting link_id and link_type into the map, but we could get other pieces of info as well. -for i, row in links.iterrows(): - points = row.geometry.wkt.replace("LINESTRING ", "").replace("(", "").replace(")", "").split(", ") - points = "[[" + "],[".join([p.replace(" ", ", ") for p in points]) + "]]" - # we need to take from x/y to lat/long - points = [[x[1], x[0]] for x in eval(points)] - - line = folium.vector_layers.PolyLine( - points, popup=f"link_id: {row.link_id}", tooltip=f"{row.link_type}", color="blue", weight=10 - ).add_to(network_links) - -# %% -# We get the center of the region we are working with some SQL magic -curr = project.conn.cursor() -curr.execute("select avg(xmin), avg(ymin) from idx_links_geometry") -long, lat = curr.fetchone() - -# %% -map_osm = folium.Map(location=[lat, long], zoom_start=14) -network_links.add_to(map_osm) +# Let's plot our network! +map_osm = links.explore(color="blue", weight=10, tooltip="link_type", popup="link_id", name="links") folium.LayerControl().add_to(map_osm) map_osm diff --git a/docs/source/examples/visualization/plot_display.py b/docs/source/examples/visualization/plot_display.py index 24892a62e..a070ef9cf 100644 --- a/docs/source/examples/visualization/plot_display.py +++ b/docs/source/examples/visualization/plot_display.py @@ -29,7 +29,7 @@ project = create_example(fldr, "nauru") # %% -# We grab all the links data as a Pandas dataframe so we can process it easier +# We grab all the links data as a geopandas GeoDataFrame so we can process it easier links = project.network.links.data nodes = project.network.nodes.data