UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 6.6 kB
import"../../../geometry.js";import e from"../../../core/Error.js";import{deg2rad as i,floatEqualAbsolute as t,floatEqualRelative as s}from"../../../core/mathUtils.js";import{isSome as r,isNone as n,unwrap as o}from"../../../core/maybe.js";import{getMetersPerUnitForSR as l}from"../../../core/unitUtils.js";import a from"../../../geometry/Extent.js";import{canProjectToWGS84ComparableLonLat as h}from"../../../geometry/projection.js";import c from"../../../geometry/SpatialReference.js";import{create as m}from"../../../geometry/support/aaBoundingRect.js";import{isMars as p,isMoon as u}from"../../../geometry/support/spatialReferenceUtils.js";import{x2lon as g,y2lat as f}from"../../../geometry/support/webMercatorUtils.js";import x from"../../../layers/support/LOD.js";import v from"../../../layers/support/TileInfo.js";import w from"../../../geometry/Point.js";const M=12;class d{constructor(e){const i=d.checkUnsupported(e);if(r(i))throw i;const t=o(e);this.spatialReference=t.spatialReference,this._isWebMercator=this.spatialReference.isWebMercator,this._isGCS=h(this.spatialReference)||p(this.spatialReference)||u(this.spatialReference),this.origin=[t.origin.x,t.origin.y],this.pixelSize=t.size[0],this.dpi=t.dpi;const s=t.lods.reduce(((e,i,t)=>(i.level<e.min&&(e.min=i.level,e.minIndex=t),e.max=Math.max(e.max,i.level),e)),{min:1/0,minIndex:0,max:-1/0}),n=t.lods[s.minIndex],l=2**n.level;let a=n.resolution*l,c=n.scale*l;this.levels=new Array(s.max+1);for(let r=0;r<this.levels.length;r++)this.levels[r]={resolution:a,scale:c,tileSize:[a*t.size[0],a*t.size[1]]},a/=2,c/=2}clone(){return new d(this.toTileInfo())}toTileInfo(){return new v({dpi:this.dpi,origin:new w({x:this.origin[0],y:this.origin[1],spatialReference:this.spatialReference}),size:[this.pixelSize,this.pixelSize],spatialReference:this.spatialReference,lods:this.levels.map(((e,i)=>new x({level:i,scale:e.scale,resolution:e.resolution})))})}getExtent(e,i,t,s){const r=this.levels[e],n=r.tileSize[0],o=r.tileSize[1];s[0]=this.origin[0]+t*n,s[2]=this.origin[0]+(t+1)*n,s[3]=this.origin[1]-i*o,s[1]=this.origin[1]-(i+1)*o}convertExtentToRadians(e,t){this._isWebMercator?(t[0]=g(e[0]),t[1]=f(e[1]),t[2]=g(e[2]),t[3]=f(e[3])):this._isGCS&&(t[0]=i(e[0]),t[1]=i(e[1]),t[2]=i(e[2]),t[3]=i(e[3]))}getExtentGeometry(e,i,t,s=new a){return this.getExtent(e,i,t,z),s.spatialReference=this.spatialReference,s.xmin=z[0],s.ymin=z[1],s.xmax=z[2],s.ymax=z[3],s.zmin=void 0,s.zmax=void 0,s}ensureMaxLod(e){if(null==e)return!1;let i=!1;for(;this.levels.length<=e;){const e=this.levels[this.levels.length-1],t=e.resolution/2;this.levels.push({resolution:t,scale:e.scale/2,tileSize:[t*this.pixelSize,t*this.pixelSize]}),i=!0}return i}capMaxLod(e){this.levels.length>e+1&&(this.levels.length=e+1)}getMaxLod(){return this.levels.length-1}scaleAtLevel(e){return this.levels[0].scale/2**e}levelAtScale(e){const i=this.levels[0].scale;return e>=i?0:Math.log(i/e)*Math.LOG2E}resolutionAtLevel(e){return this.levels[0].resolution/2**e}compatibleWith(e){if(!(e instanceof d)){if(d.checkUnsupported(e))return!1;e=new d(e)}if(!e.spatialReference.equals(this.spatialReference))return!1;if(e.pixelSize!==this.pixelSize)return!1;const i=Math.min(this.levels.length,e.levels.length)-1,s=this.levels[i].resolution;let r=.5*s;if(!t(e.origin[0],this.origin[0],r)||!t(e.origin[1],this.origin[1],r))return!1;return r=.5*s/2**i/this.pixelSize*M,t(s,e.levels[i].resolution,r)}rootTilesInExtent(e,i=null,t=1/0){const s=new Array,o=this.levels[0].tileSize;if(n(e)||0===o[0]||0===o[1])return s;d.computeRowColExtent(e,o,this.origin,z);let l=z[1],a=z[3],h=z[0],c=z[2];const m=c-h,p=a-l;if(m*p>t){const e=Math.floor(Math.sqrt(t));p>e&&(l=l+Math.floor(.5*p)-Math.floor(.5*e),a=l+e),m>e&&(h=h+Math.floor(.5*m)-Math.floor(.5*e),c=h+e)}for(let r=l;r<a;r++)for(let e=h;e<c;e++)s.push([0,r,e]);return r(i)&&(i[0]=this.origin[0]+h*o[0],i[1]=this.origin[1]-a*o[1],i[2]=this.origin[0]+c*o[0],i[3]=this.origin[1]-l*o[1]),s}static computeRowColExtent(e,i,t,s){const r=.001*(e[2]-e[0]+(e[3]-e[1]));s[0]=Math.max(0,Math.floor((e[0]+r-t[0])/i[0])),s[2]=Math.max(0,Math.ceil((e[2]-r-t[0])/i[0])),s[1]=Math.max(0,Math.floor((t[1]-e[3]+r)/i[1])),s[3]=Math.max(0,Math.ceil((t[1]-e[1]-r)/i[1]))}static isPowerOfTwo(e){const i=e.lods,t=i[0].resolution*2**i[0].level;return!i.some((e=>!s(e.resolution,t/2**e.level)))}static hasGapInLevels(e){const i=e.lods.map((e=>e.level));i.sort(((e,i)=>e-i));for(let t=1;t<i.length;t++)if(i[t]!==i[0]+t)return!0;return!1}static tileSizeSupported(e){const i=e.size[1];return i===e.size[0]&&0==(i&i-1)&&i>=128&&i<=512}static hasOriginPerLODs(e){const i=e.origin;return e.lods.some((e=>null!=e.origin&&(e.origin[0]!==i.x||e.origin[1]!==i.y)))}static getMissingTileInfoError(){return new e("tilingscheme:tile-info-missing","Tiling scheme must have tiling information")}static checkUnsupported(i){return n(i)?S():i.lods.length<1?new e("tilingscheme:generic","Tiling scheme must have at least one level"):d.isPowerOfTwo(i)?d.hasGapInLevels(i)?new e("tilingscheme:gaps","Tiling scheme levels must not have gaps between min and max level"):d.tileSizeSupported(i)?d.hasOriginPerLODs(i)?new e("tilingscheme:multiple-origin","Tiling scheme levels must not have their own origin"):null:new e("tilingscheme:tile-size","Tiles must be square and size must be one of [128, 256, 512]"):new e("tilingscheme:power-of-two","Tiling scheme must be power of two")}static fromExtent(e,i){const t=e[2]-e[0],s=e[3]-e[1],r=l(i),n=1.2*Math.max(t,s),o=256,a=96,h=.0254,c=new d(new v({size:[o,o],origin:new w({x:e[0]-.5*(n-t),y:e[3]+.5*(n-s)}),lods:[new x({level:0,resolution:n/o,scale:1/(o/a*h/(n*r))})],spatialReference:i}));return c.ensureMaxLod(20),c}static makeWebMercatorAuxiliarySphere(e){const i=new d(d.WebMercatorAuxiliarySphereTileInfo);return i.ensureMaxLod(e),i}static makeGCSWithTileSize(e,i=256,t=16){const s=256/i,r=new d(new v({size:[i,i],origin:new w({x:-180,y:90,spatialReference:e}),spatialReference:e,lods:[new x({level:0,resolution:.703125*s,scale:295497598.570834*s})]}));return r.ensureMaxLod(t),r}get test(){return{isWebMercator:this._isWebMercator,isGCS:this._isGCS}}}function S(){return new e("tilingscheme:tile-info-missing","Tiling scheme must have tiling information")}d.WebMercatorAuxiliarySphereTileInfo=new v({size:[256,256],origin:new w({x:-20037508.342787,y:20037508.342787,spatialReference:c.WebMercator}),spatialReference:c.WebMercator,lods:[new x({level:0,resolution:156543.03392800014,scale:591657527.591555})]}),d.WebMercatorAuxiliarySphere=d.makeWebMercatorAuxiliarySphere(19);const z=m();export{d as TilingScheme,S as getMissingTileInfoError};