\(~\)
Hello everyone 👋,
we hope you have enjoyed our small presentation, refreshed your knowledge about Coordinate Reference Systems and got some insights on how geographic data they works in R and what potential data operations can be done with it.
Ready to play around with sf and do some simple operations and create a nice map in the end (hopefully) 😍?
In this little exercise we would like you to
sf
- packageRefreshing you memory from the presentation…
In your own words, why do we need a “Coordinate Reference System” (CRS)?
#
#
The first step is to download the concerning packages and import the libraries for using sf
and the spatial data.
# install.packages("sf", "spData", "spDataLarge")
# The Essentials
library(sf) # classes and functions for vector data
library(tidyverse) # for tidy data and ggplot2
# Datasets
library(rgdal) # dataset of city centers (points)
library(spData) # load explanatory geographic data
library(spDataLarge) # load larger geographic data
#(Intermediary Mapping)
library(tmap) # to create nice and interactive maps
library(jsonlite) # Convert R objects to/from JSON
Be careful: There might be a little bug in installing sf
and spData
. If you’re running Mac or Linux, installing sf
may not work directly. Please have a look here to download the relevant packages. Then use the library()
command to load them.
For these exercises we will work with the same dataset from the video. Our new object, new_vector
, will be a polygon representing a world map. (For more info: ?spData::world).
First import the data and make it a readable to sf
:
<- system.file("shapes/world.gpkg", package = "spData")
vector_filepath <- read_sf(vector_filepath) new_vector
We’ve also loaded in a dataset of city centers from the rgdal
package as the object cities
.
new_vector
and cities
** Using st_crs
what can you tell about the data? Ex. the Datums and the retrieved Coordinate Reference System**
#
#
st_crs(new_vector)
## Coordinate Reference System:
## User input: WGS 84
## wkt:
## GEOGCRS["WGS 84",
## DATUM["World Geodetic System 1984",
## ELLIPSOID["WGS 84",6378137,298.257223563,
## LENGTHUNIT["metre",1]]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## CS[ellipsoidal,2],
## AXIS["geodetic latitude (Lat)",north,
## ORDER[1],
## ANGLEUNIT["degree",0.0174532925199433]],
## AXIS["geodetic longitude (Lon)",east,
## ORDER[2],
## ANGLEUNIT["degree",0.0174532925199433]],
## USAGE[
## SCOPE["Horizontal component of 3D system."],
## AREA["World."],
## BBOX[-90,-180,90,180]],
## ID["EPSG",4326]]
st_crs(cities)
## Coordinate Reference System:
## User input: EPSG:3138
## wkt:
## PROJCRS["ETRS89 / ETRS-GK31FIN",
## BASEGEOGCRS["ETRS89",
## DATUM["European Terrestrial Reference System 1989",
## ELLIPSOID["GRS 1980",6378137,298.257222101,
## LENGTHUNIT["metre",1]]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## ID["EPSG",4258]],
## CONVERSION["Finland ETRS-GK31",
## METHOD["Transverse Mercator",
## ID["EPSG",9807]],
## PARAMETER["Latitude of natural origin",0,
## ANGLEUNIT["degree",0.0174532925199433],
## ID["EPSG",8801]],
## PARAMETER["Longitude of natural origin",31,
## ANGLEUNIT["degree",0.0174532925199433],
## ID["EPSG",8802]],
## PARAMETER["Scale factor at natural origin",1,
## SCALEUNIT["unity",1],
## ID["EPSG",8805]],
## PARAMETER["False easting",500000,
## LENGTHUNIT["metre",1],
## ID["EPSG",8806]],
## PARAMETER["False northing",0,
## LENGTHUNIT["metre",1],
## ID["EPSG",8807]]],
## CS[Cartesian,2],
## AXIS["northing (N)",north,
## ORDER[1],
## LENGTHUNIT["metre",1]],
## AXIS["easting (E)",east,
## ORDER[2],
## LENGTHUNIT["metre",1]],
## USAGE[
## SCOPE["Cadastre, engineering survey, topographic mapping (large scale)."],
## AREA["Finland - east of 30°30'E."],
## BBOX[62.08,30.5,64.27,31.59]],
## ID["EPSG",3138]]
\(~\)
st_set_crs
to Add or Change CRS<- st_set_crs(world, "EPSG:4326") CRS_vector
To look up different Datums you can use a website for Coordinate Systems Worldwide
How can we change the Datum for cities
?
#
#
\(~\)
In the short presentation we have shown you the following examples of how to conduct two types of geometric measurements with the sf
-package
Example 1 from presentation:
<- world %>%
Belgium filter(name_long == "Belgium")
st_area(Belgium)
## 30019548314 [m^2]
::set_units(st_area(Belgium), km^2) units
## 30019.55 [km^2]
We can also do this calculation for a US-state:
<- us_states %>%
Florida filter(NAME == "Florida")
st_area(Florida)
## 1.51264e+11 [m^2]
st_area()
is giving us the output in m^2. The output is however really difficult to read, and the US does operate with miles. How can we get an output in squared miles?
#
#
Example 2 from presentation:
Measuring the distance from a cycle hire point at the Palace Gate in London to the center of Redbridge.
<- cycle_hire %>%
cycle_station_palace filter(name == "Palace Gate")
<- lnd %>%
redbridge filter(NAME == "Redbridge")
<- st_centroid(redbridge)
redbridge_centroid st_distance(cycle_station_palace, redbridge_centroid)
## Units: [m]
## [,1]
## [1,] 20258.6
Have a look at the Code. What are the three steps one has to make, to measure distance between two geometric data points? Can we change the units of the output here, too? How?
#
#
st_within()
,st_is_within_distance()
or st_touches()
. If you would like to find out what they do have a look here.In the presentation we have shown how to use sf
in combination with ggplot
or the sf
and plot()
function to create simple maps.
Example from the presentation using ggplot()
in combination with geom_sf()
from the sf
package on data CRS data from the US retrieved from the spData
package.
Example from the presentation using plot()
on data CRS data from the US and attributed data on the median income per state, both retrieved from the spData
package.
\(~\)
During this workshop we’ve covered what CRS data is, the functions of the sf package, and how it can be used. Now brainstorm some applications; What are scenarios (how or when) where you can apply these tools in the future?
#
#
In the presentation we mentioned the tmap
package which offers a bit more comprehensive tools, for example interactive mapping. Please do a quick research. Which function from the tmap
-package can we use to set an interactive view of a map?
#
#
\(~\)
The workshop and these exercises are based on Geocompuataion with R Science, section 2.4, tmap: get started!