-
Notifications
You must be signed in to change notification settings - Fork 3
/
README.Rmd
150 lines (93 loc) · 4.02 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
---
output: github_document
editor_options:
chunk_output_type: console
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
library(reproj)
```
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/PROJ)](https://CRAN.R-project.org/package=PROJ)
[![R-CMD-check](https://github.com/hypertidy/PROJ/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/hypertidy/PROJ/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
# PROJ
The goal of PROJ is to provide generic coordinate system transformations in R.
This is a shared goal with the [reproj](https://cran.r-project.org/package=reproj)
package, and PROJ provides the infrastructure for later versions of the underlying library.
PROJ provides basic coordinate transformations for generic numeric data in matrices or data frames. Transforming spatial data coordinates is a basic task independent of storage format.
PROJ is strictly for modern versions of the PROJ library.
We can use 'auth:code' forms, PROJ.4 strings, full WKT2, or the name of a CRS as
found in the PROJ database, e.g 'WGS84', 'NAD27', etc. Full details are provided
in the [PROJ
documentation](https://proj.org/development/reference/functions.html#c.proj_create).
## Things to be aware of
* Input can be a data frame or a matrix, but internally input is assumed to be x, y, z, *and time*. So the output is always a 4-column list.
* You can't use strings like "+init=epsg:4326" any more, it must be "EPSG:<code>", and we use "OGC:CRS84" now.
* You should know what your target projection is, and also what your source projection is. This is your responsibility.
* PROJ assumes longitude/latitude order always by setting the PROJ library context *proj_normalize_for_visualization*.
Please see [PROJ library documentation](https://proj.org/development/quickstart.html) for details on this.
## Installation
On all systems do
```R
install.packages("PROJ")
```
or
```R
remotes::install_cran("PROJ")
```
To install the development version from Github do
```R
remotes::install_github("hypertidy/PROJ")
```
## Example
Minimal code example, two lon-lat coordinates to LAEA, and back.
```{r minimal}
library(PROJ)
lon <- c(0, 147)
lat <- c(0, -42)
dst <- "+proj=laea +datum=WGS84 +lon_0=147 +lat_0=-42"
src <- "+proj=longlat +datum=WGS84"
## forward transformation
(xy <- proj_trans( cbind(lon, lat), dst, source = src))
## inverse transformation
proj_trans(cbind(xy$x_, xy$y_), src, source = dst)
## note that NAs propagate in the usual way
lon <- c(0, NA, 147)
lat <- c(NA, 0, -42)
proj_trans(cbind(lon, lat), src, source = dst)
```
A more realistic example with coastline map data.
```{r example}
library(PROJ)
w <- PROJ::xymap
lon <- na.omit(w[,1])
lat <- na.omit(w[,2])
dst <- "+proj=laea +datum=WGS84 +lon_0=147 +lat_0=-42"
xy <- proj_trans(cbind(lon, lat), dst, source = "OGC:CRS84")
plot(xy$x_, xy$y_, pch = ".")
lonlat <- proj_trans(xy, src, source = dst)
plot(lonlat$x_, lonlat$y_, pch = ".")
```
## Convert projection strings
We can generate PROJ or within limitations WKT2 strings, format 0, 1, 2 for WKT, proj4string, projjson respectively.
```{r WKT2, eval=TRUE}
cat(wkt2 <- proj_crs_text("OGC:CRS84"))
proj_crs_text(wkt2, format = 1L)
```
A geocentric example, suitable for plotting in rgl.
```{r geocentric}
xyzt <- proj_trans(cbind(w[,1], w[,2]), z_ = rep(0, dim(w)[1L]), target = "+proj=cart +datum=WGS84", source = "OGC:CRS84")
plot(as.data.frame(xyzt[1:3]), pch = ".", asp = 1)
```
Geocentric transformations aren't used in R much, but some examples are found in the [quadmesh](https://CRAN.R-project.org/package=quadmesh) and [anglr](https://github.com/hypertidy/anglr) packages.
---
Please note that the PROJ project is released with a
[Contributor Code of Conduct](https://github.com/hypertidy/PROJ/blob/main/CODE_OF_CONDUCT.md).
By contributing to this project, you agree to abide by its terms.