创建ServiceArea
/// <summary> /// Services the area. /// </summary> /// <param name="networkDataset">The network dataset.</param> public void ServiceArea(INetworkDataset networkDataset) { log.WriteLog("开始创建ServiceArea..."); IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass(); IWorkspace workspace = workspaceFactory.OpenFromFile(Functions.g_WorkSpacePath, 0); IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace; CreateSolverContext(networkDataset); if (!LoadLocations(featureWorkspace, Functions.g_Hospital)) { log.WriteLog("LoadLocations失败!"); } log.WriteLog("ServiceArea创建完成!"); } /// <summary> /// Geodatabase function: get network dataset /// </summary> /// <param name="networkDataset">Input network dataset</param> /// <returns>DE network dataset</returns> public IDENetworkDataset GetDENetworkDataset(INetworkDataset networkDataset) { // Cast from the network dataset to the DatasetComponent IDatasetComponent dsComponent = networkDataset as IDatasetComponent; // Get the data element return dsComponent.DataElement as IDENetworkDataset; } /// <summary> /// Create NASolver and NAContext /// </summary> /// <param name="networkDataset">Input network dataset</param> private void CreateSolverContext(INetworkDataset networkDataset) { if (networkDataset == null) return; //Get the Data Element IDENetworkDataset deNDS = GetDENetworkDataset(networkDataset); INASolver naSolver = new NAServiceAreaSolverClass(); m_NAContext = naSolver.CreateContext(deNDS, naSolver.Name); ((INAContextEdit)m_NAContext).Bind(networkDataset, new GPMessagesClass()); } /// <summary> /// Loads the locations. /// </summary> /// <param name="featureWorkspace">The feature workspace.</param> /// <param name="inputFacilities">The input facilities.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> private bool LoadLocations(IFeatureWorkspace featureWorkspace, string inputFacilities) { IFeatureClass inputFeatureClass = null; try { inputFeatureClass = featureWorkspace.OpenFeatureClass(inputFacilities); } catch (Exception) { log.WriteLog("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; ((INALocator3)naClassLoader.Locator).MaxSnapTolerance = 500; 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; IQueryFilter qf = new QueryFilterClass(); qf.WhereClause = "aaaaaaaaaaa"; naClassLoader.Load(inputFeatureClass.Search(qf, true) as ICursor, null, ref rowsIn, ref rowsLocated); if (rowsLocated <= 0) { log.WriteLog("Facilities were not loaded from input feature class"); 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; }