Libraries to install from CRAN if not installed:
> install.packages("sp")
> install.packages("rgeos")
> install.packages("readxl")
> install.packages("raster")
> install.packages("devtools")
> install.packages("geosphere")
Library to install from GitHub if not installed:
> devtools::install_github("choisy/gadmVN")
Some libraries to load for interactive use:
> library(sp) # coordinates, CRS, proj4string, spTransform, over
> library(rgeos) # gIntersection
rgeos version: 0.3-26, (SVN revision 560)
GEOS runtime version: 3.6.1-CAPI-1.10.1 r0
Linking to sp version: 1.2-5
Polygon checking: TRUE
The polygon of Vietnam:
> vn <- gadmVN::gadm(level = "country")
The coordinates of the cases:
> cases <- readxl::read_excel("LongLat.xlsx")
> coordinates(cases) <- ~ LONGITUDE + LATITUDE
> proj4string(cases) <- CRS("+proj=longlat")
Making sure that the coordinates of the cases have the same projection as the polygon of Vietnam:
> cases <- spTransform(cases, proj4string(vn))
Defining buffers:
> radius <- 10000 # in meters
> buffers <- raster::buffer(cases, radius, FALSE)
Checking that all the buffers overlay with the polygons of Vietnam:
> any(is.na(sapply(seq_along(cases), function(x) over(vn, buffers[x]))))
[1] FALSE
Calculating the areas of the buffers, defining as the intersection of the buffers with the polygon of Vietnam:
> denom <- 10^6 # to get area in square km
> areas <- sapply(seq_along(buffers),
+ function(x)
+ geosphere::areaPolygon(gIntersection(vn, buffers[x])) / denom)
Checking the distribution of these areas:
> hist(areas, col = "grey")