UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

3 lines (2 loc) • 9.13 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{isSome as e}from"../../core/arrayUtils.js";import t from"../../core/Error.js";import{getReferenceEllipsoidFromWKID as i}from"../../geometry/ellipsoidUtils.js";import n from"../../geometry/Extent.js";import l from"../../geometry/Point.js";import r from"../../geometry/support/WKIDUnitConversion.js";import{isAxesOrderReversedForWkid as o}from"../ogc/crsUtils.js";import{visitXML as s}from"../ogc/xmlUtils.js";import a from"./LOD.js";import c from"./TileInfo.js";const u=90.71428571428571;function p(e){const t=e.replaceAll(/ows:/gi,"");return(new DOMParser).parseFromString(t,"text/xml")}function f(e){if(!g("Contents",e.documentElement))throw new t("wmtslayer:wmts-capabilities-xml-is-not-valid","the wmts get capabilities response is not compliant")}function d(e,i){const n=e.documentElement,l=new Map,r=new Map,o=g("Contents",n);if(!o)throw new t("wmtslayer:wmts-capabilities-xml-is-not-valid","Can't retrieve xml capabilities element");const s=g("OperationsMetadata",n),a=s?.querySelector("[name='GetTile']"),c=a?.getElementsByTagName("Get"),u=c&&Array.prototype.slice.call(c),p=i.url?.indexOf("https"),f=void 0!==p&&p>-1;let d,m,x=i.serviceMode,w=i?.url;if(u?.length&&u.some(e=>{const t=g("Constraint",e);return!t||C("AllowedValues","Value",x,t)?(w=e.attributes[0].nodeValue,!0):(!t||C("AllowedValues","Value","RESTful",t)||C("AllowedValues","Value","REST",t)?m=e.attributes[0].nodeValue:t&&!C("AllowedValues","Value","KVP",t)||(d=e.attributes[0].nodeValue),!1)}),!w)if(m)w=m,x="RESTful";else if(d)w=d,x="KVP";else{const e=g("ServiceMetadataURL",n);w=e?.getAttribute("xlink:href")}const y=w.indexOf("1.0.0/");-1===y&&"RESTful"===x?w+="/":y>-1&&(w=w.slice(0,y)),"KVP"===x&&(w+=y>-1?"":"?"),f&&(w=w.replace(/^http:/i,"https:"));const A=M("ServiceIdentification>ServiceTypeVersion",n),R=M("ServiceIdentification>AccessConstraints",n),S=R&&/^none$/i.test(R)?null:R,V=h("Layer",o),L=h("TileMatrixSet",o),b=V.map(e=>{const t=M("Identifier",e);return l.set(t,e),T(t,e,L,f,A)});return{copyright:S,dimensionMap:r,layerMap:l,layers:b,serviceMode:x,tileUrl:w}}function m(e){for(const t of e.layers)for(const e of t.tileMatrixSets??[]){const{tileInfo:t}=e;if(t&&96!==t.dpi){for(const i of t.lods??[])i.scale=96*i.scale/t.dpi,i.resolution=j(t.spatialReference?.wkid,i.scale*u/96,e.id);t.dpi=96}}}function x(e){return e.nodeType===Node.ELEMENT_NODE}function g(e,t){for(let i=0;i<t.childNodes.length;i++){const n=t.childNodes[i];if(x(n)&&n.nodeName===e)return n}return null}function h(e,t){const i=[];for(let n=0;n<t.childNodes.length;n++){const l=t.childNodes[n];x(l)&&l.nodeName===e&&i.push(l)}return i}function w(t,i){const n=[];for(let e=0;e<i.childNodes.length;e++){const l=i.childNodes[e];x(l)&&l.nodeName===t&&n.push(l)}return n.map(e=>e.textContent).filter(e)}function M(e,t){return e.split(">").forEach(e=>{t&&(t=g(e,t))}),t&&t.textContent}function C(e,t,i,n){let l;return Array.prototype.slice.call(n.childNodes).some(n=>{if(n.nodeName.includes(e)){const e=g(t,n),r=e?.textContent;if(r===i||i.split(":")&&i.split(":")[1]===r)return l=n,!0}return!1}),l}function T(e,t,i,n,l){const r=M("Abstract",t),o=w("Format",t);return{id:e,fullExtent:V(t),fullExtents:L(t),description:r,formats:o,styles:b(t,n),title:M("Title",t),tileMatrixSets:E(l,t,i)}}function y(e,t){const i=[],n=e.layerMap?.get(t);if(!n)return null;const l=h("ResourceURL",n),r=h("Dimension",n);let o,s,a,c;return r.length&&(o=M("Identifier",r[0]),s=w("Default",r[0])||w("Value",r[0])),r.length>1&&(a=M("Identifier",r[1]),c=w("Default",r[1])||w("Value",r[1])),e.dimensionMap.set(t,{dimensions:s,dimensions2:c}),l.forEach(e=>{let t=e.getAttribute("template");if("tile"===e.getAttribute("resourceType")){if(o&&s.length)if(t.includes("{"+o+"}"))t=t.replace("{"+o+"}","{dimensionValue}");else{const e=t.toLowerCase().indexOf("{"+o.toLowerCase()+"}");e>-1&&(t=t.slice(0,e)+"{dimensionValue}"+t.slice(e+o.length+2))}if(a&&c.length)if(t.includes("{"+a+"}"))t=t.replace("{"+a+"}","{dimensionValue2}");else{const e=t.toLowerCase().indexOf("{"+a.toLowerCase()+"}");e>-1&&(t=t.slice(0,e)+"{dimensionValue2}"+t.slice(e+a.length+2))}i.push({template:t,format:e.getAttribute("format"),resourceType:"tile"})}}),i}function A(e,t,i,n,l,r,o,s){const a=R(e,t,n);if(!(a?.length>0))return"";const{dimensionMap:c}=e,u=c.get(t).dimensions?.[0],p=c.get(t).dimensions2?.[0];return a[o%a.length].template.replaceAll(/\{Style\}/gi,l??"").replaceAll(/\{TileMatrixSet\}/gi,i??"").replaceAll(/\{TileMatrix\}/gi,r).replaceAll(/\{TileRow\}/gi,""+o).replaceAll(/\{TileCol\}/gi,""+s).replaceAll(/\{dimensionValue\}/gi,u).replaceAll(/\{dimensionValue2\}/gi,p)}function R(e,t,i){const n=y(e,t),l=n?.filter(e=>e.format===i);return(l?.length?l:n)??[]}function S(e,t,i,n){const{dimensionMap:l}=e,r=y(e,t);let o="";if(r&&r.length>0){const e=l.get(t).dimensions?.[0],s=l.get(t).dimensions2?.[0];o=r[0].template,o.endsWith(".xxx")&&(o=o.slice(0,-4)),o=o.replaceAll(/\{Style\}/gi,n),o=o.replaceAll(/\{TileMatrixSet\}/gi,i),o=o.replaceAll(/\{TileMatrix\}/gi,"{level}"),o=o.replaceAll(/\{TileRow\}/gi,"{row}"),o=o.replaceAll(/\{TileCol\}/gi,"{col}"),o=o.replaceAll(/\{dimensionValue\}/gi,e),o=o.replaceAll(/\{dimensionValue2\}/gi,s)}return o}function V(e){const t=g("WGS84BoundingBox",e),i=t?M("LowerCorner",t).split(" "):["-180","-90"],n=t?M("UpperCorner",t).split(" "):["180","90"];return{xmin:parseFloat(i[0]),ymin:parseFloat(i[1]),xmax:parseFloat(n[0]),ymax:parseFloat(n[1]),spatialReference:{wkid:4326}}}function L(e){const t=[];return s(e,{BoundingBox:e=>{if(!e.getAttribute("crs"))return;const i=e.getAttribute("crs").toLowerCase(),n=N(i),l=i.includes("epsg")&&o(n.wkid);let r,a,c,u;s(e,{LowerCorner:e=>{[r,a]=e.textContent.split(" ").map(e=>Number.parseFloat(e)),l&&([r,a]=[a,r])},UpperCorner:e=>{[c,u]=e.textContent.split(" ").map(e=>Number.parseFloat(e)),l&&([c,u]=[u,c])}}),t.push({xmin:r,ymin:a,xmax:c,ymax:u,spatialReference:n})}}),t}function b(e,t){return h("Style",e).map(e=>{const i=g("LegendURL",e),n=g("Keywords",e),l=n?w("Keyword",n):[];let r=i?.getAttribute("xlink:href");t&&(r=r?.replace(/^http:/i,"https:"));return{abstract:M("Abstract",e),id:M("Identifier",e),isDefault:"true"===e.getAttribute("isDefault"),keywords:l,legendUrl:r,title:M("Title",e)}})}function E(e,t,i){return h("TileMatrixSetLink",t).map(t=>I(e,t,i))}function I(e,t,i){const n=g("TileMatrixSet",t).textContent,l=w("TileMatrix",t),r=i.find(e=>{const t=g("Identifier",e),i=t?.textContent;return!!(i===n||n.split(":")&&n.split(":")[1]===i)}),o=g("TileMatrixSetLimits",t),s=o&&h("TileMatrixLimits",o),a=new Map;if(s?.length)for(const c of s){const e=g("TileMatrix",c).textContent,t=+g("MinTileRow",c).textContent,i=+g("MaxTileRow",c).textContent,n=+g("MinTileCol",c).textContent,l=+g("MaxTileCol",c).textContent;a.set(e,{minCol:n,maxCol:l,minRow:t,maxRow:i})}const u=M("SupportedCRS",r).toLowerCase(),p=v(r,u),f=p.spatialReference,d=g("TileMatrix",r),m=[parseInt(M("TileWidth",d),10),parseInt(M("TileHeight",d),10)],x=[];if(l.length)l.forEach((e,t)=>{const i=C("TileMatrix","Identifier",e,r);x.push(O(i,u,t,n,a))});else{h("TileMatrix",r).forEach((e,t)=>{x.push(O(e,u,t,n,a))})}const T=U(e,r,p,m,x[0]).toJSON(),y=new c({dpi:96,spatialReference:f,size:m,origin:p,lods:x}).toJSON();return{id:n,fullExtent:T,tileInfo:y}}function N(e){e=e.toLowerCase();let t=parseInt(e.split(":").pop(),10);900913!==t&&3857!==t||(t=102100);const i=D(e);return null!=i&&(t=i),{wkid:t}}function v(e,t){return F(g("TileMatrix",e),t)}function F(e,t){const i=N(t),[n,r]=M("TopLeftCorner",e).split(" ").map(e=>parseFloat(e)),s=t.includes("epsg")&&o(i.wkid);return new l(s?{x:r,y:n,spatialReference:i}:{x:n,y:r,spatialReference:i})}function U(e,t,i,l,r){const o=g("BoundingBox",t);let s,a,c,u,p,f;if(o&&(s=M("LowerCorner",o).split(" "),a=M("UpperCorner",o).split(" ")),s&&s.length>1&&a&&a.length>1)c=parseFloat(s[0]),p=parseFloat(s[1]),u=parseFloat(a[0]),f=parseFloat(a[1]);else{const e=g("TileMatrix",t),n=parseInt(M("MatrixWidth",e),10),o=parseInt(M("MatrixHeight",e),10);c=i.x,f=i.y,u=c+n*l[0]*r.resolution,p=f-o*l[1]*r.resolution}return k(e,i.spatialReference,i)?new n(p,c,f,u,i.spatialReference):new n(c,p,u,f,i.spatialReference)}function k(e,t,i){return"1.0.0"===e&&o(t.wkid)&&!(i.spatialReference.isGeographic&&i.x<-90&&i.y>=-90)}function D(e){return e.includes("crs84")||e.includes("crs:84")?4326:e.includes("crs83")||e.includes("crs:83")?4269:e.includes("crs27")||e.includes("crs:27")?4267:null}function O(e,t,i,n,l){const r=N(t),o=M("Identifier",e);let s=parseFloat(M("ScaleDenominator",e));const c=j(r.wkid,s,n);s*=96/u;const p=+M("MatrixWidth",e),f=+M("MatrixHeight",e),{maxCol:d=p-1,maxRow:m=f-1,minCol:x=0,minRow:g=0}=l.get(o)??{},{x:h,y:w}=F(e,t);return new a({cols:[x,d],level:i,levelValue:o,origin:[h,w],scale:s,resolution:c,rows:[g,m]})}function j(e,t,n){let l;return l=r.hasOwnProperty(""+e)?r.values[r[e]]:"default028mm"===n?6370997*Math.PI/180:i(e).metersPerDegree,7*t/25e3/l}export{A as getTileUrlFromResourceUrls,S as getTileUrlTemplateFromResourceUrls,R as getTileUrlTemplates,d as parseCapabilities,m as parseResourceInfo,p as parseText,f as validateCapabilities};