Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/mwe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import growbikenet as gbn

a_edges = gbn.growbikenet(
city_name="Turin",
city_name="Bath",
proj_crs="3857",
ranking="betweenness_centrality",
existing_network_spacing=600,
export_file_format="gpkg",
export_data=True,
export_plots=False,
export_video=False,
Expand Down
2 changes: 1 addition & 1 deletion growbikenet/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def update_seed_points_with_existing_bike_network(seed_points_snapped, nodes_exn
# Bring back to original form (geometry and osmid columns, osmid index)
# This is a bit of a mess but it works. Simplify it in the future.
seed_points_snapped.loc[seed_points_snapped['osmid_1'].isnull(), 'osmid_1'] = seed_points_snapped['osmid_2'] # _1 comes from one side, _2 from the other. One has NaNs, the other too. https://stackoverflow.com/a/60132614
seed_points_snapped.drop(["y","x","street_count", "highway", "railway", "osmid_2"], axis=1, inplace=True)
seed_points_snapped = seed_points_snapped[['osmid_1','geometry']]
seed_points_snapped.rename(columns={"osmid_1": "osmid"}, inplace=True)
seed_points_snapped.set_index("osmid", drop=False, inplace=True)
return seed_points_snapped
Expand Down
28 changes: 24 additions & 4 deletions growbikenet/growbikenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def growbikenet(
existing_network_spacing=None,
export_data=True,
export_data_slug=None,
export_file_format="geojson",
export_plots=False,
export_video=False,
):
Expand All @@ -59,8 +60,10 @@ def growbikenet(
Spacing between seed points, in meters, only on the existing bicycle network. If not set to a positive integer, the existing network is ignored.
export_data : bool, optional, default True
If set to True, data will be saved to a file. The filename is [slug]-[ranking]-[seed_point_type].gpkg, where slug is a string id made out of city_name
export_data_slug : string, optional, default None
If not set to None, it will be slugified and used as the slug in the filename of the data export
export_data_slug : stri, optional, default None
If not set to None, the city_name will be slugified and used as the slug in the filename of the data export
export_file_format : str, optional, default "geojson"
File format for the data export, relevant if export_data set to True. Default "geojson", also possible "gpkg". If exporting as geojson, generates extra files for seed points and city boundary. If exporting as gkpg, these are added all in one file as extra layers.
export_plots : bool, optional, default False
If set to True, plots will be saved to a file
export_video : bool, optional, default False
Expand Down Expand Up @@ -116,6 +119,8 @@ def growbikenet(
raise ValueError(
"export_data_slug must contain at least one non-special character"
)
if export_file_format != "geojson" and export_file_format != "gpkg":
raise ValueError("export_file_format must be 'geojson' or 'gpkg'")
if type(export_plots) is not bool:
raise TypeError("export_plots must be a boolean")
if type(export_video) is not bool:
Expand Down Expand Up @@ -225,14 +230,29 @@ def growbikenet(
else:
city_string = export_data_slug
export_data_filename = (
slugify(city_string) + "-" + ranking + "-" + seed_point_type + ".gpkg"
slugify(city_string) + "-" + ranking + "-" + seed_point_type + "." + export_file_format
)

# Save to file
if export_data:
### save data
print("Saving data..")
a_edges.to_file("./results/"+export_data_filename, driver="GPKG")
seed_points_snapped.drop(["osmid"], axis=1, inplace=True)
city_boundary = ox.geocoder.geocode_to_gdf(city_name)
city_boundary.to_crs(epsg=proj_crs, inplace=True)
# We have meter precision, so rounding to integers is fine. Better would be to
# change dtypes to int, but this does not seem possible without manual looping.
city_boundary.geometry = city_boundary.geometry.set_precision(grid_size=1)
seed_points_snapped.geometry = seed_points_snapped.geometry.set_precision(grid_size=1)
a_edges.geometry = a_edges.geometry.set_precision(grid_size=1)
if export_file_format == "geojson":
a_edges.to_file("./results/"+export_data_filename, driver="GeoJSON")
seed_points_snapped.to_file("./results/"+slugify(city_string)+"-"+seed_point_type+".geojson", driver="GeoJSON")
city_boundary.to_file("./results/"+slugify(city_string)+"-city_boundary.geojson", driver="GeoJSON")
elif export_file_format == "gpkg":
a_edges.to_file("./results/"+export_data_filename, driver="GPKG", layer="Bike network")
seed_points_snapped.to_file("./results/"+export_data_filename, driver="GPKG", layer="Seed points", append=True)
city_boundary.to_file("./results/"+export_data_filename, driver="GPKG", layer="City boundary", append=True)

if export_plots or export_video:
### Visualize
Expand Down
Loading