@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 3.77 kB
JavaScript
import{isNone as e,isSome as s}from"../../../../../core/maybe.js";import{GridIndex as o,tileCoordChange as t}from"./util.js";const l=32,i=8,n=64;class r{constructor(e,s,o){this.tileCoordRange=e,this._visibleTiles=s,this._createUnique=o,this._tiles=new Map,this._uniqueSymbolsReferences=new Map}get uniqueSymbols(){return e(this._uniqueSymbolLayerArray)&&(this._uniqueSymbolLayerArray=this._createUniqueSymbolLayerArray()),this._uniqueSymbolLayerArray}add(e,s){this._uniqueSymbolLayerArray=null;let t=this._tiles.get(e.id);t||(t={symbols:new Map},this._tiles.set(e.id,t));const r=new Map;if(s)for(const o of s)t.symbols.has(o)&&(r.set(o,t.symbols.get(o)),t.symbols.delete(o));else for(const[o,l]of e.layerData)t.symbols.has(o)&&(r.set(o,t.symbols.get(o)),t.symbols.delete(o));this._removeSymbols(r);const y=e.symbols,a=new Map;for(const[f,c]of y){let e=c.length;if(e>=l){let s=this.tileCoordRange;do{s/=2,e/=4}while(e>i&&s>n);const l=new o(this.tileCoordRange,this.tileCoordRange,s);a.set(f,{flat:c,index:l}),t.symbols.set(f,{flat:c,index:l});for(const e of c)l.getCell(e.xTile,e.yTile).push(e)}else a.set(f,{flat:c}),t.symbols.set(f,{flat:c})}this._addSymbols(e.key,y)}deleteStyleLayers(e){this._uniqueSymbolLayerArray=null;for(const[s,o]of this._tiles){const t=new Map;for(const s of e)o.symbols.has(s)&&(t.set(s,o.symbols.get(s)),o.symbols.delete(s));this._removeSymbols(t),0===o.symbols.size&&this._tiles.delete(s)}}removeTile(e){this._uniqueSymbolLayerArray=null;const s=this._tiles.get(e.id);if(!s)return;const o=new Map;for(const[t,l]of e.symbols)s.symbols.has(t)&&(o.set(t,s.symbols.get(t)),s.symbols.delete(t));this._removeSymbols(o),0===s.symbols.size&&this._tiles.delete(e.id)}_removeSymbols(e){for(const[s,{flat:o}]of e)for(const e of o){const o=e.unique,t=o.tileSymbols,l=t.length-1;for(let s=0;s<l;s++)if(t[s]===e){t[s]=t[l];break}if(t.length=l,0===l){const e=this._uniqueSymbolsReferences.get(s);e.delete(o),0===e.size&&this._uniqueSymbolsReferences.delete(s)}e.unique=null}}_addSymbols(s,o){if(0===o.size)return;const t=this._visibleTiles;for(const e of t)e.parentTile||e.key.world!==s.world||e.key.level===s.level&&!e.key.equals(s)||this._matchSymbols(e,s,o);for(const[l,i]of o)for(const s of i)if(e(s.unique)){const e=this._createUnique();s.unique=e,e.tileSymbols.push(s);let o=this._uniqueSymbolsReferences.get(l);o||(o=new Set,this._uniqueSymbolsReferences.set(l,o)),o.add(e)}}_matchSymbols(e,o,l){if(e.key.level>o.level){const s=e.key.level-o.level;if(e.key.row>>s!==o.row||e.key.col>>s!==o.col)return}if(o.level>e.key.level){const s=o.level-e.key.level;if(o.row>>s!==e.key.row||o.col>>s!==e.key.col)return}if(o.equals(e.key)){for(const s of e.childrenTiles)this._matchSymbols(s,o,l);return}const i=new Map;for(const[n,r]of l){const l=[];for(const s of r){const i=t(this.tileCoordRange,s.xTile,o.level,o.col,e.key.level,e.key.col),n=t(this.tileCoordRange,s.yTile,o.level,o.row,e.key.level,e.key.row);i>=0&&i<this.tileCoordRange&&n>=0&&n<this.tileCoordRange&&l.push({symbol:s,xTransformed:i,yTransformed:n})}const y=[],a=e.key.level<o.level?1:1<<e.key.level-o.level,f=this._tiles.get(e.id).symbols.get(n);if(f){const e=f.flat;for(const o of l){let t,l=!1;const i=o.xTransformed,n=o.yTransformed;t=s(f.index)?f.index.getCell(i,n):e;const r=o.symbol,c=r.hash;for(const e of t)if(c===e.hash&&Math.abs(i-e.xTile)<=a&&Math.abs(n-e.yTile)<=a){const s=e.unique;r.unique=s,s.tileSymbols.push(r),l=!0;break}l||y.push(r)}}y.length>0&&i.set(n,y)}for(const s of e.childrenTiles)this._matchSymbols(s,o,i)}_createUniqueSymbolLayerArray(){const e=this._uniqueSymbolsReferences,s=new Array(e.size);let o,t=0;for(const[l,i]of e){const e=new Array(i.size);o=0;for(const s of i)e[o++]=s;s[t]={styleLayerUID:l,uniqueSymbols:e},t++}return s}}export{r as SymbolRepository};