UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 8.31 kB
import{_ as e}from"../../../chunks/tslib.es6.js";import t from"../../../Graphic.js";import r from"../../../core/Accessor.js";import s from"../../../core/Collection.js";import i from"../../../core/Error.js";import has from"../../../core/has.js";import{getOrCreateMapValue as o}from"../../../core/MapUtils.js";import{isSome as a,isNone as n}from"../../../core/maybe.js";import{debounce as l,eachAlways as p}from"../../../core/promiseUtils.js";import{on as c}from"../../../core/reactiveUtils.js";import{getMetersPerUnitForSR as u}from"../../../core/unitUtils.js";import{property as h}from"../../../core/accessorSupport/decorators/property.js";import"../../../core/accessorSupport/ensureType.js";import"../../../core/arrayUtils.js";import{subclass as y}from"../../../core/accessorSupport/decorators/subclass.js";import m from"../../../geometry/Extent.js";import{getResolutionForScale as f}from"../../../geometry/support/scaleUtils.js";import{getLayerFloorFilterClause as d}from"../../../layers/support/floorFilterUtils.js";import{calculateTolerance as g}from"../../../renderers/support/clickToleranceUtils.js";import{identify as w}from"../../../rest/identify.js";import b from"../../../rest/support/IdentifyParameters.js";import{loadArcade as v}from"../../../support/arcadeOnDemand.js";import j from"../../../symbols/SimpleMarkerSymbol.js";import{getRequiredFields as x,getFetchPopupTemplate as F}from"./popupUtils.js";let G=null;function P(e,t){return"tile"===t.type||"map-image"===t.type}let S=class extends r{constructor(e){super(e),this._featuresResolutions=new WeakMap,this.highlightGraphics=null,this.highlightGraphicUpdated=null,this.updateHighlightedFeatures=l((async e=>{this.destroyed||this.updatingHandles.addPromise(this._updateHighlightedFeaturesGeometries(e).catch((()=>{})))}))}initialize(){const e=e=>{this.updatingHandles.addPromise(this._updateHighlightedFeaturesSymbols(e).catch((()=>{}))),this.updateHighlightedFeatures(this._highlightGeometriesResolution)};this.addHandles([c((()=>this.highlightGraphics),"change",(t=>e(t.added)),{onListenerAdd:t=>e(t)})])}async fetchPopupFeatures(e,t){const{layerView:{layer:r,view:{scale:s}}}=this;if(!e)throw new i("fetchPopupFeatures:invalid-area","Nothing to fetch without area",{layer:r});const o=_(r.sublayers,s,t);if(!o.length)return[];const a=await R(r,o);if(!((r.capabilities?.operations?.supportsIdentify??!0)&&r.version>=10.5)&&!a)throw new i("fetchPopupFeatures:not-supported","query operation is disabled for this service",{layer:r});return a?this._fetchPopupFeaturesUsingQueries(e,o,t):this._fetchPopupFeaturesUsingIdentify(e,o,t)}clearHighlights(){this.highlightGraphics?.removeAll()}highlight(e){const r=this.highlightGraphics;if(!r)return{remove(){}};let i=null;if(e instanceof t?i=[e]:s.isCollection(e)&&e.length>0?i=e.toArray():Array.isArray(e)&&e.length>0&&(i=e),i=i?.filter(a),!i||!i.length)return{remove:()=>{}};for(const t of i){const e=t.sourceLayer;null!=e&&"geometryType"in e&&"point"===e.geometryType&&(t.visible=!1)}return r.addMany(i),{remove:()=>{r.removeMany(i??[])}}}async _updateHighlightedFeaturesSymbols(e){const{layerView:{view:t},highlightGraphics:r,highlightGraphicUpdated:s}=this;if(r&&s)for(const i of e){const e=i.sourceLayer&&"renderer"in i.sourceLayer&&i.sourceLayer.renderer;i.sourceLayer&&"geometryType"in i.sourceLayer&&"point"===i.sourceLayer.geometryType&&e&&"getSymbolAsync"in e&&e.getSymbolAsync(i).then((async o=>{o||(o=new j);let a=null;const n="visualVariables"in e?e.visualVariables?.find((e=>"size"===e.type)):void 0;n&&(G||(G=(await import("../../../renderers/visualVariables/support/visualVariableUtils.js")).getSize),a=G(n,i,{view:t.type,scale:t.scale,shape:"simple-marker"===o.type?o.style:null})),a||(a="width"in o&&"height"in o&&null!=o.width&&null!=o.height?Math.max(o.width,o.height):"size"in o?o.size:16),r.includes(i)&&(i.symbol=new j({style:"square",size:a,xoffset:"xoffset"in o?o.xoffset:0,yoffset:"yoffset"in o?o.yoffset:0}),s(i,"symbol"),i.visible=!0)}))}}async _updateHighlightedFeaturesGeometries(e){const{layerView:{layer:t,view:r},highlightGraphics:s,highlightGraphicUpdated:i}=this;if(this._highlightGeometriesResolution=e,!i||!s?.length||!t.capabilities.operations.supportsQuery)return;const a=this._getTargetResolution(e),n=new Map;for(const c of s)if(!this._featuresResolutions.has(c)||this._featuresResolutions.get(c)>a){const e=c.sourceLayer;o(n,e,(()=>new Map)).set(c.getObjectId(),c)}const l=Array.from(n,(([e,t])=>{const s=e.createQuery();return s.objectIds=[...t.keys()],s.outFields=[e.objectIdField],s.returnGeometry=!0,s.maxAllowableOffset=a,s.outSpatialReference=r.spatialReference,e.queryFeatures(s)})),p=await Promise.all(l);if(!this.destroyed)for(const{features:o}of p)for(const e of o){const t=e.sourceLayer,r=n.get(t).get(e.getObjectId());r&&s.includes(r)&&(r.geometry=e.geometry,i(r,"geometry"),this._featuresResolutions.set(r,a))}}_getTargetResolution(e){const t=e*u(this.layerView.view.spatialReference),r=t/16;return r<=10?0:e/t*r}async _fetchPopupFeaturesUsingIdentify(e,t,r){const s=await this._createIdentifyParameters(e,t,r);if(n(s))return[];const{results:i}=await w(this.layerView.layer.parsedUrl,s);return i.map((e=>e.feature))}async _createIdentifyParameters(e,t,r){const{floors:s,layer:i,timeExtent:o,view:{spatialReference:n,scale:l}}=this.layerView,p=a(r)?r.event:null;if(!t.length)return null;await Promise.all(t.map((({sublayer:e})=>e.load().catch((()=>{})))));const c=Math.min(has("mapservice-popup-identify-max-tolerance"),i.allSublayers.reduce(((e,t)=>t.renderer?g({renderer:t.renderer,event:p}):e),2)),u=this.createFetchPopupFeaturesQueryGeometry(e,c),h=f(l,n),y=Math.round(u.width/h),d=new m({xmin:u.center.x-h*y,ymin:u.center.y-h*y,xmax:u.center.x+h*y,ymax:u.center.y+h*y,spatialReference:u.spatialReference});return new b({floors:s,gdbVersion:"gdbVersion"in i?i.gdbVersion:void 0,geometry:e,height:y,layerOption:"popup",mapExtent:d,returnGeometry:!0,spatialReference:n,sublayers:i.sublayers,timeExtent:o,tolerance:c,width:y})}async _fetchPopupFeaturesUsingQueries(e,t,r){const{layerView:{floors:s,timeExtent:i}}=this,o=a(r)?r.event:null,n=t.map((async({sublayer:t,popupTemplate:r})=>{if(await t.load().catch((()=>{})),t.capabilities&&!t.capabilities.operations.supportsQuery)return[];const n=t.createQuery(),l=g({renderer:t.renderer,event:o}),p=this.createFetchPopupFeaturesQueryGeometry(e,l);if(n.geometry=p,n.outFields=await x(t,r),n.timeExtent=i,s){const e=s.clone(),r=d(e,t);a(r)&&(n.where=n.where?`(${n.where}) AND (${r})`:r)}const c=this._getTargetResolution(p.width/l),u=await U(r),h="point"===t.geometryType||u&&u.arcadeUtils.hasGeometryOperations(r);h||(n.maxAllowableOffset=c);let{features:y}=await t.queryFeatures(n);const m=h?0:c;y=await A(t,y);for(const e of y)this._featuresResolutions.set(e,m);return y}));return(await p(n)).reverse().reduce(((e,t)=>t.value?[...e,...t.value]:e),[]).filter((e=>null!=e))}};function _(e,t,r){const s=[],i=e=>{const o=0===e.minScale||t<=e.minScale,n=0===e.maxScale||t>=e.maxScale;if(e.visible&&o&&n)if(e.sublayers)e.sublayers.forEach(i);else if(e.popupEnabled){const t=F(e,{...r,defaultPopupTemplateEnabled:!1});a(t)&&s.unshift({sublayer:e,popupTemplate:t})}};return(e?.toArray()??[]).reverse().map(i),s}function U(e){return e.expressionInfos?.length||Array.isArray(e.content)&&e.content.some((e=>"expression"===e.type))?v():Promise.resolve()}async function R(e,t){if(e.capabilities?.operations?.supportsQuery)return!0;try{return await Promise.any(t.map((({sublayer:e})=>e.load().then((()=>e.capabilities.operations.supportsQuery)))))}catch{return!1}}async function A(e,t){const r=e.renderer;return r&&"defaultSymbol"in r&&!r.defaultSymbol&&(t=r.valueExpression?await Promise.all(t.map((e=>r.getSymbolAsync(e).then((t=>t?e:null))))).then((e=>e.filter((e=>null!=e)))):t.filter((e=>null!=r.getSymbol(e)))),t}e([h({constructOnly:!0})],S.prototype,"createFetchPopupFeaturesQueryGeometry",void 0),e([h({constructOnly:!0})],S.prototype,"layerView",void 0),e([h({constructOnly:!0})],S.prototype,"highlightGraphics",void 0),e([h({constructOnly:!0})],S.prototype,"highlightGraphicUpdated",void 0),e([h({constructOnly:!0})],S.prototype,"updatingHandles",void 0),S=e([y("esri.views.layers.support.MapService")],S);export{S as MapServiceLayerViewHelper,_ as collectPopupProviders,P as isMapServiceLayerView};