/// <summary> /// Set solver settings /// </summary> /// <param name="strNAClassName">NAClass name</param> /// <param name="inputFC">Input feature class</param> /// <param name="maxSnapTolerance">Max snap tolerance</param> public void LoadNANetworkLocations(string strNAClassName, IFeatureClass inputFC, double maxSnapTolerance) { INamedSet classes = m_NAContext.NAClasses; INAClass naClass = classes.get_ItemByName(strNAClassName) as INAClass; // Delete existing locations from the specified NAClass naClass.DeleteAllRows(); // Create a NAClassLoader and set the snap tolerance (meters unit) INAClassLoader classLoader = new NAClassLoader(); classLoader.Locator = m_NAContext.Locator; if (maxSnapTolerance > 0) ((INALocator3)classLoader.Locator).MaxSnapTolerance = maxSnapTolerance; classLoader.NAClass = naClass; // Create field map to automatically map fields from input class to NAClass INAClassFieldMap fieldMap = new NAClassFieldMapClass(); fieldMap.CreateMapping(naClass.ClassDefinition, inputFC.Fields); classLoader.FieldMap = fieldMap; // Avoid loading network locations onto non-traversable portions of elements INALocator3 locator = m_NAContext.Locator as INALocator3; locator.ExcludeRestrictedElements = true; locator.CacheRestrictedElements(m_NAContext); // Load network locations int rowsIn = 0; int rowsLocated = 0; classLoader.Load((ICursor)inputFC.Search(null, true), null, ref rowsIn, ref rowsLocated); // Message all of the network analysis agents that the analysis context has changed. ((INAContextEdit)m_NAContext).ContextChanged(); }
private bool LoadLocations(IFeatureWorkspace featureWorkspace) { IFeatureClass inputFeatureClass = null; try { inputFeatureClass = featureWorkspace.OpenFeatureClass(txtInputFacilities.Text); } catch (Exception) { MessageBox.Show("Specified input feature class does not exist"); return(false); } INamedSet classes = m_naContext.NAClasses; INAClass naClass = classes.get_ItemByName("Facilities") as INAClass; // delete existing locations, except barriers naClass.DeleteAllRows(); // Create a NAClassLoader and set the snap tolerance (meters unit) INAClassLoader naClassLoader = new NAClassLoaderClass(); naClassLoader.Locator = m_naContext.Locator; naClassLoader.Locator.SnapTolerance = 100; naClassLoader.NAClass = naClass; // Create field map to automatically map fields from input class to NAClass INAClassFieldMap naClassFieldMap = new NAClassFieldMapClass(); naClassFieldMap.CreateMapping(naClass.ClassDefinition, inputFeatureClass.Fields); naClassLoader.FieldMap = naClassFieldMap; // Avoid loading network locations onto non-traversable portions of elements INALocator3 locator = m_naContext.Locator as INALocator3; locator.ExcludeRestrictedElements = true; locator.CacheRestrictedElements(m_naContext); // load network locations int rowsIn = 0; int rowsLocated = 0; naClassLoader.Load(inputFeatureClass.Search(null, true) as ICursor, null, ref rowsIn, ref rowsLocated); if (rowsLocated <= 0) { MessageBox.Show("Facilities were not loaded from input feature class"); MessageBox.Show("没有导入任何结果!"); return(false); } // Message all of the network analysis agents that the analysis context has changed INAContextEdit naContextEdit = m_naContext as INAContextEdit; naContextEdit.ContextChanged(); return(true); }
/// <summary> /// Create the analysis layer, load the locations, solve the analysis, and write to disk /// </summary> public void SolveRoute() { // Open the feature workspace, input feature class, and network dataset // As Workspace Factories are Singleton objects, they must be instantiated with the Activator IWorkspaceFactory workspaceFactory = System.Activator.CreateInstance(System.Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")) as IWorkspaceFactory; IFeatureWorkspace featureWorkspace = workspaceFactory.OpenFromFile(Application.StartupPath + FGDB_WORKSPACE, 0) as IFeatureWorkspace; IFeatureClass inputStopsFClass = featureWorkspace.OpenFeatureClass(INPUT_STOPS_FC); // Obtain the dataset container from the workspace ESRI.ArcGIS.Geodatabase.IFeatureDataset featureDataset = featureWorkspace.OpenFeatureDataset(FEATURE_DATASET); var featureDatasetExtensionContainer = featureDataset as ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtensionContainer; ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtension featureDatasetExtension = featureDatasetExtensionContainer.FindExtension(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTNetworkDataset); var datasetContainer3 = featureDatasetExtension as ESRI.ArcGIS.Geodatabase.IDatasetContainer3; // Use the container to open the network dataset. ESRI.ArcGIS.Geodatabase.IDataset dataset = datasetContainer3.get_DatasetByName(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTNetworkDataset, NETWORK_DATASET); INetworkDataset networkDataset = dataset as INetworkDataset; // Create the Route NALayer INALayer naLayer = CreateRouteAnalysisLayer("Route", networkDataset); INAContext naContext = naLayer.Context; INAClass stopsNAClass = naContext.NAClasses.get_ItemByName("Stops") as INAClass; IFeatureClass routesFC = naContext.NAClasses.get_ItemByName("Routes") as IFeatureClass; // Load the Stops INAClassFieldMap naClassFieldMap = new NAClassFieldMapClass(); naClassFieldMap.set_MappedField("Name", INPUT_NAME_FIELD); INAClassLoader naLoader = new NAClassLoaderClass(); naLoader.Locator = naContext.Locator; naLoader.NAClass = stopsNAClass; naLoader.FieldMap = naClassFieldMap; // Avoid loading network locations onto non-traversable portions of elements INALocator3 locator = naContext.Locator as INALocator3; locator.ExcludeRestrictedElements = true; locator.CacheRestrictedElements(naContext); int rowsInCursor = 0; int rowsLocated = 0; naLoader.Load(inputStopsFClass.Search(new QueryFilterClass(), false) as ICursor, new CancelTrackerClass(), ref rowsInCursor, ref rowsLocated); //Message all of the network analysis agents that the analysis context has changed ((INAContextEdit)naContext).ContextChanged(); //Solve INASolver naSolver = naContext.Solver; naSolver.Solve(naContext, new GPMessagesClass(), new CancelTrackerClass()); //Save the layer to disk SaveLayerToDisk(naLayer as ILayer, System.Environment.CurrentDirectory + @"\Route.lyr"); }
/// <summary> /// Load the input table and create field map to map fields from input table to NAClass /// </summary> /// <param name="strNAClassName">NAClass name</param> /// <param name="inputTable">Input table</param> public void LoadNANetworkLocations(string strNAClassName, ITable inputTable) { INamedSet classes = m_NAContext.NAClasses; INAClass naClass = classes.get_ItemByName(strNAClassName) as INAClass; // Delete existing rows from the specified NAClass naClass.DeleteAllRows(); // Create a NAClassLoader and set the snap tolerance (meters unit) INAClassLoader loader = new NAClassLoader(); loader.Locator = m_NAContext.Locator; loader.Locator.SnapTolerance = 100; loader.NAClass = naClass; // Create field map to automatically map fields from input table to NAclass INAClassFieldMap fieldMap = new NAClassFieldMapClass(); fieldMap.CreateMapping(naClass.ClassDefinition, inputTable.Fields); loader.FieldMap = fieldMap; // Avoid loading network locations onto non-traversable portions of elements INALocator3 locator = m_NAContext.Locator as INALocator3; locator.ExcludeRestrictedElements = true; locator.CacheRestrictedElements(m_NAContext); // Load input table int rowsIn = 0; int rowsLocated = 0; loader.Load(inputTable.Search(null, true), null, ref rowsIn, ref rowsLocated); // Message all of the network analysis agents that the analysis context has changed. INAContextEdit naContextEdit = m_NAContext as INAContextEdit; naContextEdit.ContextChanged(); }
public bool ShowModal(IMapControl3 mapControl, IEngineNetworkAnalystEnvironment naEnv) { // Initialize variables m_okClicked = false; m_listDisplayTable = new System.Collections.ArrayList(); var activeCategory = naEnv.NAWindow.ActiveCategory as IEngineNAWindowCategory2; if (activeCategory == null) { return(false); } IDataLayer dataLayer = activeCategory.DataLayer; if (dataLayer == null) { return(false); } // Set up the title of this dialog String dataLayerName = GetDataLayerName(dataLayer); if (dataLayerName.Length == 0) { return(false); } this.Text = "Load Items into " + dataLayerName; // Make sure the combo box lists only the appropriate possible input data layers PopulateInputDataComboBox(mapControl, dataLayer); //Select the first display table from the list if (cboInputData.Items.Count > 0) { cboInputData.SelectedIndex = 0; } // Show the window this.ShowDialog(); // If we selected a layer and clicked OK, load the locations if (m_okClicked && (cboInputData.SelectedIndex >= 0)) { try { // Get a cursor on the source display table (either though the selection set or table) // Use IDisplayTable because it accounts for joins, querydefs, etc. // IDisplayTable is implemented by FeatureLayers and StandaloneTables. // IDisplayTable displayTable = m_listDisplayTable[cboInputData.SelectedIndex] as IDisplayTable; ICursor cursor; if (chkUseSelection.Checked) { ISelectionSet selSet; selSet = displayTable.DisplaySelectionSet; selSet.Search(null, false, out cursor); } else { cursor = displayTable.SearchDisplayTable(null, false); } // Get the NAContext from the active analysis layer INAContext naContext = naEnv.NAWindow.ActiveAnalysis.Context; // Get the dataset for the active NAClass IDataset naDataset = activeCategory.NAClass as IDataset; // Setup NAClassLoader and Load Locations INAClassLoader2 naClassLoader = new NAClassLoader() as INAClassLoader2; naClassLoader.Initialize(naContext, naDataset.Name, cursor); // Avoid loading network locations onto non-traversable portions of elements INALocator3 locator = naContext.Locator as INALocator3; locator.ExcludeRestrictedElements = true; locator.CacheRestrictedElements(naContext); int rowsIn = 0; int rowsLocated = 0; naClassLoader.Load(cursor, null, ref rowsIn, ref rowsLocated); // Let the user know if some of the rows failed to locate if (rowsIn != rowsLocated) { MessageBox.Show("Out of " + rowsIn + " + rows, " + rowsLocated + " rows were located", "Loading locations", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception e) { MessageBox.Show(e.Message, "Loading locations failure", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(true); } return(false); }
public bool ShowModal(IMapControl3 mapControl, IEngineNetworkAnalystEnvironment naEnv) { // Initialize variables m_okClicked = false; m_listDisplayTable = new System.Collections.ArrayList(); // Get the NALayer and NAContext INALayer naLayer = naEnv.NAWindow.ActiveAnalysis; INAContext naContext = naLayer.Context; //Get the active category IEngineNAWindowCategory2 activeCategory = naEnv.NAWindow.ActiveCategory as IEngineNAWindowCategory2; if (activeCategory == null) { return(false); } INAClass naClass = activeCategory.NAClass; IDataset naDataset = naClass as IDataset; IDataLayer pDataLayer = activeCategory.DataLayer; ILayer pLayer = pDataLayer as ILayer; IFeatureLayer pFeatureLayer = pDataLayer as IFeatureLayer; IStandaloneTable pStandaloneTable = pDataLayer as IStandaloneTable; esriGeometryType targetGeoType = esriGeometryType.esriGeometryNull; String dataLayerName = ""; if (pFeatureLayer != null) { if (pLayer.Valid) { IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass; if (pFeatureClass != null) { targetGeoType = pFeatureClass.ShapeType; dataLayerName = pLayer.Name; } } } else if (pStandaloneTable != null) { dataLayerName = pStandaloneTable.Name; } if (dataLayerName.Length == 0) { return(false); } this.Text = "Load Items into " + dataLayerName; // Loop through all the sourcedisplayTables having targetGeoType to the combo box and the // list of displayTables IEnumLayer sourceLayers = null; ILayer sourceLayer = null; IDisplayTable sourceDisplayTable = null; UID searchInterfaceUID = new UID(); if (targetGeoType != esriGeometryType.esriGeometryNull) { searchInterfaceUID.Value = typeof(IFeatureLayer).GUID.ToString("B"); sourceLayers = mapControl.Map.get_Layers(searchInterfaceUID, true); sourceLayer = sourceLayers.Next(); while (sourceLayer != null) { IFeatureLayer sourceFeatureLayer = sourceLayer as IFeatureLayer; sourceDisplayTable = sourceLayer as IDisplayTable; if ((sourceFeatureLayer != null) && (sourceDisplayTable != null)) { IFeatureClass sourceFeatureClass = sourceFeatureLayer.FeatureClass; esriGeometryType sourceGeoType = sourceFeatureClass.ShapeType; if ((sourceGeoType == targetGeoType) || (targetGeoType == esriGeometryType.esriGeometryPoint && sourceGeoType == esriGeometryType.esriGeometryMultipoint)) { // Add the layer name to the combobox and the layer to the list cboInputData.Items.Add(sourceLayer.Name); m_listDisplayTable.Add(sourceDisplayTable); } } sourceLayer = sourceLayers.Next(); } } else //if (targetGeoType == esriGeometryType.esriGeometryNull) { IStandaloneTableCollection sourceStandaloneTables = mapControl.Map as IStandaloneTableCollection; IStandaloneTable sourceStandaloneTable = null; sourceDisplayTable = null; int count = 0; if (sourceStandaloneTables != null) { count = sourceStandaloneTables.StandaloneTableCount; } for (int i = 0; i < count; ++i) { sourceStandaloneTable = sourceStandaloneTables.get_StandaloneTable(i); sourceDisplayTable = sourceStandaloneTable as IDisplayTable; if ((sourceStandaloneTable != null) && (sourceDisplayTable != null)) { // Add the table name to the combobox and the layer to the list cboInputData.Items.Add(sourceStandaloneTable.Name); m_listDisplayTable.Add(sourceDisplayTable); } } searchInterfaceUID.Value = typeof(INALayer).GUID.ToString("B"); sourceLayers = mapControl.Map.get_Layers(searchInterfaceUID, true); sourceLayer = sourceLayers.Next(); while (sourceLayer != null) { INALayer sourceNALayer = sourceLayer as INALayer; if (sourceNALayer != null) { sourceStandaloneTables = sourceNALayer as IStandaloneTableCollection; sourceStandaloneTable = null; sourceDisplayTable = null; count = 0; if (sourceStandaloneTables != null) { count = sourceStandaloneTables.StandaloneTableCount; } for (int i = 0; i < count; ++i) { sourceStandaloneTable = sourceStandaloneTables.get_StandaloneTable(i); sourceDisplayTable = sourceStandaloneTable as IDisplayTable; if ((sourceStandaloneTable != null) && (sourceDisplayTable != null)) { // Add the table name to the combobox and the layer to the list cboInputData.Items.Add(sourceStandaloneTable.Name); m_listDisplayTable.Add(sourceDisplayTable); } } } sourceLayer = sourceLayers.Next(); } } //Select the first display table from the list if (cboInputData.Items.Count > 0) { cboInputData.SelectedIndex = 0; } // Show the window this.ShowDialog(); // If we selected a layer and clicked OK, load the locations if (m_okClicked && (cboInputData.SelectedIndex >= 0)) { // Get a cursor on the source display table (either though the selection set or table) // Use IDisplayTable because it accounts for joins, querydefs, etc. // IDisplayTable is implemented by FeatureLayers and StandaloneTables. // IDisplayTable displayTable = m_listDisplayTable[cboInputData.SelectedIndex] as IDisplayTable; ICursor cursor; if (chkUseSelection.Checked) { ISelectionSet selSet; selSet = displayTable.DisplaySelectionSet; selSet.Search(null, false, out cursor); } else { cursor = displayTable.SearchDisplayTable(null, false); } // Setup NAClassLoader and Load Locations INAClassLoader2 naClassLoader = new NAClassLoader() as INAClassLoader2; naClassLoader.Initialize(naContext, naDataset.Name, cursor); // Avoid loading network locations onto non-traversable portions of elements INALocator3 locator = naContext.Locator as INALocator3; locator.ExcludeRestrictedElements = true; locator.CacheRestrictedElements(naContext); int rowsIn = 0; int rowsLocated = 0; naClassLoader.Load(cursor, null, ref rowsIn, ref rowsLocated); return(true); } return(false); }