public override void OnClick() { // Get the NALayer and corresponding NAContext of the layer that // was right-clicked on in the table of contents // m_MapControl.CustomProperty was set in frmMain.axTOCControl1_OnMouseDown INALayer naLayer = (INALayer)m_mapControl.CustomProperty; // Set the Active Analysis layer to be the layer right-clicked on m_naEnv.NAWindow.ActiveAnalysis = naLayer; if (!Enabled) { return; } // Show the Property Page form for Network Analyst frmLoadLocations loadLocations = new frmLoadLocations(); if (loadLocations.ShowModal(m_mapControl, m_naEnv)) { // notify that the context has changed because we have added locations to a NAClass within it INAContextEdit contextEdit = m_naEnv.NAWindow.ActiveAnalysis.Context as INAContextEdit; contextEdit.ContextChanged(); // If loaded locations, refresh the NAWindow and the Screen m_mapControl.Refresh(esriViewDrawPhase.esriViewGeography, naLayer, m_mapControl.Extent); m_naEnv.NAWindow.UpdateContent(m_naEnv.NAWindow.ActiveCategory); } }
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); }
private INAContext CreateSolverContext(INetworkDataset networkDataset) { IDENetworkDataset dENetworkDataset = GetDENetworkDataset(networkDataset); INASolver nASolver = new NARouteSolver(); INAContextEdit nAContextEdit = nASolver.CreateContext(dENetworkDataset, nASolver.Name) as INAContextEdit; nAContextEdit.Bind(networkDataset, null); return(nAContextEdit as INAContext); }
public INAContext CreateSolverContext(INetworkDataset pNetDataset) { IDENetworkDataset dENetworkDataset = this.GetDENetworkDataset(pNetDataset); INASolver solver = new NAClosestFacilitySolverClass(); INAContextEdit edit = solver.CreateContext(dENetworkDataset, solver.Name) as INAContextEdit; edit.Bind(pNetDataset, new GPMessagesClass()); return(edit as INAContext); }
/// <summary> /// 根据网络数据集创建网络分析上下文 /// </summary> /// <param name="networkDataset"></param> /// <returns></returns> private static INAContext CreateNAContext(INetworkDataset networkDataset) { IDENetworkDataset pDENetworkDataset = GetDENetworkDataset(networkDataset); INASolver pNASolver = new NARouteSolverClass(); INAContextEdit pNAContextEdit = pNASolver.CreateContext(pDENetworkDataset, pNASolver.Name) as INAContextEdit; pNAContextEdit.Bind(networkDataset, new GPMessagesClass()); return(pNAContextEdit as INAContext); }
/// <summary> /// Create NASolver and NAContext /// </summary> /// <param name="networkDataset">Input network dataset</param> /// <returns>NAContext</returns> public INAContext CreateSolverContext(INetworkDataset networkDataset) { //Get the data element IDENetworkDataset deNDS = GetDENetworkDataset(networkDataset); INASolver naSolver = new NAODCostMatrixSolver(); INAContextEdit contextEdit = naSolver.CreateContext(deNDS, naSolver.Name) as INAContextEdit; //Bind a context using the network dataset contextEdit.Bind(networkDataset, new GPMessagesClass()); return contextEdit as INAContext; }
//********************************************************************************* // ArcGIS Network Analyst extension functions // ******************************************************************************** //********************************************************************************* // Create NASolver and NAContext //********************************************************************************* public INAContext CreateSolverContext(INetworkDataset networkDataset) { //Get the Data Element IDENetworkDataset deNDS = GetDENetworkDataset(networkDataset); INASolver naSolver = new NALocationAllocationSolverClass(); INAContextEdit contextEdit = naSolver.CreateContext(deNDS, naSolver.Name) as INAContextEdit; contextEdit.Bind(networkDataset, new GPMessagesClass()); return(contextEdit as INAContext); }
//创建网络分析上下文 public static INAContext CreatePathSolverContext(INetworkDataset networkDataset) { IDENetworkDataset deNDS = GetPathDENetworkDataset(networkDataset); INASolver naSolver; naSolver = new NARouteSolver(); INAContextEdit contextEdit = naSolver.CreateContext(deNDS, naSolver.Name) as INAContextEdit; contextEdit.Bind(networkDataset, new GPMessagesClass()); return(contextEdit as INAContext); }
/// <summary> /// 创建网络分析上下文 /// </summary> public INAContext CreateSolverContext(INetworkDataset networkDataset) { //获取创建网络分析上下文所需的IDENETWORKDATASET类型参数 IDENetworkDataset deNDS = GetDENetworkDataset(networkDataset); INASolver naSolver; naSolver = new NARouteSolver(); INAContextEdit contextEdit = naSolver.CreateContext(deNDS, naSolver.Name) as INAContextEdit; contextEdit.Bind(networkDataset, new GPMessagesClass()); return(contextEdit as INAContext); }
//创建网络分析上下文 public static INAContext CreatePathSolverContext(INetworkDataset networkDataset) { IDENetworkDataset deNDS = GetPathDENetworkDataset(networkDataset); INASolver naSolver; naSolver = new NARouteSolver(); INAContextEdit contextEdit = naSolver.CreateContext(deNDS, naSolver.DisplayName) as INAContextEdit; contextEdit.Bind(networkDataset, new GPMessagesClass());//将网络数据集与网络上下文对象绑定在一起 //GPMessagesClass对象用于记录错误信息。 return(contextEdit as INAContext); }
private void CreateContextAndSolver(INetworkDataset networkDataset) { if (networkDataset == null) { return; } IDatasetComponent datasetComponent = networkDataset as IDatasetComponent; IDENetworkDataset deNetworkDataset = datasetComponent.DataElement as IDENetworkDataset; INASolver naSolver = new NAServiceAreaSolverClass(); m_naContext = naSolver.CreateContext(deNetworkDataset, "ServiceArea"); INAContextEdit naContextEdit = m_naContext as INAContextEdit; naContextEdit.Bind(networkDataset, new GPMessagesClass()); }
private void miLoadLocations_Click(object sender, System.EventArgs e) { IMapControl3 mapControl = (IMapControl3)axMapControl1.Object; // 显示网络分析的属性页窗体 frmLoadLocations loadLocations = new frmLoadLocations(); if (loadLocations.ShowModal(mapControl, m_naEnv)) { // 上下文已更改,已将位置添加到其中的一个naclass INAContextEdit contextEdit = m_naEnv.NAWindow.ActiveAnalysis.Context as INAContextEdit; contextEdit.ContextChanged(); // 如果加载,刷新NAWindow屏幕 INALayer naLayer = m_naWindow.ActiveAnalysis; mapControl.Refresh(esriViewDrawPhase.esriViewGeography, naLayer, mapControl.Extent); m_naWindow.UpdateContent(m_naWindow.ActiveCategory); } }
private void loadLocationsToolStripMenuItem_Click(object sender, EventArgs e) { IMapControl3 mapControl = (IMapControl3)axMapControl1.Object; // Show the Property Page form for Network Analyst frmLoadLocation loadLocations = new frmLoadLocation(); if (loadLocations.ShowModal(mapControl, m_naEnv)) { // notify that the context has changed because we have added locations to a NAClass within it INAContextEdit contextEdit = m_naEnv.NAWindow.ActiveAnalysis.Context as INAContextEdit; contextEdit.ContextChanged(); // If loaded locations, refresh the NAWindow and the Screen INALayer naLayer = m_naWindow.ActiveAnalysis; mapControl.Refresh(esriViewDrawPhase.esriViewGeography, naLayer, mapControl.Extent); m_naWindow.UpdateContent(m_naWindow.ActiveCategory); } }
/// <summary> /// Create a new network analysis layer and set some solver settings /// </summary> private INALayer CreateRouteAnalysisLayer(String layerName, INetworkDataset networkDataset) { INARouteSolver naRouteSolver = new NARouteSolverClass(); INASolverSettings naSolverSettings = naRouteSolver as INASolverSettings; INASolver naSolver = naRouteSolver as INASolver; //Get the NetworkDataset's Data Element IDatasetComponent datasetComponent = networkDataset as IDatasetComponent; IDENetworkDataset deNetworkDataset = datasetComponent.DataElement as IDENetworkDataset; //Create the NAContext and bind to it INAContext naContext; naContext = naSolver.CreateContext(deNetworkDataset, layerName); INAContextEdit naContextEdit = naContext as INAContextEdit; naContextEdit.Bind(networkDataset, new GPMessagesClass()); //Create the NALayer INALayer naLayer; naLayer = naSolver.CreateLayer(naContext); (naLayer as ILayer).Name = layerName; //Set properties on the route solver interface naRouteSolver.FindBestSequence = true; naRouteSolver.PreserveFirstStop = true; naRouteSolver.PreserveLastStop = false; naRouteSolver.UseTimeWindows = false; naRouteSolver.OutputLines = esriNAOutputLineType.esriNAOutputLineTrueShapeWithMeasure; //Set some properties on the general INASolverSettings interface IStringArray restrictions = naSolverSettings.RestrictionAttributeNames; restrictions.Add("Oneway"); naSolverSettings.RestrictionAttributeNames = restrictions; // Update the context based on the changes made to the solver settings naSolver.UpdateContext(naContext, deNetworkDataset, new GPMessagesClass()); //Return the layer return(naLayer); }
/// <summary> /// Set solver settings /// </summary> /// <param name="strNAClassName">NAClass name</param> /// <param name="inputFC">Input feature class</param> /// <param name="snapTolerance">Snap tolerance</param> public void LoadNANetworkLocations(string strNAClassName, IFeatureClass inputFC, double snapTolerance) { 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 loader = new NAClassLoader(); loader.Locator = m_NAContext.Locator; if (snapTolerance > 0) { loader.Locator.SnapTolerance = snapTolerance; } loader.NAClass = naClass; // Create field map to automatically map fields from input class to NAClass INAClassFieldMap fieldMap = new NAClassFieldMapClass(); fieldMap.CreateMapping(naClass.ClassDefinition, inputFC.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 network locations int rowsIn = 0; int rowsLocated = 0; loader.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 naContextEdit = m_NAContext as INAContextEdit; naContextEdit.ContextChanged(); }
private void miAddItem_Click(object sender, System.EventArgs e) { //可以双击该项来编辑属性 //只为新项填充initDefaultValues方法中的默认值和自动生成的名称值。 IMapControl3 mapControl = (IMapControl3)axMapControl1.Object; IEngineNAWindowCategory2 activeCategory = m_naWindow.ActiveCategory as IEngineNAWindowCategory2; IDataLayer pDataLayer = activeCategory.DataLayer; // 在类中创建一个新行并填充并初始默认值 ITable table = pDataLayer as ITable; IRow row = table.CreateRow(); IRowSubtypes rowSubtypes = row as IRowSubtypes; rowSubtypes.InitDefaultValues(); // 自动生成显示名称 IFeatureLayer ipFeatureLayer = activeCategory.Layer as IFeatureLayer; IStandaloneTable ipStandaloneTable = pDataLayer as IStandaloneTable; string name = ""; if (ipFeatureLayer != null) { name = ipFeatureLayer.DisplayField; } else if (ipStandaloneTable != null) { name = ipStandaloneTable.DisplayField; } //如果显示字段为空字符串或不代表NaClass上的实际字段,则跳过自动生成 string currentName = ""; int fieldIndex = row.Fields.FindField(name); if (fieldIndex >= 0) { currentName = row.get_Value(fieldIndex) as string; if (currentName.Length <= 0) { row.set_Value(fieldIndex, "Item" + ++autogenInt); } } INAClassDefinition naClassDef = activeCategory.NAClass.ClassDefinition; if (naClassDef.Name == "OrderPairs") { fieldIndex = row.Fields.FindField("SecondOrderName"); if (fieldIndex >= 0) { string secondName = row.get_Value(fieldIndex) as string; if (secondName.Length <= 0) { row.set_Value(fieldIndex, "Item" + ++autogenInt); } } } row.Store(); // 向naclass添加项 INAContextEdit contextEdit = m_naEnv.NAWindow.ActiveAnalysis.Context as INAContextEdit; contextEdit.ContextChanged(); // 刷新 NAWindow 和屏幕界面 INALayer naLayer = m_naWindow.ActiveAnalysis; mapControl.Refresh(esriViewDrawPhase.esriViewGeography, naLayer, mapControl.Extent); m_naWindow.UpdateContent(m_naWindow.ActiveCategory); }
private void addItemToolStripMenuItem_Click(object sender, EventArgs e) { IMapControl3 mapControl = (IMapControl3)axMapControl1.Object; IEngineNAWindowCategory2 activeCategory = m_naWindow.ActiveCategory as IEngineNAWindowCategory2; IDataLayer pDataLayer = activeCategory.DataLayer; // In order to add an item, we need to create a new row in the class and populate it // with the initial default values for that class. ITable table = pDataLayer as ITable; IRow row = table.CreateRow(); IRowSubtypes rowSubtypes = row as IRowSubtypes; rowSubtypes.InitDefaultValues(); IFeatureLayer ipFeatureLayer = activeCategory.Layer as IFeatureLayer; IStandaloneTable ipStandaloneTable = pDataLayer as IStandaloneTable; string name = ""; if (ipFeatureLayer != null) { name = ipFeatureLayer.DisplayField; } else if (ipStandaloneTable != null) { name = ipStandaloneTable.DisplayField; } string currentName = ""; int fieldIndex = row.Fields.FindField(name); if (fieldIndex >= 0) { currentName = row.get_Value(fieldIndex) as string; if (currentName.Length <= 0) { row.set_Value(fieldIndex, "Item" + ++autogenInt); } } INAClassDefinition naClassDef = activeCategory.NAClass.ClassDefinition; if (naClassDef.Name == "OrderPairs") { fieldIndex = row.Fields.FindField("SecondOrderName"); if (fieldIndex >= 0) { string secondName = row.get_Value(fieldIndex) as string; if (secondName.Length <= 0) { row.set_Value(fieldIndex, "Item" + ++autogenInt); } } } row.Store(); // notify that the context has changed because we have added an item to a NAClass within it INAContextEdit contextEdit = m_naEnv.NAWindow.ActiveAnalysis.Context as INAContextEdit; contextEdit.ContextChanged(); // refresh the NAWindow and the Screen INALayer naLayer = m_naWindow.ActiveAnalysis; mapControl.Refresh(esriViewDrawPhase.esriViewGeography, naLayer, mapControl.Extent); m_naWindow.UpdateContent(m_naWindow.ActiveCategory); }