forked from kaktus40/Cesium-GeoserverTerrainProvider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeoserverTerrainProvider.js
1097 lines (1036 loc) · 41.3 KB
/
GeoserverTerrainProvider.js
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
(function() {
var OGCHelper = {};
/**
* static array where CRS availables for OGCHelper are defined
*/
OGCHelper.CRS = [ {
name : "CRS:84",
ellipsoid : Cesium.Ellipsoid.WGS84,
firstAxeIsLatitude : false,
tilingScheme : Cesium.GeographicTilingScheme,
supportedCRS:"urn:ogc:def:crs:OGC:2:84"
}, {
name : "EPSG:4326",
ellipsoid : Cesium.Ellipsoid.WGS84,
firstAxeIsLatitude : true,
tilingScheme : Cesium.GeographicTilingScheme,
SupportedCRS:"urn:ogc:def:crs:EPSG::4326"
}, {
name : "EPSG:3857",
ellipsoid : Cesium.Ellipsoid.WGS84,
firstAxeIsLatitude : false,
tilingScheme : Cesium.WebMercatorTilingScheme,
SupportedCRS: "urn:ogc:def:crs:EPSG::3857"
}, {
name : "OSGEO:41001",
ellipsoid : Cesium.Ellipsoid.WGS84,
firstAxeIsLatitude : false,
tilingScheme : Cesium.WebMercatorTilingScheme,
SupportedCRS: "urn:ogc:def:crs:EPSG::3857"
} ];
/**
* static array where image formats available for OGCHelper are
* defined
*/
OGCHelper.FormatImage = [ {
format : "image/png",
extension: "png"
}, {
format : "image/jpeg",
extension: "jpg"
}, {
format : "image/jpeg",
extension: "jpeg"
}, {
format : "image/gif",
extension: "gif"
}, {
format : "image/png; mode=8bit",
extension: "png"
} ];
/**
* static array where data array availables for OGCHelper are defined
*/
OGCHelper.FormatArray = [ {
format : "image/bil",
/**
* bufferIn : buffer to process (switch byte order and check the data limitations)
* size: defines the dimension of the array (size.height* size.width cells)
* highest: defines the highest altitude (without offset) of the data.
* lowest: defines the lowest altitude (without offset) of the data.
* offset: defines the offset of the data in order adjust the limitations
*/
postProcessArray : function(bufferIn, size,highest,lowest,offset) {
var resultat;
var viewerIn = new DataView(bufferIn);
var littleEndianBuffer = new ArrayBuffer(size.height * size.width * 2);
var viewerOut = new DataView(littleEndianBuffer);
if (littleEndianBuffer.byteLength === bufferIn.byteLength) {
// time to switch bytes!!
var temp, goodCell = 0, somme = 0;
for (var i = 0; i < littleEndianBuffer.byteLength; i += 2) {
temp = viewerIn.getInt16(i, false)-offset;
if (temp > lowest && temp < highest) {
viewerOut.setInt16(i, temp, true);
somme += temp;
goodCell++;
} else {
var val = (goodCell == 0 ? 1 : somme / goodCell);
viewerOut.setInt16(i, val, true);
}
}
resultat = new Int16Array(littleEndianBuffer);
}
return resultat;
}
} ];
OGCHelper.WMSParser={};
OGCHelper.TMSParser={};
OGCHelper.WMTSParser={};
/**
* parse wms,TMS or WMTS url from an url and a layer. request metadata information on server.
*
*
* @param {String}
* description.layerName the name of the layer.
* @param {String}
* [description.url] The URL of the server providing wms.
* @param {String}
* [description.xml] the xml after requesting "getCapabilities"
* from web map server.
* @param {String}
* [description.service] the type of service requested (WMS,TMS,WMTS). WMS is default
* from web map server.
* @param {Object}
* [description.proxy] A proxy to use for requests. This object
* is expected to have a getURL function which returns the
* proxied URL, if needed.
* @param {Number}
* [description.heightMapWidth] width of a tile in pixels
* @param {Number}
* [description.heightMapHeight] height of a tile in pixels
* @param {Number}
* [description.offset] offset of the tiles (in meters)
* @param {Number}
* [description.highest] highest altitude in the tiles (in meters)
* @param {Number}
* [description.lowest] lowest altitude in the tiles (in meters)
* @param {String}
* [description.styleName] name of the Style used for images.
* @param {boolean}
* [description.hasStyledImage] indicates if the requested images are styled with SLD
* @param {Boolean}
* [description.waterMask] indicates if a water mask will be
* displayed (experimental)
* @param {Number}
* [description.maxLevel] maximum level to request
* @param {Object}
* [description.formatImage] see OGCHelper.FormatImage
* @param {Object}
* [description.formatArray] see OGCHelper.FormatArray
* return a promise with:
* - ready : boolean which indicates that the parsing didn't have issue
* - [URLtemplateImage]: function which takes in parameters x,y,level and return the good URL template to request an image
* - [URLtemplateArray]: function which takes in parameters x,y,level and return the good URL template to request an typedArray
* - highest: integer indicates the highest elevation of the terrain provider
* - lowest: integer indicates the lowest elevation of the terrain provider
* - offset: integer indicates the offset of the terrain
* - hasStyledImage: boolean indicates if the images use a style (change the offset)
* - heightMapWidth: integer with of the hightMapTerrain
* - heightMapHeight: integer height of the hightMapTerrain
* - waterMask: boolean indicates if a water mask should be used
* - getTileDataAvailable: function determines whether data for a tile is available to be loaded
* - tilingScheme: the tiling scheme to use
* - [imageSize]: {width:integer, height:integer} dimension of the requested images
*/
OGCHelper.parser=function(description){
var resultat;
description = Cesium.defaultValue(description,
Cesium.defaultValue.EMPTY_OBJECT);
switch(description.service){
case "TMS":
resultat=OGCHelper.TMSParser.generate(description);
break;
case "WMTS":
resultat=OGCHelper.WMTSParser.generate(description);
break;
default: resultat=OGCHelper.WMSParser.generate(description);
}
return resultat;
}
OGCHelper.WMSParser.generate=function(description){
var resultat;
description = Cesium.defaultValue(description,
Cesium.defaultValue.EMPTY_OBJECT);
if (Cesium.defined(description.url)) {
var urlofServer=description.url;
var index=urlofServer.lastIndexOf("?");
if(index>-1){
urlofServer=urlofServer.substring(0,index);
}
var urlGetCapabilities = urlofServer
+ '?SERVICE=WMS&REQUEST=GetCapabilities&tiled=true';
if (Cesium.defined(description.proxy)) {
urlGetCapabilities = description.proxy.getURL(urlGetCapabilities);
}
resultat=Cesium.when(Cesium.loadXML(urlGetCapabilities), function(xml) {
return OGCHelper.WMSParser.getMetaDatafromXML(xml, description);
});
} else if (Cesium.defined(description.xml)) {
resultat=OGCHelper.WMSParser.getMetaDatafromXML(description.xml, description);
}else{
throw new Cesium.DeveloperError(
'either description.url or description.xml are required.');
}
return resultat;
};
OGCHelper.WMSParser.getMetaDatafromXML = function(xml, description) {
if (!(xml instanceof XMLDocument)) {
throw new Cesium.DeveloperError('xml must be a XMLDocument');
}
// get version of wms 1.1.X or 1.3.X=> for 1.3 use firstAxe for order of
// CRS
if (!Cesium.defined(description.layerName)) {
throw new Cesium.DeveloperError(
'description.layerName is required.');
}
var resultat={};
var layerName = description.layerName;
var maxLevel = Cesium.defaultValue(description.maxLevel, 11);
var version = undefined;
resultat.heightMapWidth = Cesium.defaultValue(description.heightMapWidth,65);
resultat.heightMapHeight = Cesium.defaultValue(description.heightMapHeight,resultat.heightMapWidth);
var requestedSize={width:65,height:65};
var CRS = undefined;
resultat.formatImage = description.formatImage;
resultat.formatArray = description.formatArray;
resultat.tilingScheme = undefined;
var firstAxeIsLatitude = undefined;
var isNewVersion = undefined;
resultat.ready = false;
resultat.levelZeroMaximumGeometricError = undefined;
resultat.waterMask = Cesium.defaultValue(description.waterMask, false);
if (typeof (resultat.waterMask) != "boolean") {
resultat.waterMask = false;
}
resultat.offset=Cesium.defaultValue(description.offset,0);
resultat.highest=Cesium.defaultValue(description.highest,12000);
resultat.lowest=Cesium.defaultValue(description.lowest,-500);
var styleName = description.styleName;
resultat.hasStyledImage=Cesium.defaultValue(description.hasStyledImage,typeof(description.styleName)==="string");
// get version
var versionNode = xml.querySelector("[version]");
if (versionNode !== null) {
version = versionNode.getAttribute("version");
isNewVersion = /^1\.[3-9]\./.test(version);
}
var url=xml.querySelector("Request>GetMap OnlineResource").getAttribute("xlink:href");
var index=url.indexOf("?");
if(index>-1){
url=url.substring(0,index);
}
if (Cesium.defined(description.proxy)) {
url = description.proxy.getURL(url);
}
// get list of map format
var nodeFormats = xml.querySelectorAll("Request>GetMap>Format");
if (!Cesium.defined(resultat.formatImage)) {
for(var j=0;j<nodeFormats.length && !Cesium.defined(resultat.formatArray);j++){
var OGCAvailables=OGCHelper.FormatArray.filter(function(elt){
return elt.format===nodeFormats[j].textContent;
});
if(OGCAvailables.length>0){
resultat.formatArray=OGCAvailables[0];
}
}
}
if (Cesium.defined(resultat.formatArray)
&& typeof (resultat.formatArray.format) === "string"
&& typeof (resultat.formatArray.postProcessArray) === "function") {
resultat.formatArray.terrainDataStructure = {
heightScale : 1.0,
heightOffset : 0,
elementsPerHeight : 1,
stride : 1,
elementMultiplier : 256.0,
isBigEndian : false
};
} else {
resultat.formatArray = undefined;
}
// a formatImage should always exist !!
for(var j=0;j<nodeFormats.length && !Cesium.defined(resultat.formatImage);j++){
var OGCAvailables=OGCHelper.FormatImage.filter(function(elt){
return elt.format===nodeFormats[j].textContent;
});
if(OGCAvailables.length>0){
resultat.formatImage=OGCAvailables[0];
}
}
if (Cesium.defined(resultat.formatImage)
&& typeof (resultat.formatImage.format) === "string") {
resultat.formatImage.terrainDataStructure = {
heightScale : 1.0,
heightOffset : 0,
elementsPerHeight : 2,
stride : 4,
elementMultiplier : 256.0,
isBigEndian : true
};
} else {
resultat.formatImage = undefined;
}
var layerNodes = xml
.querySelectorAll("Layer[queryable='1'],Layer[queryable='true']");
var layerNode;
for (var m = 0; m < layerNodes.length && !Cesium.defined(layerNode); m++) {
if (layerNodes[m].querySelector("Name").textContent === layerName) {
layerNode = layerNodes[m];
var fixedHeight=layerNode.getAttribute("fixedHeight");
var fixedWidth=layerNode.getAttribute("fixedWidth");
if(Cesium.defined(fixedHeight)){
fixedHeight=parseInt(fixedHeight);
resultat.heightMapHeight=fixedHeight>0&&fixedHeight<resultat.heightMapHeight?fixedHeight:resultat.heightMapHeight;
requestedSize.height=fixedHeight>0?fixedHeight:requestedSize.height;
}
if(Cesium.defined(fixedWidth)){
fixedWidth=parseInt(fixedWidth);
resultat.heightMapWidth=fixedWidth>0&&fixedWidth<resultat.heightMapWidth?fixedWidth:resultat.heightMapWidth;
requestedSize.width=fixedWidth>0?fixedWidth:requestedSize.width;
}
}
}
if (Cesium.defined(layerNode) && Cesium.defined(version)) {
var found = false;
for (var n = 0; n < OGCHelper.CRS.length && !found; n++) {
var CRSSelected = OGCHelper.CRS[n];
var referentialName = CRSSelected.name;
var nodeBBox = layerNode.querySelector("BoundingBox[SRS='"
+ referentialName + "'],BoundingBox[CRS='"
+ referentialName + "']");
if (nodeBBox !== null) {
CRS = referentialName;
firstAxeIsLatitude = CRSSelected.firstAxeIsLatitude;
resultat.tilingScheme = new CRSSelected.tilingScheme({
ellipsoid : CRSSelected.ellipsoid
});
var west,east,south,north;
if(firstAxeIsLatitude && isNewVersion){
west=parseFloat(nodeBBox.getAttribute("miny"));
east=parseFloat(nodeBBox.getAttribute("maxy"));
south=parseFloat(nodeBBox.getAttribute("minx"));
north=parseFloat(nodeBBox.getAttribute("maxx"));
}else{
west=parseFloat(nodeBBox.getAttribute("minx"));
east=parseFloat(nodeBBox.getAttribute("maxx"));
south=parseFloat(nodeBBox.getAttribute("miny"));
north=parseFloat(nodeBBox.getAttribute("maxy"));
}
var rectReference=new Cesium.Rectangle(west,south,east,north);
var scratchRect=new Cesium.Rectangle();
resultat.getTileDataAvailable = function(x, y, level){
var retour=false;
var rectangleCalcul = resultat.tilingScheme.tileXYToNativeRectangle(x, y,level);
if(level<maxLevel){
Cesium.Rectangle.intersectWith(rectReference,rectangleCalcul,scratchRect);
if(!Cesium.Rectangle.isEmpty(scratchRect)){
retour=true;
}
}
return retour;
};
found = true;
}
}
// style défini et existant?
if(Cesium.defined(styleName)){
var styleNodes = layerNode.querySelectorAll("Style>Name");
var styleFound = false;
for (var z = 0; z < styleNodes.length && !styleFound; z++) {
if (styleName === styleNodes[z].textContent) {
styleFound = true;
}
}
if (!styleFound) {
styleName = undefined;
}
}
//changer resolution height et width si existence de tileset dans le xml!!
var tileSets=xml.querySelectorAll("VendorSpecificCapabilities>TileSet");
var out=false;
for (var q=0;q<tileSets.length&&!out;q++){
var isGoodSRS=tileSets[q].querySelector("BoundingBox[SRS='"
+ CRS + "'],BoundingBox[CRS='"
+ CRS + "']")!==null;
var isGoodLayer=tileSets[q].querySelector("Layers").textContent=== layerName;
if(isGoodLayer&&isGoodSRS){
requestedSize.width=parseInt(tileSets[q].querySelector("Width").textContent);
requestedSize.height=parseInt(tileSets[q].querySelector("Height").textContent);
out=true;
}
}
resultat.ready = found
&& (Cesium.defined(resultat.formatImage) || Cesium.defined(resultat.formatArray))
&& Cesium.defined(version);
}
if(resultat.ready){
var URLtemplate=url+'?SERVICE=WMS&REQUEST=GetMap&layers='+ layerName + '&version=' + version+'&bbox=';
if(isNewVersion && firstAxeIsLatitude){
URLtemplate+='{south},{west},{north},{east}';
}else{
URLtemplate+='{west},{south},{east},{north}';
}
URLtemplate+='&crs='+CRS+'&srs='+CRS;
if(resultat.formatImage){
var URLtemplateImage=URLtemplate+'&format=' + resultat.formatImage.format+'&width='+ requestedSize.width +'&height=' + requestedSize.height;
if (Cesium.defined(styleName)) {
URLtemplateImage += "&styles=" + styleName+ "&style="+ styleName;
}
resultat.URLtemplateImage=function(){return URLtemplateImage;};
resultat.imageSize=requestedSize;
}
if(resultat.formatArray){
var URLtemplateArray=URLtemplate+ '&format=' +resultat.formatArray.format+ '&width='
+ resultat.heightMapWidth + '&height=' + resultat.heightMapHeight;
resultat.URLtemplateArray=function(){return URLtemplateArray;};
}
}
return resultat;
};
OGCHelper.TMSParser.generate=function(description){
var resultat;
description = Cesium.defaultValue(description,
Cesium.defaultValue.EMPTY_OBJECT);
if (Cesium.defined(description.url)) {
resultat=Cesium.loadXML(description.url).then(function(xml){return OGCHelper.TMSParser.parseXML(xml,description);});
} else if (Cesium.defined(description.xml)) {
resultat=OGCHelper.TMSParser.parseXML(description.xml,description);
}else{
throw new Cesium.DeveloperError(
'either description.url or description.xml are required.');
}
return resultat;
};
OGCHelper.TMSParser.parseXML=function(xml,description){
if (!(xml instanceof XMLDocument)) {
throw new Cesium.DeveloperError('xml must be a XMLDocument');
}
var resultat;
//description of a tile map service or of a tile map?
if(xml.querySelector("TileMapService")!=null){
if (!Cesium.defined(description.layerName)) {
throw new Cesium.DeveloperError('layerName is required.');
}
var mapServiceNodes=[].slice.apply(xml.querySelectorAll("TileMap[title='"+description.layerName+"']"));
var promises=mapServiceNodes.map(function(elt){
var url=elt.getAttribute("href");
if(Cesium.defined(description.proxy)){
url=description.proxy.getURL(url);
}
return Cesium.when(Cesium.loadXML(url),function(xml){
return OGCHelper.TMSParser.getMetaDatafromXML(xml,description);
});
});
var promise=Cesium.when.all(promises).then(function(tabResult){
var retour;
for(var i=0;i<tabResult.length&&!Cesium.defined(retour);i++){
if(Cesium.defined(tabResult[i])){
retour=tabResult[i];
}
}
return retour;
});
resultat=promise.then(function(retour){return retour;});
}else{
resultat=OGCHelper.TMSParser.getMetaDatafromXML(xml,description);
}
return resultat;
};
OGCHelper.TMSParser.getMetaDatafromXML=function(xml,description){
var resultat={};
resultat.ready = false;
resultat.heightMapWidth = Cesium.defaultValue(description.heightMapWidth,65);
resultat.heightMapHeight = Cesium.defaultValue(description.heightMapHeight,resultat.heightMapWidth);
var maxLevel = Cesium.defaultValue(description.maxLevel, 11);
var proxy=description.proxy;
resultat.hasStyledImage=Cesium.defaultValue(description.hasStyledImage,typeof(description.styleName)==="string");
resultat.waterMask=Cesium.defaultValue(description.waterMask, false);
if (typeof (resultat.waterMask) != "boolean") {
resultat.waterMask = false;
}
resultat.offset=Cesium.defaultValue(description.offset,0);
resultat.highest=Cesium.defaultValue(description.highest,12000);
resultat.lowest=Cesium.defaultValue(description.lowest,-500);
var srs=xml.querySelector("SRS").textContent;
var goodCRS=OGCHelper.CRS.filter(function(elt){
return elt.name===srs;
});
if(goodCRS.length>0){
resultat.tilingScheme = new goodCRS[0].tilingScheme({
ellipsoid : goodCRS[0].ellipsoid
});
}
var format=xml.querySelector("TileFormat");
var goodFormatImage=OGCHelper.FormatImage.filter(function(elt){
return elt.extension==format.getAttribute("extension");
});
if(goodFormatImage.length>0){
resultat.formatImage = goodFormatImage[0];
resultat.imageSize={};
resultat.imageSize.width=parseInt(format.getAttribute("width"));
resultat.imageSize.height=parseInt(format.getAttribute("height"));
}
var tilsetsNode=[].slice.call(xml.querySelectorAll("TileSets>TileSet"));
var tileSets=[];
if(Cesium.defined(resultat.formatImage)){
tileSets=tilsetsNode.map(function(tileSet){
var url=tileSet.getAttribute("href")+"/{x}/{tmsY}."+resultat.formatImage.extension;
if(Cesium.defined(proxy)){
url=proxy.getURL(url);
}
var level=parseInt(tileSet.getAttribute("order"));
return {url:url,level:level};
});
tileSets.sort(function(a,b){
return a.level-b.level;
});
if(tileSets.length>0){
resultat.tileSets=tileSets;
}
}
if(!Cesium.defined(resultat.tileSets)||!Cesium.defined(resultat.formatImage)||!Cesium.defined(resultat.tilingScheme)){
resultat=undefined;
}else{
resultat.URLtemplateImage=function(x,y,level){
var retour="";
if(level<tileSets.length){
retour=tileSets[level].url;
}
return retour;
}
var boundingBoxNode=xml.querySelector("BoundingBox");
var miny=parseFloat(boundingBoxNode.getAttribute("miny"));
var maxy=parseFloat(boundingBoxNode.getAttribute("maxy"));
var minx=parseFloat(boundingBoxNode.getAttribute("minx"));
var maxx=parseFloat(boundingBoxNode.getAttribute("maxx"));
var limites=new Cesium.Rectangle(minx,miny,maxx,maxy);
resultat.getTileDataAvailable=function(x,y,level){
var rect= resultat.tilingScheme.tileXYToNativeRectangle(x, y,level);
var scratchRectangle=Cesium.Rectangle.intersectWith(limites, rect);
return !Cesium.Rectangle.isEmpty(scratchRectangle) && level<maxLevel && level<tileSets.length;
}
resultat.ready=true;
}
return resultat;
};
OGCHelper.WMTSParser.generate=function(description){
description = Cesium.defaultValue(description,
Cesium.defaultValue.EMPTY_OBJECT);
var resultat;
if (Cesium.defined(description.url)) {
var urlofServer=description.url;
var index=urlofServer.lastIndexOf("?");
if(index>-1){
urlofServer=urlofServer.substring(0,index);
}
var urlGetCapabilities = urlofServer
+ '?REQUEST=GetCapabilities';
if (Cesium.defined(description.proxy)) {
urlGetCapabilities = description.proxy.getURL(urlGetCapabilities);
}
resultat=Cesium.loadXML(urlGetCapabilities).then(function(xml){return OGCHelper.WMTSParser.getMetaDatafromXML(xml,description);});
} else if (Cesium.defined(description.xml)) {
resultat=OGCHelper.WMTSParser.getMetaDatafromXML(description.xml,description);
}else{
throw new Cesium.DeveloperError(
'either description.url or description.xml are required.');
}
return resultat;
};
OGCHelper.WMTSParser.getMetaDatafromXML = function(xml,description){
if (!(xml instanceof XMLDocument)) {
throw new Cesium.DeveloperError('xml must be a XMLDocument');
}
var resultat={};
var layerName = description.layerName;
resultat.ready = false;
resultat.heightMapWidth = Cesium.defaultValue(description.heightMapWidth,65);
resultat.heightMapHeight = Cesium.defaultValue(description.heightMapHeight,resultat.heightMapWidth);
var maxLevel = Cesium.defaultValue(description.maxLevel, 12);
var proxy=description.proxy;
var styleName = description.styleName;
resultat.hasStyledImage=Cesium.defaultValue(description.hasStyledImage,typeof(description.styleName)==="string");
resultat.waterMask=Cesium.defaultValue(description.waterMask, false);
if (typeof (resultat.waterMask) != "boolean") {
resultat.waterMask = false;
}
resultat.offset=Cesium.defaultValue(description.offset,0);
resultat.highest=Cesium.defaultValue(description.highest,12000);
resultat.lowest=Cesium.defaultValue(description.lowest,-500);
var template;
var listTileMatrixSetLinkNode=[];
var urlKVP,urlRESTful;
var formatImage;
//KVP support for now
var nodesGetOperation=[].slice.call(xml.querySelectorAll('Operation[name="GetTile"] HTTP Get'));
var correctEncoding=nodesGetOperation.map(function(elt){
var val=elt.querySelector("Value").textContent;
var retour;
if("KVP"===val){
retour={node:elt,type:"KVP"};
}
if("RESTful"===val){
retour={node:elt,type:"RESTful"};
}
return retour;
}).filter(function(elt){
return Cesium.defined(elt);
});
for(var i=0;i<correctEncoding.length;i++){
var node=correctEncoding[i];
if(node.type==="RESTful" && !Cesium.defined(urlRESTful)){
urlRESTful=node.node.getAttribute("xlink:href");
if (Cesium.defined(proxy)) {
urlRESTful = proxy.getURL(urlRESTful);
}
}
if(node.type==="KVP" && !Cesium.defined(urlKVP)){
urlKVP=node.node.getAttribute("xlink:href");
if (Cesium.defined(proxy)) {
urlKVP = proxy.getURL(urlKVP);
}
}
}
var nodeIdentifiers=xml.querySelectorAll("Contents>Layer>Identifier");
var layerNode;
for (var i = 0; i < nodeIdentifiers.length && !Cesium.defined(layerNode); i++) {
if(layerName===nodeIdentifiers[i].textContent){
layerNode=nodeIdentifiers[i].parentNode;
}
}
if(Cesium.defined(layerNode)){
//optionality of style in geoserver is not compliant with OGC rules!!
var styleNodes=layerNode.querySelectorAll("Style");
var defaultStyle;
var selectedStyle;
for (var i = 0; i < styleNodes.length; i++) {
var style=styleNodes[i].querySelector("Identifier").textContent;
if(styleNodes[i].getAttribute("isDefault")!=null){
defaultStyle=style;
}
if(style===styleName){
selectedStyle=style;
}
}
//Work with attribute isDefault when no style was defined!!
if(!Cesium.defined(styleName) || styleName!=selectedStyle){
styleName=Cesium.defaultValue(defaultStyle,"");
}
//format
var nodeFormats=[].slice.call(layerNode.querySelectorAll("Format"));
for (var l = 0; l < OGCHelper.FormatImage.length
&& !Cesium.defined(formatImage); l++) {
var validFormats=nodeFormats.filter(function(elt){
return elt.textContent === OGCHelper.FormatImage[l].format;
});
if(validFormats.length>0){
formatImage = OGCHelper.FormatImage[l];
}
}
//TileMatrixSetLink =>TileMatrixSet
listTileMatrixSetLinkNode=layerNode.querySelectorAll("TileMatrixSetLink");
}
var nodeMatrixSetIds=[].slice.call(xml.querySelectorAll("TileMatrixSet>Identifier"));
for(var a=0;a<listTileMatrixSetLinkNode.length && !resultat.ready;a++){
var matrixSetLinkNode=listTileMatrixSetLinkNode[a];
var tileMatrixSetLinkName=matrixSetLinkNode.querySelector("TileMatrixSet").textContent;
var tileMatrixSetNode;
var CRSSelected;
for (var i = 0; i < nodeMatrixSetIds.length && !Cesium.defined(tileMatrixSetNode); i++) {
if(nodeMatrixSetIds[i].textContent===tileMatrixSetLinkName){
tileMatrixSetNode=nodeMatrixSetIds[i].parentNode;
}
}
var supportedCRS=tileMatrixSetNode.querySelector("SupportedCRS").textContent;
for (var n = 0; n < OGCHelper.CRS.length && !Cesium.defined(CRSSelected); n++) {
if(OGCHelper.CRS[n].SupportedCRS===supportedCRS){
CRSSelected = OGCHelper.CRS[n];
}
}
if(Cesium.defined(CRSSelected)){
var tileSets;
var nodeTileSets=[].slice.call(tileMatrixSetNode.querySelectorAll("TileMatrix"));
tileSets=nodeTileSets.map(function(noeud){
var id=noeud.querySelector("Identifier").textContent;
var maxWidth=parseInt(noeud.querySelector("MatrixWidth").textContent);
var maxHeight=parseInt(noeud.querySelector("MatrixHeight").textContent);
var tileWidth=parseInt(noeud.querySelector("TileWidth").textContent);
var tileHeight=parseInt(noeud.querySelector("TileHeight").textContent);
var scaleDenominator=parseFloat(noeud.querySelector("ScaleDenominator").textContent);
return {id:id,maxWidth:maxWidth,maxHeight:maxHeight,scaleDenominator:scaleDenominator,complete:false,
tileWidth:tileWidth,tileHeight:tileHeight};
});
tileSets.sort(function(a,b){
return b.scaleDenominator-a.scaleDenominator;
});
listTileMatrixLimits=matrixSetLinkNode.querySelectorAll("TileMatrixSetLimits>TileMatrixLimits");
for(var t=0;t<tileSets.length;t++){
var tile=tileSets[t];
for(var w=0;w<listTileMatrixLimits.length;w++){
var nodeLink=listTileMatrixLimits[w];
if(tile.id===nodeLink.querySelector("TileMatrix").textContent){
tile.minTileRow=parseInt(nodeLink.querySelector("MinTileRow").textContent);
tile.maxTileRow=parseInt(nodeLink.querySelector("MaxTileRow").textContent);
tile.minTileCol=parseInt(nodeLink.querySelector("MinTileCol").textContent);
tile.maxTileCol=parseInt(nodeLink.querySelector("MaxTileCol").textContent);
tile.complete=true;
tileSets[t]=tile;
}
}
}
if(tileSets.length>0){
resultat.tilingScheme = new CRSSelected.tilingScheme({
ellipsoid : CRSSelected.ellipsoid,
numberOfLevelZeroTilesX:tileSets[0].maxWidth,
numberOfLevelZeroTilesY:tileSets[0].maxHeight});
var resourceURL=layerNode.querySelector("ResourceURL[format='"+formatImage.format+"']");
if(resourceURL!=null){
template=resourceURL.getAttribute("template").replace("{TileRow}","{y}").replace("{TileCol}","{x}").replace("{Style}",styleName).
replace("{TileMatrixSet}",tileMatrixSetLinkName).replace("{layer}",layerName).replace("{infoFormatExtension}",formatImage.extension);
}else if(Cesium.defined(urlKVP)){
template=urlKVP+"service=WMTS&request=GetTile&version=1.0.0&layer="+layerName+"&style=&"+styleName+"format="+formatImage.format+"&TileMatrixSet="+tileMatrixSetLinkName+"&TileMatrix={TileMatrix}&TileRow={y}&TileCol={x}"
}
if(Cesium.defined(template)){
resultat.getTileDataAvailable=function(x,y,level){
var retour=false;
if(level<maxLevel && level<tileSets.length){
var tile=tileSets[level];
if(tile.complete){
retour= (y<=tile.maxTileRow && y>=tile.minTileRow) && (x<=tile.maxTileCol && x>=tile.minTileCol);
}else{
retour= x<tile.maxWidth && y<tile.maxHeight;
}
}
return retour;
};
resultat.URLtemplateImage=function(x,y,level){
var retour="";
if(resultat.getTileDataAvailable(x,y,level)){
var tile=tileSets[level];
retour=template.replace("{TileMatrix}",tile.id);
}
return retour;
};
var imageSize={width:tileSets[0].tileWidth,height:tileSets[0].tileHeight};
var checkSize=tileSets.filter(function(elt){
return elt.tileWidth!=imageSize.width || elt.tileHeight!=imageSize.height;
});
if(checkSize.length==0){
resultat.imageSize=imageSize;
}
resultat.ready=true;
}
}
}
}
return resultat;
};
/**
* A {@link TerrainProvider} that produces geometry by tessellating height
* maps retrieved from a geoserver terrain server.
*
* @alias GeoserverTerrainProvider
* @constructor
*
* @param {String}
* description.url The URL of the geoserver terrain server.
* @param {String}
* description.layerName The layers to include, separated by
* commas.
* @param {Proxy}
* [description.proxy] A proxy to use for requests. This object
* is expected to have a getURL function which returns the
* proxied URL, if needed.
* @param {Credit|String}
* [description.credit] A credit for the data source, which is
* displayed on the canvas.
* @param {Number}
* [description.heightMapWidth] width and height of the tiles
* @param {Number}
* [description.maxLevel] max level of tiles
* @param {String}
* [description.service] type of service to use (WMS, TMS or WMTS)
* @param {String}
* [description.xml] the xml after requesting "getCapabilities".
* @see TerrainProvider
*/
var GeoserverTerrainProvider = function GeoserverTerrainProvider(
description) {
if (!Cesium.defined(description)) {
throw new Cesium.DeveloperError('description is required.');
}
var errorEvent = new Cesium.Event();
var credit = description.credit;
if (typeof credit === 'string') {
credit = new Cesium.Credit(credit);
}
this.ready=false;
Cesium.defineProperties(this, {
errorEvent : {
get : function() {
return errorEvent;
}
},
credit : {
get : function() {
return credit;
}
},
hasVertexNormals : {
get : function() {
return false;
}
}
});
var promise=OGCHelper.parser(description);
TerrainParser(promise,this);
};
/**
*
* arrayBuffer: the arrayBuffer to process to have a HeightmapTerrainData
* limitations: object which defines highest (limitations.highest), lowest (limitations.lowest) altitudes
* and the offset (limitations.offset) of the terrain.
* size: number defining the height and width of the tile (can be a int or an object with two attributs: height and width)
* formatArray: object which defines the terrainDataStructure (formatArray.terrainDataStructure) and
* the postProcessArray (formatArray.postProcessArray)
* hasWaterMask: boolean to indicate to generate a waterMask
* childrenMask: Number defining the childrenMask
*
*/
GeoserverTerrainProvider.arrayToHeightmapTerrainData=function(arrayBuffer,limitations,size,formatArray,hasWaterMask,childrenMask){
if(typeof(size)=="number"){
size={width:size,height:size};
}
var heightBuffer = formatArray.postProcessArray(arrayBuffer,size,limitations.highest,limitations.lowest,
limitations.offset);
if (!Cesium.defined(heightBuffer)) {
throw new Cesium.DeveloperError("no good size");
}
var optionsHeihtmapTerrainData={
buffer : heightBuffer,
width : size.width,
height : size.height,
childTileMask : childrenMask,
structure : formatArray.terrainDataStructure
};
if(hasWaterMask){
var waterMask = new Uint8Array(
heightBuffer.length);
for (var i = 0; i < heightBuffer.length; i++) {
if (heightBuffer[i] <= 0) {
waterMask[i] = 255;
}
}
optionsHeihtmapTerrainData.waterMask=waterMask;
}
return new Cesium.HeightmapTerrainData(optionsHeihtmapTerrainData);
};
/**
*
* image: the image to process to have a HeightmapTerrainData
* limitations: object which defines highest (limitations.highest), lowest (limitations.lowest) altitudes
* and the offset (limitations.offset) of the terrain. The style defined in mySLD use an offset of 32768 meters
* size: number defining the height and width of the tile
* hasWaterMask: boolean to indicate to generate a waterMask
* childrenMask: Number defining the childrenMask
*/
GeoserverTerrainProvider.imageToHeightmapTerrainData=function(image,limitations,size,hasWaterMask,childrenMask,hasStyledImage){
if(typeof(size)=="number"){
size={width:size,height:size};
}
var dataPixels = Cesium.getImagePixels(image,size.width,size.height);
var waterMask = new Uint8Array(dataPixels.length / 4);
var buffer = new Int16Array(dataPixels.length / 4);
var goodCell = 0, somme = 0;
for (var i = 0; i < dataPixels.length; i += 4) {
var msb=dataPixels[i];
var lsb=dataPixels[i+1];
var isCorrect=dataPixels[i+2]>128;
var valeur = (msb<< 8 | lsb) - limitations.offset-32768;
if (valeur > limitations.lowest && valeur < limitations.highest && (isCorrect||hasStyledImage)) {
buffer[i / 4] = valeur;
somme += valeur;
goodCell++;
} else {
buffer[i / 4] = (goodCell == 0 ? 0 : somme / goodCell);
//buffer[i / 4] = 0;
}
}
var optionsHeihtmapTerrainData={
buffer : buffer,
width : size.width,
height : size.height,
childTileMask : childrenMask,
structure : {heightScale : 1.0,
heightOffset : 0.0,
elementsPerHeight : 1,
stride : 1,
elementMultiplier : 256.0,
isBigEndian : false}
};
if(hasWaterMask){
var waterMask = new Uint8Array(
heightBuffer.length);
for (var i = 0; i < heightBuffer.length; i++) {
if (heightBuffer[i] <= 0) {
waterMask[i] = 255;
}
}
optionsHeihtmapTerrainData.waterMask=waterMask;
}
return new Cesium.HeightmapTerrainData(optionsHeihtmapTerrainData);
};
function TerrainParser(promise,provider){
Cesium.when(promise,function(resultat){
console.log(resultat);
if(Cesium.defined(resultat)&&(resultat.ready)){
resultat.levelZeroMaximumGeometricError = Cesium.TerrainProvider.getEstimatedLevelZeroGeometricErrorForAHeightmap(
resultat.tilingScheme.ellipsoid, resultat.heightMapWidth,
resultat.tilingScheme.getNumberOfXTilesAtLevel(0));
if(Cesium.defined(resultat.URLtemplateImage)){
resultat.getHeightmapTerrainDataImage=function(x,y,level){
var retour;
if(!isNaN(x+y+level)){
var urlArray=templateToURL(resultat.URLtemplateImage(x,y,level),x, y, level,provider);
var limitations={highest:resultat.highest,lowest:resultat.lowest,offset:resultat.offset};
var hasChildren = terrainChildrenMask(x, y, level,provider);
var promise = Cesium.throttleRequestByServer(urlArray,Cesium.loadImage);
if (Cesium.defined(promise)) {
retour = Cesium.when(promise,function(image){
return GeoserverTerrainProvider.imageToHeightmapTerrainData(image,limitations,
{width:resultat.heightMapWidth,height:resultat.heightMapHeight},resultat.waterMask,hasChildren,resultat.hasStyledImage);
}).otherwise(function(){
return new Cesium.HeightmapTerrainData({
buffer : new Uint16Array(
resultat.heightMapWidth
* resultat.heightMapHeight),
width : resultat.heightMapWidth,
height : resultat.heightMapHeight,
childTileMask : hasChildren,
waterMask : new Uint8Array(resultat.heightMapWidth
* resultat.heightMapHeight),
structure : resultat.formatImage.terrainDataStructure
});
});}
}
return retour;
};
}
if(Cesium.defined(resultat.URLtemplateArray)){
resultat.getHeightmapTerrainDataArray=function(x, y, level){
var retour;
if(!isNaN(x+y+level)){
var urlArray=templateToURL(resultat.URLtemplateArray(x,y,level),x, y, level,provider);
var limitations={highest:resultat.highest,lowest:resultat.lowest,offset:resultat.offset};
var hasChildren = terrainChildrenMask(x, y, level,provider);
var promise = Cesium.throttleRequestByServer(urlArray,Cesium.loadArrayBuffer);
if (Cesium.defined(promise)) {
retour = Cesium.when(promise,
function(arrayBuffer) {
return GeoserverTerrainProvider.arrayToHeightmapTerrainData(arrayBuffer,limitations,
{width:resultat.heightMapWidth,height:resultat.heightMapHeight},resultat.formatArray,resultat.waterMask,hasChildren);
}
).otherwise(
function() {
if (Cesium.defined(resultat.getHeightmapTerrainDataImage)) {
return resultat.getHeightmapTerrainDataImage(x, y, level);
}else{
return new Cesium.HeightmapTerrainData({