2

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

0

1 Answer 1

1

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)
14
  • 1
    Also: if the solution provided worked, please consider accepting the answer and upvoting it too =)
    – Felipe D.
    Dec 30, 2021 at 16:40
  • 1
    I have selected it as an accepted answer, tho I can't upvote for the reason I need some reputation to do so :) Dec 30, 2021 at 16:51
  • 1
    It is clear now Thank you so much for your time Dec 30, 2021 at 17:16
  • 1
    Thank you, and regarding the master dictionary, I will see if I can replace it with the current one and what is the logic behind 2 crs? Dec 30, 2021 at 23:13
  • 1
    Ah, sorry, that was a typo! the command should be gpd.GeoDataFrame(master_results, geometry='destination', crs=oslo_edges_proj.crs).
    – Felipe D.
    Dec 31, 2021 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.