vtra package

Vietnam Transport Risk Analysis

Subpackages

Submodules

vtra.transport_flow_and_failure_functions module

Functions used in the provincial and national-scale network failure analysis

add_igraph_generalised_costs(G, vehicle_numbers, tonnage)[source]
edge_failure_sampling(failure_scenarios, edge_column)[source]

Criteria for selecting failure samples

Parameters:
  • - Pandas DataFrame of failure scenarios (failure_scenarios) –
  • - String name of column to select failed edge ID's (edge_column) –
Returns:

Return type:

edge_failure_samples - List of lists of failed edge sets

identify_all_failure_paths(network_df_in, edge_failure_set, flow_dataframe, path_criteria)[source]

Identify all paths that contain an edge

Parameters:
  • - Pandas DataFrame of network (network_df) –
  • - List of string edge ID's (edge_failure_set) –
  • - Pandas DataFrame of list of edge paths (flow_dataframe) –
  • - String name of column of edge paths in flow dataframe (path_criteria) –
  • Outputs
  • -------
  • - Pandas DataFrame of network – With removed edges
  • - List of integer indexes (edge_path_index) – Of locations of paths in flow dataframe
igraph_scenario_edge_failures(network_df_in, edge_failure_set, flow_dataframe, vehicle_weight, path_criteria, tons_criteria, cost_criteria, time_criteria)[source]

Estimate network impacts of each failures When the tariff costs of each path are fixed by vehicle weight

Parameters:
  • - Pandas DataFrame of network (network_df_in) –
  • - List of string edge ID's (edge_failure_set) –
  • - Pandas DataFrame of list of edge paths (flow_dataframe) –
  • - Float weight of vehcile weight (vehicle_weight) –
  • - String name of column of edge paths in flow dataframe (path_criteria) –
  • - String name of column of path tons in flow dataframe (tons_criteria) –
  • - String name of column of path costs in flow dataframe (cost_criteria) –
  • - String name of column of path travel time in flow dataframe (time_criteria) –
Returns:

edge_failure_dictionary – With attributes edge_id - String name or list of failed edges origin - String node ID of Origin of disrupted OD flow destination - String node ID of Destination of disrupted OD flow no_access - Boolean 1 (no reroutng) or 0 (rerouting) new_cost - Float value of estimated cost of OD journey after disruption new_distance - Float value of estimated distance of OD journey after disruption new_path - List of string edge ID’s of estimated new route of OD journey after disruption new_time - Float value of estimated time of OD journey after disruption

Return type:

list[dict]

igraph_scenario_edge_failures_changing_tonnages(network_df_in, edge_failure_set, flow_dataframe, vehicle_weight, path_criteria, tons_criteria, cost_criteria, time_criteria)[source]

Estimate network impacts of each failures When the tariff costs of each path depends on the changing tonnages

Parameters:
  • - Pandas DataFrame of network (network_df_in) –
  • - List of string edge ID's (edge_failure_set) –
  • - Pandas DataFrame of list of edge paths (flow_dataframe) –
  • - Float weight of vehcile weight (vehicle_weight) –
  • - String name of column of edge paths in flow dataframe (path_criteria) –
  • - String name of column of path tons in flow dataframe (tons_criteria) –
  • - String name of column of path costs in flow dataframe (cost_criteria) –
  • - String name of column of path travel time in flow dataframe (time_criteria) –
Returns:

edge_failure_dictionary – With attributes edge_id - String name or list of failed edges origin - String node ID of Origin of disrupted OD flow destination - String node ID of Destination of disrupted OD flow no_access - Boolean 1 (no reroutng) or 0 (rerouting) new_cost - Float value of estimated cost of OD journey after disruption new_distance - Float value of estimated distance of OD journey after disruption new_path - List of string edge ID’s of estimated new route of OD journey after disruption new_time - Float value of estimated time of OD journey after disruption

Return type:

list[dict]

merge_failure_results(flow_df_select, failure_df, tons_col, dist_col, time_col, cost_col, vehicle_col, changing_tonnages=True)[source]

Merge failure results with flow results

Parameters:
  • flow_df_select (pandas.DataFrame) – edge flow values
  • failure_df (pandas.DataFrame) – edge failure values
  • tons_col (str) – name of column of tonnages in flow dataframe
  • dist_col (str) – name of column of distance in flow dataframe
  • time_col (str) – name of column of time in flow dataframe
  • cost_col (str) – name of column of cost in flow dataframe
  • vehicle_col (str) – name of column of vehicle counts in flow dataframe
  • changing_tonnages (bool) –
Returns:

flow_df_select – Of edge flow and failure values merged

Return type:

pandas.DataFrame

network_failure_assembly_shapefiles(edge_failure_dataframe, gdf_edges, save_edges=True, shape_output_path='')[source]

Write results to Shapefiles

Outputs gdf_edges - a Shapefile with results of edge failure dataframe

Parameters:
  • edge_failure_dataframe – Pandas DataFrame of edge failure results
  • gdf_edges – GeoDataFrame of network edge set with edge ID’s and geometry
  • save_edges (bool) – Boolean condition to tell code to save created edge shapefile
  • shape_output_path (str) – Path where the output shapefile will be stored
network_od_path_estimations(graph, source, target, tonnage, vehicle_weight, cost_criteria, time_criteria)[source]

Estimate the paths, distances, times, and costs for given OD pair

Parameters:
  • graph – igraph network structure
  • source – String/Float/Integer name of Origin node ID
  • source – String/Float/Integer name of Destination node ID
  • tonnage (float) – value of tonnage
  • vehicle_weight (float) – unit weight of vehicle
  • cost_criteria (str) – name of generalised cost criteria to be used: min_gcost or max_gcost
  • time_criteria (str) – name of time criteria to be used: min_time or max_time
  • fixed_cost (bool) –
Returns:

  • edge_path_list (list[list]) – nested lists of Strings/Floats/Integers of edge ID’s in routes
  • path_dist_list (list[float]) – estimated distances of routes
  • path_time_list (list[float]) – estimated times of routes
  • path_gcost_list (list[float]) – estimated generalised costs of routes

rearrange_minmax_values(edge_failure_dataframe)[source]

Write results to Shapefiles

Parameters:edge_failure_dataframe (pandas.DataFrame) – with min-max columns
Returns:edge_failure_dataframe – With columns where min < max
Return type:pandas.DataFrame
spatial_scenario_selection(network_shapefile, polygon_shapefile, hazard_dictionary, data_dictionary, network_type='nodes', name_province='')[source]

Intersect network edges/nodes and boundary Polygons to collect boundary and hazard attributes

Parameters
  • network_shapefile - Shapefile of edge LineStrings or node Points
  • polygon_shapefile - Shapefile of boundary Polygons
  • hazard_dictionary - Dictionary of hazard attributes
  • data_dictionary - Dictionary of network-hazard-boundary intersection attributes
  • network_type - String value -‘edges’ or ‘nodes’ - Default = ‘nodes’
  • name_province - String name of province if needed - Default = ‘’
Outputs
data_dictionary - Dictionary of network-hazard-boundary intersection attributes:
  • edge_id/node_id - String name of intersecting edge ID or node ID
  • length - Float length of intersection of edge LineString and hazard Polygon: Only for edges
  • province_id - String/Integer ID of Province
  • province_name - String name of Province in English
  • district_id - String/Integer ID of District
  • district_name - String name of District in English
  • commune_id - String/Integer ID of Commune
  • commune_name - String name of Commune in English
  • hazard_attributes - Dictionary of all attributes from hazard dictionary
swap_min_max(x, min_col, max_col)[source]

Swap columns if necessary

write_flow_paths_to_network_files(save_paths_df, industry_columns, min_max_exist, gdf_edges, save_csv=True, save_shapes=True, shape_output_path='', csv_output_path='')[source]

Write results to Shapefiles

Outputs gdf_edges - a shapefile with minimum and maximum tonnage flows of all commodities/industries for each edge of network.

Parameters:
  • save_paths_df – Pandas DataFrame of OD flow paths and their tonnages
  • industry_columns – List of string names of all OD commodities/industries indentified
  • min_max_exist – List of string names of commodity/industry columns for which min-max tonnage column names already exist
  • gdf_edges – GeoDataFrame of network edge set
  • save_csv – Boolean condition to tell code to save created edge csv file
  • save_shapes – Boolean condition to tell code to save created edge shapefile
  • shape_output_path – Path where the output shapefile will be stored
  • csv_output_path – Path where the output csv file will be stored

vtra.utils module

Shared plotting functions

class Style

Bases: tuple

Style(color, zindex, label): class to hold an element’s styles

Used to generate legend entries, apply uniform style to groups of map elements (See network_map.py for example.)

color

Alias for field number 0

label

Alias for field number 2

zindex

Alias for field number 1

assign_value_in_area_proportions(poly_1_gpd, poly_2_gpd, poly_attribute)[source]
assign_value_in_area_proportions_within_common_region(poly_1_gpd, poly_2_gpd, poly_attribute, common_region_id)[source]
count_points_in_polygon(x, points_sindex)[source]

Count points in a polygon

Parameters:
  • x – row of dataframe
  • points_sindex – spatial index of dataframe with points in the region to consider
Returns:

Return type:

Number of points in polygon

extract_gdf_values_containing_nodes(x, sindex_input_gdf, input_gdf, column_name)[source]
extract_nodes_within_gdf(x, input_nodes, column_name)[source]
extract_value_from_gdf(x, gdf_sindex, gdf, column_name)[source]

Access value

Parameters:
  • x – row of dataframe
  • gdf_sindex – spatial index of dataframe of which we want to extract the value
  • gdf – GeoDataFrame of which we want to extract the value
  • column_name – column that contains the value we want to extract
Returns:

Return type:

extracted value from other gdf

gdf_clip(shape_in, clip_geom)[source]

Filter a file to contain only features within a clipping geometry

Parameters:
  • shape_in – path string to shapefile to be clipped
  • province_geom – shapely geometry of province for what we do the calculation
Returns:

Return type:

filtered dataframe

gdf_geom_clip(gdf_in, clip_geom)[source]

Filter a dataframe to contain only features within a clipping geometry

Parameters:
  • gdf_in – geopandas dataframe to be clipped in
  • province_geom – shapely geometry of province for what we do the calculation
Returns:

Return type:

filtered dataframe

generate_weight_bins(weights, n_steps=9, width_step=0.01)[source]

Given a list of weight values, generate <n_steps> bins with a width value to use for plotting e.g. weighted network flow maps.

generate_weight_bins_with_colour_gradient(weights, n_steps=9, width_step=0.01, colours=['orange', 'red'])[source]

Given a list of weight values, generate <n_steps> bins with a width value to use for plotting e.g. weighted network flow maps.

get_axes(extent=None, figsize=None, epsg=None)[source]

Get transverse mercator axes (default to Vietnam extent) EPSG:4756

get_data(filename)[source]

Read in data (as array) and extent of each raster

get_district_label(record)[source]
get_nearest_node(x, sindex_input_nodes, input_nodes, id_column)[source]

Get nearest node in a dataframe

Parameters:
  • x – row of dataframe
  • sindex_nodes – spatial index of dataframe of nodes in the network
  • nodes – dataframe of nodes in the network
  • id_column – name of column of id of closest node
Returns:

Return type:

Nearest node to geometry of row

get_nearest_node_within_region(x, input_nodes, id_column, region_id)[source]
get_node_edge_files(mode_file_path, file_identification)[source]

Get the paths of edge and node files in folder

Parameters:
  • mode_file_path (str) – path of mode file
  • file_identification (str) – name of file
Returns:

  • edges_in – Path of edges shapefile
  • nodes_in – Path of nodes shapefile
  • Error Exception
  • —————
  • Prints error if node or edge file missing

get_node_edge_files_in_path(mode_file_path)[source]

Get the paths of edge and node files in folder

Parameters:mode_file_path (Path of mode file) –
Returns:
  • edges_in (Path of edges shapefile)
  • nodes_in (Path of nodes shapefile)
  • Error Exception
  • —————
  • Prints error if node or edge file missing
get_region_plot_settings(region)[source]

Common definition of region plot settings

legend_from_style_spec(ax, styles, loc='lower left')[source]

Plot legend

line_length(line, ellipsoid='WGS-84')[source]

Length of a line in meters, given in geographic coordinates.

Adapted from https://gis.stackexchange.com/questions/4022/looking-for-a-pythonic-way-to-calculate-the-length-of-a-wkt-linestring#answer-115285

Parameters:
Returns:

Length of line in kilometers.

load_config()[source]

Read config.json

plot_basemap(ax, data_path, focus='VNM', neighbours=None, country_border='white', plot_regions=True, plot_states=True, plot_districts=False, highlight_region=None)[source]

Plot countries and regions background

plot_basemap_labels(ax, data_path, labels=None, province_zoom=False, plot_regions=True, plot_international_left=True, plot_international_right=True)[source]

Plot countries and regions background

plot_basemap_labels_large_region(ax, data_path)[source]
plot_district_labels(ax, data_path, highlight_region=None)[source]
round_sf(x, places=1)[source]

Round number to significant figures

save_fig(output_filename)[source]
scale_bar(ax, length=100, location=(0.5, 0.05), linewidth=3)[source]

Draw a scale bar

Adapted from https://stackoverflow.com/questions/32333870/how-can-i-show-a-km-ruler-on-a-cartopy-matplotlib-plot/35705477#35705477

Parameters:
  • ax (axes) –
  • length (int) – length of the scalebar in km.
  • location (tuple) – center of the scalebar in axis coordinates (ie. 0.5 is the middle of the plot)
  • linewidth (float) – thickness of the scalebar.
set_ax_bg(ax, color='#c6e0ff')[source]

Set axis background color

voronoi_finite_polygons_2d(vor, radius=None)[source]

Reconstruct infinite voronoi regions in a 2D diagram to finite regions.

Source: https://stackoverflow.com/questions/36063533/clipping-a-voronoi-diagram-python

Parameters:
  • vor (Voronoi) – Input diagram
  • radius (float, optional) – Distance to ‘points at infinity’
Returns:

  • regions (list of tuples) – Indices of vertices in each revised Voronoi regions.
  • vertices (list of tuples) – Coordinates for revised Voronoi vertices. Same as coordinates of input vertices, with ‘points at infinity’ appended to the end

within_extent(x, y, extent)[source]