trhelpers module

tripsender.trhelpers.alder2age(alder)

A function to take the Swedish age categories from the travel survey and get a random age as int within those bounds.

Parameters

alder – (str) A string representing a Swedish age category from the Travel Survey

Returns

(int) Return a random age as integer from the provided age category

tripsender.trhelpers.fix_closest(closest, A, amenityName)

A function to post-process the closest amenities gdf from nxhelpers.get_closest_from_kml to a format that tripsender can use.

Parameters
  • closest – (GeoPandas GeoDataFrame) GDF containing origins and closest destinations for each destination class

  • A – (numpy array)

  • amenityName – (list) A ordered list of amenity names matching those in the closest GDF

Returns

(GeoPandas GeoDataFrame) The post-processed GDF from the nxhelpers.get_closest_from_kml function result

tripsender.trhelpers.generate_synth_pop(area, year='2019')

A function that generates a synthetic population using Iterative proportional fitting. The function uses the IPFn python module. The IPF is done in three dimensions by default, age, gender and household type. Population data is fetched on-the-fly from the scb pxWeb database for Gothenburg and uses a simple IPF generator.

Parameters
  • area – (str) The as string. Note: This should be a valid area name recognised by the PostGIS server

  • year – (str: Optional) Optional String for the year. If not supplied it defaults to 2019

Returns

(Pandas DataFrame) Returns a Dataframe with age, gender and household attributes for the population.

tripsender.trhelpers.get_pnbr(closest, amenityName)

A function to generate the perceived neighbourhood by matching the closest Origin Destination pairs.

Parameters
  • closest – (GeoPandas GeoDataFrame) GDF containing origins and closest destinations for each destination class.

  • amenityName – (list) A ordered list of amenity names matching those in the closest GDF

Returns

(GeoPandas GeoDataFrame) A GDF containing the origin and amenity locations

tripsender.trhelpers.plot_od_lines(pNbr)

A function to visualise the closest amenity with linestring connecting the origin to the destination.

Parameters

pNbr – (GeoPandas GeoDataFrame) A GDF containing the origin and amenity locations

Returns

(contextily plot) A contextily plot containing the Linestring of the Origin Destination pairs

tripsender.trhelpers.plot_trip(gdf)

A function to plot the result of the process_trip function on a basemap using contextily.

Parameters

gdf – (GeoPandas GeoDataFrame)

Returns

(contextily plot)

tripsender.trhelpers.process_trip(output)

A function to post-process the output of the tripsender function. It reads the list of coordinates on the shortest path and appends the linestring formed by the list of coordinates. The function also processes the length and trip time for each path.

Parameters

output – (GeoPandas GeoDataFrame) The output GDF of the tripsender function

Returns

(GeoPandas GeoDataFrame) A fixed output of the tripsender function with linestring for shortest path.

tripsender.trhelpers.process_trip_gdf(gdf)

A Function that combines the step of processing the output of the tripsender function and turns it into a dictionary string that can be read by keplerGL for inline visualisation.

Parameters

gdf – (GeoPandas GeoDataFrame) The output GDF of the tripsender function

Returns

(str) A KeplerGL compatible geojson dictionary string

tripsender.trhelpers.profile(fnc)

A decorator that uses cProfile to profile a function

tripsender.trhelpers.read_gdf_to_dict(path)

A function to read the saved geojson output from tripsender as a dict.

Parameters

path – (str) The string path to the geojson file output from the trip sender function

Returns

(dict) The dictionary created from the geojson file

tripsender.trhelpers.read_synth_pop(myPath)

A function to read a local csv saved from the synthetic population generation step.

Parameters

myPath – (str) A path to the csv containing results from the synthetic population step

Returns

(Pandas DataFrame) Returns a DataFrame with age, gender and household attributes for the total population

tripsender.trhelpers.run_tripsender(A, G, SynthPop, closest, dict_Personas)

A function that runs the tripsender function.

Parameters
  • A – (numpy array) Array of all nodes in the graph

  • G – (networkX graph object) networkX Graph object

  • SynthPop – (Pandas DataFrame) DF containing the synthetic profiles

  • closest – (GeoPandas GeoDataFrame) GDF containing origins and closest destinations for each destination class

  • dict_Personas – (List) List of dictionaries containing the 14 synthetic population groups

Returns

(GeoPandas GeoDataFrame) GDF of the result with a route column containing nodes for the shortest path.

tripsender.trhelpers.time2secs(t)

A utility function that converts hh:mm:ss to a unix epoch in seconds. The date always starts from 00 hours on January 1st, 2021.

Parameters

t – (str) A string of the hh:mm:ss format (Note: Must be above 6 chars)

Returns

(None)

tripsender.trhelpers.write_dict_to_kepler(myDict, area, myString='_0')

Write the tripsender dict to a kepler compatible geojson file.

Parameters
  • myDict – (dict) Dictionary created from the output of the tripsender output

  • area – (str) The as string. Note: This should be a valid area name recognised by the PostGIS server

  • myString – (str: Optional) An optional identifier string if you would like to save multiple versions

Returns

tripsender.trhelpers.write_gdf(gdf, name='test')

A function to re-project, post process and save a geojson from the output of the tripsender function.

Parameters
  • gdf – (GeoPandas GeoDataFrame) The output GDF from the tripsender Function

  • name – (str: Optional) An optional name to write the output of the tripsender function to

Returns

(None)