Subsetting 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” and “ged191.RData” were created in previous examples.


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

# This fails due to different coordinate reference systems
Colombia.conflicts <- ged191[Colombia,]
Error in st_geos_binop("intersects", x, y, sparse = sparse, prepared = prepared): st_crs(x) == st_crs(y) is not TRUE
Traceback:

1. ged191[Colombia, ]
2. `[.sf`(ged191, Colombia, )
3. lengths(op(x, i, ...))
4. op(x, i, ...)
5. st_intersects.sf(x, i, ...)
6. st_geos_binop("intersects", x, y, sparse = sparse, prepared = prepared)
7. stopifnot(st_crs(x) == st_crs(y))

st_crs(Colombia)
Coordinate Reference System:
  User input: +proj=longlat +ellps=WGS84
  wkt:
GEOGCS["WGS 84",
    DATUM["unknown",
        SPHEROID["WGS84",6378137,298.257223563]],
    PRIMEM["Greenwich",0],
    UNIT["degree",0.0174532925199433]]

st_crs(ged191)
Coordinate Reference System:
  User input: EPSG:4326
  wkt:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0,
        AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.0174532925199433,
        AUTHORITY["EPSG","9122"]],
    AUTHORITY["EPSG","4326"]]

# Here we make sure that the coordinate reference systems match
ged191_ellips <- st_transform(ged191,st_crs(Colombia))
Colombia.conflicts <- ged191_ellips[Colombia,]
although coordinates are longitude/latitude, st_intersects assumes that they are planar

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


mypal <- function(n)gray.colors(n,start=.2,end=.9,alpha=.5)
plot(st_geometry(Colombia))
plot(Colombia.conflicts["deaths_civilians"],
     add=TRUE,pch=19,cex=.2,
     pal=mypal,
     nbreaks=30
     )
/book/data-management-r/08-spatial-geographical-data/spatial-subsets/book_data-management-r_08-spatial-geographical-data_spatial-subsets_9_0.png

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

Bogota.200km <- st_circ(Bogota,dist.km=200)
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).


Bogota.conflicts <- ged191_ellips[Bogota.200km,]
although coordinates are longitude/latitude, st_intersects assumes that they are planar

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


plot(st_geometry(Colombia))
plot(st_geometry(Colombia.conflicts),
     add=TRUE,pch=1,cex=.3,col="gray80"
     )
plot(st_geometry(Bogota.conflicts),
     add=TRUE,pch=19,cex=.3)
plot(Bogota.200km,lty=3,add=TRUE)
/book/data-management-r/08-spatial-geographical-data/spatial-subsets/book_data-management-r_08-spatial-geographical-data_spatial-subsets_13_0.png

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.