Properties of spatial objects

The following code makes use of two packages:

  • the sf package, which is available from https://cran.r-project.org/package=sf
  • the units package, which is available from https://cran.r-project.org/package=units To run the code of this notebook you can install the packages by install.packages(c("sf","units")). However, they are already installed in the container on which this example is run.
library(sf)
Linking to GEOS 3.9.0, GDAL 3.2.2, PROJ 7.2.1; sf_use_s2() is TRUE

The files “south-america-1990.RData” and “three-countries.RData” were created in a previous example.

load("south-america-1990.RData")
load("three-countries.RData")
st_area(Brazil)
8.504611e+12 [m^2]
in_km2 <- function(x) units::set_units(x,"km^2")
in_km2(st_area(Brazil))
8504611 [km^2]
in_km2(st_area(SthAmCountries))
Units: [km^2]
 [1]  211508.7  145626.6  914740.9 2781219.6 1090257.6 8504611.0  744141.5
 [8]  256452.2  399757.1 1296121.7  177959.4 1140150.9
structure(in_km2(st_area(SthAmCountries)),
          names=as.character(SthAmCountries$CNTRY_NAME))
Units: [km^2]
   Guyana  Suriname Venezuela Argentina   Bolivia    Brazil     Chile   Ecuador 
 211508.7  145626.6  914740.9 2781219.6 1090257.6 8504611.0  744141.5  256452.2 
 Paraguay      Peru   Uruguay  Colombia 
 399757.1 1296121.7  177959.4 1140150.9 
st_distance(Brasilia,Bogota)
Units: [m]
        [,1]
[1,] 3668619
st_distance(Chile,Bogota)
Units: [m]
        [,1]
[1,] 2509338
# This takes a while, because R needs to figure out which points of the borders are the closest to one another
st_distance(Chile,Colombia)
Units: [m]
        [,1]
[1,] 1476222
in_km <- function(x) units::set_units(x,"km")
in_km(st_distance(Brasilia,Bogota))
Units: [km]
         [,1]
[1,] 3668.619
in_km(st_distance(ThreeCapitals))
Units: [km]
         [,1]     [,2]     [,3]
[1,]    0.000 3016.837 3668.619
[2,] 3016.837    0.000 4251.617
[3,] 3668.619 4251.617    0.000