Skip to content

Commit

Permalink
removes geopandas from some examples
Browse files Browse the repository at this point in the history
  • Loading branch information
r-akemii committed Sep 10, 2024
1 parent 6e0c349 commit 64b1aed
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import itertools

import pandas as pd
import geopandas as gpd
import numpy as np
import folium

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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()

Expand Down
31 changes: 3 additions & 28 deletions docs/source/examples/creating_models/plot_create_from_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"<b>link_id: {row.link_id}</b>", 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

Expand Down
33 changes: 5 additions & 28 deletions docs/source/examples/creating_models/plot_create_from_osm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"<b>link_id: {row.link_id}</b>", 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

Expand Down
2 changes: 1 addition & 1 deletion docs/source/examples/visualization/plot_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 64b1aed

Please sign in to comment.