UDST / urbanaccess

A tool for GTFS transit and OSM pedestrian network accessibility analysis by UrbanSim
https://udst.github.io/urbanaccess/index.html
GNU Affero General Public License v3.0
236 stars 56 forks source link

Saving network to shapefile #58

Closed rxl204 closed 5 years ago

rxl204 commented 5 years ago

Please note that while GTFS feeds are a standardized format, not all transit services follow the same data standards or supply all of the necessary information in their GTFS feed that are required for UrbanAccess to compute a travel time weighted graph. UrbanAccess attempts to deal with the most common GTFS feed data schema issues however it is expected some GTFS feeds may raise errors or may not fit the expected data schema. If you encounter a bug in UrbanAccess please: 1) first search the previously opened issues to see if the problem has already been reported; 2) If not, fill in the template below and tag with the appropriate Type label (and Data label if applicable). You can delete any sections that do not apply.

Description of the bug

Any way to save an integrated urban access network with pandana to shapefile?

GTFS feed or OSM data (optional)

If the issue is related to a specific GTFS feed or OSM data please provide the URL to download the GTFS feed or the bounding box used to extract the OSM data.

https://github.com/UDST/urbanaccess/blob/dev/demo/simple_example.ipynb

Environment

Paste the code that reproduces the issue here:

# place code here

Paste the error message (if applicable):

# place error message here
sablanchard commented 5 years ago

Hi @rxl204 ,

Yes you can use geopandas and shapely to accomplish this. For example to export the network made in the demo notebook you could do something like this:

import pandas as pd
import geopandas as gpd
from shapely.geometry import Point, LineString

# read in the h5 data
with pd.HDFStore('./final_net.h5') as store:
    nodes=store.nodes
    edges=store.edges

# export the nodes as a point shp
point_gdf = gpd.GeoDataFrame(nodes, crs={'init': 'epsg:4326'},
                             geometry=[Point(xy) for xy in zip(nodes.x, nodes.y)])
point_gdf.to_file('net_nodes.shp')

# export the edges as a line shp
edges = edges.merge(nodes[['x', 'y']], how='left', left_on=['from_int'], right_on=['id_int'])
edges.rename(columns={'x':'from_x','y':'from_y'}, inplace=True)

edges = edges.merge(nodes[['x', 'y']], how='left', left_on=['to_int'], right_on=['id_int'])
edges.rename(columns={'x':'to_x','y':'to_y'}, inplace=True)

edges['to_point'] = [Point(xy) for xy in edges[['to_x','to_y']].values]
edges['from_point'] = [Point(xy) for xy in edges[['from_x', 'from_y']].values]
edges['geom'] = edges.apply(lambda row: LineString((row['from_point'], row['to_point'])), axis=1)

line_gdf = gpd.GeoDataFrame(edges, geometry='geom', crs={'init': 'epsg:4326'})
line_gdf.drop(columns=['from_point', 'to_point'], inplace=True)
line_gdf.to_file('net_edges.shp')

hope that helps!