-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-gridlayer-subset.R
executable file
·276 lines (226 loc) · 7.32 KB
/
data-gridlayer-subset.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#' Subsetting a gridlayer object.
#'
#' The function extracts subsets of the \code{gridlayer} depending on different criteria.
#'
#' The following methods are incorporated into the function: If \code{i} argument is a vector of integers, they will be interpreted as indices. If the \code{numeric} \code{i} contains either the lamin, lamax, lomin or lomax names, the subsetting will be done using the latitude-longitude coordinates outlined by these 4 values. Logical subsetting and subsetting by face names are also possible.
#'
#' @param x (\code{\link{trigrid}}, \code{\link{hexagrid}} or \code{\link{facelayer}}) The object to be subsetted.
#' @param i (\code{logical}, \code{numeric} or \code{character}) The subscript vector, specifying the faces that are used for subsetting. As in \code{\link[base]{subset}}.
#'
#' @rdname subset
#' @exportMethod subset
setMethod(
"subset",
signature="gridlayer",
definition=function(x, i){
if(is.numeric(i)){
#add checking for lat/long subsetting
# lat-long mode of subsetting
potConds<-c("lamin", "lamax", "lomin", "lomax")
if(sum(names(i)%in%potConds)>0){
#if it contains an unitelligible names
if(sum(!names(i)%in%potConds)>0)
warning("Some subscript condition names were not recognized.")
#in case you want something at the dateline
normal <- T
if(sum(c("lomax", "lomin")%in%names(i))==2){
if(i["lomin"]>i["lomax"]){
normal<- F
}
}
#get the facecenters
actGrid<-get(x@grid)
pol <- CarToPol(actGrid@faceCenters, norad=TRUE, origin=actGrid@center)
boolSelect<-rep(T, nrow(pol))
#longitude
if(normal){
#minimum longitude condition
if("lomin"%in%names(i)){
boolSelect <- boolSelect & pol[,1]>=i["lomin"]
}
#maximum longitude condition
if("lomax"%in%names(i)){
boolSelect <- boolSelect & pol[,1]<=i["lomax"]
}
}else{
#minimum longitude condition
if("lomin"%in%names(i)){
boolSelect <- boolSelect & pol[,1]>=i["lomin"]
}
#maximum longitude condition
if("lomax"%in%names(i)){
boolSelect <- boolSelect | pol[,1]<=i["lomax"]
}
}
#minimum latitude condition
if("lamin"%in%names(i)){
boolSelect <- boolSelect & pol[,2]>=i["lamin"]
}
#minimum latitude condition
if("lamax"%in%names(i)){
boolSelect <- boolSelect & pol[,2]<=i["lamax"]
}
i<-rownames(actGrid@faceCenters)[boolSelect]
# control will pass over to the subsetting by facenames
}else{
# index subsetting
y<-x
y@names<-y@names[i]
y@values<-y@values[i]
y@length<-length(y@values)
}
}
if(is.logical(i)){
if(length(i)==(length(x@names))){
i<-x@names[i]
}else{
stop("Length of logical subscript does not match the facelayer.")
}
}
if(is.character(i)){
if(sum(i%in%x@names)==length(i)){
y<-x
y@names<-i
y@values<-y@values[x@names%in%i]
y@length<-length(y@values)
}
}
return(y)
}
)
#subsetting for layers
#' Extraction from a gridlayer using indices
#'
#' Shorthand to the \code{\link[icosa]{subset}} function.
#'
#' @param x (\code{\link{facelayer}}) The object to be subsetted.
#' @param i (\code{logical}, \code{numeric} or \code{\link[terra:ext]{SpatExtent}}) The subscript vector, or extent, specifying the faces that are used for subsetting. As in \code{\link[base]{subset}}.
#' @exportMethod "["
#' @return The extraction methods return \code{\link{facelayer}}-class objects.
#' @rdname extract-methods
setMethod(
"[",
signature=c("gridlayer","ANY", "missing"),
definition=function(x,i){
subset(x, i)
}
)
#' @exportMethod "["
#' @rdname extract-methods
setMethod(
"[",
signature=c("gridlayer","SpatExtent", "missing"),
definition=function(x,i){
#check the extent object
if(!requireNamespace("terra", quietly = TRUE)) stop("Install the 'terra' package to run this function.")
actGrid <- get(x@grid)
pol <- CarToPol(actGrid@faceCenters, origin=actGrid@center)
boolLong<-pol[,1]>=terra::ext(i)[1] & pol[,1]<=terra::ext(i)[2]
boolLat<-pol[,2]>=terra::ext(i)[3] & pol[,2]<=terra::ext(i)[4]
nm<-rownames(pol)[boolLong & boolLat]
subset(x, nm)
}
)
# this method produces a warning without the aliases!!!
#' Replacement of elements in a gridlayer object.
#'
#' Function to replace specific elements in a gridlayer object
#'
#' All these methods are implementing direct replacement in the \code{@values} slot of a layer, depending on criteria used for subsetting.
#'
#' @param value The replacement values.
#'
#' @docType methods
#' @aliases [<-,gridlayer-method
#' @exportMethod "[<-"
#' @rdname extract-methods
setReplaceMethod(
"[",
signature=c("gridlayer"),
# definition=function(x,i,j,..., value){
definition=function(x,i,value){
y<-x
#named vector replacement
if(length(names(value))>0 & missing(i)){
if(sum(names(value)%in%y@names)==length(value)){
u<-y@values
names(u)<-y@names
u[names(value)]<-value
y@values<-u
}
}else{
#numeric
if(is.numeric(i)){
#add checking for lat/long subsetting
# lat-long mode of subsetting
potConds<-c("lamin", "lamax", "lomin", "lomax")
if(sum(names(i)%in%potConds)>0){
#if it contains an unitelligible names
if(sum(!names(i)%in%potConds)>0)
warning("Some subscript condition names were not recognized.")
#in case you want something at the dateline
normal <- T
if(sum(c("lomax", "lomin")%in%names(i))==2){
if(i["lomin"]>i["lomax"]){
normal<- F
}
}
#get the facecenters
actGrid<-get(x@grid)
pol <- CarToPol(actGrid@faceCenters, norad=TRUE, origin=actGrid@center)
boolSelect<-rep(T, nrow(pol))
#longitude
if(normal){
#minimum longitude condition
if("lomin"%in%names(i)){
boolSelect <- boolSelect & pol[,1]>=i["lomin"]
}
#maximum longitude condition
if("lomax"%in%names(i)){
boolSelect <- boolSelect & pol[,1]<=i["lomax"]
}
}else{
#minimum longitude condition
if("lomin"%in%names(i)){
boolSelect <- boolSelect & pol[,1]>=i["lomin"]
}
#maximum longitude condition
if("lomax"%in%names(i)){
boolSelect <- boolSelect | pol[,1]<=i["lomax"]
}
}
#minimum latitude condition
if("lamin"%in%names(i)){
boolSelect <- boolSelect & pol[,2]>=i["lamin"]
}
#minimum latitude condition
if("lamax"%in%names(i)){
boolSelect <- boolSelect & pol[,2]<=i["lamax"]
}
i<-rownames(actGrid@faceCenters)[boolSelect]
# control will pass over to the subsetting by facenames
}else{
# index subsetting
actGrid<-get(x@grid)
subGrid<-subset(actGrid,i)
i<-rownames(subGrid@faces)
}
}
# pass on from the numeric too!
if(is.character(i)){
if(sum(i%in%y@names)==length(i)){
u<-y@values
names(u)<-y@names
u[i]<-value
y@values<-u
}else{
stop("Invalid character subscript.")
}
}
if(is.logical(i)){
y@values[i]<-value
}
}
return(y)
}
)