刚刚写了新建几何网络的代码,由于对于里面的一些权重的参数设置我本身并不是很精通就不说什么了,直接按照ArcCatalog中的默认设置进行下一步的设置,上代码~
如果你想看AO文档的示例代码,请百度How to create a geodatabase network dataset
///
/// 创建网络数据集
///
/// gdb文件的featureclass全路径
/// 生成的网络数据集名称,可以为中文
///
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 sourceList = new List();
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;
}
///
/// 创建DE网络数据集
///
/// 源数据集
/// 输出网络数据集名称
/// 网络数据集
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;
}
///
/// 创建网络源对象
///
///
///
private INetworkSource CreateEdgeFeatureNetworkSource(string strFeatureClassName)
{
INetworkSource edgeNetworkSource = new EdgeFeatureSourceClass();
edgeNetworkSource.Name = strFeatureClassName;
//类型
edgeNetworkSource.ElementType = esriNetworkElementType.esriNETEdge;
return edgeNetworkSource;
}
///
/// 设置源连通性,不使用字段设置
///
///
private void SetNetworkSourceWithoutSubtype(INetworkSource pNetworkSource)
{
IEdgeFeatureSource edgeFeatureSource = pNetworkSource as IEdgeFeatureSource;
//不使用子类
edgeFeatureSource.UsesSubtypes = false;
//分组
edgeFeatureSource.ClassConnectivityGroup = 1;
//使用终节点参与
edgeFeatureSource.ClassConnectivityPolicy = esriNetworkEdgeConnectivityPolicy.esriNECPEndVertex;
}
///
/// 设置源Direcition
///
/// 道路属性名(默认为NAME)
///
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;
}
///
/// 网络权重属性设置,多个源参与同一个网络数据集属性设置
///
/// 源对象集合
/// 属性名称
/// 表达式
/// 逻辑表达式,可空
///
private IEvaluatedNetworkAttribute CreateNetworkSourceAttribute(List 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;
}
///
/// 指定网络数据集的方向属性
///
/// 源
/// 单位类型
/// 长度属性名称
/// 时间属性名称,可空
/// 创建道路类型属性名称,可空
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;
}
///
/// 根据网络节点信息,创建网络数据集对象
///
/// 源数据集
/// 源网络
///
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;
}