R for Data Science
November 21, 2024
Assigned Reading: Geocomputation with R Chapter 2.
GIS has always had big data, and distinct statistics…
‘Geodesy is the science of accurately measuring and understanding the Earth’s geometric shape, orientation in space, and gravity field.’ - NOAA
Geoid Cross Section & IGCM Geoid
- Reference frame established to represent locations within the frame.
- Historically these were locally focused and based on Geoids.
- either horizontal (X & Y) or vertical (Z) features.
- locations are then measured in relation to the control points.
::::
“The geodetic (or geographic) latitude is the angle between the equatorial plane and the normal (vertical) to the ellipsoid surface at the considered point.” - Proj
\[ \text{Latitude of Tech} = 42°03'27.7" N \]
Convert the latitude of the tech building from DMS to DD
\[ \text{Decimal Degrees} = \text{Degrees } + \frac{\text{Minutes}}{60} + \frac{\text{Seconds}}{3600} \]
\[ 42 + (\frac{03'}{60}) + (\frac{27.7"}{3600}) \]
\[ 42 + 0.05 + 0.007694 = 42.05759 \text{N Decimal Degrees} \]Common for field work, planar measurements of meters.
Going from three to two dimensions does not work well. But we have worked around this. sort of
Type | Examples | Pros | Cons |
---|---|---|---|
Equal Area | Lambert Cylindrical | No distortion of area near equator | Distorts area near the Poles |
Equal Distance | Equi-rectangular | Looks good in mapping applications | Distorts both shape and directions |
Conformal | Mercator | Boundaries are accurate | Distorts area near the Poles |
Compromise | Sensitive to area and direction | Sensitive to area and direction |
- There are various models (geoids & ellipsoids) to represent the earths shape
- Different datums are used to represent different parts of the earth.
- You will almost always use WGS 84 (based on a ellipsoid – which is fit through a special model of earths gravitational fields a geoid) NAD83 (based on a ellipsoid) for a geographic coordinate system. These +/-1 m from each other across much of North America. More useful than a geoid.
- You will usually want to use a UTM grid based on WGS or State Planes based on NAD83 for projections.
- Different coordinates notation systems are used, focus on decimal degrees.
Vector data tends to only include features of interest, e.g. bodies of water; whereas a Raster will include, explicitly, the absence of features (e.g. both water and terrestrial areas).
TAXON | DBH | HEIGHT |
---|---|---|
Robinia pseudoacacia | 40 | 24 |
Quercus alba | 32 | 21 |
SFG is the spatial topology associated with a feature
POINT - one-dimensional location
LINESTRING - two points connected by a string
POLYGON - Sequence of points connected by strings
A list column of S3 type containing attributes of the Geometry/Geometries
Coordinate Reference System (‘crs’)
Precision (‘precision’)
Bounding box (‘bbox’)
Number Empty (‘n_empty’)
TAXON | DBH | HEIGHT | LONG | LAT | geometry |
---|---|---|---|---|---|
Robinia pseudoacacia | 40 | 24 | -87.67607 | 42.05663 | POINT (-87.67607 42.05663) |
Quercus alba | 32 | 21 | -87.67399 | 42.05715 | POINT (-87.67399 42.05715) |
Geometry set for 1 feature
Geometry type: POINT
Dimension: XY
Bounding box: xmin: -87.67607 ymin: 42.05663 xmax: -87.67607 ymax: 42.05663
Geodetic CRS: WGS 84
Precision: 2
sfc_POINT of length 1; first list element: 'XY' num [1:2] -87.7 42.1
Released in 2005
First package to hold all major vector types of geometries
Unified spatial class allowed for support in many spatial statistics packages
Improved mapping of spatial objects
Formed the centerpiece of spatial statistics in R for over a dozen years.
SF holds different geometries in list columns, sp has many different types of objects/classes for holding geometries.
SpatialMultiPoints
SpatialMultiLines
SpatialMultiPolygons
SpatialMultiGrids
sf
(‘n_missing’, ‘precision’ lacking)Formal class 'SpatialPoints' [package "sp"] with 3 slots
..@ coords : num [1:15, 1:2] -106.2 -97.6 -127.3 -122.4 -88.7 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "xc" "yc"
..@ bbox : num [1:2, 1:2] -127.3 4.7 -88.7 98.4
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "xc" "yc"
.. .. ..$ : chr [1:2] "min" "max"
..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..@ projargs: chr "+proj=longlat +datum=WGS84 +no_defs"
.. .. ..$ comment: chr "GEOGCRS[\"WGS 84\",\n ENSEMBLE[\"World Geodetic System 1984 ensemble\",\n MEMBER[\"World Geodetic Sys"| __truncated__
Note that both cell sizes, and the resolution of the values in cells can, within reason, be converted to finer and coarser resolution. We will discuss these types of calculations next class.
empty_raster <- raster(
# rasters have 4 bounding edges
# Here we define each 'corner' of the raster'
xmn = 697129.7,
xmx = 811775.7,
ymn = 4388466,
ymx = 4502382,
# Here we set the number of cells 118*118
nrows = 118,
ncols = 118,
# set the rasters Coordinate Reference System
crs = "+proj=utm +zone=10 +datum=WGS84",
# we are using the WGS84 ellipsoid, with a UTM planar projection
)
The raster currently looks like this, a frame in space with a specified origin, CRS, and cells, but lacking any content (values).
0 | 1 | 1 | 0 | 1 | 0 | 0 | 1 |
1 | 0 | 1 | 0 | 0 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 |
0 | 1 | 1 | 0 | 1 | 0 | 0 | 1 |
1 | 0 | 1 | 0 | 0 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 |
The width of each raster cell is: 971.57627 meters
The height of each raster cell is: 965.38983 meters
This example_raster_dec contains: 13924 elements
A single raster layer
The main use of Raster Stacks is to hold layers of similar themes.
sf
!sf
objects are ggplot2
compatiblesf
and tidvyerse
geom_sf
coord_sf
is a helpful modifier to geom_sfbound <- st_bbox(north_carolina)
# retrieve the bounding box from the sfc list column
ggplot() +
geom_sf(data = us_states) +
geom_sf(data = north_carolina, aes(fill = BIR79)) +
coord_sf( # use the bbox to 'crop' the extent of the map
xlim = c(bound[1], bound[3]),
ylim = c(bound[2], bound[4])
) +
theme_bw()
For next Lab: Install these packages (if you have not done so already):
optionally install these:
Download the labs .R script from the course website.
If you are interested in how Drone/LiDAR data are collected please check out the ‘RMBL Spatial Data Science Webinar Series’:
For next Lecture:
Assigned Reading: Chapter 3 of Spatial Data Science
Future Bonus SDS Office Hours
Wednesday Night at 5:00 - 6:00.
Notes on this Lab
My lecture notes are in the R script I used to generate all of the novel figures for this presentation. Likewise this presentation is an .HTML file and can be launched from your computer (it was rendered directly from R using the script).
Anna Krystalli Accessed 01.20.2022
Geocomputation with R Accessed 01.09.2022
raster Accessed 01.09.2022
NEON Accessed 01.19.2022
sf Accessed 01.10.2022
proj Accessed 01.20.2022.
R spatial Accessed 01.11.2022
Wikipedia Accessed 01.09.2022
NOAA Accessed 01.25.2022
[[1]]
Hijmans R (2024). _raster: Geographic Data Analysis and Modeling_. R
package version 3.6-30, <https://CRAN.R-project.org/package=raster>.
[[2]]
Pebesma E, Bivand R (2005). "Classes and methods for spatial data in
R." _R News_, *5*(2), 9-13. <https://CRAN.R-project.org/doc/Rnews/>.
Bivand R, Pebesma E, Gomez-Rubio V (2013). _Applied spatial data
analysis with R, Second edition_. Springer, NY.
<https://asdar-book.org/>.
[[3]]
Pebesma E, Bivand R (2023). _Spatial Data Science: With applications in
R_. Chapman and Hall/CRC. doi:10.1201/9780429459016
<https://doi.org/10.1201/9780429459016>, <https://r-spatial.org/book/>.
Pebesma E (2018). "Simple Features for R: Standardized Support for
Spatial Vector Data." _The R Journal_, *10*(1), 439-446.
doi:10.32614/RJ-2018-009 <https://doi.org/10.32614/RJ-2018-009>,
<https://doi.org/10.32614/RJ-2018-009>.
[[4]]
Wickham H, Averick M, Bryan J, Chang W, McGowan LD, François R,
Grolemund G, Hayes A, Henry L, Hester J, Kuhn M, Pedersen TL, Miller E,
Bache SM, Müller K, Ooms J, Robinson D, Seidel DP, Spinu V, Takahashi
K, Vaughan D, Wilke C, Woo K, Yutani H (2019). "Welcome to the
tidyverse." _Journal of Open Source Software_, *4*(43), 1686.
doi:10.21105/joss.01686 <https://doi.org/10.21105/joss.01686>.
[[5]]
Hijmans R (2024). _terra: Spatial Data Analysis_. R package version
1.7-83, <https://CRAN.R-project.org/package=terra>.