UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) 4.63 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import e from"../../../../Graphic.js";import{getOrCreateMapValue as t}from"../../../../core/MapUtils.js";import s from"../../../../core/pbf.js";import{PooledRBush as r}from"../../../../core/libs/rbush/PooledRBush.js";import i from"../../../../graphic/VectorTileGraphicOrigin.js";import{tileSizeInPixels as o,tileSizeInTileUnits as l}from"./constants.js";import n from"./Feature.js";import{getTileMargins as a}from"./GeometryUtils.js";import y from"./SourceLayerData.js";import{IndexItem as c}from"./style/StyleLayer.js";const u=(e,t)=>{const s=e.vtlSymbol.sourceTile,r=t.vtlSymbol.sourceTile;return s.level!==r.level?s.level-r.level:s.row!==r.row?s.row-r.row:s.col!==r.col?s.col-r.col:e.styleLayerUID-t.styleLayerUID};class h{constructor(e,t,s,r,i){this.tileKey=e,this._tileLayerData=t,this._styleRepository=s,this._tileHandler=r,this._parentLayer=i,this._index=null,this._tileKeyToPBF=new Map}static create(e,t,s,r,i){return new h(e,t,s,r,i)}clear(){this._index?.clear(),this._tileKeyToPBF.clear()}async queryAttributes(e,t,s,i,o){if(0===this._tileLayerData.size||!this._styleRepository||!this._tileHandler)return[];null===this._index&&(this._index=new r(100,m),await this._indexLayers());const l=[];return this._queryIndex(l,e,t,s,this.tileKey.level,i),o&&o?.length>0&&await this._getSymbolsAttributes(l,o),l}async _indexLayers(){const e=this.tileKey,t=this._styleRepository.layers,s=await this._getTilePayload(e);for(const[r,i]of this._tileLayerData){const o=t[r],l=s.find(e=>e.sourceName===o.source);if(!l)continue;const{protobuff:n,key:a}=l;if(3!==i.type){const t=1<<e.level-a.level,s=e.row-a.row*t,r=e.col-a.col*t;this._indexLayer(o,n,e.level,t,s,r)}}}_indexLayer(e,t,r,i,u,h){const m=e.sourceLayer,f=e.getFeatureFilter(),d=r,_=r+1,p=a(d),g=new s(new Uint8Array(t),new DataView(t));for(;g.next();)switch(g.tag()){case 3:{const t=g.getMessage(),s=new y(t);if(t.release(),s.name!==m)continue;const a=s.getData(),w=s.extent/i,b=w*h-p,x=w*u-p,L=b+w+2*p,v=x+w+2*p,I=w/o,T=l/w,D=w*h,S=w*u;for(;a.nextTag(2);){const t=a.getMessage(),i=new n(t,s);if(t.release(),f&&!f.filter(i,r))continue;const o=i.values||{},l=o._minzoom,y=o._maxzoom;if(l&&l>=10*_||y&&y<=10*d)continue;const u=e.getFeatureInflatedBounds(i,d,s.extent,I);null==u||u[0]>L||u[1]>v||u[2]<b||u[3]<x||(u[0]=(u[0]-D)*T,u[1]=(u[1]-S)*T,u[2]=(u[2]-D)*T,u[3]=(u[3]-S)*T,this._index.insert(new c(e,i,u,T,D,S)))}break}default:g.skip()}g.release()}async _getSymbolsAttributes(e,t){if(!t||0===t.length)return e;const s=[];if(t.sort(u),t.length>0){let e=0,{styleLayerUID:r}=t[0];for(let o=1;o<t.length;o++){const{styleLayerUID:i}=t[o];i!==r&&(s.push({from:e,to:o,styleLayerUID:r,sourceTileKey:t[o-1].vtlSymbol.sourceTile}),e=o,r=i)}const i=t.length-1;s.push({from:e,to:t.length,styleLayerUID:r,sourceTileKey:t[i].vtlSymbol.sourceTile})}const r=this._styleRepository.layers;for(const i of s){const s=await this._getTilePayload(i.sourceTileKey),o=r[i.styleLayerUID],l=!!o&&s.find(e=>e.sourceName===o.source);l&&this._addSymbolsAttributes(e,t.slice(i.from,i.to).map(e=>e.vtlSymbol.featureIndex),i.styleLayerUID,l)}return e}_addSymbolsAttributes(t,s,r,o){const l=this._styleRepository.layers,n=o.key,a=this.tileKey,y=1<<a.level-n.level,c=a.row-n.row*y,u=a.col-n.col*y;this._getSymbolAttributes(o.protobuff,s,r,y,c,u).forEach(s=>{const{attributes:o,tilePoint:n}=s;t.push({layerId:l[r].id,layerIndex:r,graphic:new e({attributes:o,origin:new i(this._parentLayer,l[r].id,r)}),tilePoint:n})})}_getSymbolAttributes(e,t,r,i,o,a){const c=[],u=this._styleRepository.layers;let h=0;t.sort((e,t)=>e-t);const m=new s(new Uint8Array(e),new DataView(e));for(;m.next();)switch(m.tag()){case 3:{const e=m.getMessage(),s=new y(e);if(e.release(),s.name!==u[r].sourceLayer)continue;const f=s.getData(),d=s.extent/i,_=l/d,p=d*a,g=d*o;let w=0;for(;f.nextTag(2);){const e=f.getMessage();if(w++===t[h]){const t=new n(e,s),r=t.values,i=t.getGeometry(),o=null!=i?[_*(i[0][0].x-p),_*(i[0][0].y-g)]:null;c.push({attributes:r,tilePoint:o}),h++}if(e.release(),h===t.length)return c}break}default:m.skip()}return m.release(),c}_queryIndex(t,s,r,o,l,n){return this._index?.search({minX:s-o,minY:r-o,maxX:s+o,maxY:r+o},a=>{const{layer:y,feature:c}=a;y.isIntersectingFeature(s,r,o,c,l,n,a)&&t.push({layerId:y.id,layerIndex:y.uid,tilePoint:null,graphic:new e({attributes:c.values,origin:new i(this._parentLayer,a.layer.id,a.layer.uid)})})}),t}async _getTilePayload(e){return t(this._tileKeyToPBF,e.id,()=>this._tileHandler.fetchTilePBFs(e)).then(e=>e)}}const m=e=>({minX:e.bounds[0],minY:e.bounds[1],maxX:e.bounds[2],maxY:e.bounds[3]});export{h as default};