Chapter 2 Basic Geospatial Operations in R
In this chapter we get our hands dirty with spatial manipulation of data. Thus far, our data manipulation exercises (using
dplyr) were such that you might be familiar with, from any earlier exposures to data analysis. For example, linking datasets using a common column is a task which you can perform on spatial or non-spatial data. These are referred to as attribute operations. However, in this chapter we will explore some exercises in data manipulation which are specific to spatial data analysis. We will be learning some key spatial operations: a set of functions that allow you to create new and manipulate spatial data.
The main objectives for this chapter are that by the end you will have:
-
met a new format for accessing boundary data, called geoJSON.
-
carried out spatial operations such as:
-
subset points that are within a certain area,
-
created new polygons by generating buffers around points,
-
counted the number of points that fall within a polygon (known as points in polygon),
-
finding the nearest feature in one data set to observations in another data set, and
-
measured distance between points in a map.
-
-
made interactive point map with leaflet.
-
used geocoding methods to translate text fields such as addresses into geographic coordinates.
These are all very useful tools for the spatial crime analyst, and we will hope to demonstrate this by working through an example project. The packages we will use in this chapter are:
# Packages for reading data and data carpentry
library(readr)
library(dplyr)
library(janitor)
library(units)
library(purrr)
# Packages for handling spatial data and for geospatial carpentry
library(sf)
library(tidygeocoder)
library(crsuggest)
# Packages for mapping and visualisation
library(leaflet)
library(RColorBrewer)
# Packages providing accesss to spatial data
library(osmdata)
