刚刚写了新建几何网络的代码,由于对于里面的一些权重的参数设置我本身并不是很精通就不说什么了,直接按照ArcCatalog中的默认设置进行下一步的设置,上代码~
如果你想看AO文档的示例代码,请百度How to create a geodatabase network dataset
/// <summary>
/// 创建网络数据集
/// </summary>
/// <param name="featureClassPath">gdb文件的featureclass全路径</param>
/// <param name="netName">生成的网络数据集名称,可以为中文</param>
/// <returns></returns>
private bool ConstructNetworkDataset(string featureClassPath, string netName)
{
string featureClassName = featureClassPath.Substring(featureClassPath.LastIndexOf('\\') + 1);
string datasetPath = featureClassPath.Substring(0, featureClassPath.LastIndexOf('\\'));
string datasetName = datasetPath.Substring(datasetPath.LastIndexOf('\\') + 1);
string gdbPath = datasetPath.Substring(0, datasetPath.LastIndexOf('\\'));
IWorkspaceFactory pWorkspceFac = new FileGDBWorkspaceFactory();
IWorkspace pWorkspace = pWorkspceFac.OpenFromFile(gdbPath, 0);
IFeatureWorkspace featureWorkspace = pWorkspace as IFeatureWorkspace;
IFeatureDataset pFeatureDataset;//数据源数据集
IFeatureClass pFeatureClass = null;//数据源要素类
pFeatureDataset = featureWorkspace.OpenFeatureDataset(datasetName);
IFeatureClassContainer container = pFeatureDataset as IFeatureClassContainer;
pFeatureClass = container.get_ClassByName(featureClassName);//不能再打开数据的情况下get
if (pFeatureDataset != null)
{
IDENetworkDataset pDENetworkDataset = CreateNetworkDataset(pFeatureDataset, netName);
pDENetworkDataset.SupportsTurns = true;//支持turn
INetworkSource networkSource;
if (pFeatureClass != null)
{
networkSource = CreateEdgeFeatureNetworkSource(((IDataset)pFeatureClass).Name);
SetNetworkSourceWithoutSubtype(networkSource);
SetNetworkDatasetDirections("NAME", networkSource);
List<INetworkSource> sourceList = new List<INetworkSource>();
sourceList.Add(networkSource);
IEvaluatedNetworkAttribute networkAttribute = CreateNetworkSourceAttribute(sourceList, "Length", "[Shape]", "");
SetNetworkDirection(pDENetworkDataset, esriNetworkAttributeUnits.esriNAUMiles, "Length", "", "");
IArray array = new ArrayClass();
array.Add(networkAttribute);
pDENetworkDataset.Attributes = array;
IArray arraySource = new ArrayClass();
arraySource.Add(networkSource);
pDENetworkDataset.Sources = arraySource;
bool success = CreateBuildingDataset(pFeatureDataset, pDENetworkDataset as IDENetworkDataset2);
}
}
return true;
}
/// <summary>
/// 创建DE网络数据集
/// </summary>
/// <param name="pFeatureDataset">源数据集</param>
/// <param name="strNetWorkName">输出网络数据集名称</param>
/// <returns>网络数据集</returns>
private IDENetworkDataset CreateNetworkDataset(IFeatureDataset pFeatureDataset, string strNetWorkName)
{
if (string.IsNullOrEmpty(strNetWorkName) || pFeatureDataset == null)
{
return null;
}
//设置范围和空间参考
IDENetworkDataset deNetworkDataset = new DENetworkDatasetClass();
IGeoDataset geoDataset = pFeatureDataset as IGeoDataset;
IDEGeoDataset deGeoDataset = deNetworkDataset as IDEGeoDataset;
deGeoDataset.Extent = geoDataset.Extent;
deGeoDataset.SpatialReference = geoDataset.SpatialReference;
//设置名称
IDataElement dataElement = deNetworkDataset as IDataElement;
dataElement.Name = strNetWorkName;
//设置可创建
deNetworkDataset.Buildable = true;
//设置源类型
deNetworkDataset.NetworkType = esriNetworkDatasetType.esriNDTGeodatabase;
return deNetworkDataset;
}
/// <summary>
/// 创建网络源对象
/// </summary>
/// <param name="strFeatureClassName"></param>
/// <returns></returns>
private INetworkSource CreateEdgeFeatureNetworkSource(string strFeatureClassName)
{
INetworkSource edgeNetworkSource = new EdgeFeatureSourceClass();
edgeNetworkSource.Name = strFeatureClassName;
//类型
edgeNetworkSource.ElementType = esriNetworkElementType.esriNETEdge;
return edgeNetworkSource;
}
/// <summary>
/// 设置源连通性,不使用字段设置
/// </summary>
/// <param name="pNetworkSource"></param>
private void SetNetworkSourceWithoutSubtype(INetworkSource pNetworkSource)
{
IEdgeFeatureSource edgeFeatureSource = pNetworkSource as IEdgeFeatureSource;
//不使用子类
edgeFeatureSource.UsesSubtypes = false;
//分组
edgeFeatureSource.ClassConnectivityGroup = 1;
//使用终节点参与
edgeFeatureSource.ClassConnectivityPolicy = esriNetworkEdgeConnectivityPolicy.esriNECPEndVertex;
}
/// <summary>
/// 设置源Direcition
/// </summary>
/// <param name="pStreetNameFieldName">道路属性名(默认为NAME)</param>
/// <param name="pNetworkSource"></param>
private void SetNetworkDatasetDirections(string pStreetNameFieldName, INetworkSource pNetworkSource)
{
IStreetNameFields streetNameFields = new StreetNameFieldsClass();
streetNameFields.Priority = 1;
//设置名称
streetNameFields.StreetNameFieldName = pStreetNameFieldName;
//添加到集合中
IArray nsdArray = new ArrayClass();
nsdArray.Add(streetNameFields);
//创建网络方向对象
INetworkSourceDirections nsDirection = new NetworkSourceDirectionsClass();
nsDirection.StreetNameFields = nsdArray;
pNetworkSource.NetworkSourceDirections = nsDirection;
}
/// <summary>
/// 网络权重属性设置,多个源参与同一个网络数据集属性设置
/// </summary>
/// <param name="sourceList">源对象集合</param>
/// <param name="strAttributeName">属性名称</param>
/// <param name="strExpression">表达式</param>
/// <param name="preLogic">逻辑表达式,可空</param>
/// <returns></returns>
private IEvaluatedNetworkAttribute CreateNetworkSourceAttribute(List<INetworkSource> sourceList, string strAttributeName, string strExpression, string preLogic)
{
IEvaluatedNetworkAttribute pEvaluateNetworkAttribute;
INetworkAttribute2 pNetworkAttribute;
INetworkFieldEvaluator pNetFieldEval;
INetworkConstantEvaluator pNetConsEval;
pEvaluateNetworkAttribute = new EvaluatedNetworkAttributeClass();
pNetworkAttribute = pEvaluateNetworkAttribute as INetworkAttribute2;
pNetworkAttribute.Name = strAttributeName;
//设置属性
pNetworkAttribute.UsageType = esriNetworkAttributeUsageType.esriNAUTCost;
pNetworkAttribute.DataType = esriNetworkAttributeDataType.esriNADTDouble;
pNetworkAttribute.Units = esriNetworkAttributeUnits.esriNAUMeters;
pNetworkAttribute.UseByDefault = true;
//计算表达式
pNetFieldEval = new NetworkFieldEvaluatorClass();
//INetworkEvaluator networkEvaluator = pNetFieldEval as INetworkEvaluator;
pNetFieldEval.SetExpression(strExpression, preLogic);
//参与的每个源的计算表达式设置
sourceList.ForEach(pEdgeNetworkSource =>
{
//正向计算表达式
pEvaluateNetworkAttribute.set_Evaluator(pEdgeNetworkSource, esriNetworkEdgeDirection.esriNEDAlongDigitized, (INetworkEvaluator)pNetFieldEval);
//反向计算表达式pEvaluateNetworkAttribute.set_Evaluator(pEdgeNetworkSource, esriNetworkEdgeDirection.esriNEDAgainstDigitized, (INetworkEvaluator)pNetFieldEval);
});
pNetConsEval = new NetworkConstantEvaluatorClass();
pNetConsEval.ConstantValue = 0;
//设置边,交汇点和转弯默认值为常数
pEvaluateNetworkAttribute.set_DefaultEvaluator(esriNetworkElementType.esriNETEdge, (INetworkEvaluator)pNetConsEval);
pEvaluateNetworkAttribute.set_DefaultEvaluator(esriNetworkElementType.esriNETJunction, (INetworkEvaluator)pNetConsEval);
pEvaluateNetworkAttribute.set_DefaultEvaluator(esriNetworkElementType.esriNETTurn, (INetworkEvaluator)pNetConsEval);
return pEvaluateNetworkAttribute;
}
/// <summary>
/// 指定网络数据集的方向属性
/// </summary>
/// <param name="deNetworkDataset">源</param>
/// <param name="unitType">单位类型</param>
/// <param name="strLengthAttribute">长度属性名称</param>
/// <param name="strTimeAttribute">时间属性名称,可空</param>
/// <param name="strRoadClassAttribute">创建道路类型属性名称,可空</param>
private void SetNetworkDirection(IDENetworkDataset deNetworkDataset, esriNetworkAttributeUnits unitType, string strLengthAttribute, string strTimeAttribute, string strRoadClassAttribute)
{
//创建网络方向对象
INetworkDirections netDirections = new NetworkDirectionsClass();
netDirections.DefaultOutputLengthUnits = unitType;
if (!string.IsNullOrEmpty(strLengthAttribute))
{
netDirections.LengthAttributeName = strLengthAttribute;
}
if (!string.IsNullOrEmpty(strTimeAttribute))
{
netDirections.TimeAttributeName = strTimeAttribute;
}
if (!string.IsNullOrEmpty(strRoadClassAttribute))
{
netDirections.RoadClassAttributeName = strRoadClassAttribute;
}
deNetworkDataset.Directions = netDirections;
}
/// <summary>
/// 根据网络节点信息,创建网络数据集对象
/// </summary>
/// <param name="pFeatureDataset">源数据集</param>
/// <param name="pDENetworkDataset2">源网络</param>
/// <returns></returns>
private bool CreateBuildingDataset(IFeatureDataset pFeatureDataset, IDENetworkDataset2 pDENetworkDataset2)
{
IFeatureDatasetExtensionContainer pExtensionContainer = pFeatureDataset as IFeatureDatasetExtensionContainer;
IFeatureDatasetExtension pExtention = pExtensionContainer.FindExtension(esriDatasetType.esriDTNetworkDataset);
IDatasetContainer2 pContainer2 = pExtention as IDatasetContainer2;
IDEDataset pDEDataset = pDENetworkDataset2 as IDEDataset;
//创建
INetworkDataset pNetworkDataset = null;
try
{
pNetworkDataset = pContainer2.CreateDataset(pDEDataset) as INetworkDataset;
}
catch (Exception ex)
{
if (ex.Message.Contains("0x80040301"))
{
MessageBox.Show("没有找到数据集!");
}
else if (ex.Message.Contains("0x80042267"))
{
MessageBox.Show("已经存在网络数据集!");
}
return false;
}
INetworkBuild build = pNetworkDataset as INetworkBuild;
IEnvelope envelope = build.BuildNetwork(((IGeoDataset)pFeatureDataset).Extent);
return true;
}