Python GeoDataFrame – Creating a GeoDataFrame from List of LineStrings or Routes

geodataframegeopandaslinestringosmnxpython

I have created a separate LineString for each origin-destination couples
of subdivisions and I did it manually cause when I use a for loop it creates a dataframe with 1 LineString from all lines…
Now I want to create a DataFrame containing all lineStrings with length of each one,
I have found this block of code online but it takes only 1 line at a time and when I pass the [routes lines variables] list,
I get the error

ValueError: 1 columns passed, passed data had 16 columns

Gamle_routes_geom = gpd.GeoDataFrame([[Gamle_route_Grünerløkka_line,Gamle_route_Sagene_line,Gamle_route_Hanshaugen_line,Gamle_route_Frogner_line,
                                  Gamle_route_Ullern_line,Gamle_route_Vestre_line,Gamle_route_Nordre_Aker_line,
                                  Gamle_route_Bjerke_line,Gamle_route_Grorud_line,Gamle_route_Stovner_line,
                                  Gamle_route_Alna_line,Gamle_route_Østensjø_line,Gamle_route_nordstrand_line,
                                  Gamle_route_Søndre_line,Gamle_route_Sentrum_line,Gamle_route_Marka_line]],
                             geometry='geometry', crs=oslo_edges_proj.crs, columns=['geometry'])

Add a list of osmids associated with the route
had to change this to index as osmid was the column index of the object

Gamle_routes_geom.loc[0, 'osmids'] = str(list(gamle_routes_nodes.index.values))

Calculate the route length

Gamle_routes_geom['length_m'] = Gamle_routes_geom.length

Edit:
I will share the process:

1- pulled the data from osm using ox.graph_from_place()

2- got the nearest point for each centroid separately as follow:

Gamle_orig_node = ox.distance.nearest_nodes(oslo_graph_proj, Gamle_centroid.x, Gamle_centroid.y)

3- used nx.shortest_path() for each couple of orig-dest as follow:

Gamle_route_Grünerløkka = nx.shortest_path(G=oslo_graph_proj, source=Gamle_orig_node, target=Grünerløkka_orig_node, weight='length')

4- got the routes nodes from the main graph

for i in gamle_routes:
    gamle_routes_nodes = oslo_nodes_proj.loc[i]

5- got the nodes of each route from the nodes_graph

Gamle_route_Grünerløkka_nodes = oslo_nodes_proj.loc[Gamle_route_Grünerløkka]

6- got each route as a LineString

Gamle_route_Grünerløkka_line = LineString(list(Gamle_route_Grünerløkka_nodes.geometry.values))

7- now as the process done separately for each couple I have got separated lines and I want them in a dataframe a row for each couple to have the length of each route and plot on folium

Best Answer

Try changing your code as seen below:

Gamle_routes_geom = gpd.GeoDataFrame({'geometry':[Gamle_route_Grünerløkka_line,
                                                  Gamle_route_Sagene_line,
                                                  Gamle_route_Hanshaugen_line,
                                                  Gamle_route_Frogner_line,
                                                  Gamle_route_Ullern_line,
                                                  Gamle_route_Vestre_line,
                                                  Gamle_route_Nordre_Aker_line,
                                                  Gamle_route_Bjerke_line,
                                                  Gamle_route_Grorud_line,
                                                  Gamle_route_Stovner_line,
                                                  Gamle_route_Alna_line,
                                                  Gamle_route_Østensjø_line,
                                                  Gamle_route_nordstrand_line,
                                                  Gamle_route_Søndre_line,
                                                  Gamle_route_Sentrum_line,
                                                  Gamle_route_Marka_line]},
                                      geometry='geometry', 
                                      crs=oslo_edges_proj.crs)