vtra package¶
Vietnam Transport Risk Analysis
Subpackages¶
- vtra.adaptation package
- vtra.failure package
- vtra.failure_scenario_selection package
- Submodules
- vtra.failure_scenario_selection.collect_network_hazard_scenarios_national module
- vtra.failure_scenario_selection.collect_network_hazard_scenarios_provincial module
- vtra.failure_scenario_selection.hazard_network_scenarios module
- vtra.failure_scenario_selection.hazards_network_intersections_results_collect module
- vtra.failure_scenario_selection.hazards_networks_intersections module
- vtra.flow_mapping package
- vtra.mria package
- vtra.mrio package
- vtra.plot package
- Submodules
- vtra.plot.adaptation_box_plots module
- vtra.plot.admin_map module
- vtra.plot.air_network_flows module
- vtra.plot.air_network_flows_max_scales module
- vtra.plot.air_network_map module
- vtra.plot.coastal_network_flows module
- vtra.plot.coastal_network_flows_max_scales module
- vtra.plot.coastal_network_map module
- vtra.plot.cost_benefits module
- vtra.plot.create_crop_maps module
- vtra.plot.district_center_heatmap module
- vtra.plot.inland_network_flows module
- vtra.plot.inland_network_flows_max_scales module
- vtra.plot.inland_network_map module
- vtra.plot.multimodal_network_map module
- vtra.plot.national_hazard_exposure_plots module
- vtra.plot.national_rail_risks module
- vtra.plot.national_roads_risks_and_adaptation module
- vtra.plot.network_rerouting_losses module
- vtra.plot.plot_range_provinces module
- vtra.plot.plot_ranges module
- vtra.plot.province_hazard_exposure_plots module
- vtra.plot.province_roads_failures module
- vtra.plot.province_roads_maps module
- vtra.plot.province_roads_risks_and_adaptation module
- vtra.plot.rail_network_failures module
- vtra.plot.rail_network_failures_multi_modal module
- vtra.plot.rail_network_flows module
- vtra.plot.rail_network_flows_max_scales module
- vtra.plot.rail_network_map module
- vtra.plot.rail_network_routes module
- vtra.plot.rail_transfers module
- vtra.plot.rice_atlas_maps module
- vtra.plot.road_network_failures module
- vtra.plot.road_network_failures_multi_modal module
- vtra.plot.road_network_flows module
- vtra.plot.road_network_flows_max_scales module
- vtra.plot.road_network_map module
- vtra.plot.road_transfers module
- vtra.plot.water_network_flows module
- vtra.plot.water_network_map module
- vtra.plot.water_transfers module
- vtra.preprocess package
- Submodules
- vtra.preprocess.convert_hazard_data module
- vtra.preprocess.create_transport_networks module
- vtra.preprocess.cvts module
- vtra.preprocess.cvts_speeds module
- vtra.preprocess.national_modes_od_creation module
- vtra.preprocess.province_roads_access_od_creation module
- vtra.preprocess.transport_network_inputs module
- vtra.stats package
- Submodules
- vtra.stats.air_water_impacts module
- vtra.stats.boundary_hazard_percentages module
- vtra.stats.flow_senstivities module
- vtra.stats.national_roads_criticality_risk_stats module
- vtra.stats.network_boundary_stats module
- vtra.stats.network_hazard_stats module
- vtra.stats.network_stats_calculations module
- vtra.stats.province_roads_criticality_risk_stats module
Submodules¶
vtra.transport_flow_and_failure_functions module¶
Functions used in the provincial and national-scale network failure analysis
-
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:
-
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:
-
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:
-
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:
-
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
-
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_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_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_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_node_edge_files
(mode_file_path, file_identification)[source]¶ Get the paths of edge and node files in folder
Parameters: 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
-
line_length
(line, ellipsoid='WGS-84')[source]¶ Length of a line in meters, given in geographic coordinates.
Parameters: - line – a shapely LineString object with WGS-84 coordinates.
- ellipsoid – string name of an ellipsoid that geopy understands (see http://geopy.readthedocs.io/en/latest/#module-geopy.distance).
Returns: Length of line in kilometers.
-
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
-
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:
-
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