-
Notifications
You must be signed in to change notification settings - Fork 2
/
esri-featureset-list.R
247 lines (220 loc) · 7.08 KB
/
esri-featureset-list.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#' Create Esri FeatureSet Objects
#'
#' These functions create an Esri FeatureSet object. A FeatureSet contains an inner array
#' of features as well as additional metadata about the the collection such as the
#' geometry type, spatial reference, and object ID field.
#'
#' @inheritParams as_esri_features
#' @export
#' @rdname featureset
#' @references [API Reference](https://developers.arcgis.com/documentation/common-data-types/featureset-object.htm)
#' @examples
#'
#' library(sf)
#' # POINT
#' # create sfg points
#' xy <- st_sfc(st_point(c(1, 2)))
#' xyz <- st_sfc(st_point(c(1, 2, 3)))
#' xym <- st_sfc(st_point(c(1, 2, 3), dim = "XYM"))
#'
#' as_esri_featureset(xy)
#' as_esri_featureset(xyz)
#' as_esri_featureset(xym)
#'
#' # MULTIPOINT
#' # vector to create matrix points
#' set.seed(0)
#' x <- rnorm(12)
#'
#' xy <- st_sfc(st_multipoint(matrix(x, ncol = 2)))
#' xyz <- st_sfc(st_multipoint(matrix(x, ncol = 3)))
#' xym <- st_sfc(st_multipoint(matrix(x, ncol = 3), dim = "XYM"))
#'
#' as_esri_featureset(xy)
#' as_esri_featureset(xyz)
#' as_esri_featureset(xym)
#'
#' # LINESTRING
#' xy <- st_sfc(st_linestring(matrix(x, ncol = 2)))
#' xyz <- st_sfc(st_linestring(matrix(x, ncol = 3)))
#' xym <- st_sfc(st_linestring(matrix(x, ncol = 3), dim = "XYM"))
#'
#' as_esri_featureset(xy)
#' as_esri_featureset(xyz)
#' as_esri_featureset(xym)
#'
#' # MULTILINESTRING
#' as_esri_featureset(st_sfc(st_multilinestring(list(xy[[1]], xy[[1]]))))
#' as_esri_featureset(st_sfc(st_multilinestring(list(xyz[[1]], xyz[[1]]))))
#' as_esri_featureset(st_sfc(st_multilinestring(list(xym[[1]], xym[[1]]))))
#'
#' # POLYGON
#' coords <- rbind(
#' c(0, 0, 0, 1),
#' c(0, 1, 0, 1),
#' c(1, 1, 1, 1),
#' c(1, 0, 1, 1),
#' c(0, 0, 0, 1)
#' )
#'
#' xy <- st_sfc(st_polygon(list(coords[, 1:2])))
#' xyz <- st_sfc(st_polygon(list(coords[, 1:3])))
#' xym <- st_sfc(st_polygon(list(coords[, 1:3]), dim = "XYM"))
#'
#' as_esri_featureset(xy)
#' as_esri_featureset(xyz)
#' as_esri_featureset(xym)
#'
#' # MULTIPOLYGON
#' as_esri_featureset(st_sfc(st_multipolygon(list(xy[[1]], xy[[1]]))))
#' as_esri_featureset(st_sfc(st_multipolygon(list(xyz[[1]], xyz[[1]]))))
#' as_esri_featureset(st_sfc(st_multipolygon(list(xym[[1]], xym[[1]]))))
as_featureset <- function(x, crs = sf::st_crs(x), call = rlang::caller_env()) {
# class check
valid_sfg_classes <- c(
"sf",
"data.frame",
"sfc"
)
# exit if an invalid geometry type is provided
if (!rlang::inherits_any(x, valid_sfg_classes)) {
cli::cli_abort(
"{.arg x} must inherit one of the following classes {.cls {valid_sfg_classes}}",
call = call
)
}
sr <- validate_crs(crs)[[1]] %||% list()
# store the class name to be used in a switch statement
x_class <- inherits_which(x, valid_sfg_classes)[1]
switch(x_class,
"sf" = as_featureset_sf(x, sr, call = call),
"data.frame" = as_featureset_sf(x, sr, call = call),
"sfc" = as_featureset_sfc(x, sr, call = call)
)
}
as_featureset_sfc <- function(x, crs = NULL, call = rlang::caller_env()) {
# TODO handle CRS
sr <- crs %||% list()
# class check
valid_sfg_classes <- c(
"sfc_POINT",
"sfc_MULTIPOINT",
"sfc_LINESTRING",
"sfc_MULTILINESTRING",
"sfc_POLYGON",
"sfc_MULTIPOLYGON"
)
# exit if an invalid geometry type is provided
if (!rlang::inherits_any(x, valid_sfg_classes)) {
cli::cli_abort(
"{.arg x} must inherit one of the following classes {.cls {valid_sfg_classes}}",
call = call
)
}
# store the class name to be used in a switch statement
sfc_class <- inherits_which(x, valid_sfg_classes)
# dimension check
z <- has_z(x)
m <- has_m(x)
# abort if 4D
if (z && m) {
cli::cli_abort(
c(
"{.cls XYZM} geometries detected. Only {.cls XY}, {.cls XYZ}, and {.cls XYM} geometries supported. ",
">" = "{.href [please make an issue on GitHub](https://github.com/r-arcgis/arcgisutils)}"
),
call = call
)
}
# determine if 3D
three_dim <- z || m
# switch based on dimensions
if (three_dim) {
switch(sfc_class,
"sfc_POINT" = sfc_point_featureset_3d_list(x, sr),
"sfc_MULTIPOINT" = sfc_multipoint_featureset_3d_list(x, sr),
"sfc_LINESTRING" = sfc_linestring_featureset_3d_list(x, sr),
"sfc_MULTILINESTRING" = sfc_multilinestring_featureset_3d_list(x, sr),
"sfc_POLYGON" = sfc_polygon_featureset_3d_list(x, sr),
"sfc_MULTIPOLYGON" = sfc_multipolygon_featureset_3d_list(x, sr),
)
} else {
switch(sfc_class,
"sfc_POINT" = sfc_point_featureset_2d_list(x, sr),
"sfc_MULTIPOINT" = sfc_multipoint_featureset_2d_list(x, sr),
"sfc_LINESTRING" = sfc_linestring_featureset_2d_list(x, sr),
"sfc_MULTILINESTRING" = sfc_multilinestring_featureset_2d_list(x, sr),
"sfc_POLYGON" = sfc_polygon_featureset_2d_list(x, sr),
"sfc_MULTIPOLYGON" = sfc_multipolygon_featureset_2d_list(x, sr),
)
}
}
as_featureset_sf <- function(x, crs = NULL, call = rlang::caller_env()) {
# must be a data.frame
check_data_frame(x)
# TODO handle CRS
sr <- crs %||% list()
# get the geometry column
geom_col <- attr(x, "sf_column") %||% ""
# if it is null then make it into a list
.geoms <- x[[geom_col]] %||% list()
# determine dims
if (rlang::is_empty(.geoms)) {
z <- FALSE
m <- FALSE
} else {
z <- has_z(.geoms)
m <- has_m(.geoms)
}
# abort if 4D
if (z && m) {
cli::cli_abort(
c(
"{.cls XYZM} geometries detected. Only {.cls XY}, {.cls XYZ}, and {.cls XYM} geometries supported. ",
">" = "{.href [please make an issue on GitHub](https://github.com/r-arcgis/arcgisutils)}"
),
call = call
)
}
# check if three dim
three_dim <- z || m
# extract the data.frame without using sf
# we would otherwise use st_drop_geometry
.data <- x
.data[[geom_col]] <- NULL
# we've verified that x is a data.frame so here we remove any other subclasses
attr(.data, "class") <- "data.frame"
# find any factors and convert them to character
factor_check <- which(vapply(.data, is.factor, logical(1)))
# convert to factor
# this wont execute if there aren't any factors
for (idx in factor_check) {
.data[[idx]] <- as.character(.data[[idx]])
}
are_dates <- which(vapply(.data, is_date, logical(1)))
for (col in are_dates) {
.data[[col]] <- date_to_ms(x[[col]])
}
# find columns that cannot be supported
esri_types <- infer_esri_type(.data)
invalid_types <- is.na(esri_types$type)
bad_names <- esri_types$name[invalid_types]
# here we extract the friendly label for error printing
bad_types <- lapply(.data[, invalid_types], obj_type_friendly)
# report an error in the case that theres a list column or something else weird
if (!rlang::is_empty(bad_types)) {
cli::cli_abort(
c(
"{.var {bad_names}} cannot be converted into EsriJSON. Found {bad_types}"
),
call = call
)
}
# use the appropriate function based on if there is a Z or M value
# note that st_as_features_2d is used in the case of data.frame
if (three_dim) {
as_featureset_3d_list(.data, .geoms, nrow(.data), sr)
} else {
as_featureset_2d_list(.data, .geoms, nrow(.data), sr)
}
}