I want to subdivide streets by maximum distance as described in this question and response. My problem is that I do not understand the "Interpolate Points" function output.
My current code is:
# Imports
!pip install osmnx
import osmnx as ox
import networkx as nx
# Enter the place name you want to search
# Find accepted names at https://nominatim.openstreetmap.org/ui/search.html?q=Loop%2C+Chicago%2C+Illinois
place_name = "Macon, Macon County, Illinois, United States"
# Generate a graph of pedestrian circulation in nodes and edges
graph = ox.graph_from_place(place_name, network_type="walk")
fig, ax = ox.plot_graph(graph)
# Interpolate every 10 meters
multiline_r = ox.utils_geo.interpolate_points(graph, 10)
display(multiline_r)
And it gives me a strange output type called "generator" that cannot be transformed into a graph. I have also tried feeding the interpolate points function individual line segments and it gives me this same value. I was expecting some sort of output that I could build into a graph, for example a list of points.
In short, does anybody have a more detailed explanation for implementing the "Interpolate Points" function described in the previous post?
Edit: My solution code is
# imports
!pip install osmnx
import osmnx as ox
import pandas as pd
import geopandas as gpd
!pip install momepy
import momepy
import networkx as nx
import matplotlib.pyplot as plt
# Enter the place name you want to search
# Find accepted names at https://nominatim.openstreetmap.org/ui/search.html?q=Loop%2C+Chicago%2C+Illinois
place_name = "Macon, Macon County, Illinois, United States"
# Generate a graph of pedestrian circulation in nodes and edges
graph = ox.graph_from_place(place_name, network_type="walk")
fig, ax = ox.plot_graph(graph)
# Extract nodes and edges from the graph
nodes, edges = ox.graph_to_gdfs(graph)
# gdf to dataframe
edges_df = pd.DataFrame(edges)
# return geometry column as list of linestrings
geometry_list = edges_df['geometry'].tolist()
display(geometry_list)
# create new evenly spaced edges
lst = [] # initiallize emtpy list
for i in range(len(geometry_list)): # iterate over geometry list
temp_lst = list(ox.utils_geo.interpolate_points(geometry_list[i], 0.0005))
for i in range(len(temp_lst)-1): # append every item in temp list
temp_edge = [temp_lst[i+1], temp_lst[i]] # create edge
lst.append(temp_edge)
# Convert coordinate list into linestring list
linestrings = [] # create empty list
for i in range(len(lst)): # iterate over lst
L = ox.utils_geo.LineString(lst[i])
linestrings.append(L)
linestrings
# convert linestrings from list to geodataframe
gdf1 = gpd.GeoDataFrame({'geometry':linestrings},
geometry='geometry',
crs=3528)
# transform the geopandas file into a networkx graph
G_primal = momepy.gdf_to_nx(gdf1, approach="primal")
f, ax = plt.subplots(1, 2, figsize=(12, 6), sharex=True, sharey=True)
gdf1.plot(color="k", ax=ax[0])
for i, facet in enumerate(ax):
facet.set_title(("pedestrian", "Graph")[i])
facet.axis("off")
#add_basemap(facet) # this wasn't working for some reason so I cut it out
nx.draw(
G_primal, {n: [n[0], n[1]] for n in list(G_primal.nodes)}, ax=ax[1], node_size=50
)
I do not understand the "Interpolate Points" function output.
The OSMnx documentation describes the function's output: "points (generator) – tuples of (x, y) floats of the interpolated points’ coordinates"
it gives me a strange output type called "generator"
If you are wondering what a Python generator is: https://stackoverflow.com/a/1756156/7321942
I was expecting some sort of output that I could build into a graph, for example a list of points.
This function returns a generator of points, which you could cast to a list if you'd like.