arcgis Engine C# 最近设施分析实现部分关键代码。

时间:2015-1-30    作者:悬浮的青春    分类: gis二次开发


  private void Initialize()

        {

            IFeatureWorkspace featureWorkspace;

            INetworkDataset networkDataset;

            // Open GDB and NDS

            featureWorkspace = OpenWorkspace(@"C:Program FilesArcGISDeveloperKitSamplesNETdataSanFranciscoNetwork") as IFeatureWorkspace;

            networkDataset = OpenNetworkDataset(featureWorkspace as IWorkspace, "Streets_nd");

            // Create NAContext and NASolver

            m_NAContext = CreateSolverContext(networkDataset);

            // Get Cost Attributes

            INetworkAttribute networkAttribute;

            for (int i = 0; i < networkDataset.AttributeCount - 1; i++)

            {

                networkAttribute = networkDataset.get_Attribute(i);

                if (networkAttribute.UsageType == esriNetworkAttributeUsageType.esriNAUTCost)

                {

                    cboCostAttribute.Items.Add(networkAttribute.Name);

                    cboCostAttribute.SelectedIndex = 0;

                }

            }

            txtTargetFacility.Text = "1";

            txtCutOff.Text = "";

            // Load locations from FC

            IFeatureClass inputFClass = featureWorkspace.OpenFeatureClass("BayAreaIncident");

            LoadNANetworkLocations("Incidents", inputFClass, 100);

            inputFClass = featureWorkspace.OpenFeatureClass("BayAreaLocations");

            LoadNANetworkLocations("Facilities", inputFClass, 100);

            //Create Layer for Network Dataset and add to ArcMap

            ILayer layer;

            INetworkLayer networkLayer;

            networkLayer = new NetworkLayerClass();

            networkLayer.NetworkDataset = networkDataset;

            layer = networkLayer as ILayer;

            layer.Name = "Network Dataset";

            axMapControl1.AddLayer(layer, 0);

            //Create a Network Analysis Layer and add to ArcMap

            INALayer naLayer = m_NAContext.Solver.CreateLayer(m_NAContext);

            layer = naLayer as ILayer;

            layer.Name = m_NAContext.Solver.DisplayName;

         

        }


 public INAContext CreateSolverContext(INetworkDataset networkDataset)
        {
            //Get the Data Element
            IDENetworkDataset deNDS = GetDENetworkDataset(networkDataset);
            INASolver naSolver;
            naSolver = new NAClosestFacilitySolver();
            INAContextEdit contextEdit = naSolver.CreateContext(deNDS, naSolver.Name) as INAContextEdit;
            contextEdit.Bind(networkDataset, new GPMessagesClass());
            return contextEdit as INAContext;
        }
        //*********************************************************************************
        // Set Solver Settings
        //*********************************************************************************
        public void LoadNANetworkLocations(string strNAClassName, IFeatureClass inputFC, double snapTolerance)
        {
            INAClass naClass;
            INamedSet classes;
            classes = m_NAContext.NAClasses;
            naClass = classes.get_ItemByName(strNAClassName) as INAClass;
            // delete existing Locations except if that a barriers
            naClass.DeleteAllRows();
            // Create a NAClassLoader and set the snap tolerance (meters unit)
            INAClassLoader classLoader = new NAClassLoader();
            classLoader.Locator = m_NAContext.Locator;
            if (snapTolerance > 0) classLoader.Locator.SnapTolerance = snapTolerance;
            classLoader.NAClass = naClass;
            //Create field map to automatically map fields from input class to naclass
            INAClassFieldMap fieldMap;
            fieldMap = new NAClassFieldMap();
            fieldMap.CreateMapping(naClass.ClassDefinition, inputFC.Fields);
            classLoader.FieldMap = fieldMap;
            //Load Network Locations
            int rowsIn = 0;
            int rowsLocated = 0;
            IFeatureCursor featureCursor = inputFC.Search(null, true);
            classLoader.Load((ICursor)featureCursor, null, ref rowsIn, ref rowsLocated);
            //Message all of the network analysis agents that the analysis context has changed
            ((INAContextEdit)m_NAContext).ContextChanged();
        }
        //*********************************************************************************
        // Set Solver Settings
        //*********************************************************************************
        public void SetSolverSettings()
        {
            //Set Route specific Settings
            INASolver naSolver = m_NAContext.Solver;
            INAClosestFacilitySolver cfSolver = naSolver as INAClosestFacilitySolver;
            if (txtCutOff.Text.Length > 0 && IsNumeric(txtCutOff.Text.Trim()))
                cfSolver.DefaultCutoff = txtCutOff.Text;
            else
                cfSolver.DefaultCutoff = null;
            if (txtTargetFacility.Text.Length > 0 && IsNumeric(txtTargetFacility.Text))
                cfSolver.DefaultTargetFacilityCount = int.Parse(txtTargetFacility.Text);
            else
                cfSolver.DefaultTargetFacilityCount = 1;
            cfSolver.OutputLines = esriNAOutputLineType.esriNAOutputLineTrueShapeWithMeasure;
            cfSolver.TravelDirection = esriNATravelDirection.esriNATravelDirectionToFacility;
            // Set generic solver settings
            // Set the impedance attribute
            INASolverSettings naSolverSettings;
            naSolverSettings = naSolver as INASolverSettings;
            naSolverSettings.ImpedanceAttributeName = cboCostAttribute.Text;
            // Set the OneWay Restriction if necessary
            IStringArray restrictions;
            restrictions = naSolverSettings.RestrictionAttributeNames;
            restrictions.RemoveAll();
            if (chkUseRestriction.Checked)
                restrictions.Add("oneway");
            naSolverSettings.RestrictionAttributeNames = restrictions;
            //Restrict UTurns
            naSolverSettings.RestrictUTurns = esriNetworkForwardStarBacktrack.esriNFSBNoBacktrack;
            naSolverSettings.IgnoreInvalidLocations = true;
            // Set the Hierachy attribute
            naSolverSettings.UseHierarchy = chkUseHierarchy.Checked;
            if (naSolverSettings.UseHierarchy)
            {
                naSolverSettings.HierarchyAttributeName = "hierarchy";
                naSolverSettings.HierarchyLevelCount = 3;
                naSolverSettings.set_MaxValueForHierarchy(1, 1);
                naSolverSettings.set_NumTransitionToHierarchy(1, 9);
                naSolverSettings.set_MaxValueForHierarchy(2, 2);
                naSolverSettings.set_NumTransitionToHierarchy(2, 9);
            }
            // Do not forget to update the context after you set your impedance
            naSolver.UpdateContext(m_NAContext, GetDENetworkDataset(m_NAContext.NetworkDataset), new GPMessagesClass());
        }
        //*********************************************************************************
        // Solve the problem
        //*********************************************************************************
        public string Solve(INAContext naContext, IGPMessages gpMessages)
        {
            string errStr = "";
            try
            {
                //Solving the Problem
                errStr = "Error when solving";
                bool isPartialSolution = naContext.Solver.Solve(naContext, gpMessages, null);
                if (!isPartialSolution)
                    errStr = "OK";
                else
                    errStr = "Partial Solution";
            }
            catch (Exception e)
            {
                errStr += " Error Description " + e.Message;
            }
            return errStr;
        }

标签: arcgis二次开发

WRITTEN BY

avatar