-
Notifications
You must be signed in to change notification settings - Fork 0
/
iNat2STAplus.htm
1619 lines (1462 loc) · 70 KB
/
iNat2STAplus.htm
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<html>
<head>
<script>
"use strict"
function create_UUID(guions){
// Codi inspirat en https://www.w3resource.com/javascript-exercises/javascript-math-exercise-23.php
var dt = new Date().getTime();
var patro= guions ? 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' : 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx';
var uuid = patro.replace(/[xy]/g, function(c) {
var r = (dt + Math.random()*16)%16 | 0;
dt = Math.floor(dt/16);
return (c=='x' ? r :(r&0x3|0x8)).toString(16);
});
return uuid;
}
// Funció inspirada en una de SitePoint Pty. Ltd, www.sitepoint.com
function Ajax()
{
this.req = null;
this.url = null;
this.status = null;
this.statusText = '';
this.method = 'GET';
this.async = true;
this.dataPayload = "";
this.readyState = null;
this.responseText = null;
this.responseXML = null;
this.handleResp = null;
this.responseFormat = 'text/plain', // 'text/plain', 'text/xml', 'object'
this.requestFormat = 'application/x-www-form-urlencoded' //només per POST
this.structResp=null;
this.mimeType = null;
//this.headers = [];
this.requestHeaders=[];
this.accessToken = null;
this.accessTokenType =null;
this.init = function() {
var i = 0;
var reqTry = [
function() { return new XMLHttpRequest(); },
function() { return new ActiveXObject('Msxml2.XMLHTTP') },
function() { return new ActiveXObject('Microsoft.XMLHTTP' )} ];
while (!this.req && (i < reqTry.length)) {
try {
this.req = reqTry[i++]();
}
catch(e) {}
}
return true;
};
this.doGet = function(url, hand, response_format, struct)
{
//alert(url); //·$·Per a depurar
this.url = url;
this.handleResp = hand;
this.responseFormat = response_format || 'text/plain';
this.structResp = struct;
this.method = 'GET';
this.doReq();
};
this.doPost = function(url, request_format, dataPayload, hand, response_format, struct)
{
this.url = url;
this.requestFormat = request_format || 'application/x-www-form-urlencoded';
this.dataPayload = dataPayload;
this.handleResp = hand;
this.responseFormat = response_format || 'text/plain';
this.structResp = struct;
this.method = 'POST';
this.doReq();
};
this.doPut = function(url, request_format, dataPayload, hand, response_format, struct)
{
this.url = url;
this.requestFormat = request_format || 'application/x-www-form-urlencoded';
this.dataPayload = dataPayload;
this.handleResp = hand;
this.responseFormat = response_format || 'text/plain';
this.structResp = struct;
this.method = 'PUT';
this.doReq();
};
this.doDelete = function(url, hand, response_format, struct)
{
//alert(url);
this.url = url;
this.handleResp = hand;
this.responseFormat = response_format || 'text/plain';
this.structResp = struct;
this.method = 'DELETE';
this.doReq();
};
this.doReqIndirect = function(method, url, request_format, dataPayload, hand, response_format, struct)
{
if (method=="POST")
this.doPost(url, request_format, dataPayload, hand, response_format, struct);
else if (method=="PUT")
this.doPut(url, request_format, dataPayload, hand, response_format, struct);
else if (method=="DELETE")
this.doDelete(url, hand, response_format, struct);
else
this.doGet(url, hand, response_format, struct);
};
this.doReq = function()
{
var self = null;
var req = null;
var headArr = [];
if (!this.init()) {
alert('Could not create XMLHttpRequest object.');
return;
}
req = this.req;
req.open(this.method, this.url, this.async);
if (this.accessTokenType && this.accessTokenType.length)
this.setRequestHeader("nb-access-token-type", this.accessTokenType);
if (this.accessToken && this.accessToken.length)
this.setRequestHeader("Authorization", "Bearer " + this.accessToken);
if ((this.method == 'POST' || this.method == 'PUT') && this.requestFormat)
req.setRequestHeader('Content-Type', this.requestFormat);
if ((this.method == 'POST' || this.method == 'PUT') && this.responseFormat)
req.setRequestHeader('Accept', this.responseFormat);
//req.setRequestHeader('Access-Control-Expose-Headers', '*'); Aquesta línia no hi hauria de ser.
for (var i=0; i<this.requestHeaders.length; i++)
req.setRequestHeader(this.requestHeaders[i].name, this.requestHeaders[i].value);
self = this;
req.onreadystatechange = function() {
var resp = null;
self.readyState = req.readyState;
if (req.readyState == 2) // this.HEADERS_RECEIVED
{
// Get the raw header string
var headers = req.getAllResponseHeaders();
// Convert the header string into an array
// of individual headers
var arr = headers.trim().split(/[\r\n]+/);
// Create a map of header names to values
req.responseHeaders = {};
arr.forEach(function (line) {
var parts = line.split(': ');
var header = parts.shift();
var value = parts.join(': ');
req.responseHeaders[header] = value;
});
}
else if (req.readyState == 4)
{
self.status = req.status;
self.statusText = req.statusText;
self.responseText = req.responseText;
self.responseXML = req.responseXML;
switch(self.responseFormat) {
default:
case 'text/plain':
resp = self.responseText;
break;
case 'text/xml':
resp = self.responseXML;
break;
case 'application/json':
if (self.responseText=="")
{
resp = "";
break;
}
if (self.responseText)
{
try {
resp = JSON.parse(self.responseText);
}
catch (e) {
self.handleErr("JSON file error: " + self.responseText, self.structResp);
return;
}
}
break;
case 'object':
resp = req;
break;
}
if(self.structResp)
self.structResp.text=self.responseText;
if (self.status > 199 && self.status < 300) {
if (!self.handleResp) {
alert('No response handler defined for this XMLHttpRequest object.');
return;
}
if(self.structResp)
self.handleResp(resp, self.structResp);
else
self.handleResp(resp);
} else {
self.handleErr(resp, self.structResp);
}
}
}
req.send(this.dataPayload);
};
this.abort = function() {
if (this.req) {
this.req.onreadystatechange = function() { };
this.req.abort();
this.req = null;
}
};
this.handleErr = function() {
var errorWin;
// Create new window and display error
try {
errorWin = window.open('', 'errorWin');
errorWin.document.body.innerHTML = this.responseText;
}
// If pop-up gets blocked, inform user
catch(e) {
alert('An error occurred, but the error message cannot be displayed because of your browser\'s pop-up blocker.\n' +
'You could try to allow pop-ups from this Web site. Meanwhile, the text version of the error is:\n' + this.responseText);
}
};
this.setMimeType = function(mimeType) {
this.mimeType = mimeType;
};
this.setHandlerResp = function(funcRef) {
this.handleResp = funcRef;
};
this.setHandlerErr = function(funcRef) {
this.handleErr = funcRef;
};
this.setHandlerBoth = function(funcRef) {
this.handleResp = funcRef;
this.handleErr = funcRef;
};
this.setRequestHeader = function(headerName, headerValue) {
this.requestHeaders.push({"name": headerName, "value": headerValue});
//this.headers.push(headerName + ': ' + headerValue);
};
this.getResponseHeader = function(headerName) {
return this.req.responseHeaders[headerName.toLowerCase()];
};
this.setAccessToken = function(accessToken, accessTokenType) {
this.accessToken=accessToken;
this.accessTokenType=accessTokenType;
};
}
function UpdateProgressArea(obj,text)
{
obj.progressArea.value+=text;
obj.progressArea.scrollTop = obj.progressArea.scrollHeight;
}
function ErrorResposeAjax(resp, obj)
{
if (typeof resp === 'object')
UpdateProgressArea(obj,"Error: \n" + JSON.stringify(resp));
else if (!resp)
UpdateProgressArea(obj,"Network error: Service unreachable");
else
UpdateProgressArea(obj,"Error: \n" + resp);
}
function ExtractIdFromURL(url)
{
var id;
if (-1!=url.indexOf("('") && -1!=url.indexOf("')", url.indexOf("('")+2))
{
id=url.substring(url.indexOf("('")+2,url.indexOf("')",url.indexOf("('")+2));
if (id==+id) //Is it a numerical id? /inspired in https://stackoverflow.com/questions/20169217/how-to-write-isnumber-in-javascript
return +id; //returns a number
return id; //returns a string
}
if (-1!=url.indexOf("(") && -1!=url.indexOf(")", url.indexOf("(")+1))
{
id=url.substring(url.indexOf("(")+1,url.indexOf(")",url.indexOf("(")+1));
return +id; //returns a number
}
else
return url;
}
function ExtractNatEndPoint(url)
{
var start=url.indexOf("://");
if (-1==start)
start=0;
else
start+=3;
if (-1==url.substring(start).indexOf("/"))
return url;
return url.substring(0, url.substring(start).indexOf("/")+start);
}
function SayDone(obj)
{
obj.iFunction++;
UpdateProgressArea(obj,"\nDone!\n");
obj.linkResultDiv.innerHTML+="<a target=\"_blank\" href=\"" + obj.sta + "/Groups?$filter=name eq '" + obj.natObs.uri + "'&$select=name,description,creationTime,Observations,@iot.id&$expand=Observations($select=resultTime,result,phenomenonTime,parameters,FeatureOfInterest,Datastream;$expand=Datastream($select=Party,Sensor,Thing,License,ObservedProperty;$expand=Party($select=displayName),Sensor($select=name),Thing($select=name),License($select=name),ObservedProperty($select=name)),FeatureOfInterest($select=feature/geometry/coordinates))\">Get the STA+ representation of the iNaturalist record " + obj.natObsUrl + " in JSON</a><br>";
if (obj.iFunction<obj.functions.length)
obj.functions[obj.iFunction](obj);
}
///////////////Parties
///////////////StartingParty
function CreateStartingPartyIfNeeded(obj)
{
obj.iFunction++;
obj.iIdentification=-1;
UpdateProgressArea(obj,"\niNaturalist user name is '"+obj.natObs.user_login+"'. Checking STAplus Parties...\n");
var ajax=new Ajax();
ajax.setHandlerErr(ErrorResposeAjax);
//ajax.doGet(obj.sta+"/Parties?$select=name,role,id&$filter=name eq '"+obj.natObs.user_login+"' and role eq 'individual'", InterpretParty, "application/json", obj);
ajax.doGet(obj.sta+"/Parties?$filter=displayName eq '"+obj.natObs.user_login+"' and role eq 'individual'", InterpretParty, "application/json", obj);
}
function InterpretParty(resources, obj)
{
if (resources && resources.value && resources.value.length)
{
var id=resources.value[0]["@iot.id"];
UpdateProgressArea(obj,"Party \"" + (obj.iIdentification==-1 ? obj.natObs.user_login : obj.natObs.identifications[obj.iIdentification].user.login) + "\" already existed under id=\""+id+"\"\n");
StorePartyId(id, obj);
}
else
CreateParty(obj);
}
function CreateParty(obj)
{
var party={
"displayName": (obj.iIdentification==-1 ? obj.natObs.user_login : obj.natObs.identifications[obj.iIdentification].user.login),
"role": "individual", //"institution"
"@iot.id": create_UUID(true) //Only specify if you want to have control on the @iot.id value.
}
UpdateProgressArea(obj,"Creating the Party \"" + (obj.iIdentification==-1 ? obj.natObs.user_login : obj.natObs.identifications[obj.iIdentification].user.login) + "\"...\n");
obj.ajax=new Ajax();
obj.ajax.setAccessToken(obj.accessToken, null);
obj.ajax.setHandlerErr(ErrorResposeAjax);
obj.ajax.doPost(obj.sta+"/Parties", "application/json", JSON.stringify(party), EvaluateResponseCreateParty, "application/json", obj);
}
function EvaluateResponseCreateParty(data, obj)
{
var id=ExtractIdFromURL(obj.ajax.getResponseHeader("Location"));
UpdateProgressArea(obj,"Party \"" + (obj.iIdentification==-1 ? obj.natObs.user_login : obj.natObs.identifications[obj.iIdentification].user.login) + "\" CREATED under id=\""+id+"\"\n");
StorePartyId(id, obj);
}
function StorePartyId(id, obj)
{
if (obj.iIdentification==-1)
{
obj.startingPartyId=id;
if (obj.iFunction<obj.functions.length)
obj.functions[obj.iFunction](obj);
}
else
{
obj.partyId[obj.iIdentification]=id;
obj.iIdentification++;
CreateOnePartyIfNeeded(obj);
}
}
///////////////Parties
function CreatePartiesIfNeeded(obj)
{
obj.iFunction++;
obj.iIdentification=0;
obj.partyId=[];
CreateOnePartyIfNeeded(obj);
}
function CreateOnePartyIfNeeded(obj)
{
if (!obj.natObs.identifications || obj.iIdentification==obj.natObs.identifications.length)
{
//No hi ha observacions o s'ha acabat. Faig la següent cosa
if (obj.iFunction<obj.functions.length)
obj.functions[obj.iFunction](obj);
return;
}
UpdateProgressArea(obj,"\niNaturalist user name is '"+obj.natObs.identifications[obj.iIdentification].user.login+"'. Checking STAplus Parties...\n");
var ajax=new Ajax();
ajax.setHandlerErr(ErrorResposeAjax);
//ajax.doGet(obj.sta+"/Parties?$select=name,role,id&$filter=name eq '"+obj.natObs.identifications[obj.iIdentification].user.login+"' and role eq 'individual'", InterpretParty, "application/json", obj);
ajax.doGet(obj.sta+"/Parties?$filter=displayName eq '"+obj.natObs.identifications[obj.iIdentification].user.login+"' and role eq 'individual'", InterpretParty, "application/json", obj);
}
///////////////Project
function CreateProjectIfNeeded(obj)
{
obj.iFunction++;
if (obj.natObs.project_observations && obj.natObs.project_observations.length && obj.natObs.project_observations[0].project && obj.natObs.project_observations[0].project.id && obj.natObs.project_observations[0].project.title)
{
var name=obj.natObs.project_observations[0].project.title;
UpdateProgressArea(obj,"\niNaturalist project title is '"+name+"'. Checking STAplus Projects...\n");
var ajax=new Ajax();
ajax.setHandlerErr(ErrorResposeAjax);
//ajax.doGet(obj.sta+"/Projects?$select=name,id&$filter=name eq '"+encodeURIComponent(name)+"'", InterpretProject, "application/json", obj);
ajax.doGet(obj.sta+"/Projects?$filter=name eq '"+encodeURIComponent(name)+"'", InterpretProject, "application/json", obj);
}
else
StoreProjectId(null, obj);
}
function InterpretProject(resources, obj)
{
if (resources && resources.value && resources.value.length)
{
var id=resources.value[0]["@iot.id"];
UpdateProgressArea(obj,"Project \"" + obj.natObs.project_observations[0].project.title + "\" already existed under id=\""+id+"\"\n");
StoreProjectId(id, obj);
}
else
GetCreationDateProject(obj);
}
function GetCreationDateProject(obj)
{
var ajax=new Ajax();
ajax.setHandlerErr(ErrorResposeAjax);
ajax.doGet(obj.nat+"/projects/"+obj.natObs.project_observations[0].project.id + ".json", InterpretNatProject, "application/json", obj);
}
function InterpretNatProject(projects, obj)
{
var d;
if (projects && projects.created_at)
{
d = new Date(projects.created_at);
CreateProject(obj, d.toISOString(), projects.terms ? projects.terms : "");
}
else
{
d = new Date();
CreateProject(obj, d.toISOString(), "");
}
}
function CreateProject(obj, created_at, terms)
{
var project={
"name": obj.natObs.project_observations[0].project.title,
"description": "This is the project " + obj.natObs.project_observations[0].project.id + " in iNaturalist",
"url" : obj.nat+"/projects/" + obj.natObs.project_observations[0].id,
"termsOfUse": terms,
"creationTime" : created_at
}
UpdateProgressArea(obj,"Creating the Project \"" + obj.natObs.project_observations[0].project.title + "\"...\n");
obj.ajax=new Ajax();
obj.ajax.setAccessToken(obj.accessToken, null);
obj.ajax.setHandlerErr(ErrorResposeAjax);
obj.ajax.doPost(obj.sta+"/Projects", "application/json", JSON.stringify(project), EvaluateResponseCreateProject, "application/json", obj);
}
function EvaluateResponseCreateProject(data, obj)
{
var id=ExtractIdFromURL(obj.ajax.getResponseHeader("Location"));
UpdateProgressArea(obj,"Project \"" + obj.natObs.project_observations[0].project.title + "\" CREATED under id=\""+id+"\"\n");
StoreProjectId(id, obj);
}
function StoreProjectId(id, obj)
{
if (id)
obj.projectId=id;
if (obj.iFunction<obj.functions.length)
obj.functions[obj.iFunction](obj);
}
///////////////Human Sensor
function CreateHumanSensorIfNeeded(obj)
{
obj.iFunction++;
UpdateProgressArea(obj,"\nChecking STAplus Sensors...\n");
var ajax=new Ajax();
ajax.setHandlerErr(ErrorResposeAjax);
//ajax.doGet(obj.sta+"/Sensors?$select=name,id&$filter=name eq 'Human Eye'", InterpretHumanSensor, "application/json", obj);
ajax.doGet(obj.sta+"/Sensors?$filter=name eq 'Human Eye'", InterpretHumanSensor, "application/json", obj);
}
function InterpretHumanSensor(resources, obj)
{
if (resources && resources.value && resources.value.length)
{
var id=resources.value[0]["@iot.id"];
UpdateProgressArea(obj,"Sensor \"Human Eye\" already existed under id=\""+id+"\"\n");
StoreHumanSensorId(id, obj);
}
else
CreateHumanSensor(obj);
}
function CreateHumanSensor(obj)
{
var sensor={
"name": "Human Eye",
"description": "Eye of the observer",
"encodingType": "text/html",
"metadata": "https://en.wikipedia.org/wiki/Human_eye"
}
UpdateProgressArea(obj,"Creating the Sensor \"Human Eye\"...\n");
obj.ajax=new Ajax();
obj.ajax.setAccessToken(obj.accessToken, null);
obj.ajax.setHandlerErr(ErrorResposeAjax);
obj.ajax.doPost(obj.sta+"/Sensors", "application/json", JSON.stringify(sensor), EvaluateResponseCreateHumanSensor, "application/json", obj);
}
function EvaluateResponseCreateHumanSensor(data, obj)
{
var id=ExtractIdFromURL(obj.ajax.getResponseHeader("Location"));
UpdateProgressArea(obj,"Sensor \"Human Eye\" CREATED under id=\""+id+"\"\n");
StoreHumanSensorId(id, obj);
}
function StoreHumanSensorId(id, obj)
{
obj.humanSensorId=id;
if (obj.iFunction<obj.functions.length)
obj.functions[obj.iFunction](obj);
}
///////////////Camera Sensor
function CreateCameraSensorIfNeeded(obj)
{
obj.iFunction++;
UpdateProgressArea(obj,"\nChecking STAplus Sensors...\n");
var ajax=new Ajax();
ajax.setHandlerErr(ErrorResposeAjax);
//ajax.doGet(obj.sta+"/Sensors?$select=name,id&$filter=name eq 'Generic camera'", InterpretCameraSensor, "application/json", obj);
ajax.doGet(obj.sta+"/Sensors?$filter=name eq 'Generic camera'", InterpretCameraSensor, "application/json", obj);
}
function InterpretCameraSensor(resources, obj)
{
if (resources && resources.value && resources.value.length)
{
var id=resources.value[0]["@iot.id"];
UpdateProgressArea(obj, "Sensor \"Generic camera\" already existed under id=\""+id+"\"\n");
StoreCameraSensorId(id, obj);
}
else
CreateCameraSensor(obj);
}
function CreateCameraSensor(obj)
{
var sensor={
"name": "Generic camera",
"description": "A camera or a smartphone build-in camera",
"encodingType": "text/html",
"metadata": "https://en.wikipedia.org/wiki/Camera"
}
UpdateProgressArea(obj,"Creating the Sensor \"Generic camera\"...\n");
obj.ajax=new Ajax();
obj.ajax.setAccessToken(obj.accessToken, null);
obj.ajax.setHandlerErr(ErrorResposeAjax);
obj.ajax.doPost(obj.sta+"/Sensors", "application/json", JSON.stringify(sensor), EvaluateResponseCreateCameraSensor, "application/json", obj);
}
function EvaluateResponseCreateCameraSensor(data, obj)
{
var id=ExtractIdFromURL(obj.ajax.getResponseHeader("Location"));
UpdateProgressArea(obj,"Sensor \"Generic camera\" CREATED under id=\""+id+"\"\n");
StoreCameraSensorId(id, obj);
}
function StoreCameraSensorId(id, obj)
{
obj.cameraSensorId=id;
if (obj.iFunction<obj.functions.length)
obj.functions[obj.iFunction](obj);
}
///////////////ObservedProperties
///////////////Taxon ObservedProperty
function CreateTaxonObservedPropertyIfNeeded(obj)
{
obj.iFunction++;
UpdateProgressArea(obj,"\nChecking STAplus ObservedProperties...\n");
var ajax=new Ajax();
ajax.setHandlerErr(ErrorResposeAjax);
//ajax.doGet(obj.sta+"/ObservedProperties?$select=name,definition,id&$filter=name eq 'Taxon' and definition eq 'https://www.gbif.org/dataset/d7dddbf4-2cf0-4f39-9b2a-bb099caae36c'", InterpretTaxonObservedProperty, "application/json", obj);
ajax.doGet(obj.sta+"/ObservedProperties?$filter=name eq 'Taxon' and definition eq 'https://www.gbif.org/dataset/d7dddbf4-2cf0-4f39-9b2a-bb099caae36c'", InterpretTaxonObservedProperty, "application/json", obj);
}
function InterpretTaxonObservedProperty(resources, obj)
{
if (resources && resources.value && resources.value.length)
{
var id=resources.value[0]["@iot.id"];
UpdateProgressArea(obj,"ObserverProperty \"Taxon\" already existed under id=\""+id+"\"\n");
StoreTaxonObservedPropertyId(id, obj);
}
else
CreateTaxonObservedProperty(obj);
}
function CreateTaxonObservedProperty(obj)
{
var observedProperty={
"name": "Taxon",
"description": "GBIF Backbone Taxonomy",
"definition": "https://www.gbif.org/dataset/d7dddbf4-2cf0-4f39-9b2a-bb099caae36c"
}
UpdateProgressArea(obj,"Creating the ObservedProperty \"Taxon\"...\n");
obj.ajax=new Ajax();
obj.ajax.setAccessToken(obj.accessToken, null);
obj.ajax.setHandlerErr(ErrorResposeAjax);
obj.ajax.doPost(obj.sta+"/ObservedProperties", "application/json", JSON.stringify(observedProperty), EvaluateResponseCreateTaxonObservedProperty, "application/json", obj);
}
function EvaluateResponseCreateTaxonObservedProperty(data, obj)
{
var id=ExtractIdFromURL(obj.ajax.getResponseHeader("Location"));
UpdateProgressArea(obj,"Observed Property \"Taxon\" CREATED under id=\""+id+"\"\n");
StoreTaxonObservedPropertyId(id, obj);
}
function StoreTaxonObservedPropertyId(id, obj)
{
obj.taxonObservedPropertyId=id;
if (obj.iFunction<obj.functions.length)
obj.functions[obj.iFunction](obj);
}
///////////////Picture ObservedProperty
function CreatePictureObservedPropertyIfNeeded(obj)
{
obj.iFunction++;
UpdateProgressArea(obj,"\nChecking STAplus ObservedProperties...\n");
var ajax=new Ajax();
ajax.setHandlerErr(ErrorResposeAjax);
//ajax.doGet(obj.sta+"/ObservedProperties?$select=name,definition,id&$filter=name eq 'Species picture' and definition eq 'https://www.inaturalist.org/guides/2465'", InterpretPictureObservedProperty, "application/json", obj);
ajax.doGet(obj.sta+"/ObservedProperties?$filter=name eq 'Species picture' and definition eq 'https://www.inaturalist.org/guides/2465'", InterpretPictureObservedProperty, "application/json", obj);
}
function InterpretPictureObservedProperty(resources, obj)
{
if (resources && resources.value && resources.value.length)
{
var id=resources.value[0]["@iot.id"];
UpdateProgressArea(obj, "ObserverProperty \"Species picture\" already existed under id=\""+id+"\"\n");
StorePictureObservedPropertyId(id, obj);
}
else
CreatePictureObservedProperty(obj);
}
function CreatePictureObservedProperty(obj)
{
var observedProperty={
"name": "Species picture",
"description": "A picture for species identification",
"definition": "https://www.inaturalist.org/guides/2465"
}
UpdateProgressArea(obj,"Creating the ObservedProperty \"Species picture\"...\n");
obj.ajax=new Ajax();
obj.ajax.setAccessToken(obj.accessToken, null);
obj.ajax.setHandlerErr(ErrorResposeAjax);
obj.ajax.doPost(obj.sta+"/ObservedProperties", "application/json", JSON.stringify(observedProperty), EvaluateResponseCreatePictureObservedProperty, "application/json", obj);
}
function EvaluateResponseCreatePictureObservedProperty(data, obj)
{
var id=ExtractIdFromURL(obj.ajax.getResponseHeader("Location"));
UpdateProgressArea(obj,"Observed Property \"Species picture\" CREATED under id=\""+id+"\"\n");
StorePictureObservedPropertyId(id, obj);
}
function StorePictureObservedPropertyId(id, obj)
{
obj.pictureObservedPropertyId=id;
if (obj.iFunction<obj.functions.length)
obj.functions[obj.iFunction](obj);
}
///////////////License
function GetSTALicense(natLicense)
{
if (natLicense=="CC0")
{
return {"name":"CC0",
"description":"CC0 1.0 Universal (CC0 1.0) Public Domain Dedication",
"logo":"https://mirrors.creativecommons.org/presskit/buttons/88x31/png/cc-zero.png",
"definition":"https://creativecommons.org/publicdomain/zero/1.0/"};
}
if (natLicense=="CC-BY")
{
return {"name":"CC BY 3.0",
"description":"The Creative Commons Attribution license",
"logo":"https://mirrors.creativecommons.org/presskit/buttons/88x31/png/by.png",
"definition":"https://creativecommons.org/licenses/by/3.0/de/deed.en"};
}
if (natLicense=="CC-BY-NC")
{
return {"name":"CC BY-NC 3.0",
"description":"The Creative Commons Attribution-NonCommercial license",
"logo":"https://mirrors.creativecommons.org/presskit/buttons/88x31/png/by-nc.png",
"definition":"https://creativecommons.org/licenses/by-nc/3.0/de/deed.en"};
}
if (natLicense=="CC-BY-SA")
{
return {"name":"CC BY-SA 3.0",
"description":"The Creative Commons Attribution & Share-alike license",
"logo":"https://mirrors.creativecommons.org/presskit/buttons/88x31/png/by-sa.png",
"definition":"https://creativecommons.org/licenses/by-sa/3.0/de/deed.en"};
}
if (natLicense=="CC-BY-ND")
{
return {"name":"CC BY-ND 3.0",
"description":"The Creative Commons Attribution & No Derivatives license",
"logo":"https://mirrors.creativecommons.org/presskit/buttons/88x31/png/by-nd.png",
"definition":"https://creativecommons.org/licenses/by-nd/3.0/de/deed.en"};
}
if (natLicense=="CC-BY-NC-SA")
{
return {"name":"CC BY-NC-SA 3.0",
"description":"The Creative Commons Attribution & Share-alike non-commercial license",
"logo":"https://mirrors.creativecommons.org/presskit/buttons/88x31/png/by-nc-sa.png",
"definition":"https://creativecommons.org/licenses/by-nc-sa/3.0/de/deed.en"};
}
if (natLicense=="CC-BY-NC-ND")
{
return {"name":"CC BY-NC-ND 3.0",
"description":"The Creative Commons Attribution, non-commercial & No Derivatives license",
"logo":"https://mirrors.creativecommons.org/presskit/buttons/88x31/png/by-nc-nd.png",
"definition":"https://creativecommons.org/licenses/by-nc-nd/3.0/de/deed.en"};
}
if (!natLicense || natLicense=="")
return null;
return {"name": natLicense,
"description":""};
}
function CreateLicenseIfNeeded(obj)
{
obj.iFunction++;
UpdateProgressArea(obj,"\niNaturalist license is '"+obj.natObs.license+"'. Checking STAplus Licenses...\n");
var license=GetSTALicense(obj.natObs.license);
if (!license)
{
StoreLicenseId(null, obj);
return;
}
var ajax=new Ajax();
ajax.setHandlerErr(ErrorResposeAjax);
//ajax.doGet(obj.sta+"/Licenses?$select=name,id&$filter=name eq '"+license.name+"'", InterpretLicense, "application/json", obj);
ajax.doGet(obj.sta+"/Licenses?$filter=name eq '"+license.name+"'", InterpretLicense, "application/json", obj);
}
function InterpretLicense(resources, obj)
{
if (resources && resources.value && resources.value.length)
{
var id=resources.value[0]["@iot.id"];
UpdateProgressArea(obj,"License \"" + obj.natObs.license + "\" already existed under id=\""+id+"\"\n");
StoreLicenseId(id, obj);
}
else
CreateLicense(obj);
}
function CreateLicense(obj)
{
var license=GetSTALicense(obj.natObs.license);
UpdateProgressArea(obj,"Creating the License \"" + obj.natObs.license + "\"...\n");
obj.ajax=new Ajax();
obj.ajax.setAccessToken(obj.accessToken, null);
obj.ajax.setHandlerErr(ErrorResposeAjax);
obj.ajax.doPost(obj.sta+"/Licenses", "application/json", JSON.stringify(license), EvaluateResponseCreateLicense, "application/json", obj);
}
function EvaluateResponseCreateLicense(data, obj)
{
var id=ExtractIdFromURL(obj.ajax.getResponseHeader("Location"));
UpdateProgressArea(obj,"License \"" + obj.natObs.license + "\" CREATED under id=\""+id+"\"\n");
StoreLicenseId(id, obj);
}
function StoreLicenseId(id, obj)
{
obj.licenseId=id;
if (obj.iFunction<obj.functions.length)
obj.functions[obj.iFunction](obj);
}
///////////////Things
///////////////Starting Human Thing
function CreateStartingHumanThingIfNeeded(obj)
{
obj.iFunction++;
obj.iIdentification=-1;
UpdateProgressArea(obj,"\niNaturalist user name is '"+obj.natObs.user_login+"'. Checking STAplus Things...\n");
var ajax=new Ajax();
ajax.setHandlerErr(ErrorResposeAjax);
//ajax.doGet(obj.sta+"/Things?$select=name,id&$filter=name eq '"+obj.natObs.user_login+"'", InterpretHumanThing, "application/json", obj);
ajax.doGet(obj.sta+"/Things?$filter=name eq '"+obj.natObs.user_login+"'", InterpretHumanThing, "application/json", obj);
}
function InterpretHumanThing(resources, obj)
{
if (resources && resources.value && resources.value.length)
{
var id=resources.value[0]["@iot.id"];
UpdateProgressArea(obj,"Thing \"" + (obj.iIdentification==-1 ? obj.natObs.user_login : obj.natObs.identifications[obj.iIdentification].user.login) + "\" already existed under id=\""+id+"\"\n");
StoreHumanThingId(id, obj);
}
else
CreateHumanThing(obj);
}
function CreateHumanThing(obj)
{
var thing={
"name": (obj.iIdentification==-1 ? obj.natObs.user_login : obj.natObs.identifications[obj.iIdentification].user.login),
"description": "Human as a sensor",
"Party": { "@iot.id": obj.startingPartyId }
}
UpdateProgressArea(obj,"Creating the human Thing \"" + thing.name + "\"...\n");
obj.ajax=new Ajax();
obj.ajax.setAccessToken(obj.accessToken, null);
obj.ajax.setHandlerErr(ErrorResposeAjax);
obj.ajax.doPost(obj.sta+"/Things", "application/json", JSON.stringify(thing), EvaluateResponseCreateHumanThing, "application/json", obj);
}
function EvaluateResponseCreateHumanThing(data, obj)
{
var id=ExtractIdFromURL(obj.ajax.getResponseHeader("Location"));
UpdateProgressArea(obj,"Thing \"" + (obj.iIdentification==-1 ? obj.natObs.user_login : obj.natObs.identifications[obj.iIdentification].user.login) + "\" CREATED under id=\""+id+"\"\n");
StoreHumanThingId(id, obj);
}
function StoreHumanThingId(id, obj)
{
if (obj.iIdentification==-1)
{
obj.startingHumanThingId=id;
if (obj.iFunction<obj.functions.length)
obj.functions[obj.iFunction](obj);
}
else
{
obj.humanThingId[obj.iIdentification]=id;
obj.iIdentification++;
CreateOneHumanThingIfNeeded(obj);
}
}
///////////////Human Things
function CreateHumanThingsIfNeeded(obj)
{
obj.iFunction++;
obj.iIdentification=0;
obj.humanThingId=[];
CreateOneHumanThingIfNeeded(obj);
}
function CreateOneHumanThingIfNeeded(obj)
{
if (!obj.natObs.identifications || obj.iIdentification==obj.natObs.identifications.length)
{
//No hi ha observacions o s'ha acabat. Faig la següent cosa
if (obj.iFunction<obj.functions.length)
obj.functions[obj.iFunction](obj);
return;
}
UpdateProgressArea(obj,"\niNaturalist user name is '"+obj.natObs.identifications[obj.iIdentification].user.login+"'. Checking STAplus Things...\n");
var ajax=new Ajax();
ajax.setHandlerErr(ErrorResposeAjax);
//ajax.doGet(obj.sta+"/Things?$select=name,id&$filter=name eq '"+obj.natObs.identifications[obj.iIdentification].user.login+"'", InterpretHumanThing, "application/json", obj);
ajax.doGet(obj.sta+"/Things?$filter=name eq '"+obj.natObs.identifications[obj.iIdentification].user.login+"'", InterpretHumanThing, "application/json", obj);
}
///////////////Camera Thing
function CreateCameraThingIfNeeded(obj)
{
obj.iFunction++;
UpdateProgressArea(obj,"\niNaturalist user name is '"+obj.natObs.user_login+"'. Checking STAplus Camera Things...\n");
var ajax=new Ajax();
ajax.setHandlerErr(ErrorResposeAjax);
//ajax.doGet(obj.sta+"/Things?$select=name,id&$filter=name eq 'Camera of "+obj.natObs.user_login+"'", InterpretCameraThing, "application/json", obj);
ajax.doGet(obj.sta+"/Things?$filter=name eq 'Camera of "+obj.natObs.user_login+"'", InterpretCameraThing, "application/json", obj);
}
function InterpretCameraThing(resources, obj)
{
if (resources && resources.value && resources.value.length)
{
var id=resources.value[0]["@iot.id"];
UpdateProgressArea(obj,"Thing \"Camera of " + obj.natObs.user_login + "\" already existed under id=\""+id+"\"\n");
StoreCameraThingId(id, obj);
}
else
CreateCameraThing(obj);
}
function CreateCameraThing(obj)
{
var thing={
"name": "Camera of " + obj.natObs.user_login,
"description": "Camera of " + obj.natObs.user_login + "as a sensor to identify a species",
"Party": { "@iot.id": obj.startingPartyId }
}
UpdateProgressArea(obj, "Creating the Thing \"Camera of " + obj.natObs.user_login + "\"...\n");
obj.ajax=new Ajax();
obj.ajax.setAccessToken(obj.accessToken, null);
obj.ajax.setHandlerErr(ErrorResposeAjax);
obj.ajax.doPost(obj.sta+"/Things", "application/json", JSON.stringify(thing), EvaluateResponseCreateCameraThing, "application/json", obj);
}
function EvaluateResponseCreateCameraThing(data, obj)
{
var id=ExtractIdFromURL(obj.ajax.getResponseHeader("Location"));
UpdateProgressArea(obj,"Thing \"Camera of " + obj.natObs.user_login + "\" CREATED under id=\""+id+"\"\n");
StoreCameraThingId(id, obj);
}
function StoreCameraThingId(id, obj)
{
obj.cameraThingId=id;
if (obj.iFunction<obj.functions.length)
obj.functions[obj.iFunction](obj);
}
///////////////FeatureOfInterest
function CreateFeatureOfInterestIfNeeded(obj)
{
obj.iFunction++;
UpdateProgressArea(obj,"\niNaturalist position is longitude: "+obj.natObs.longitude+ " latitude: "+obj.natObs.latitude+". Checking STAplus FeaturesOfInterest...\n");
var ajax=new Ajax();
ajax.setHandlerErr(ErrorResposeAjax);
//ajax.doGet(obj.sta+"/FeaturesOfInterest?$select=id&$filter=st_equals(feature, geography'POINT (" + obj.natObs.longitude + " " + obj.natObs.latitude + ")')", InterpretFeatureOfInterest, "application/json", obj);
ajax.doGet(obj.sta+"/FeaturesOfInterest?$filter=st_equals(feature, geography'POINT (" + obj.natObs.longitude + " " + obj.natObs.latitude + ")')", InterpretFeatureOfInterest, "application/json", obj);
}
function InterpretFeatureOfInterest(resources, obj)
{
if (resources && resources.value && resources.value.length)
{
var id=resources.value[0]["@iot.id"];
UpdateProgressArea(obj,"FeatureOfInterest longitude: "+obj.natObs.longitude+ " latitude: "+obj.natObs.latitude+" already existed under id=\""+id+"\"\n");
StoreFeatureOfInterestId(id, obj);
}
else
CreateFeatureOfInterest(obj);
}
function CreateFeatureOfInterest(obj)
{
var featureOfInterest={
"name": obj.natObs.place_guess,
"description": obj.natObs.place_guess + (obj.natObs.positional_accuracy ? " positional accuracy: " + obj.natObs.positional_accuracy : "") + (obj.natObs.positioning_device ? " positioning device: " + obj.natObs.positioning_device : "") + (obj.natObs.positioning_method ? " positioning method: " + obj.natObs.positioning_method : "") + (obj.natObs.coordinates_obscured ? " coordinates obscured" : "") + ".",
"encodingType": "application/geo+json",
"feature": {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [obj.natObs.longitude, obj.natObs.latitude]
}
}
};
UpdateProgressArea(obj,"Creating the FeatureOfInterest longitude: "+obj.natObs.longitude+ " latitude: "+obj.natObs.latitude+"...\n");