Aggregating spatial feature objects

The following makes use of the sf package. You may need to install it from CRAN using the code install.packages("sf") if you want to run this on your computer. (The package is already installed on the notebook container, however.)


library(sf)
Linking to GEOS 3.7.1, GDAL 2.4.0, PROJ 5.2.0

The files “south-america-1990.RData”, “ged101.RData”, and “cshapes-1990.RData” where created by an earlier example.


load("south-america-1990.RData")
load("ged191.RData")
load("cshapes-1990.RData")

ged191_ellips <- st_transform(ged191,st_crs(cshapes.1990))

# Civilian deaths per country
aggregate(ged191_ellips["deaths_civilians"],by=SthAmCountries,sum)
although coordinates are longitude/latitude, st_intersects assumes that they are planar

   deaths_civilians geometry
0    20             MULTIPOLYGON (((-58.17262 6...
1    NA             MULTIPOLYGON (((-55.12796 5...
3   792             MULTIPOLYGON (((-66.31029 1...
6     0             MULTIPOLYGON (((-71.85916 -...
7     0             MULTIPOLYGON (((-62.19884 -...
8   164             MULTIPOLYGON (((-44.69501 -...
9    NA             MULTIPOLYGON (((-73.0421 -4...
10   15             MULTIPOLYGON (((-79.45167 -...
11    0             MULTIPOLYGON (((-57.67267 -...
12 1021             MULTIPOLYGON (((-69.49973 -...
13   NA             MULTIPOLYGON (((-58.38889 -...
27 6015             MULTIPOLYGON (((-74.86081 1...

# Civilian deaths per country, with country names
within(
    aggregate(ged191_ellips["deaths_civilians"],by=SthAmCountries,sum),
    country <- SthAmCountries$CNTRY_NAME)
although coordinates are longitude/latitude, st_intersects assumes that they are planar

   deaths_civilians geometry                       country
0    20             MULTIPOLYGON (((-58.17262 6... Guyana
1    NA             MULTIPOLYGON (((-55.12796 5... Suriname
3   792             MULTIPOLYGON (((-66.31029 1... Venezuela
6     0             MULTIPOLYGON (((-71.85916 -... Argentina
7     0             MULTIPOLYGON (((-62.19884 -... Bolivia
8   164             MULTIPOLYGON (((-44.69501 -... Brazil
9    NA             MULTIPOLYGON (((-73.0421 -4... Chile
10   15             MULTIPOLYGON (((-79.45167 -... Ecuador
11    0             MULTIPOLYGON (((-57.67267 -... Paraguay
12 1021             MULTIPOLYGON (((-69.49973 -... Peru
13   NA             MULTIPOLYGON (((-58.38889 -... Uruguay
27 6015             MULTIPOLYGON (((-74.86081 1... Colombia

st_circ <- function(x,dist.km){
    dist.degr <- 360*dist.km/40007.863
    st_buffer(st_geometry(x),dist=dist.degr)
}

Bogota.region <- st_circ(Bogota,dist.km=200)
Colombia.rest <- st_difference(st_geometry(Colombia),Bogota.region)
Warning message in st_buffer.sfc(st_geometry(x), dist = dist.degr):
“st_buffer does not correctly buffer longitude/latitude data”
dist is assumed to be in decimal degrees (arc_degrees).

although coordinates are longitude/latitude, st_difference assumes that they are planar


# Civilian deaths in the Bogota region and the rest of Colombia
aggregate(ged191_ellips["deaths_civilians"],
          by=c(Bogota.region,Colombia.rest),
          sum)
although coordinates are longitude/latitude, st_intersects assumes that they are planar

  deaths_civilians geometry
1 1021             POLYGON ((-72.30035 4.6, -7...
2 4994             MULTIPOLYGON (((-74.86081 1...

Downloadable R script and interactive version

Explanation

The link with the “jupyterhub” icon directs you to an interactive Jupyter1 notebook, which runs inside a Docker container2. There are two variants of the interative notebook. One shuts down after 60 seconds and does not require a sign it. The other requires sign in using your ORCID3 credentials, yet shuts down only after 24 hours. (There is no guarantee that such a container persists that long, it may be shut down earlier for maintenance purposes.) After shutdown all data within the container will be reset, i.e. all files created by the user will be deleted.4

Above you see a rendered version of the Jupyter notebook.5

1

For more information about Jupyter see http://jupyter.org. The Jupyter notebooks make use of the IRKernel package.

2

For more information about Docker see https://docs.docker.com/. The container images were created with repo2docker, while containers are run with docker spawner.

3

ORCID is a free service for the authentication of researchers. It also allows to showcase publications and contributions to the academic community such as peer review.. See https://info.orcid.org/what-is-orcid/ for more information.

4

The Jupyter notebooks come with NO WARRANTY whatsoever. They are provided for educational and illustrative purposes only. Do not use them for production work.

5

The notebook is rendered with the help of the nbsphinx extension.