UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 5.12 kB
import{getInfo as e}from"../../../geometry/support/spatialReferenceUtils.js";import t from"./LODInfo.js";import o from"./TileCoverage.js";import l from"./TileKey.js";import s from"./TileSpan.js";const i=new l("0/0/0/0");class n{static create(e,t){e[1]>t[1]&&([e,t]=[t,e]);const[o,l]=e,[s,i]=t,r=s-o,a=i-l,h=0!==a?r/a:0,c=(Math.ceil(l)-l)*h,f=(Math.floor(l)-l)*h;return new n(o,Math.floor(l),Math.ceil(i),h,r<0?c:f,r<0?f:c,r<0?s:o,r<0?o:s)}constructor(e,t,o,l,s,i,n,r){this.x=e,this.ymin=t,this.ymax=o,this.invM=l,this.leftAdjust=s,this.rightAdjust=i,this.leftBound=n,this.rightBound=r}incrRow(){this.x+=this.invM}getLeftCol(){return Math.max(this.x+this.leftAdjust,this.leftBound)}getRightCol(){return Math.min(this.x+this.rightAdjust,this.rightBound)}}const r=[[0,0],[0,0],[0,0],[0,0]],a=1e-6;class h{constructor(e,o=null){this.tileInfo=e,this.fullExtent=o,this.scales=[],this._infoByScale={},this._infoByLevel={};const l=e.lods.slice();l.sort(((e,t)=>t.scale-e.scale));const s=this._lodInfos=l.map((l=>t.create(e,l,o)));l.forEach(((e,t)=>{this._infoByLevel[e.level]=s[t],this._infoByScale[e.scale]=s[t],this.scales[t]=e.scale}),this),this._wrap=e.isWrappable}get spatialReference(){return this.tileInfo.spatialReference}getLODInfoAt(e){return this._infoByLevel["number"==typeof e?e:e.level]}getTileBounds(e,t,o=!1){i.set(t);const l=this._infoByLevel[i.level];return l?l.getTileBounds(e,i,o):e}getTileCoords(e,t,o=!1){i.set(t);const l=this._infoByLevel[i.level];return l?l.getTileCoords(e,i,o):e}getTileCoverage(e,t=192,l="closest"){const i="closest"===l?this.getClosestInfoForScale(e.scale):this.getSmallestInfoForScale(e.scale),a=o.pool.acquire(i),h=this._wrap;let c,f,u,m=1/0,g=-1/0;const d=a.spans;r[0][0]=r[0][1]=r[1][1]=r[3][0]=-t,r[1][0]=r[2][0]=e.size[0]+t,r[2][1]=r[3][1]=e.size[1]+t;for(const o of r)e.toMap(o,o),o[0]=i.getColumnForX(o[0]),o[1]=i.getRowForY(o[1]);const y=[];let v=3;for(let o=0;o<4;o++){if(r[o][1]===r[v][1]){v=o;continue}const e=n.create(r[o],r[v]);m=Math.min(e.ymin,m),g=Math.max(e.ymax,g),void 0===y[e.ymin]&&(y[e.ymin]=[]),y[e.ymin].push(e),v=o}if(null==m||null==g||g-m>100)return null;let _=[];for(c=m;c<g;){null!=y[c]&&(_=_.concat(y[c])),f=1/0,u=-1/0;for(let e=_.length-1;e>=0;e--){const t=_[e];f=Math.min(f,t.getLeftCol()),u=Math.max(u,t.getRightCol())}if(f=Math.floor(f),u=Math.floor(u),c>=i.first[1]&&c<=i.last[1])if(h)if(i.size[0]<i.worldSize[0]){const e=Math.floor(u/i.worldSize[0]);for(let t=Math.floor(f/i.worldSize[0]);t<=e;t++)d.push(new s(c,Math.max(i.getFirstColumnForWorld(t),f),Math.min(i.getLastColumnForWorld(t),u)))}else d.push(new s(c,f,u));else f>i.last[0]||u<i.first[0]||(f=Math.max(f,i.first[0]),u=Math.min(u,i.last[0]),d.push(new s(c,f,u)));c+=1;for(let e=_.length-1;e>=0;e--){const t=_[e];t.ymax>=c?t.incrRow():_.splice(e,1)}}return a}getTileParentId(e){i.set(e);const t=this._infoByLevel[i.level],o=this._lodInfos.indexOf(t)-1;return o<0?null:(this._getTileIdAtLOD(i,this._lodInfos[o],i),i.id)}getTileResolution(e){const t=this._infoByLevel["object"==typeof e?e.level:e];return t?t.resolution:-1}getTileScale(e){const t=this._infoByLevel[e.level];return t?t.scale:-1}intersects(e,t){i.set(t);const o=this._infoByLevel[i.level],l=e.lodInfo;if(l.resolution>o.resolution){this._getTileIdAtLOD(i,l,i);const t=l.denormalizeCol(i.col,i.world);for(const o of e.spans)if(o.row===i.row&&o.colFrom<=t&&o.colTo>=t)return!0}if(l.resolution<o.resolution){const[t,s,n,r]=e.spans.reduce(((e,t)=>(e[0]=Math.min(e[0],t.row),e[1]=Math.max(e[1],t.row),e[2]=Math.min(e[2],t.colFrom),e[3]=Math.max(e[3],t.colTo),e)),[1/0,-1/0,1/0,-1/0]),a=o.denormalizeCol(i.col,i.world),h=l.getColumnForX(o.getXForColumn(a)),c=l.getRowForY(o.getYForRow(i.row)),f=l.getColumnForX(o.getXForColumn(a+1))-1,u=l.getRowForY(o.getYForRow(i.row+1))-1;return!(h>r||f<n||c>s||u<t)}const s=l.denormalizeCol(i.col,i.world);return e.spans.some((e=>e.row===i.row&&e.colFrom<=s&&e.colTo>=s))}normalizeBounds(t,o,l){if(t[0]=o[0],t[1]=o[1],t[2]=o[2],t[3]=o[3],this._wrap){const o=e(this.tileInfo.spatialReference),s=-l*(o.valid[1]-o.valid[0]);t[0]+=s,t[2]+=s}return t}getSmallestInfoForScale(e){const t=this.scales;if(this._infoByScale[e])return this._infoByScale[e];if(e>t[0])return this._infoByScale[t[0]];for(let o=1;o<t.length-1;o++)if(e>t[o]+a)return this._infoByScale[t[o-1]];return this._infoByScale[t[t.length-1]]}getClosestInfoForScale(e){const t=this.scales;return this._infoByScale[e]||(e=t.reduce(((t,o)=>Math.abs(o-e)<Math.abs(t-e)?o:t),t[0])),this._infoByScale[e]}scaleToLevel(e){const t=this.scales;if(this._infoByScale[e])return this._infoByScale[e].level;for(let o=t.length-1;o>=0;o--)if(e<t[o]){if(o===t.length-1)return this._infoByScale[t[t.length-1]].level;return this._infoByScale[t[o]].level+(t[o]-e)/(t[o]-t[o+1])}return this._infoByScale[t[0]].level}scaleToZoom(e){return this.tileInfo.scaleToZoom(e)}_getTileIdAtLOD(e,t,o){const l=this._infoByLevel[o.level];return e.set(o),t.resolution<l.resolution?null:(t.resolution===l.resolution||(e.level=t.level,e.col=Math.floor(o.col*l.resolution/t.resolution+.01),e.row=Math.floor(o.row*l.resolution/t.resolution+.01)),e)}}export{h as default};