Wednesday, 20 September 2017

Find borders between all unique country pairs with R


I would like to obtain a border as a line between all unique country pairs that are contiguous. Here is how to do it:


library(spData)
library(sf)
library(rnaturalearth)
library("rmapshaper")

#load countries dataset:
countries_sp <- ne_countries(scale = 10, continent= "Europe")


# find borders
border_sf <- st_as_sf(countries_sp) %>%
ms_innerlines() %>%
as_tibble() %>%
st_as_sf()

# create ids to visualise lines found by ms_innerlines
border_sf$ids <- rep(1:length(border_sf$geometry),1)

# plot

tm_shape(countries_sp, bbox = c(c(-8.750803, 36.14111), c(40.15954, 70.07531))) +
tm_fill() +
tm_shape(border_sf) +
tm_lines(col = "ids")

enter image description here


Example shows how to find have one line for the border between France and Germany, one for France and Switzerland, France and Italy for each contiguous country pair in the sample.




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...