---
title: "Coordinate reference system"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Coordinate reference system transformations in 3-D}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---
<style>
body {
text-align: justify}
</style>
```{r, echo = FALSE, eval=TRUE, include=FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
options(knitr.table.format = "html", rmarkdown.html_vignette.check_title = FALSE)

library(eRTG3D)
set.seed(123)
WGS84 <- "+init=epsg:4326"
LV95 <- "+init=epsg:2056"
track.wgs84 <- transformCRS.3d(niclas, fromCRS=LV95, toCRS=WGS84)[ ,1:3]
```

## Definition
*"A [...] coordinate reference system (CRS) is a coordinate-based local, regional or global system used to locate geographical entities. A spatial reference system defines a specific map projection, as well as transformations between different spatial reference systems. [...] spatial reference systems can be referred to using a SRID integer, including EPSG codes ..."* ([wikipedia.org](https://en.wikipedia.org/wiki/Spatial_reference_system), 2019).

The SRIDs and corresponding EPSG codes of all CRS can be found on [spatialreference.org](https://spatialreference.org). The most common global CRS in latitude and longitude is WGS84 (used by the GPS satellite navigation system) with the following CRS string `"+init=epsg:4326"` as identifier.

## Transform CRS in 3-D
The eRTG3D only supports calculations in *Cartesian* coordinate systems. In case of latitude / longitude data (e.g.: WGS84), these must be converted into a *Cartesian* coordinate system, as for example the Universal Transverse Mercator (UTM).
```{r eval=TRUE}
head(track.wgs84)
```

To carry out a CRS transformation, please use `transformCRS.3d()`, which is based on the `st_transform()` from the `sf` package. Therefore is supports CRS transformations in 3-D.

```{r eval=TRUE}
WGS84 <- "+init=epsg:4326"
LV95 <- "+init=epsg:2056"
track <- transformCRS.3d(track.wgs84, fromCRS=WGS84, toCRS=LV95)
head(track)
```

**Note:** `spTransform()` from the `sp` package only supports transformations in the 2-D plane, which will cause distortions in the third dimension.
