+"use strict";var e=Object.defineProperty,t=(t,r,i)=>((t,r,i)=>r in t?e(t,r,{enumerable:!0,configurable:!0,writable:!0,value:i}):t[r]=i)(t,"symbol"!=typeof r?r+"":r,i);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const r=require("cesium");function i(e,t){const i=Math.max(e.west,t.west),l=Math.min(e.east,t.east),a=Math.max(e.south,t.south),o=Math.min(e.north,t.north);let n;return n=l<=i||a>=o?null:new r.Rectangle(i,a,l,o),n}const l=[{name:"CRS:84",ellipsoid:r.Ellipsoid.WGS84,firstAxeIsLatitude:!1,tilingScheme:r.GeographicTilingScheme,supportedCRS:"urn:ogc:def:crs:OGC:2:84"},{name:"EPSG:4326",ellipsoid:r.Ellipsoid.WGS84,firstAxeIsLatitude:!0,tilingScheme:r.GeographicTilingScheme,supportedCRS:"urn:ogc:def:crs:EPSG::4326"},{name:"EPSG:3857",ellipsoid:r.Ellipsoid.WGS84,firstAxeIsLatitude:!1,tilingScheme:r.WebMercatorTilingScheme,supportedCRS:"urn:ogc:def:crs:EPSG::3857"},{name:"OSGEO:41001",ellipsoid:r.Ellipsoid.WGS84,firstAxeIsLatitude:!1,tilingScheme:r.WebMercatorTilingScheme,supportedCRS:"urn:ogc:def:crs:EPSG::3857"}],a=[{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"}],o=[{format:"image/bil",postProcessArray:function(e,t,r,i,l){let a=null;const o=new DataView(e),n=new ArrayBuffer(t.height*t.width*2),s=new DataView(n);if(n.byteLength===e.byteLength){let e,t=0,h=0;for(let a=0;a<n.byteLength;a+=2)if(e=o.getInt16(a,!1)-l,e>i&&e<r)s.setInt16(a,e,!0),h+=e,t++;else{const e=0===t?1:h/t;s.setInt16(a,e,!0)}a=new Int16Array(n)}return a}}],n={service:"WMS",maxLevel:11,heightMapWidth:65,heightMapHeight:65,offset:0,highest:12e3,lowest:-500,hasStyledImage:!1},s={heightScale:1,heightOffset:0,elementsPerHeight:1,stride:1,elementMultiplier:256,isBigEndian:!1,lowestEncodedHeight:0,highestEncodedHeight:1e4};function h(e,t){t.heightMapWidth=e.heightMapWidth,t.heightMapHeight=e.heightMapHeight,t.ready=!1,t.maximumLevel=e.maxLevel,t.levelZeroMaximumGeometricError=void 0,t.offset=e.offset,t.highest=e.highest,t.lowest=e.lowest,t.hasStyledImage=e.hasStyledImage||"string"==typeof e.styleName}const{fetchXML:m}=r.Resource;async function g(e){const t=e.xml;if(!(t instanceof XMLDocument))throw new Error("xml must be a XMLDocument");let r;if(null!==t.querySelector("TileMapService")){if(!e.layerName)throw new Error("layerName is required.");const i=Array.from(t.querySelectorAll("TileMap[title='"+e.layerName+"']")).map((t=>{let r=t.getAttribute("href");return e.proxy&&(r=e.proxy.getURL(r)),m({url:r}).then((t=>(e.xml=t,c(e))))}));r=await Promise.race(i)}else r=c(e);return r}function c(e){const t=e.xml;let o={};h(e,o);const n=e.maxLevel,s=e.proxy,m=t.querySelector("SRS").textContent,g=l.find((e=>e.name===m));g&&(o.tilingScheme=new g.tilingScheme({ellipsoid:g.ellipsoid}));const c=t.querySelector("TileFormat"),u=a.find((e=>e.extension==c.getAttribute("extension")));u&&(o.formatImage=u,o.imageSize={width:parseInt(c.getAttribute("width"),10),height:parseInt(c.getAttribute("height"),10)});const f=Array.from(t.querySelectorAll("TileSets>TileSet"));let p=[];if(o.formatImage&&(p=f.map((function(e){let t=e.getAttribute("href")+"/{x}/{tmsY}."+o.formatImage.extension;s&&(t=s.getURL(t));return{url:t,level:parseInt(e.getAttribute("order"))}})),p.sort(((e,t)=>e.level-t.level))),0===p.length||!o.formatImage||!o.tilingScheme)throw new Error("no tilesets, no formatImage and no formatArray");o.URLtemplateImage=function(e,t,r){let i="";return r<p.length&&(i=p[r].url),i};const y=t.querySelector("BoundingBox"),d=parseFloat(y.getAttribute("miny")),x=parseFloat(y.getAttribute("maxy")),S=parseFloat(y.getAttribute("minx")),w=parseFloat(y.getAttribute("maxx")),A=new r.Rectangle(S,d,w,x);return o.getTileDataAvailable=function(e,t,r){const l=o.tilingScheme.tileXYToNativeRectangle(e,t,r);return null!==i(A,l)&&r<n&&r<p.length},o.ready=!0,o}const{fetchXML:u}=r.Resource;function f(e){if(!(e.xml instanceof XMLDocument))throw new Error("xml must be a XMLDocument");if(!e.layerName)throw new Error("description.layerName is required.");const t=e.xml,n={},m=e.layerName,g=e.maxLevel;let c=null;const u={width:65,height:65};let f,p,y;h(e,n),n.formatImage=e.formatImage,n.formatArray=e.formatArray,n.tilingScheme=void 0;let d=e.styleName;const x=t.querySelector("[version]");null!==x&&(c=x.getAttribute("version"),y=/^1\.[3-9]\./.test(c));let S=t.querySelector("Request>GetMap OnlineResource").getAttribute("xlink:href");const w=S.indexOf("?");w>-1&&(S=S.substring(0,w)),e.proxy&&(S=e.proxy.getURL(S));const A=Array.from(t.querySelectorAll("Request>GetMap>Format")).map((e=>e.textContent));for(let r=0;r<A.length;r++)n.formatImage||(n.formatImage=a.find((e=>e.format===A[r]))),n.formatArray||(n.formatArray=o.find((e=>e.format===A[r])));n.formatArray&&"string"==typeof n.formatArray.format&&"function"==typeof n.formatArray.postProcessArray?n.formatArray.terrainDataStructure=Object.assign({},s):delete n.formatArray,n.formatImage&&"string"==typeof n.formatImage.format||(n.formatImage=void 0);const M=t.querySelectorAll("Layer[queryable='1'],Layer[queryable='true']");let b=null;for(let r=0;r<M.length&&null===b;r++)if(M[r].querySelector("Name").textContent===m){b=M[r];let e=b.getAttribute("fixedHeight"),t=b.getAttribute("fixedWidth");null!==e&&(e=parseInt(e,10),n.heightMapHeight=e>0&&e<n.heightMapHeight?e:n.heightMapHeight,u.height=e>0?e:u.height),null!==t&&(t=parseInt(t,10),n.heightMapWidth=t>0&&t<n.heightMapWidth?t:n.heightMapWidth,u.width=t>0?t:u.width)}if(null!==b&&null!==c){let e=!1;for(let t=0;t<l.length&&!e;t++){const a=l[t],o=a.name,s=b.querySelector("BoundingBox[SRS='"+o+"'],BoundingBox[CRS='"+o+"']");if(null!==s){let t,l,h,m;f=o,p=a.firstAxeIsLatitude,n.tilingScheme=new a.tilingScheme({ellipsoid:a.ellipsoid}),p&&y?(t=parseFloat(s.getAttribute("miny")),l=parseFloat(s.getAttribute("maxy")),h=parseFloat(s.getAttribute("minx")),m=parseFloat(s.getAttribute("maxx"))):(t=parseFloat(s.getAttribute("minx")),l=parseFloat(s.getAttribute("maxx")),h=parseFloat(s.getAttribute("miny")),m=parseFloat(s.getAttribute("maxy")));const c=new r.Rectangle(t,h,l,m);n.getTileDataAvailable=function(e,t,r){let l=!1,a=n.tilingScheme.tileXYToNativeRectangle(e,t,r);if(r<g){l=null!==i(c,a)}return l},e=!0}}if(d){const e=b.querySelectorAll("Style>Name");let t=!1;for(let r=0;r<e.length&&!t;r++)d===e[r].textContent&&(t=!0);t||(d=null)}const a=t.querySelectorAll("VendorSpecificCapabilities>TileSet");let o=!1;for(let t=0;t<a.length&&!o;t++){const e=null!==a[t].querySelector("BoundingBox[SRS='"+f+"'],BoundingBox[CRS='"+f+"']");a[t].querySelector("Layers").textContent===m&&e&&(u.width=parseInt(a[t].querySelector("Width").textContent,10),u.height=parseInt(a[t].querySelector("Height").textContent,10),o=!0)}n.ready=e&&(n.formatImage||n.formatArray)&&null!==c}if(n.ready){let e=S+"?SERVICE=WMS&REQUEST=GetMap&layers="+m+"&version="+c+"&bbox=";if(e+=y&&p?"{south},{west},{north},{east}":"{west},{south},{east},{north}",e+="&crs="+f+"&srs="+f,n.formatImage){let t=e+"&format="+n.formatImage.format+"&width="+u.width+"&height="+u.height;d&&(t+="&styles="+d+"&style="+d),n.URLtemplateImage=()=>t,n.imageSize=u}if(n.formatArray){const t=e+"&format="+n.formatArray.format+"&width="+n.heightMapWidth+"&height="+n.heightMapHeight;n.URLtemplateArray=()=>t}}return n}const{fetchXML:p}=r.Resource;function y(e){const t=e.xml;if(!(t instanceof XMLDocument))throw new Error("xml must be a XMLDocument");const r={},i=e.layerName;h(e,r);const o=e.maxLevel,n=e.proxy;let s=e.styleName,m=null,g=[],c=null,u=null,f=null;Array.from(t.querySelectorAll('Operation[name="GetTile"] HTTP Get')).map((e=>({node:e,type:e.querySelector("Value").textContent}))).forEach((e=>{"RESTful"===e.type&&null===u&&(u=e.node.getAttribute("xlink:href"),n&&(u=n.getURL(u))),"KVP"===e.type&&null===c&&(c=e.node.getAttribute("xlink:href"),n&&(c=n.getURL(c)))}));const p=t.querySelectorAll("Contents>Layer>Identifier");let y=null;for(let l=0;l<p.length&&null===y;l++)i===p[l].textContent&&(y=p[l].parentNode);if(null!==y){let e=null,t=null;Array.from(y.querySelectorAll("Style")).forEach((r=>{const i=r.querySelector("Identifier").textContent;null!=r.getAttribute("isDefault")&&(e=i),i===s&&(t=i)})),s&&s===t||(s=e||"");const r=Array.from(y.querySelectorAll("Format"));for(let i=0;i<a.length&&null===f;i++){r.filter((e=>e.textContent===a[i].format)).length>0&&(f=a[i])}g=Array.from(y.querySelectorAll("TileMatrixSetLink"))}const d=Array.from(t.querySelectorAll("TileMatrixSet>Identifier"));for(let a=0;a<g.length&&!r.ready;a++){const e=g[a],t=e.querySelector("TileMatrixSet").textContent;let n=null,h=null;for(let r=0;r<d.length&&null===n;r++)d[r].textContent===t&&(n=d[r].parentNode);const u=n.querySelector("SupportedCRS").textContent;for(let r=0;r<l.length&&null===h;r++)l[r].supportedCRS===u&&(h=l[r]);if(null!==h){const l=Array.from(n.querySelectorAll("TileMatrix")).map((function(e){let t=e.querySelector("Identifier").textContent,r=parseInt(e.querySelector("MatrixWidth").textContent,10),i=parseInt(e.querySelector("MatrixHeight").textContent,10),l=parseInt(e.querySelector("TileWidth").textContent,10),a=parseInt(e.querySelector("TileHeight").textContent,10);return{id:t,maxWidth:r,maxHeight:i,scaleDenominator:parseFloat(e.querySelector("ScaleDenominator").textContent),complete:!1,tileWidth:l,tileHeight:a}})).sort(((e,t)=>t.scaleDenominator-e.scaleDenominator)),a=Array.from(e.querySelectorAll("TileMatrixSetLimits>TileMatrixLimits")).map((e=>({id:e.querySelector("TileMatrix").textContent,bbox:{minTileRow:parseInt(e.querySelector("MinTileRow").textContent,10),maxTileRow:parseInt(e.querySelector("MaxTileRow").textContent,10),minTileCol:parseInt(e.querySelector("MinTileCol").textContent,10),maxTileCol:parseInt(e.querySelector("MaxTileCol").textContent,10)}})));if(l.forEach((e=>{a.forEach((t=>{e.id===t.id&&(e.bbox=t.bbox,e.complete=!0)}))})),l.length>0){r.tilingScheme=new h.tilingScheme({ellipsoid:h.ellipsoid,numberOfLevelZeroTilesX:l[0].maxWidth,numberOfLevelZeroTilesY:l[0].maxHeight});const e=y.querySelector("ResourceURL[format='"+f.format+"']");if(null!==e?m=e.getAttribute("template").replace("{TileRow}","{y}").replace("{TileCol}","{x}").replace("{style}",s).replace("{Style}",s).replace("{TileMatrixSet}",t).replace("{layer}",i).replace("{infoFormatExtension}",f.extension):null!==c&&(m=c+"service=WMTS&request=GetTile&version=1.0.0&layer="+i+"&style="+s+"&format="+f.format+"&TileMatrixSet="+t+"&TileMatrix={TileMatrix}&TileRow={y}&TileCol={x}"),null!==m){r.getTileDataAvailable=(e,t,r)=>{let i=!1;if(r<o&&r<l.length){const a=l[r],o=a.bbox;i=a.complete?t<=o.maxTileRow&&t>=o.minTileRow&&e<=o.maxTileCol&&e>=o.minTileCol:e<a.maxWidth&&t<a.maxHeight}return i},r.URLtemplateImage=function(e,t,i){let a="";if(r.getTileDataAvailable(e,t,i)){let e=l[i];a=m.replace("{TileMatrix}",e.id)}return a};const e={width:l[0].tileWidth,height:l[0].tileHeight};0===l.filter((t=>t.tileWidth!=e.width||t.tileHeight!=e.height)).length&&(r.imageSize=e),r.ready=!0}}}}return r}let d=class{constructor(e,i=""){t(this,"errorEvent",new r.Event),t(this,"credit"),t(this,"tilingScheme"),t(this,"ready",!1),t(this,"hasVertexNormals",!1),t(this,"readyPromise"),t(this,"hasWaterMask"),t(this,"heightMapHeight"),t(this,"heightMapWidth"),t(this,"availability"),this.credit=new r.Credit(i),this.tilingScheme=e.tilingScheme,this.getTileDataAvailable=e.getTileDataAvailable,this.hasWaterMask=!1,this.heightMapHeight=e.heightMapHeight,this.heightMapWidth=e.heightMapWidth,this.availability=new r.TileAvailability(this.tilingScheme,e.maximumLevel),this.getLevelMaximumGeometricError=t=>e.levelZeroMaximumGeometricError/(1<<t),this.requestTileGeometry=async(t,i,l)=>{const a=await e.GeometryCallback(t,i,l),o=function(e,t,r,i){let l=0,a=r+1;return l|=i.getTileDataAvailable(2*e,2*t,a)?1:0,l|=i.getTileDataAvailable(2*e+1,2*t,a)?2:0,l|=i.getTileDataAvailable(2*e,2*t+1,a)?4:0,l|=i.getTileDataAvailable(2*e+1,2*t+1,a)?8:0,l}(t,i,l,e);return new r.HeightmapTerrainData({buffer:a,width:e.heightMapWidth,height:e.heightMapHeight,childTileMask:o})},this.ready=e.ready,this.readyPromise=new Promise((e=>!0))}requestTileGeometry(e,t,r,i){throw new Error("Method not implemented.")}getLevelMaximumGeometricError(e){throw new Error("Method not implemented.")}getTileDataAvailable(e,t,r){throw new Error("Method not implemented.")}loadTileDataAvailability(e,t,r){throw new Error("Method not implemented.")}};const{fetchArrayBuffer:x,fetchImage:S}=r.Resource;async function w(e){let t;switch((e=Object.assign(n,e)).service){case"TMS":t=async function(e){let t;if(e.url)e.xml=await m({url:e.url+"/gwc/service/tms/1.0.0"}),t=await g(e);else{if(!e.xml)throw new Error("either description.url or description.xml are required.");t=await g(e)}return t}(e);break;case"WMTS":t=async function(e){let t;if(e.url){let r=e.url;const i=r.lastIndexOf("?");i>-1&&(r=r.substring(0,i));let l=r+"/gwc/service/wmts?REQUEST=GetCapabilities";e.proxy&&(l=e.proxy.getURL(l)),console.log(l),e.xml=await p({url:l}),t=y(e)}else{if(!e.xml)throw new Error("either description.url or description.xml are required.");t=y(e)}return t}(e);break;default:t=async function(e){let t;if(e.url){let r=e.url,i=r.lastIndexOf("?");i>-1&&(r=r.substring(0,i));let l=r+"/ows?SERVICE=WMS&REQUEST=GetCapabilities&tiled=true";console.log(l),e.proxy&&(l=e.proxy.getURL(l)),e.xml=await u({url:l}),t=f(e)}else{if(!e.xml)throw new Error("either description.url or description.xml are required.");t=f(e)}return t}(e)}const i=await t;return A(i),"WMTS"===e.service?new d(i):new r.CustomHeightmapTerrainProvider({height:i.heightMapHeight,width:i.heightMapWidth,tilingScheme:i.tilingScheme,callback:i.GeometryCallback})}function A(e){e.levelZeroMaximumGeometricError=r.TerrainProvider.getEstimatedLevelZeroGeometricErrorForAHeightmap(e.tilingScheme.ellipsoid,e.heightMapWidth,e.tilingScheme.getNumberOfXTilesAtLevel(0)),e.URLtemplateImage&&(e.getBufferImage=async(t,i,l)=>{let a=null;if(!isNaN(t+i+l)){const o=M(e.URLtemplateImage(t,i,l),t,i,l,e),n={highest:e.highest,lowest:e.lowest,offset:e.offset};let s=S({url:o});a=await s.then((t=>function(e,t,i,l){const a=r.getImagePixels(e,i.width,i.height),o=new Int16Array(a.length/4);let n=0,s=0;for(let r=0;r<a.length;r+=4){const e=a[r],i=a[r+1],h=a[r+2]>128,m=(e<<8|i)-t.offset-32768;m>t.lowest&&m<t.highest&&(h||l)?(o[r/4]=m,s+=m,n++):o[r/4]=0===n?0:s/n}return o}(t,n,{width:e.heightMapWidth,height:e.heightMapHeight},e.hasStyledImage))).catch((()=>new Int16Array(e.heightMapWidth*e.heightMapHeight)))}return a}),e.URLtemplateArray&&(e.getBufferArray=async(t,r,i)=>{let l=null;if(!isNaN(t+r+i)){const a=M(e.URLtemplateArray(t,r,i),t,r,i,e),o={highest:e.highest,lowest:e.lowest,offset:e.offset};let n=x({url:a});l=await n.then((t=>function(e,t,r,i){return i.postProcessArray(e,r,t.highest,t.lowest,t.offset)}(t,o,{width:e.heightMapWidth,height:e.heightMapHeight},e.formatArray))).catch((()=>new Int16Array(e.heightMapWidth*e.heightMapHeight)))}return l}),e.GeometryCallback=async(t,r,i)=>{let l;return e.getBufferArray?l=await e.getBufferArray(t,r,i):e.getBufferImage&&(l=await e.getBufferImage(t,r,i)),l}}function M(e,t,r,i,l){const a=l.tilingScheme.tileXYToNativeRectangle(t,r,i),o=(a.east-a.west)/(l.heightMapWidth-1),n=(a.north-a.south)/(l.heightMapHeight-1);a.west-=.5*o,a.east+=.5*o,a.south-=.5*n,a.north+=.5*n;const s=l.tilingScheme.getNumberOfYTilesAtLevel(i)-r-1;return e.replace("{south}",a.south.toString()).replace("{north}",a.north.toString()).replace("{west}",a.west.toString()).replace("{east}",a.east.toString()).replace("{x}",t.toString()).replace("{y}",r.toString()).replace("{tmsY}",s.toString())}window.Cesium.GeoserverTerrainProvider=w,exports.TerrainParser=A,exports.default=w;
0 commit comments