-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstac.js
More file actions
334 lines (282 loc) · 7.95 KB
/
Copy pathstac.js
File metadata and controls
334 lines (282 loc) · 7.95 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* Copyright 2018 GravityLab LLC, David Hemphill */
function Node(url, rootNode, parentNode, entry) {
this.url = url; //URL associated with this node. this is essentially the unique id.
this.rootNode = rootNode;
this.parentNode = parentNode;
this.entry = entry;
this.children = [];
this.getEntry = function(){
return this.entry;
}
this.computeUrlBase = function(){
var r = /[^\/]*$/;
return this.url.replace(r, '');
}
this.computeAbsoluteUrl = function(href){
if (href.indexOf('//') >= 0) {
return href; //is already absolute
} else {
return this.computeUrlBase() + href; //generate absolute path
}
}
this.fetchChildNode = function(link) {
var dataObj = null;
if ('child' == link.rel) {
dataObj = new Catalog();
} else if ('item' == link.rel) {
dataObj = new Item();
} else { /* TODO, how to handle 'alternate' or other rel extensions ? */
dataObj = new Catalog();
}
var fullUrl = this.computeAbsoluteUrl(link.href);
var rtnstatus;
var rtnexception;
$.ajax({
url: fullUrl,
type: 'GET',
crossDomain: true,
async: false,
dataType: 'text',
success: function(data){
var obj = JSON.parse(data);
dataObj.fromJson(obj);
},
error: function(xhr, status, exception){
rtnstatus = status;
rtnexception = exception;
return false;
}
});
if (null != rtnstatus || null != rtnexception){
throw "Unable to read node: " + status + ' ' + exception;
}
var node = new Node(fullUrl, this.rootNode, this, dataObj);
node.entry = dataObj;
this.children.push(node);
return node;
}
}
function Index() {
this.rootNode = null;
this.getRootNode = function(){
return this.rootNode;
}
this.initialize = function(rootCatalogUrl){
var idx = this;
var rcurl = rootCatalogUrl;
var rootNode = null;
var rtnstatus;
var rtnexception;
$.ajax({
url: rcurl,
type: 'GET',
crossDomain: true,
async: false,
dataType: 'text', /* had to set so response is not parsed and causing errors */
success: function(data){
var obj = JSON.parse(data);
var rc = new RootCatalog();
rc.fromJson(obj);
rootNode = new Node(rcurl, null, null, rc);
},
error: function(xhr, status, exception){
rtnstatus = status;
rtnexception = exception;
return false;
}
});
if (null != rtnstatus || null != rtnexception){
throw "Unable to read catalog: " + rtnstatus + ' ' + rtnexception;
}
this.rootNode = rootNode;
}
};
/*************** Model Objects ***************************/
/** Root catalog, contains contact info, licensing, etc. */
function RootCatalog(){
this.name = '';
this.description = '';
this.links = [];
this.license = {
'name' : '',
'short_name' : '',
'link' : '',
'copyright' : ''
};
this.contact = {
'name' : '',
'organization' : '',
'email' : '',
'url' : ''
};
this.keywords = '';
this.homepage = '';
this.provider = {
'scheme': '',
'region': '',
'requesterPays': ''
};
this.findLink = function(href){
var rtn = null;
$.each(this.links, function(idx, value){
if (href == value.href){
rtn = value;
return false;
}
});
return rtn;
}
this.fromJson = function(json){
if ("name" in json){
this.name = json.name;
}
if ("description" in json){
this.description = json.description;
}
if ("links" in json){
var arr = this.links;
$.each(json.links, function(idx, value){
var link = new Link();
link.fromJson(value);
arr.push(link);
});
}
if ("license" in json) {
this.license = json.license;
}
if ("contact" in json) {
this.contact = json.contact;
}
if ("keywords" in json) {
this.keywords = json.keywords;
}
if ("homepage" in json) {
this.homepage = json.homepage;
}
if ("provider" in json) {
this.provider = json.provider;
}
};
this.isValid = function(){
return (this.name != '' && this.description != '' && this.links.length > 0);
}
};
/** Link Catalog */
function Catalog(){
this.name = '';
this.description = '';
this.links = [];
this.fromJson = function(json){
if ("name" in json){
this.name = json.name;
}
if ("description" in json){
this.description = json.description;
}
if ("links" in json){
this.links = json.links;
}
}
this.findLink = function(href){
var rtn = null;
$.each(this.links, function(idx, value){
if (href == value.href){
rtn = value;
return false;
}
});
return rtn;
}
this.isValid = function(){
return (this.name != '' && this.description != '' && this.links.length > 0);
}
};
function Link(){
this.rel = null;
this.href = null;
this.fromJson = function(json){
if ("rel" in json){
this.rel = json.rel;
}
if ("href" in json){
this.href = json.href;
}
}
this.isValid = function(){
return (this.rel != '' && this.href != '');
}
};
function Asset(){
this.type;
this.name;
this.href;
this.fromJson = function(json){
if ("type" in json){
this.type = json.type;
}
if ("name" in json){
this.name = json.name;
}
if ("href" in json){
this.href = json.href;
}
}
this.isValid = function(){
return (this.type != '' && this.name != '' && this.href != '');
}
};
function Geometry() {
this.type = [];
this.coordinates = [];
this.fromJson = function(json){
if ("type" in json){
this.type = json.type;
}
if ("coordinates" in json){
this.coordinates = json.coordinates;
}
}
};
function Item(){
this.type = null;
this.id = null;
this.bbox = null;
this.geometry = null;
this.properties = [];
this.links = [];
this.assets = [];
this.fromJson = function(json){
if ("type" in json){
this.type = json.type;
}
if ("id" in json){
this.id = json.id;
}
if ("bbox" in json){
this.bbox = json.bbox;
}
if ("geometry" in json){
this.geometry = new Geometry();
this.geometry.fromJson(json.geometry);
}
if ("properties" in json){
this.properties = json.properties;
}
if ("links" in json){
var arr = this.links;
$.each(json.links, function(idx, value){
var link = new Link();
link.fromJson(value);
arr.push(link);
});
}
if ("assets" in json){
var arr = this.assets;
$.each(json.assets, function(idx, value){
var asset = new Asset();
asset.fromJson(value);
arr.push(asset);
});
}
}
};