User reference¶
This is the user reference for the BikeNetLib package. If you are looking for an introduction to BikeNetLib, read the Getting started guide.
The standard way to import the BikeNetLib package is via import bikenetlib as bnl.
bikenetlib.constants¶
Global constants for bikenetlib. Especially technical or internal constants start with an underscore.
- _PROGRESS_BAR_LENGTHint, default 23
Character length of tqdm progress bars.
- _PROGRESS_BAR_DESC_LENGTHint, default 23
Character length of tqdm progress bar descriptions. This is the space given to text like “Importing network data “, which is at the maximum of 23 characters.
bikenetlib.io¶
Import/Output functions of bikenetlib, including format conversions and user feedback tools.
- silentbool, default False
If set to True, suppresses all user feedback. Useful for batch exports.
- bikenetlib.io.export_gdfs(gdfs, filename='exported_data', path='./', format='gpkg', overwrite=True, crs='4326', bar=False)[source]¶
Export GeoDataFrames as geojson, gpkg, or shp file(s).
If crs==’4326’ and format==’geojson’, export with RFC7946.
- Parameters:
- gdfsdict
Dictionary with the layer names as keys with dict values that contain gdf and single filename. Structure: {
‘layername1’: {‘gdf’: gdf1, ‘filename_single’:str}, ‘layername2’: {‘gdf’: gdf2, ‘filename_single’:str}, …
} filename_single is relevant for geojson and shp files which ignore filename and export each gdf as individual files. layername is relevant for gpkg which exports all gdfs merged into one file filename as their own layers.
- filenamestr, default ‘exported_data’
Name of exported file, if exported as gpkg.
- pathstr, default ‘./’
Export path.
- format{‘gpkg’, ‘geojson’, ‘shp’}, default ‘gpkg’
Export file format.
- overwritebool, default True
Decision whether to overwrite existing files.
- crsstr, default ‘4326’
Coordinate Reference System of exported data.
- barbool, default False
Decision whether to show a progress bar.
- Returns:
- bool
Returns True if all files have been written. If overwrite is set to False and at least one file already existed, returns False.
- bikenetlib.io.initialize_progress_bar(desc_string, total=1, unit='step')[source]¶
Initialize tqdm progress bar.
- bikenetlib.io.slugify(s)[source]¶
Slugify a string.
Adapted from: https://github.com/Chalarangelo/30-seconds-of-code/blob/master/content/snippets/python/s/slugify.md
- Parameters:
- sstr
String to slufigy.
- Returns:
- sstr
Slugified string.
Notes
A clean global solution would be using unidecode, but we do not want extra dependencies for this. We assume European city names in latin alphabet, and some special letters like Hungarian ő already mapped.
bikenetlib.osm¶
Global constants of bikenetlib related to OpenStreetMap (OSM). These are meant for advanced users only who have a good understanding of OSM tags.
- CARPBI_NETWORK_CUSTOM_FILTERlist[str] or None
Custom filter for all infrastructure elements that are the combined drive and pbi (protected bicycle infrastructure) network. By default, CARPBI_NETWORK_CUSTOM_FILTER is a superset of PBI_CUSTOM_FILTER, so it is best to keep them in sync. To only consider the drive network, set CARPBI_NETWORK_CUSTOM_FILTER to None and CARPBI_NETWORK_TYPE to ‘drive’. However, doing so can lead to issues: https://github.com/BikeNetKit/GrowBikeNet/issues/255. By default, CARPBI_NETWORK_CUSTOM_FILTER is a superset of PBI_CUSTOM_FILTER, so it is best to keep them in sync.
- CARPBI_NETWORK_TYPE{‘drive’, ‘all’, ‘all_public’, ‘bike’, ‘drive_service’, ‘walk’}, default ‘drive’
What type of street network to retrieve for the car&pbi network if CARPBI_NETWORK_CUSTOM_FILTER is None.
- PBI_CUSTOM_FILTERlist[str]
Custom filter for protected bicycle infrastructure (pbi). Always keep in sync with PBI_DICT and PBI_DICT_SUB. By default, PBI_CUSTOM_FILTER is a subset of CARPBI_NETWORK_CUSTOM_FILTER, so it is best to keep them in sync.
- PBI_DICTdict
Dictionary for protected bicycle infrastructure (pbi), for single criteria. Always keep in sync with PBI_CUSTOM_FILTER.
- PBI_DICT_SUBdict
Dictionary for protected bicycle infrastructure (pbi), with one sub-criterion. For example, highway~path AND bicycle~designated. Always adds a second criterion access!~private. Always keep in sync with PBI_CUSTOM_FILTER.
bikenetlib.utils¶
Utility functions of bikenetlib.
- bikenetlib.utils.assign_pbi_to_edges(g)[source]¶
Assign a boolean pbi attribute to all edges in a graph depending if they are considered protected bike infrastructure (pbi).
- Parameters:
- gnetworkx.MultiDiGraph
Simplified graph representing the street network.
- Returns:
- gnetworkx.MultiDiGraph
Simplified graph representing the street network, with added binary edge attribute “pbi”.
- bikenetlib.utils.get_principal_bearing(G, bearingbins=72)[source]¶
Determine the most common (principal) bearing, for the best grid orientation.
Adapted from: https://github.com/gboeing/osmnx-examples/blob/v0.11/notebooks/17-street-network-orientations.ipynb The bearing is determined from edges weighted by length.
- Parameters:
- Gnetworkx MultiGraph (undirected)
The graph from which to determine the principal bearing. Its coordinate reference system must be geographical, not projected.
- bearing_binsint, default 72
Number of bins to determine bearing. e.g. 72 will create 5 degrees bins.
- Returns:
- principal_bearing: float
The principal bearing, precise to 5 degrees.
- bikenetlib.utils.node_to_edge_attributes(values_nodes, edges)[source]¶
Map node to edge attributes.
Creates edge attributes by taking the average values of adjacent node attributes.
- Parameters:
- values_nodesdict
Keys: node ids, Values: Node attributes (for example a scalar)
- edgesnetworkx.classes.reportviews.EdgeView
A view of edge attributes of a networkx graph. Could also be a list of tuples of node ids.
- Returns:
- values_edges: dict
Keys: tuples of node ids, Values: Edge attributes
- bikenetlib.utils.nodelist_to_edgetuples(edge_gdf, nodelist)[source]¶
Map a list of nodes (from nx.shortest_paths()) to a list of edge tuples that can be used for indexing an edge geodataframe.
Useful to apply as a lambda function to turn path nodes into path edges.
- Parameters:
- edge_gdfgeopandas.geodataframe.GeoDataFrame
The edges of a spatial network, in a projected CRS.
- nodelistlist
A list of nodes that make up source and targets of edges.
- Returns:
- edgelist_finallist
List of edge tuples that can be used for indexing the edge geodataframe.
- bikenetlib.utils.route_nodepairs(nodepairs, edges, g_undir, weight='weight')[source]¶
Route pairs of nodes on the graph g_undir to a merged geometry of corresponding OSM nodes and edges.
- Parameters:
- nodepairspandas.DataFrame
Dataframe with node pair integer IDs, columns source and target.
- edgesgeopandas.geodataframe.GeoDataFrame
The street network, in a projected CRS.
- g_undirnetworkx.graph undirected
Graph to use for routing.
- weightstring, default “weight”
The name of the weight variable.
- Returns:
- nodepairspandas.DataFrame
Dataframe of node pairs with added path nodes and path edges.
bikenetlib.viz¶
Visualization functions of bikenetlib.