-
Notifications
You must be signed in to change notification settings - Fork 24
/
coverages.go
240 lines (207 loc) · 8.43 KB
/
coverages.go
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
package geoserver
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
)
// Coverage is geoserver Coverage (raster layer) data struct
type Coverage struct {
Name string `json:"name,omitempty"`
NativeCoverageName string `json:"nativeCoverageName,omitempty"`
NativeName string `json:"nativeName,omitempty"`
NativeFormat string `json:"nativeFormat,omitempty"`
Namespace *Resource `json:"namespace,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Abstract string `json:"abstract,omitempty"`
Keywords *Keywords `json:"keywords,omitempty"`
NativeCRS *CRSType `json:"nativeCRS,omitempty"`
Srs string `json:"srs,omitempty"`
Enabled bool `json:"enabled,omitempty"`
NativeBoundingBox *NativeBoundingBox `json:"nativeBoundingBox,omitempty"`
LatLonBoundingBox *LatLonBoundingBox `json:"latLonBoundingBox,omitempty"`
ProjectionPolicy string `json:"projectionPolicy,omitempty"`
Store *Resource `json:"store,omitempty"`
CqlFilter string `json:"cqlFilter,omitempty"`
OverridingServiceSRS bool `json:"overridingServiceSRS,omitempty"`
//Metadata *Metadata `json:"metadata,omitempty"` //need to fix the implementation due to json parse error
//SupportedFormats []string `json:"supportedFormats,omitempty"` //need to fix the implementation due to json parse error
}
type publishedCoverageDescr struct {
Name string `json:"name,omitempty"`
NativeCoverageName string `json:"nativeCoverageName,omitempty"`
}
type publishCoverageRequest struct {
CoverageDescr *publishedCoverageDescr `json:"coverage,omitempty"`
}
// GetCoverages returns all published raster layers (coverages) for workspace as resources,
// err is an error if error occurred else err is nil
func (g *GeoServer) GetCoverages(workspaceName string) (coverages []*Resource, err error) {
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "coverages")
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
err = g.GetError(responseCode, response)
return
}
var coveragesResponse struct {
Coverages struct {
Coverage []*Resource `json:"coverage,omitempty"`
} `json:"coverages,omitempty"`
}
var coveragesEmptyResponse struct {
Coverages string
}
if err = json.Unmarshal(response, &coveragesResponse); err != nil {
if err = g.DeSerializeJSON(response, &coveragesEmptyResponse); err != nil {
return nil, fmt.Errorf("can't parse the coverage data, %v", err)
} else {
return []*Resource{}, nil
}
}
return coveragesResponse.Coverages.Coverage, nil
}
// GetStoreCoverages returns a list for all coverages (raster layers) names including unpublished for coverageStore,
// err is an error if error occurred else err is nil
func (g *GeoServer) GetStoreCoverages(workspaceName string, coverageStore string) (coverages []string, err error) {
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "coveragestores", coverageStore, "coverages")
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: map[string]string{"list": "all"},
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
err = g.GetError(responseCode, response)
return
}
var coveragesResponse struct {
List struct {
CoverageName []string `json:"string,omitempty"`
} `json:"list,omitempty"`
}
if err = g.DeSerializeJSON(response, &coveragesResponse); err != nil {
return nil, fmt.Errorf("can't parse the coverages data, %v", err)
}
return coveragesResponse.List.CoverageName, nil
}
// GetCoverage returns the coverage with name coverageName
// err is an error if error occurred else err is nil
func (g *GeoServer) GetCoverage(workspaceName string, coverageName string) (coverage *Coverage, err error) {
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "coverages", coverageName)
httpRequest := HTTPRequest{
Method: getMethod,
Accept: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
err = g.GetError(responseCode, response)
return
}
var coverageResponse struct {
Coverage Coverage
}
if err = g.DeSerializeJSON(response, &coverageResponse); err != nil {
return nil, fmt.Errorf("can't parse the coverage data, %v", err)
}
return &coverageResponse.Coverage, nil
}
// DeleteCoverage removes the coverage,
// err is an error if error occurred else err is nil
func (g *GeoServer) DeleteCoverage(workspaceName string, layerName string, recurse bool) (deleted bool, err error) {
//it's just a wrapper about DeleteLayer function as it does the same in the most use cases
return g.DeleteLayer(workspaceName, layerName, recurse)
}
//UpdateCoverage updates geoserver coverage (raster layer), else returns error,
func (g *GeoServer) UpdateCoverage(workspaceName string, coverage *Coverage) (modified bool, err error) {
items := strings.Split(coverage.Store.Name, ":")
if len(items) != 2 {
return false, errors.New("internal error during coverage update, can't build store name")
}
targetURL := g.ParseURL("rest", "workspaces", workspaceName, "coveragestores", items[1], "coverages", coverage.Name)
type coverageUpdateRequestBody struct {
Coverage Coverage `json:"coverage,omitempty"`
}
data := coverageUpdateRequestBody{Coverage: *coverage}
serializedLayer, _ := g.SerializeStruct(data)
httpRequest := HTTPRequest{
Method: putMethod,
Accept: jsonType,
Data: bytes.NewBuffer(serializedLayer),
DataType: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusOk {
g.logger.Error(string(response))
err = g.GetError(responseCode, response)
return
}
modified = true
return
}
// PublishCoverage publishes coverage from coverageStore
// coverageName - the name of the layer in the coverageStore (use GetStoreCoverages to get them), publishName - the name it was presented at geoserver
func (g *GeoServer) PublishCoverage(workspaceName string, coverageStoreName string, coverageName string, publishName string) (published bool, err error) {
if publishName == "" {
publishName = coverageName
}
publishRequest := publishCoverageRequest{
&publishedCoverageDescr{
Name: publishName,
NativeCoverageName: coverageName,
},
}
return g.publishCoverage(workspaceName, coverageStoreName, publishRequest)
}
// publishCoverage publishes coverage
func (g *GeoServer) publishCoverage(workspaceName string, coverageStoreName string, publishCoverageRequest publishCoverageRequest) (published bool, err error) {
if workspaceName != "" {
workspaceName = fmt.Sprintf("workspaces/%s/", workspaceName)
}
targetURL := g.ParseURL("rest", workspaceName, "coveragestores", coverageStoreName, "/coverages")
serializedLayer, _ := g.SerializeStruct(publishCoverageRequest)
httpRequest := HTTPRequest{
Method: postMethod,
Accept: jsonType,
Data: bytes.NewBuffer(serializedLayer),
DataType: jsonType,
URL: targetURL,
Query: nil,
}
response, responseCode := g.DoRequest(httpRequest)
if responseCode != statusCreated {
g.logger.Error(string(response))
err = g.GetError(responseCode, response)
return
}
return true, nil
}
//PublishGeoTiffLayer publishes geotiff to geoserver
func (g *GeoServer) PublishGeoTiffLayer(workspaceName string, coverageStoreName string, publishName string, fileName string) (published bool, err error) {
//it was moved from layers.go because this is the better place for raster layers functions (coverages)
//I tried to maintain the original behavior for backward compatibilities,
//but it didn't seem to be working as expected from scratch
//there were no tests for this function and I couldn't reproduce the working case
publishRequest := publishCoverageRequest{
&publishedCoverageDescr{
Name: publishName,
NativeCoverageName: fileName,
},
}
return g.publishCoverage(workspaceName, coverageStoreName, publishRequest)
}