@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 6.05 kB
JavaScript
import{isNone as e,isSome as t}from"../../../../core/maybe.js";import{pt2px as r}from"../../../../core/screenUtils.js";import{r as i}from"../../../../chunks/rbush.js";import{create as s,intersects as o,clone as n}from"../../../../geometry/support/aaBoundingRect.js";import{getBoundsXY as h}from"../../../../geometry/support/boundsUtils.js";import{normalizeMapX as a}from"../../../../geometry/support/normalizeUtils.js";import{getInfo as u}from"../../../../geometry/support/spatialReferenceUtils.js";import{HittestDrawHelper as l}from"../../../../symbols/cim/CIMSymbolDrawHelper.js";import{CIMSymbolInflatedSizeHelper as c}from"../../../../symbols/cim/CIMSymbolHelper.js";import d from"./GraphicStoreItem.js";import{PIXEL_BUFFER as m,graphicGeometryToNumber as p}from"./graphicsUtils.js";const g={minX:0,minY:0,maxX:0,maxY:0},f=s(),_=1e-5;function b(e,t,r,i,s){return g.minX=t,g.minY=r,g.maxX=i,g.maxY=s,e.search(g)}function y(e){return{minX:e.bounds[0],minY:e.bounds[1],maxX:e.bounds[2],maxY:e.bounds[3]}}class I{constructor(e,t,r,s,o,n,h){this._graphics=s,this._onAdd=o,this._onRemove=n,this._hashToCIM=h,this._index=i(9,y),this._itemByGraphic=new Map,this._inflatedSizeHelper=new c,this._tileInfoView=e,this._uidFieldName=r;const a=e.getClosestInfoForScale(t);a&&(this._resolution=this._tileInfoView.getTileResolution(a.level))}setResourceManager(e){this._cimResourceManager=e,this._hittestDrawHelper=new l(e)}hitTest(e,t,r,i,n){e=a(e,this._tileInfoView.spatialReference);const h=.5*i*window.devicePixelRatio*r;f[0]=e-h,f[1]=t-h,f[2]=e+h,f[3]=t+h;const u=.5*i*(r+m),l=b(this._index,e-u,t-u,e+u,t+u);if(!l||0===l.length)return[];const c=[],d=s(),p=s();for(const s of l){const{geometry:e,symbolResource:t}=s;this._getSymbolBounds(d,t,e,p,n),p[3]=p[2]=p[1]=p[0]=0,o(d,f)&&s.graphic?.visible&&c.push(s)}if(0===c.length)return[];const g=this._hittestDrawHelper,_=[];for(const s of c){const{geometry:e,symbolResource:t}=s,{hash:r,textInfo:o}=t,h=this._hashToCIM.get(r);h&&(g.hitTest(f,h.symbol,e,o,n,i)&&_.push(s))}return _.sort(z),_.map((e=>e.graphic))}getGraphicsData(e,t,r){const i=this._searchForItems(t);if(0===i.length||0===r.length)return[];i.sort(((e,t)=>e.zorder-t.zorder)),i[0].insertAfter=-1;for(let l=1;l<i.length;l++)i[l].insertAfter=i[l-1].graphic.uid;i.sort(((e,t)=>e.graphic.uid-t.graphic.uid)),r.sort(((e,t)=>e.uid-t.uid));let s,o=0,n=0;const h=t.resolution,a=[],u={originPosition:"upperLeft",scale:[h,h],translate:[t.bounds[0],t.bounds[3]]};for(const l of r){for(n=-2;o<i.length;)if(s=i[o],o++,l.uid===s.graphic.uid){n=s.insertAfter;break}if(!s?.geometry||-2===n)continue;const r=s.getGeometryQuantized(u,t.bounds,this._tileInfoView.spatialReference,h),c={...s.graphic.attributes};c[this._uidFieldName]=l.uid,null==s.groupId&&(s.groupId=e.createTemplateGroup(s.symbol,null)),a.push({centroid:d.getCentroidQuantized(s,u),geometry:r,attributes:c,symbol:s.symbol,groupId:s.groupId,insertAfter:n,zorder:s.zorder})}return a.sort(((e,t)=>e.zorder-t.zorder)),a}queryTileData(e,t){if(0===this._graphics.length)return[];const{bounds:r,resolution:i}=t,s=this._searchForItems(t),o=[];return 0===s.length||this._createTileGraphics(o,e,s,{originPosition:"upperLeft",scale:[i,i],translate:[r[0],r[3]]},t),o}has(e){return this._itemByGraphic.has(e)}getBounds(e){const t=this._itemByGraphic.get(e);return t?t.bounds:null}getAllBounds(){return Array.from(this._itemByGraphic.values()).filter((e=>e.graphic.visible)).map((e=>e.bounds))}addOrModify(r,i,s){if(!r||e(i))return;this.has(r)&&this.remove(r),this._onAdd(r);const o=[0,0,0,0],n=this._getSymbolBounds(null,i,s,o,0),h=d.acquire(r,i,s,t(n)?n:null,o);return this._itemByGraphic.set(r,h),s&&this._index.insert(h),h.bounds}remove(e){if(!this._itemByGraphic.has(e))return;this._onRemove(e);const t=this._itemByGraphic.get(e);t?.bounds&&this._index.remove(t),this._itemByGraphic.delete(e)}updateZ(){const e=this._graphics.items;let t,r;for(let i=0;i<e.length;i++)r=e[i],t=this._itemByGraphic.get(r),t&&(t.zorder=i)}update(e,r,i){const s=this._itemByGraphic.get(e);s.groupId=null;const o=n(s.bounds);this._index.remove(s);const h=this._getSymbolBounds(s.bounds,r,i,s.size,0);return t(h)&&s.set(e,r,i,h,s.size),i&&this._index.insert(s),{oldBounds:o,newBounds:s.bounds}}updateLevel(e){if(this._resolution===e)return;this._resolution=e,this._index.clear();const r=this._itemByGraphic,i=[];for(const[s,o]of r){const e=this._getSymbolBounds(o.bounds,o.symbolResource,o.geometry,o.size,0);o.geometry&&t(e)&&(o.bounds=e,i.push(o))}this._index.load(i)}clear(){this._itemByGraphic.clear(),this._index.clear()}_createTileGraphics(e,t,r,i,s){const o=this._uidFieldName,n=this._tileInfoView.spatialReference,{bounds:h,resolution:a}=s;let u,l,c,m;r.sort(((e,t)=>e.zorder-t.zorder));for(let p=0;p<r.length;p++){c=r[p],u=c.graphic,l=c.getGeometryQuantized(i,h,n,a),m=0===p?-1:r[p-1].graphic.uid;const s={...c.graphic.attributes};s[o]=u.uid,null==c.groupId&&(c.groupId=t.createTemplateGroup(c.symbol,null)),e.push({centroid:d.getCentroidQuantized(c,i),geometry:l,attributes:s,symbol:c.symbol,groupId:c.groupId,insertAfter:m,zorder:c.zorder})}}_searchForItems(e){const t=this._tileInfoView.spatialReference,r=e.bounds,i=u(t);if(i&&t.isWrappable){const[t,o]=i.valid,n=Math.abs(r[2]-o)<_,h=Math.abs(r[0]-t)<_;if((!n||!h)&&(n||h)){const i=e.resolution;let h;h=s(n?[t,r[1],t+i*m,r[3]]:[o-i*m,r[1],o,r[3]]);const a=b(this._index,r[0],r[1],r[2],r[3]),u=b(this._index,h[0],h[1],h[2],h[3]);return[...new Set([...a,...u])]}}return b(this._index,r[0],r[1],r[2],r[3])}_getSymbolBounds(e,t,i,o,n){if(!t||!t.symbol||!i)return null;if(e||(e=s()),h(e,i),!o||0===o[0]&&0===o[1]&&0===o[2]&&0===o[3]){const{hash:e,textInfo:i}=t,s=this._hashToCIM.get(e);if(!s)return null;o||(o=[0,0,0,0]);const h=this._inflatedSizeHelper.getSymbolInflateSize(o,s.symbol,this._cimResourceManager,n,i);o[0]=r(h[0]),o[1]=r(h[1]),o[2]=r(h[2]),o[3]=r(h[3])}const a=this._resolution,u=c.safeSize(o);return e[0]-=u*a,e[1]-=u*a,e[2]+=u*a,e[3]+=u*a,e}}const z=(e,t)=>{const r=p(e.graphic),i=p(t.graphic);return r===i?t.zorder-e.zorder:r-i};export{I as default};