-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpull_crs.R
113 lines (108 loc) · 2.61 KB
/
pull_crs.R
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
#' Extract CRS on WKT format
#'
#' @description
#'
#' Extract the WKT version of the CRS associated to a string, number of
#' sf/Spat* object.
#'
#' The [Well-known text (WKT)](https://en.wikipedia.org/wiki/Well-known_text_representation_of_coordinate_reference_systems)
#' representation of coordinate reference systems (CRS) is a character string
#' that identifies precisely the parameters of each CRS. This is the current
#' standard used on sf and terra packages.
#'
#' @seealso [terra::crs()], [sf::st_crs()]
#'
#' @family helpers
#' @export
#'
#' @return A WKT representation of the corresponding CRS.
#'
#' @param .data Input potentially including or representing a CRS. It could be
#' a `sf/sfc` object, a `SpatRaster/SpatVector` object, a `crs` object from
#' [sf::st_crs()], a character (for example a proj4 string) or a integer
#' (representing an EPSG code).
#'
#' @param ... ignored
#'
#' @details
#'
#' Although the WKT representation is the same, sf and terra slightly differs.
#' For example, a sf user could do:
#'
#' `sf::st_transform(x, 25830)`
#'
#' While a terra user needs to:
#'
#' `terra::project(bb, "epsg:25830")`
#'
#' Knowing the WKT would help to smooth workflows when working with different
#' packages and object types.
#'
#' @section Internals:
#'
#' This is a thin wrapper of [sf::st_crs()] and [terra::crs()].
#' @examples
#'
#' # sf objects
#'
#' sfobj <- sf::st_as_sfc("MULTIPOINT ((0 0), (1 1))", crs = 4326)
#'
#' fromsf1 <- pull_crs(sfobj)
#' fromsf2 <- pull_crs(sf::st_crs(sfobj))
#'
#' # terra
#'
#' v <- terra::vect(sfobj)
#' r <- terra::rast(v)
#'
#' fromterra1 <- pull_crs(v)
#' fromterra2 <- pull_crs(r)
#'
#' # integers
#' fromint <- pull_crs(4326)
#'
#' # Characters
#' fromchar <- pull_crs("epsg:4326")
#'
#'
#' all(
#' fromsf1 == fromsf2,
#' fromsf2 == fromterra1,
#' fromterra1 == fromterra2,
#' fromterra2 == fromint,
#' fromint == fromchar
#' )
#'
#' cat(fromsf1)
pull_crs <- function(.data, ...) {
# Spat* objects handled by crs
if (any(
inherits(.data, "SpatRaster"),
inherits(.data, "SpatVector")
)) {
.data <- terra::crs(.data)
}
if (any(
is.na(.data), is.null(.data)
)) {
return(NA)
}
if (inherits(.data, "character")) {
if (.data == "") {
return(NA)
}
}
# sf objects, characters and numerics are handled by sf
if (any(
inherits(.data, "sf"),
inherits(.data, "sfc"),
inherits(.data, "crs"),
inherits(.data, "character"),
inherits(.data, "numeric")
)) {
return(sf::st_crs(.data)$wkt)
}
# All the rest, return empty with an alert
cli::cli_alert_warning("No wkt equivalent found. Returning NA")
return(NA)
}