I have a trip data including lat,lng. I want to simulate the shortest paths of the trip,and export the paths to shpfile.Then I'll do the Linedensity Analysis to discover changes in the trips. I don't know how to export the paths as a shpfile in once. my sample data is below.you can save as station.csv
{ ride_id rideable_type started_at ended_at start_station_name start_station_id end_station_name end_station_id start_lat start_lng end_lat end_lng member_casual A847FADBBC638E45 docked_bike 2020/4/26 17:45 2020/4/26 18:12 Eckhart Park 86 Lincoln Ave & Diversey Pkwy 152 41.8964 -87.661 41.9322 -87.6586 member 5405B80E996FF60D docked_bike 2020/4/17 17:08 2020/4/17 17:17 Drake Ave & Fullerton Ave 503 Kosciuszko Park 499 41.9244 -87.7154 41.9306 -87.7238 member 5DD24A79A4E006F4 docked_bike 2020/4/1 17:54 2020/4/1 18:08 McClurg Ct & Erie St 142 Indiana Ave & Roosevelt Rd 255 41.8945 -87.6179 41.8679 -87.623 member 2A59BBDF5CDBA725 docked_bike 2020/4/7 12:50 2020/4/7 13:02 California Ave & Division St 216 Wood St & Augusta Blvd 657 41.903 -87.6975 41.8992 -87.6722 member 27AD306C119C6158 docked_bike 2020/4/18 10:22 2020/4/18 11:15 Rush St & Hubbard St 125 Sheridan Rd & Lawrence Ave 323 41.8902 -87.6262 41.9695 -87.6547 casual 356216E875132F61 docked_bike 2020/4/30 17:55 2020/4/30 18:01 Mies van der Rohe Way & Chicago Ave 173 Streeter Dr & Grand Ave 35 41.8969 -87.6217 41.8923 -87.612 member A2759CB06A81F2BC docked_bike 2020/4/2 14:47 2020/4/2 14:52 Streeter Dr & Grand Ave 35 Fairbanks St & Superior St 635 41.8923 -87.612 41.8957 -87.6201 member FC8BC2E2D54F35ED docked_bike 2020/4/7 12:22 2020/4/7 13:38 Ogden Ave & Roosevelt Rd 434 Western Ave & Congress Pkwy 382 41.8665 -87.6847 41.8747 -87.6864 casual 9EC5648678DE06E6 docked_bike 2020/4/15 10:30 2020/4/15 10:35 LaSalle Dr & Huron St 627 Larrabee St & Division St 359 41.8949 -87.6323 41.9035 -87.6434 casual }
This is my code(only get the picture of the path):
`
import networkx as nx
import osmnx as ox
import geopandas as gpd
import pandas as pd
from shapely.geometry import LineString, MultiLineString
ox.config(log_console=True, use_cache=True)
# get a graph for some city
startlat = []
startlng = []
endlat = []
endlng = []
data = pd.read_csv("station.csv")
startlat = data['start_lat']
startlng = data['start_lng']
endlat = data['end_lat']
endlng = data['end_lng']
G2 = ox.graph_from_place('Chicago, Illinois', network_type='drive')
route_list = []
# get nodes and edges
nodes, edges = ox.graph_to_gdfs(G2, nodes=True, edges=True)
for i in range(len(startlng)):
origin = (startlat[i], startlng[i])
destination = (endlat[i], endlng[i])
origin_node = ox.get_nearest_node(G2, origin)
destination_node = ox.get_nearest_node(G2, destination)
# exception handling, skipping points without path
try:
route = nx.shortest_path(G2, origin_node, destination_node, )
route_nodes = nodes.loc[route]
# Create a geometry for the shortest path
route_line = MultiLineString(list(route_nodes.geometry.values))
# Create a GeoDataFrame
route_geom = gpd.GeoDataFrame([[route_line]], geometry='geometry', crs=edges.crs, columns=['geometry'])
except:
pass
route_list.append(route)
fig, ax = ox.plot_graph_routes(G2, route_list, node_size=0)
`