| Title: | Compute International Trade Routes |
|---|---|
| Description: | Provides tools to query border relationships between countries and to compute weighted shortest-path trade routes on a worldwide transportation network combining roads, shipping lanes, and ports. Border queries use a precomputed adjacency graph; route computation supports per-country and per-country-pair cost overrides for roads, ports, and land borders. |
| Authors: | Mario Milone [aut, cre] |
| Maintainer: | Mario Milone <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.0.3 |
| Built: | 2026-11-09 20:39:54 UTC |
| Source: | https://github.com/milonemario/traderoutes |
Provides tools to query border relationships between countries and to compute weighted shortest-path trade routes on a worldwide transportation network combining roads, shipping lanes, and ports.
Before installing or testing the package, make sure the generated RDS graph
files are present under inst/extdata/.
are_neighbors(), get_neighbors(), get_second_neighbors(),
get_common_neighbors()
countries(), get_main_city(), get_great_circle(), get_path(),
get_pattern(), get_countries(), road_distance(), sea_distance(),
transits_i(), transits_j()
Maintainer: Mario Milone [email protected]
Authors:
Mario Milone [email protected]
Check whether two countries share a land border
are_neighbors(country1, country2)are_neighbors(country1, country2)
country1 |
ISO 3-letter code of the first country (e.g. |
country2 |
ISO 3-letter code of the second country (e.g. |
TRUE if the countries are direct neighbors, FALSE otherwise.
## Not run: are_neighbors("FRA", "ITA") # TRUE are_neighbors("FRA", "USA") # FALSE ## End(Not run)## Not run: are_neighbors("FRA", "ITA") # TRUE are_neighbors("FRA", "USA") # FALSE ## End(Not run)
Export city locations to KML
cities_to_kml(filename, all_cities = FALSE)cities_to_kml(filename, all_cities = FALSE)
filename |
Output KML path. |
all_cities |
If |
A country is included if it has at least one city vertex (vtype = "city")
in the full graph.
countries()countries()
Sorted character vector of ISO 3-letter country codes.
## Not run: cs <- countries() "FRA" %in% cs # TRUE ## End(Not run)## Not run: cs <- countries() "FRA" %in% cs # TRUE ## End(Not run)
Return countries that border both of two given countries
get_common_neighbors(country1, country2)get_common_neighbors(country1, country2)
country1 |
ISO 3-letter code of the first country. |
country2 |
ISO 3-letter code of the second country. |
Character vector of ISO3 codes of countries neighboring both inputs.
Returns character(0) if no common neighbors exist.
## Not run: get_common_neighbors("FRA", "ITA") # "CHE" ## End(Not run)## Not run: get_common_neighbors("FRA", "ITA") # "CHE" ## End(Not run)
Each element of the returned list contains the ISO3 country code and the distance (km) traveled within that segment.
get_countries(path, with_sea = FALSE, clean_dist = NULL)get_countries(path, with_sea = FALSE, clean_dist = NULL)
path |
List with elements |
with_sea |
Logical. If |
clean_dist |
Numeric or |
A list of named lists, each with elements country (character) and
dist (numeric, km), in traversal order.
## Not run: path <- get_path(get_main_city("FRA"), get_main_city("POL"), road_cost = 7) sapply(get_countries(path), `[[`, "country") ## End(Not run)## Not run: path <- get_path(get_main_city("FRA"), get_main_city("POL"), road_cost = 7) sapply(get_countries(path), `[[`, "country") ## End(Not run)
Great-circle distance between two countries' main cities
get_great_circle(country1, country2)get_great_circle(country1, country2)
country1 |
ISO 3-letter code of the origin country. |
country2 |
ISO 3-letter code of the destination country. |
Great-circle distance in kilometres (numeric scalar).
## Not run: round(get_great_circle("FRA", "ITA"), 2) # 1109.56 ## End(Not run)## Not run: round(get_great_circle("FRA", "ITA"), 2) # 1109.56 ## End(Not run)
The main city is the vertex with vtype = "city" and maincity = "1" (or
1) for the specified country. It is used as the default path endpoint and
for great-circle distance calculations.
get_main_city(country)get_main_city(country)
country |
ISO 3-letter code of the country (e.g. |
Integer vertex index, or NA if no main city is found.
A warning is issued when none or more than one main city is found.
## Not run: v <- get_main_city("FRA") igraph::vertex_attr(g, "name", v) ## End(Not run)## Not run: v <- get_main_city("FRA") igraph::vertex_attr(g, "name", v) ## End(Not run)
Return neighboring countries of a given country
get_neighbors(country)get_neighbors(country)
country |
ISO 3-letter code of the country (e.g. |
Character vector of ISO3 codes of countries sharing a land border
with country. Returns character(0) if the country is not in the graph.
## Not run: get_neighbors("USA") # c("CAN", "MEX") ## End(Not run)## Not run: get_neighbors("USA") # c("CAN", "MEX") ## End(Not run)
Applies configurable cost weights to graph edges and returns the shortest path. Costs are applied in this order:
get_path( v1, v2, road_cost = 1, road_cost_countries = list(), port_cost = 1, port_cost_countries = list(), border_cost = 0, border_cost_countries = list() )get_path( v1, v2, road_cost = 1, road_cost_countries = list(), port_cost = 1, port_cost_countries = list(), border_cost = 0, border_cost_countries = list() )
v1 |
Integer vertex index of the origin (from |
v2 |
Integer vertex index of the destination. |
road_cost |
Multiplicative cost for road edges. Default |
road_cost_countries |
Named list of per-country road cost overrides,
e.g. |
port_cost |
Additive cost for land-to-sea transitions. Default |
port_cost_countries |
Named list of per-country port cost overrides.
Default |
border_cost |
Additive cost for crossing a country border. Default |
border_cost_countries |
Named list of per-country-pair border cost
overrides. Keys are |
All edges are initialized to their physical distance (wdist = dist).
Port edges (etype = "road_sea"): wdist = dist + port_cost.
Per-country overrides in port_cost_countries take precedence.
Border edges (etype in c("road_border", "sea_border")):
wdist = dist + border_cost.
Per-pair overrides in border_cost_countries take precedence
(keys are "C1-C2" strings).
Road edges (etype = "road"): wdist = dist * road_cost.
Per-country overrides in road_cost_countries take precedence.
A named list with:
epathInteger vector of edge indices along the path.
vpathInteger vector of vertex indices along the path.
## Not run: v_fra <- get_main_city("FRA") v_nld <- get_main_city("NLD") path <- get_path(v_fra, v_nld, road_cost = 7, port_cost = 1e4) get_pattern(path) ## End(Not run)## Not run: v_fra <- get_main_city("FRA") v_nld <- get_main_city("NLD") path <- get_path(v_fra, v_nld, road_cost = 7, port_cost = 1e4) get_pattern(path) ## End(Not run)
Compresses consecutive vertices of the same type into a single character:
c = city, r = road, p = port, s = sea. Intermediate city vertices
(not the first or last vertex) are treated as road.
get_pattern(path)get_pattern(path)
path |
List with element |
Character string, e.g. "crc" (land route) or "csrc" (sea route).
## Not run: path <- get_path(get_main_city("FRA"), get_main_city("DEU"), road_cost = 7) get_pattern(path) # "crc" ## End(Not run)## Not run: path <- get_path(get_main_city("FRA"), get_main_city("DEU"), road_cost = 7) get_pattern(path) # "crc" ## End(Not run)
Returns countries that are two hops away from country in the border graph,
excluding country itself and its direct neighbors.
get_second_neighbors(country)get_second_neighbors(country)
country |
ISO 3-letter code of the country. |
Character vector of ISO3 codes of second-degree neighbors,
in traversal order. Returns character(0) if the country is not found.
## Not run: "PRT" %in% get_second_neighbors("FRA") # TRUE ## End(Not run)## Not run: "PRT" %in% get_second_neighbors("FRA") # TRUE ## End(Not run)
Return a list with country codes and the great-circle distance between them
get_trade_route_great_circle(country1, country2)get_trade_route_great_circle(country1, country2)
country1 |
ISO 3-letter code of the origin country. |
country2 |
ISO 3-letter code of the destination country. |
A named list with iso_i, iso_j, and dist.
Export port locations to KML
ports_to_kml(filename)ports_to_kml(filename)
filename |
Output KML path. |
Sums the dist attribute of all edges with etype in
c("road", "road_sea").
road_distance(path)road_distance(path)
path |
List with element |
Numeric scalar, distance in km.
Export the road network to KMZ
roads_to_kml(filename)roads_to_kml(filename)
filename |
Output path prefix. |
Sums the dist attribute of all edges with etype in
c("sea", "sea_border").
sea_distance(path)sea_distance(path)
path |
List with element |
Numeric scalar, distance in km.
Export the sea network, including sea-border edges, to KMZ
seas_to_kml(filename)seas_to_kml(filename)
filename |
Output path prefix. |
Export a trade route path to KML
to_kml(path, filename)to_kml(path, filename)
path |
Path as returned by |
filename |
Output KML path. |
For land-only routes: all intermediate countries between origin and destination. For routes with sea: countries between the origin and the first sea segment.
transits_i(path, clean_dist = NULL)transits_i(path, clean_dist = NULL)
path |
List as returned by |
clean_dist |
Numeric or |
Character vector of ISO3 transit country codes (may be empty).
## Not run: path <- get_path(get_main_city("CHE"), get_main_city("POL"), road_cost = 7) transits_i(path) ## End(Not run)## Not run: path <- get_path(get_main_city("CHE"), get_main_city("POL"), road_cost = 7) transits_i(path) ## End(Not run)
Equivalent to transits_i() applied to the reversed path.
transits_j(path, clean_dist = NULL)transits_j(path, clean_dist = NULL)
path |
List as returned by |
clean_dist |
Numeric or |
Character vector of ISO3 transit country codes (may be empty).