/// <summary>
        /// This method makes sure
        /// 1. The Mapview is Active
        /// 2. There is at least one layer selected
        /// 3. That layer is either
        ///   a. A utility network layer
        ///   b. A feature layer whose feature class belongs to a utility network
        ///   c. A subtype group layer whose feature class belongs to a utility network
        ///
        /// If all of these hold true, we populate the combo box with the list of categories that are registered with this utility network
        /// </summary>
        private async void UpdateCategoryList(MapViewEventArgs mapViewEventArgs)
        {
            // Verify that the map view is active and at least one layer is selected
            if (MapView.Active == null ||
                mapViewEventArgs.MapView.GetSelectedLayers().Count < 1)
            {
                Enabled = false;
                return;
            }

            // Verify that we have the correct kind of layer
            Layer selectedLayer = mapViewEventArgs.MapView.GetSelectedLayers()[0];

            if (!(selectedLayer is UtilityNetworkLayer) && !(selectedLayer is FeatureLayer) && !(selectedLayer is SubtypeGroupLayer))
            {
                Enabled = false;
                return;
            }

            // Switch to the MCT to access the geodatabase
            await QueuedTask.Run(() =>
            {
                // Get the utility network from the layer.
                // It's possible that the layer is a FeatureLayer or SubtypeGroupLayer that doesn't refer to a utility network at all.
                using (UtilityNetwork utilityNetwork = UtilityNetworkUtils.GetUtilityNetworkFromLayer(selectedLayer))
                {
                    if (utilityNetwork == null)
                    {
                        Enabled = false;
                        return;
                    }

                    // Enable the combo box and clear out its contents
                    Enabled = true;
                    Clear();

                    // Fill the combo box with all of the categories in the utility network
                    using (UtilityNetworkDefinition utilityNetworkDefinition = utilityNetwork.GetDefinition())
                    {
                        IReadOnlyList <string> categories = utilityNetworkDefinition.GetAvailableCategories();
                        foreach (string category in categories)
                        {
                            Add(new ComboBoxItem(category));
                        }
                    }
                }
            });

            // Store the layer
            if (Enabled)
            {
                myLayer = selectedLayer;
            }
        }
Пример #2
0
        // Get the Utility Network from the currently active layer
        private UtilityNetwork GetUtilityNetwork()
        {
            UtilityNetwork utilityNetwork = null;

            if (MapView.Active != null)
            {
                IReadOnlyList <Layer> selectedLayers = MapView.Active.GetSelectedLayers();
                if (selectedLayers.Count > 0)
                {
                    utilityNetwork = UtilityNetworkUtils.GetUtilityNetworkFromLayer(selectedLayers[0]);
                }
            }
            return(utilityNetwork);
        }
Пример #3
0
        // Get the Utility Network from the currently active layer
        private UtilityNetwork GetUtilityNetwork()
        {
            UtilityNetwork utilityNetwork = null;

            if (MapView.Active != null)
            {
                MapViewEventArgs      mapViewEventArgs = new MapViewEventArgs(MapView.Active);
                IReadOnlyList <Layer> selectedLayers   = mapViewEventArgs.MapView.GetSelectedLayers();
                if (selectedLayers.Count > 0)
                {
                    utilityNetwork = UtilityNetworkUtils.GetUtilityNetworkFromLayer(selectedLayers[0]);
                }
            }
            return(utilityNetwork);
        }
Пример #4
0
        /// <summary>
        /// GenerateReport
        ///
        /// This routine takes a feature layer that references a feature class that participates in a utility network.
        /// It returns a set of data to display on the UI thread.
        ///
        ///
        /// </summary>

        public LoadTraceResults GenerateReport(Layer selectedLayer)
        {
            // Create a new results object.  We use this class to pass back a set of data from the worker thread to the UI thread

            LoadTraceResults results = new LoadTraceResults();

            // Initialize a number of geodatabase objects

            using (UtilityNetwork utilityNetwork = UtilityNetworkUtils.GetUtilityNetworkFromLayer(selectedLayer))
            {
                if (utilityNetwork == null)
                {
                    results.Message = "Please select a utility network layer.";
                    results.Success = false;
                }
                else
                {
                    using (Geodatabase utilityNetworkGeodatabase = utilityNetwork.GetDatastore() as Geodatabase)
                        using (UtilityNetworkDefinition utilityNetworkDefinition = utilityNetwork.GetDefinition())
                            using (Geodatabase defaultGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(Project.Current.DefaultGeodatabasePath))))
                                using (TraceManager traceManager = utilityNetwork.GetTraceManager())
                                {
                                    // First check to make sure we have a feature service workspace.  Utility Network functionality requires this.
                                    if (utilityNetworkGeodatabase.GetGeodatabaseType() != GeodatabaseType.Service)
                                    {
                                        results.Message = "A feature service workspace connection is required.";
                                        results.Success = false;
                                        return(results);
                                    }

                                    // Get a row from the starting points table in the default project workspace.  This table is created the first time the user creates a starting point
                                    // If the table is missing or empty, a null row is returned

                                    using (Row startingPointRow = GetStartingPointRow(defaultGeodatabase, ref results))
                                    {
                                        if (startingPointRow != null)
                                        {
                                            // Convert starting point row into network element

                                            Element startingPointElement = GetElementFromPointRow(startingPointRow, utilityNetwork, utilityNetworkDefinition);

                                            // Obtain a tracer object

                                            DownstreamTracer downstreamTracer = traceManager.GetTracer <DownstreamTracer>();

                                            // Get the network attributes that we will use in our trace

                                            using (NetworkAttribute phasesNetworkAttribute = GetAttribute(utilityNetworkDefinition, PhaseAttributeNames))
                                                using (NetworkAttribute loadNetworkAttribute = GetAttribute(utilityNetworkDefinition, LoadAttributeNames))
                                                    using (NetworkAttribute deviceStatusNetworkAttribute = GetAttribute(utilityNetworkDefinition, DeviceStatusAttributeNames))
                                                    {
                                                        if (phasesNetworkAttribute == null || loadNetworkAttribute == null || deviceStatusNetworkAttribute == null)
                                                        {
                                                            results.Success = false;
                                                            results.Message = "This add-in requires network attributes for phase, service load, and device status.\n";
                                                            return(results);
                                                        }


                                                        // Get the Tier for Medium Voltage Radial

                                                        DomainNetwork electricDomainNetwork = utilityNetworkDefinition.GetDomainNetwork(ElectricDomainNetwork);
                                                        Tier          mediumVoltageTier     = electricDomainNetwork.GetTier(MediumVoltageTier);


                                                        // Set up the trace configuration

                                                        TraceConfiguration traceConfiguration = new TraceConfiguration();

                                                        // Configure the trace to use the electric domain network

                                                        traceConfiguration.DomainNetwork = electricDomainNetwork;

                                                        // Take the default TraceConfiguration from the Tier for Traversability

                                                        Traversability tierTraceTraversability = mediumVoltageTier.TraceConfiguration.Traversability;
                                                        traceConfiguration.Traversability.FunctionBarriers = tierTraceTraversability.FunctionBarriers;
                                                        traceConfiguration.IncludeBarriersWithResults      = mediumVoltageTier.TraceConfiguration.IncludeBarriersWithResults;
                                                        traceConfiguration.Traversability.Scope            = tierTraceTraversability.Scope;
                                                        ConditionalExpression baseCondition = tierTraceTraversability.Barriers as ConditionalExpression;

                                                        // Create a condition to only return features that have the service point category

                                                        ConditionalExpression servicePointCategoryCondition = new CategoryComparison(CategoryOperator.IsEqual, ServicePointCategory);

                                                        // Create function to sum loads on service points where phase = A

                                                        ConditionalExpression aPhaseCondition = new NetworkAttributeComparison(phasesNetworkAttribute, Operator.DoesNotIncludeTheValues, APhase);
                                                        Add aPhaseLoad = new Add(loadNetworkAttribute, servicePointCategoryCondition);


                                                        // Create function to sum loads on service points where phase = B

                                                        ConditionalExpression bPhaseCondition = new NetworkAttributeComparison(phasesNetworkAttribute, Operator.DoesNotIncludeTheValues, BPhase);
                                                        Add bPhaseLoad = new Add(loadNetworkAttribute, servicePointCategoryCondition);

                                                        // Create function to sum loads on service points where phase = C

                                                        ConditionalExpression cPhaseCondition = new NetworkAttributeComparison(phasesNetworkAttribute, Operator.DoesNotIncludeTheValues, CPhase);
                                                        Add cPhaseLoad = new Add(loadNetworkAttribute, servicePointCategoryCondition);

                                                        // Set the output condition to only return features that have the service point category

                                                        traceConfiguration.OutputCondition = servicePointCategoryCondition;

                                                        // Create starting point list and trace argument object

                                                        List <Element> startingPointList = new List <Element>()
                                                        {
                                                            startingPointElement
                                                        };
                                                        TraceArgument traceArgument = new TraceArgument(startingPointList);
                                                        traceArgument.Configuration = traceConfiguration;

                                                        // Trace on the A phase

                                                        traceConfiguration.Traversability.Barriers = new Or(baseCondition, aPhaseCondition);
                                                        traceConfiguration.Functions = new List <Function>()
                                                        {
                                                            aPhaseLoad
                                                        };
                                                        traceArgument.Configuration = traceConfiguration;

                                                        try
                                                        {
                                                            IReadOnlyList <Result> resultsA = downstreamTracer.Trace(traceArgument);

                                                            ElementResult elementResult = resultsA.OfType <ElementResult>().First();
                                                            results.NumberServicePointsA = elementResult.Elements.Count;

                                                            FunctionOutputResult functionOutputResult = resultsA.OfType <FunctionOutputResult>().First();
                                                            results.TotalLoadA = (double)functionOutputResult.FunctionOutputs.First().Value;
                                                        }
                                                        catch (ArcGIS.Core.Data.GeodatabaseUtilityNetworkException e)
                                                        {
                                                            //No A phase connectivity to source
                                                            if (!e.Message.Equals("No subnetwork source was discovered."))
                                                            {
                                                                results.Success  = false;
                                                                results.Message += e.Message;
                                                            }
                                                        }

                                                        // Trace on the B phase

                                                        traceConfiguration.Traversability.Barriers = new Or(baseCondition, bPhaseCondition);
                                                        traceConfiguration.Functions = new List <Function>()
                                                        {
                                                            bPhaseLoad
                                                        };
                                                        traceArgument.Configuration = traceConfiguration;

                                                        try
                                                        {
                                                            IReadOnlyList <Result> resultsB = downstreamTracer.Trace(traceArgument);

                                                            ElementResult elementResult = resultsB.OfType <ElementResult>().First();
                                                            results.NumberServicePointsB = elementResult.Elements.Count;

                                                            FunctionOutputResult functionOutputResult = resultsB.OfType <FunctionOutputResult>().First();
                                                            results.TotalLoadB = (double)functionOutputResult.FunctionOutputs.First().Value;
                                                        }
                                                        catch (ArcGIS.Core.Data.GeodatabaseUtilityNetworkException e)
                                                        {
                                                            // No B phase connectivity to source
                                                            if (!e.Message.Equals("No subnetwork source was discovered."))
                                                            {
                                                                results.Success  = false;
                                                                results.Message += e.Message;
                                                            }
                                                        }

                                                        // Trace on the C phase
                                                        traceConfiguration.Traversability.Barriers = new Or(baseCondition, cPhaseCondition);
                                                        traceConfiguration.Functions = new List <Function>()
                                                        {
                                                            cPhaseLoad
                                                        };
                                                        traceArgument.Configuration = traceConfiguration;

                                                        try
                                                        {
                                                            IReadOnlyList <Result> resultsC = downstreamTracer.Trace(traceArgument);

                                                            ElementResult elementResult = resultsC.OfType <ElementResult>().First();
                                                            results.NumberServicePointsC = elementResult.Elements.Count;

                                                            FunctionOutputResult functionOutputResult = resultsC.OfType <FunctionOutputResult>().First();
                                                            results.TotalLoadC = (double)functionOutputResult.FunctionOutputs.First().Value;
                                                        }
                                                        catch (ArcGIS.Core.Data.GeodatabaseUtilityNetworkException e)
                                                        {
                                                            // No C phase connectivity to source
                                                            if (!e.Message.Equals("No subnetwork source was discovered."))
                                                            {
                                                                results.Success  = false;
                                                                results.Message += e.Message;
                                                            }
                                                        }
                                                    }

                                            // append success message to the output string

                                            results.Message += "Trace successful.";
                                            results.Success  = true;
                                        }
                                    }
                                }
                }
            }
            return(results);
        }
        private string ValidateChangedFeatures(Layer layer)
        {
            StringBuilder resultString = new StringBuilder();

            // Get utility network and geodatabase
            using (UtilityNetwork utilityNetwork = UtilityNetworkUtils.GetUtilityNetworkFromLayer(layer))
                using (Geodatabase geodatabase = utilityNetwork.GetDatastore() as Geodatabase)
                {
                    // Determine what to validate
                    //    File geodatabase - validate everything, synchronously
                    //    Default version - validate everything, asynchronously
                    //    Branch version - validate changes only, synchronously

                    bool shouldValidateEverything;
                    bool runAsync;

                    if (!geodatabase.IsVersioningSupported())
                    {
                        shouldValidateEverything = true;
                        runAsync = false;
                    }
                    else
                    {
                        using (VersionManager versionManager = geodatabase.GetVersionManager())
                            using (Version currentVersion = versionManager.GetCurrentVersion())
                            {
                                if (IsDefaultVersion(currentVersion))
                                {
                                    shouldValidateEverything = true;
                                    runAsync = true;
                                }
                                else
                                {
                                    shouldValidateEverything = false;
                                    runAsync = false;
                                }
                            }
                    }

                    // If we validating everything, get an envelope from the dirty areas table
                    EnvelopeBuilderEx envelopeBuilder = new EnvelopeBuilderEx(layer.GetSpatialReference());

                    if (shouldValidateEverything)
                    {
                        using (Table dirtyAreaTable = utilityNetwork.GetSystemTable(SystemTableType.DirtyAreas))
                            using (RowCursor rowCursor = dirtyAreaTable.Search())
                            {
                                envelopeBuilder = GetExtentFromRowCursor(envelopeBuilder, rowCursor);
                            }
                    }

                    // else get an envelope using version differences
                    else
                    {
                        using (VersionManager versionManager = geodatabase.GetVersionManager())
                            using (Version currentVersion = versionManager.GetCurrentVersion())
                                using (Version defaultVersion = currentVersion.GetParent())
                                    using (Geodatabase defaultGeodatabase = defaultVersion.Connect())
                                        using (UtilityNetwork defaultUtilityNetwork = defaultGeodatabase.OpenDataset <UtilityNetwork>(utilityNetwork.GetName()))
                                            using (Table dirtyAreaTable = utilityNetwork.GetSystemTable(SystemTableType.DirtyAreas))
                                                using (Table defaultDirtyAreaTable = defaultUtilityNetwork.GetSystemTable(SystemTableType.DirtyAreas))
                                                    using (DifferenceCursor inserts = dirtyAreaTable.Differences(defaultDirtyAreaTable, DifferenceType.Insert))
                                                    {
                                                        envelopeBuilder = GetExtentFromDifferenceCursor(envelopeBuilder, inserts);
                                                    }
                    }

                    // Run validate topology on our envelope
                    Envelope         extent = envelopeBuilder.ToGeometry();
                    ValidationResult result = utilityNetwork.ValidateNetworkTopologyInEditOperation(extent, runAsync ? ServiceSynchronizationType.Asynchronous : ServiceSynchronizationType.Synchronous);
                    if (result.HasErrors)
                    {
                        resultString.AppendLine("Errors found.");
                    }
                    else
                    {
                        resultString.AppendLine("No errors found.");
                    }
                }

            return(resultString.ToString());
        }