Tasks

  • Write parallel for loops to process spatial data

Background

library(tidyverse)
library(spData)
library(sf)

## New Packages
library(foreach)
library(doParallel)
registerDoParallel(2)
getDoParWorkers() # check registered cores

#define working projection (EASE-Grid, https://nsidc.org/data/ease)
proj="+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"

Steps

Write an Rmd script that:

  • Loads the world dataset in the spData package
    • Reproject the world dataset to the Equal-Area Scalable Earth Grid (EASE-Grid) (EASE-Grid) using st_transform() and the proj4 projection in the code above
  • Runs a parallel foreach() to loop over countries (name_long) that:
    • filter the world object to include only on country at a time.
    • use st_is_within_distance to find the distance from that country to all other countries in the world object within 100000m Set sparse=F to return a simple vector of TRUE/FALSE for countries within the distance.
    • set .combine=rbind to return a simple matrix.
  • Confirm that you get the same answer without using foreach:
    • simply use st_is_within_distance with the transformed world object as both x and y object.
    • compare the results with identical()
    • if you are curious, you can also check the time difference with system.time().

The first 10 rows/columns of the resulting matrix (e.g. x_par[1:10,1:10]) should look like this:

1 2 3 4 5 6 7 8 9 10
TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE
FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE
FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE
FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE
FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE
FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE
FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE

Note that in this example the sequential version typically runs faster than the parallel version due to the relatively small size of the dataset and computation needed.


This approach could be used to identify which countries were ‘close’ to others. For example, Identify which countries are within 10^{5}m of Costa Rica:

Nearbye Countries
Panama
Costa Rica
Nicaragua

And plot them: