@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 15.1 kB
JavaScript
import{_ as e}from"../../chunks/tslib.es6.js";import t from"../../core/Collection.js";import s from"../../core/Error.js";import r from"../../core/Evented.js";import{HandleOwnerMixin as i}from"../../core/HandleOwner.js";import l from"../../core/Logger.js";import{isSome as a}from"../../core/maybe.js";import{when as o}from"../../core/reactiveUtils.js";import{property as n}from"../../core/accessorSupport/decorators/property.js";import"../../core/accessorSupport/ensureType.js";import"../../core/arrayUtils.js";import{subclass as c}from"../../core/accessorSupport/decorators/subclass.js";import h from"../../geometry/Point.js";import{getObjectIdsFromElements as p,getFeaturesFromLayers as u}from"../../networks/support/utils.js";import{trace as d}from"../../rest/networks/trace.js";import g from"../../rest/networks/support/TraceLocation.js";import m from"../../rest/networks/support/TraceParameters.js";import{GeometryHandler as y}from"./support/GeometryHandler.js";import{GraphicHandler as f}from"./support/GraphicHandler.js";let _=class extends(i(r.EventedAccessor)){constructor(e){super(e),this._activeProgress=!1,this._clickHandler=null,this._flags=[],this._geometryHandler=null,this._graphicHandler=null,this._highlightHandler=[],this._traces=[],this._traceResults=[],this._unObject=null,this._watchHandler=null,this.defaultGraphicColor={color:[255,255,0,.6],haloOpacity:.9,fillOpacity:.2,hex:"#FFFF00"},this.flags=[],this.gdbVersion="sde.DEFAULT",this.selectedTraces=[],this.selectOnComplete=!0,this.showGraphicsOnComplete=!0,this.showSelectionAttributes=!0}initialize(){this._geometryHandler=new y,this._graphicHandler=new f}destroy(){this.view=null}get state(){return this.view?.ready?"ready":"loading"}get view(){return this._get("view")}set view(e){e&&"2d"!==e.type&&l.getLogger(this.declaredClass).error(new s("view:invalid-view","SceneView is not supported",{view:e})),this._set("view",e)}addFlagByHit(e,t){return new Promise(((s,r)=>{this.view.popup.autoOpenEnabled=!1,this._clickHandler&&this._clickHandler.remove(),this.emit("add-flag",{type:e}),this._clickHandler=this.view.on("click",(i=>{this.queryFlagByHitTest(i,e,t).then((r=>{this.view.popup.autoOpenEnabled=!0,this._clickHandler.remove(),this.emit("add-flag-complete",{type:e,symbol:t}),s(r)})).catch((s=>{this.view.popup.autoOpenEnabled=!0,this._clickHandler.remove(),this.emit("add-flag-error",{type:e,symbol:t}),r(s)}))}))}))}async addFlagsOnLoad(){return new Promise((e=>{const t=[];this._watchHandler=o((()=>!this.view.updating),(async()=>{if(this._flags.length>0)e(t);else{const s=this.flags.map((async e=>{if(e.mapPoint){const s=new h({x:e.mapPoint.x,y:e.mapPoint.y,spatialReference:e.mapPoint.spatialReference.wkid}),r={screenPoint:this.view.toScreen(s),mapPoint:s};await this.queryFlagByHitTest(r,e.type)||("barrier"===e.type?t.push("barrier"):t.push("starting-point"))}}));await Promise.all(s),e(t)}}),{initial:!0})}))}async addResultGraphicToView(e,t){for(const s in e.results.aggregatedGeometry)if("line,multipoint,polygon".includes(s)&&null!==e.results.aggregatedGeometry[s]){e.results.aggregatedGeometry[s].spatialReference=this._unObject.spatialReference,e.graphicEnabled=!0;const r=await this._geometryHandler.projectGeometry(e.results.aggregatedGeometry[s],this.view.spatialReference),i={globalid:e.trace.globalId};if(null!==r){const e=this._graphicHandler.makeGraphic(r,t.color,i,this.view.spatialReference);this.view.graphics.add(e)}}}addTerminal(e,t){const s=[...this._flags];s.forEach((s=>{s.globalId===t.globalId&&(t.selectedTerminals.includes(parseInt(e,10))||s.selectedTerminals.push(parseInt(e,10)))})),this._flags=s}async callTrace(){const e=this._traces.filter((e=>e.selected));return e.length>0&&(this._traceResults.length>0&&this._traceResults.forEach((e=>{this.removeResultGraphicFromView(e)})),this._traceResults=[],this.removeSelection(),await Promise.all(e.map((async(e,t)=>{const s=e,r=new m({gdbVersion:this.gdbVersion,moment:null,traceType:s.traceType,traceLocations:this._flags,namedTraceConfigurationGlobalId:s.globalId,traceConfiguration:null,outSpatialReference:null,resultTypes:null});await this.executeTrace(s,this._unObject.networkServiceUrl,r).then((e=>{if(e.hasOwnProperty("results")){const s={...e};if(null!==s.results){const e=[...s.results.elements];s.results.elements.length=0;const r=new Map;for(const t of e)r.has(t.globalId)||(r.set(t.globalId,!0),s.results.elements.push(t));const i=[...this._traceResults];i.splice(t,0,s),this._traceResults=i,null!==s.results&&(this.selectOnComplete&&this.mergeSelection(!0,s.trace),this.showGraphicsOnComplete&&this.addResultGraphicToView(s,s.graphicColor))}else{const s=[...this._traceResults];s.splice(t,0,e),this._traceResults=s}this._activeProgress=!1}else{this._activeProgress=!1;const s=[...this._traceResults];s.splice(t,0,e),this._traceResults=s}})).catch((e=>{throw this._activeProgress=!1,e}))}))),!0)}changeResultGraphicColor(e,t){const s=[...this._traceResults];s.length>0&&s.forEach((s=>{s.trace.globalId===t.trace.globalId&&(s.graphicColor=e,s.graphicEnabled=!0)})),this._traceResults=s,this.removeResultGraphicFromView(t),this.addResultGraphicToView(t,e)}changeFlagSymbol(e,t){this._flags.length>0&&this._flags.forEach((s=>{s.type===e&&t&&(s.mapGraphic.symbol=t)}))}checkCanTrace(){const e={status:!0,issues:[]};let t=!1;const s=this._flags.some((e=>"starting-point"===e.type)),r=this._flags.filter((e=>null!==e.allTerminals));r.length>0&&(t=r.some((e=>e.selectedTerminals.length<=0)));let i=[];return s?(i=this._traces.filter((e=>e.selected)),i.length<=0?(e.status=!1,e.issues=["noTrace"],t&&(e.status=!1,e.issues=["noTrace","noTerminalSelected"])):t&&(e.status=!1,e.issues=["noTerminalSelected"])):(i=this._traces.filter((e=>!0===e.selected)),i.length>0?(e.status=!1,e.issues=["noStart"],t&&(e.status=!1,e.issues=["noStart","noTerminalSelected"])):(e.status=!1,e.issues=["noStart","noTrace"],t&&(e.status=!1,e.issues=["noStart","noTrace","noTerminalSelected"]))),e}checkSelectionExist(){let e=!1;return this._highlightHandler.some((t=>{t&&(e=!0)})),e}clearResult(e){let t=this._traceResults;if(t.length>0){const s=t.filter((t=>t.trace.globalId===e.globalId));s.length>0&&this.removeResultGraphicFromView(s[0]),t=t.filter((t=>t.trace.globalId!==e.globalId))}this._traceResults=t,0===t.length?(this.removeSelection(),this.emit("clear-selection",{resultSet:[]})):this.mergeSelection(!1,e)}executeTrace(e,t,s){const r=this._processFlags(s.traceLocations);return s.traceLocations=r,d(t,s).then((t=>({trace:e,results:t,selectionEnabled:!1,graphicEnabled:!1,graphicColor:this.defaultGraphicColor,status:"success"}))).catch((t=>({trace:e,results:null,selectionEnabled:!1,graphicEnabled:!1,graphicColor:this.defaultGraphicColor,status:t.message})))}getValidSources(){let e=[];const t=this._unObject.dataElement.domainNetworks;for(const s of t)e=e.concat(s.junctionSources),e=e.concat(s.edgeSources);return e}async loadUtilityNetwork(){await this.view.when();const e=this.view.map;if(e.utilityNetworks?.length){const s=e.utilityNetworks.getItemAt(0);await s.load(),this._unObject=s;try{await e.loadAll(),this._populateOutfields()}catch(t){this._populateOutfields()}return s}return null}manageFilterBarrier(e,t){const s=[...this._flags];s.forEach((s=>{s.globalId===t.globalId&&"barrier"===t.type&&s.id===t.id&&(s.isFilterBarrier=e)})),this._flags=s}mergeSelection(e,t){let s=[];const r=[...this._traceResults],i=t.globalId;r.forEach((t=>{i===t.trace.globalId&&(t.selectionEnabled=e),t.selectionEnabled&&null!==t.results.elements&&(s=s.concat(t.results.elements))})),this.selectResults([...new Set(s)])}async queryFeaturesById(e){const s=p(this._unObject,e),r=this._getUniqueMapLayerViews(this.view),i={layerUrl:s[0].layerUrl,objectIds:s[0].objectIds,outFields:["*"]},l=r.filter((e=>e.layer?.parsedUrl?.path===s[0].layerUrl));if(l.length>0){const e=(await Promise.all(l.map((async e=>{const s=new t,r=e.layer;s.add(r);const l={layers:s,layerInfos:[i],returnGeometry:!0,outSpatialReference:this.view.spatialReference};return(await u(l))[0]})))).filter((e=>e.featureSet.features.length>0));return e.length>0?e:null}return null}queryFlagByHitTest(e,t,s){return this._lookupFlagByHit(e).then((e=>{if(e.length>0){const r=[...this._flags],i=s;return e.forEach((e=>{const s=e.graphic,l=s.attributes.hasOwnProperty("GLOBALID")?s.attributes.GLOBALID:s.attributes.globalid;if(r.filter((e=>e.globalId===l)).length<=0){const e=this._graphicHandler.getFlagGraphic(s.mapPoint,t,s,i);this.view.graphics.add(e),r.push({...s,type:t,globalId:s.attributes.globalid?s.attributes.globalid:s.attributes.GLOBALID,details:s,mapGraphic:e,id:r.length+1})}else if(null!==s.percentAlong){const e=this._graphicHandler.getFlagGraphic(s.mapPoint,t,s,i);this.view.graphics.add(e),r.push({...s,type:t,globalId:s.attributes.globalid?s.attributes.globalid:s.attributes.GLOBALID,details:s,mapGraphic:e,id:r.length+1})}})),this._flags=r,!0}return!1}))}removeResultGraphicFromView(e){const t=this.view.graphics;e.graphicEnabled=!1;t.filter((t=>t.attributes[t.attributes.hasOwnProperty("GLOBALID")?"GLOBALID":"globalid"]===e.trace.globalId)).forEach((e=>{this.view.graphics.remove(e)}))}removeFlag(e){const t=this._flags.filter((t=>{if(t.id!==e.id)return t}));this._removeGraphic(e),this._flags=t}removeSelection(){this._highlightHandler.forEach((e=>{e&&e.remove()})),this._highlightHandler=[]}removeTerminal(e,t){const s=[...this._flags];s.forEach((s=>{if(s.globalId===t.globalId&&t.selectedTerminals.includes(parseInt(e,10))){const r=t.selectedTerminals.indexOf(parseInt(e,10));s.selectedTerminals.splice(r,1)}})),this._flags=s}removeFlagsOnLoadWatcher(){this._watchHandler&&null!==this._watchHandler&&this._watchHandler.remove()}reset(){this._flags=[],this._traceResults=[];const e=[...this._traces];e.forEach((e=>{e.selected=!1})),this._traces=e,this.view&&(this.view.graphics.removeAll(),this.removeSelection(),this.emit("clear-selection",{resultSet:[]}))}selectFeaturesById(e){const t=p(this._unObject,e);this._getUniqueMapLayerViews(this.view).forEach((e=>{e.layer?.parsedUrl?.path===t[0].layerUrl&&this._highlightHandler.push(e.highlight(t[0].objectIds))}))}selectResults(e){if(e.length>0){this.removeSelection();const t=this._groupResultsByNetworkSource(e),s=[];for(const e in t)this.selectFeaturesById(t[e]),s.push(this.queryFeaturesById(t[e]));Promise.all(s).then((e=>{this.emit("select-features",{resultSet:e})}))}else this.removeSelection(),this.emit("clear-selection",{resultSet:[]})}selectTraces(e,t){const s=[...this._traces];s.forEach((s=>{t===s.globalId&&(s.selected=e)})),this._traces=s}selectTracesOnLoad(){this._unObject.hasOwnProperty("sharedNamedTraceConfigurations")&&(this._traces=[...this._unObject.sharedNamedTraceConfigurations],this._traces.forEach((e=>{e.selected=!1,this.selectedTraces.includes(e.globalId)&&(e.selected=!0)})))}zoomToAsset(e){this.view.goTo(e).catch((e=>{console.error(e)}))}_getUniqueMapLayerViews(e){const t=[];return e.layerViews.filter((e=>"feature"===e.layer.type||"group"===e.layer.type)).forEach((e=>{"group"===e.type?e.layerViews.forEach((e=>{t.push(e)})):t.some((t=>t.layer.layerId===e.layer.layerId))||t.push(e)})),t}_processFlags(e){const t=[];return e.forEach((e=>{if(null!==e.selectedTerminals&&e.selectedTerminals.length>0)e.selectedTerminals.forEach((s=>{const r=new g({globalId:e.globalId,percentAlong:e.percentAlong,terminalId:s,type:e.type,isFilterBarrier:e.isFilterBarrier});t.push(r)}));else{const s=new g({globalId:e.globalId,percentAlong:e.percentAlong,terminalId:null,type:e.type,isFilterBarrier:e.isFilterBarrier});t.push(s)}})),t}_getDisplayField(e){const t=e.layer;let s=t.displayField,r="";for(const i in e.attributes){const l=i.toLowerCase();if(l===s.toLowerCase())if(r=e.attributes[i],"assetgroup"===l||"assettype"===l){let l=e.attributes[t.typeIdField.toUpperCase()];l||(l=e.attributes[t.typeIdField.toLowerCase()]),s=t.typeIdField,r=this._checkSubtype(t,l),""===s&&(t.templates?.length>0?(s=t.templates[0]?.name,r=t.templates[0]?.name):(s=t.displayField,r=e.attributes[i]))}else r=this._checkDomain(t.fields,i,r)}return{field:s,value:r}}_checkSubtype(e,t){let s=t;if(null!==e.types&&e.types.length>0){const r=e.types.filter((e=>e.id===t));r.length>0&&(s=r[0].name)}return s}_checkDomain(e,t,s){let r=s;const i=e.filter((e=>e.name.toLowerCase()===t.toLowerCase()));if(i.length>0&&null!==i[0].domain){const e=i[0].domain.codedValues.filter((e=>e.code===s));e.length>0&&(r=e[0].name)}return r}_groupBy(e,t){return e.reduce(((e,s)=>((e[s[t]]=e[s[t]]||[]).push(s),e)),{})}_groupResultsByNetworkSource(e){if(e.length>0){if(e[0].hasOwnProperty("results")){const t=e[0].results.elements;return this._groupBy(t,"networkSourceId")}return this._groupBy(e,"networkSourceId")}if(e.hasOwnProperty("results")){const t=e.results.elements;return this._groupBy(t,"networkSourceId")}return[]}_lookupFlagByHit(e){return this.view.hitTest(e.screenPoint).then((t=>{const s=[];if(t.results.length>0){const r=t.results.find((e=>null!==e.layer));if(r.graphic&&a(r.graphic.geometry))if("polyline"===r.graphic.geometry.type){const t=this._geometryHandler.getPercentageAlong(r.graphic.geometry,e.mapPoint,r.graphic.geometry.spatialReference),i=this._getDisplayField(r.graphic);r.graphic.terminalId=null,r.graphic.isFilterBarrier=!1,r.graphic.allTerminals=null,r.graphic.selectedTerminals=null,r.graphic.percentAlong=t,r.graphic.displayValue=i,r.graphic.mapPoint=r.mapPoint,s.push(r)}else if(("point"===r.graphic.geometry.type||"polygon"===r.graphic.geometry.type)&&null!==this._unObject){const e=this._unObject.getTerminalConfiguration(r.graphic),t=this._getDisplayField(r.graphic);r.graphic.terminalId=e?e.terminals[0].id?e.terminals[0].id:null:1,r.graphic.isFilterBarrier=!1,r.graphic.allTerminals=e||null,r.graphic.selectedTerminals=[e?e.terminals[0].id?e.terminals[0].id:null:1],r.graphic.percentAlong=null,r.graphic.displayValue=t,r.graphic.mapPoint=r.mapPoint,s.push(r)}}return s}))}async _populateOutfields(){const e=this.view.map,t=this.getValidSources();e.layers.forEach((e=>{"group"===e.type?e.layers.forEach((e=>{t.some((t=>t.layerId===e.layerId))&&e.fields.some((e=>"assetgroup"===e.name.toLowerCase()))&&(e.outFields=["assetgroup","assettype","globalid","objectid"])})):t.some((t=>t.layerId===e.layerId))&&e.fields.some((e=>"assetgroup"===e.name.toLowerCase()))&&(e.outFields=["assetgroup","assettype","globalid","objectid"])}))}_removeGraphic(e){this.view.graphics.remove(e.mapGraphic)}};e([n()],_.prototype,"_activeProgress",void 0),e([n()],_.prototype,"_flags",void 0),e([n()],_.prototype,"_traces",void 0),e([n()],_.prototype,"_traceResults",void 0),e([n()],_.prototype,"defaultGraphicColor",void 0),e([n()],_.prototype,"flags",void 0),e([n()],_.prototype,"gdbVersion",void 0),e([n()],_.prototype,"selectedTraces",void 0),e([n()],_.prototype,"selectOnComplete",void 0),e([n()],_.prototype,"showGraphicsOnComplete",void 0),e([n()],_.prototype,"showSelectionAttributes",void 0),e([n({readOnly:!0})],_.prototype,"state",null),e([n({value:null})],_.prototype,"view",null),_=e([c("esri.widgets.UtilityNetworkTrace.UtilityNetworkTraceViewModel")],_);const w=_;export{w as default};