/
GSCoverageBand.R
144 lines (130 loc) · 4.58 KB
/
GSCoverageBand.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
#' Geoserver REST API GSCoverageBand
#'
#' @docType class
#' @importFrom R6 R6Class
#' @export
#' @keywords geoserver rest api coverageBand
#' @return Object of \code{\link{R6Class}} for modelling a GeoServer coverage band
#' @format \code{\link{R6Class}} object.
#'
#' @examples
#' GSCoverageBand$new()
#'
#'
#' @section Methods:
#' \describe{
#' \item{\code{new(xml)}}{
#' This method is used to instantiate a \code{GSCoverageBand}
#' }
#' \item{\code{decode(xml)}}{
#' This method is used to decode a \code{GSCoverageBand} from XML
#' }
#' \item{\code{encode()}}{
#' This method is used to encode a \code{GSCoverageBand} to XML
#' }
#' \item{\code{setDefinition(definition)}}{
#' Sets the coverage band definition
#' }
#' \item{\code{setIndex(index)}}{
#' Sets the coverage band index
#' }
#' \item{\code{setCompositionType}}{
#' Sets the composition type. Only 'BAND_SELECT' is supported by GeoServer for now.
#' }
#' \item{\code{addInputBand(band)}}{
#' Adds a input coverage band, object of class \code{GSInputCoverageBand}
#' }
#' \item{\code{delInputBand(band)}}{
#' Removes a input coverage band, object of class \code{GSInputCoverageBand}
#' }
#' }
#'
#' @author Emmanuel Blondel <emmanuel.blondel1@@gmail.com>
#'
GSCoverageBand <- R6Class("GSCoverageBand",
inherit = GSRESTResource,
public = list(
#' @field inputCoverageBands list of input coverage bands
inputCoverageBands = list(),
#' @field definition coverage band definition
definition = NULL,
#' @field index coverage band index
index = NULL,
#' @field compositionType coverage band composition type
compositionType = "BAND_SELECT",
#'@description Initalizes a \link{GSCoverageBand}
#'@param xml object of class \link{xml_node-class}
initialize = function(xml = NULL){
super$initialize(rootName = "coverageBand")
if(!missing(xml) & !is.null(xml)){
self$decode(xml)
}
},
#'@description Decodes from XML
#'@param xml object of class \link{xml_node-class}
decode = function(xml){
xml = xml2::as_xml_document(xml)
def <- xml2::xml_find_first(xml, "//definition")
self$definition <- xml2::xml_text(def)
idx <- xml2::xml_find_first(xml, "//index")
self$index <- xml2::xml_text(idx)
ct <- xml2::xml_find_first(xml, "//compositionType")
self$compositionType <- xml2::xml_text(ct)
bands <- as(xml2::xml_find_all(xml, "//inputCoverageBands/inputCoverageBand"),"list")
if(length(bands)>0){
for(band in bands){
band <- GSInputCoverageBand$new(xml = band)
self$addInputBand(band)
}
}
},
#'@description Set name
#'@param name name
setName = function(name){
self$name = name
},
#'@description Set definition
#'@param definition definition
setDefinition = function(definition){
self$definition = definition
},
#'@description Set index
#'@param index index
setIndex = function(index){
self$index = index
},
#'@description Set composition type
#'@param compositionType composition type
setCompositionType = function(compositionType){
self$compositionType = compositionType
},
#'@description Adds an input band
#'@param band object of class \link{GSInputCoverageBand}
#'@return \code{TRUE} if added, \code{FALSE} otherwise
addInputBand = function(band){
if(!is(band, "GSInputCoverageBand")){
stop("The 'band' object object should be of class 'GSInputCoverageBand'")
}
startNb = length(self$inputCoverageBands)
availableInputBands <- sapply(self$inputCoverageBands, function(x){x$band})
if(length(which(availableInputBands == band$band)) == 0){
self$inputCoverageBands = c(self$inputCoverageBands, band)
}
endNb = length(self$inputCoverageBands)
return(endNb == startNb+1)
},
#'@description Deletes an input band
#'@param band object of class \link{GSInputCoverageBand}
#'@return \code{TRUE} if deleted, \code{FALSE} otherwise
delInputBand = function(band){
if(!is(band, "GSInputCoverageBand")){
stop("The 'band' object object should be of class 'GSInputCoverageBand'")
}
startNb = length(self$inputCoverageBands)
availableInputBands <- sapply(self$inputCoverageBands, function(x){x$band})
self$inputCoverageBands = self$inputCoverageBands[which(availableInputBands != band$band)]
endNb = length(self$inputCoverageBands)
return(endNb == startNb-1)
}
)
)