本文大部分内容参考AO帮助文档和ArcMap帮助文档,大家实际使用中遇到的问题也可以在本帖下方留言交流,谢谢!
欢迎浏览,拒绝转载!
ESRI.ArcGIS.Geoprocessor.Geoprocessor geoProcessor = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
ESRI.ArcGIS.DataManagementTools.CheckGeometry checkGeometryTool = new ESRI.ArcGIS.DataManagementTools.CheckGeometry();
checkGeometryTool.in_features = otherPara[0];
checkGeometryTool.out_table = otherPara[1];
IGeoProcessorResult gpResult = geoProcessor.Execute(checkGeometryTool, null) as IGeoProcessorResult;
IPointCollection polygonVertices = new PolygonClass();
IPointCollection lineVertices = pGeometry as IPointCollection;
polygonVertices.AddPointCollection(lineVertices);
ITopologicalOperator3 pTopology = polygonVertices as ITopologicalOperator3;
esriNonSimpleReasonEnum reason = esriNonSimpleReasonEnum.esriNonSimpleSelfIntersections; //自相交
pTopology.IsKnownSimple_2 = false;
if (!pTopology.get_IsSimpleEx(out reason))
{
if (reason == esriNonSimpleReasonEnum.esriNonSimpleSelfIntersections)
{
//记录拓扑错误
}
}
创建拓扑
用到的方法:
1.ITopologyContainer.CreateTopology
2.ITopologyContainer2.CreateTopologyEx
这两个方法的返回类型都是ITopology 对象
示例代码1:
// featureDataset is an IFeatureDataset where the topology will be located.
// specifyZClusterTolerance is a System.Boolean whether a ZClusterTolerance has been specified.
// topologyName is a string with the topology's name.
// Cast the feature dataset to the ITopologyContainer2 interface to create a topology.
//下面代码大概意思是如果需要z容差用CreateTopologyEx方法,不需要z容差用CreateTopology方法
ITopologyContainer2 topologyContainer = (ITopologyContainer2)featureDataset;
ITopology topology = null;
if (specifyZClusterTolerance)
{
topology = topologyContainer.CreateTopologyEx(topologyName,
topologyContainer.DefaultClusterTolerance,
topologyContainer.DefaultZClusterTolerance, - 1, "");
}
else
{
topology = topologyContainer.CreateTopology(topologyName,
topologyContainer.DefaultClusterTolerance, - 1, "");
}
示例代码2:
IFeatureDataset featureDataset = featureClass.FeatureDataset;
ITopologyContainer2 topologyContainer = (ITopologyContainer2)featureDataset;
if (topologyContainer == null)
{
return true;
}
//判断当前命名的拓扑是否存在,如果存在,删除
bool bTopExists = (featureDataset.Workspace as IWorkspace2).get_NameExists(esriDatasetType.esriDTTopology, sTopologyName);
if (bTopExists)
{
ITopology topologyTemp = topologyContainer.get_TopologyByName(sTopologyName);
//删除拓扑
IDataset pDatasetTemp = (IDataset)topologyTemp;
pDatasetTemp.Delete();
Marshal.ReleaseComObject(pDatasetTemp);
}
//创建一个新的拓扑
if (dTolerance == -1)
{
dTolerance = topologyContainer.DefaultClusterTolerance;
}
//ITopology topology = topologyContainer.CreateTopology(sTopologyName, topologyContainer.DefaultClusterTolerance, -1, "");
ITopology topology = topologyContainer.CreateTopology(TopologyName, dTolerance, -1, "");
添加图层和拓扑规则
用到的方法:
1.ITopology.AddClass
2.ITopologyRuleContainer.AddRule
示例代码1:
//添加拓扑图层
topology.AddClass(featureClass, weight, xyRank, zRank, false);
//添加拓扑规则
public void AddRuleToTopology(ITopology topology, esriTopologyRuleType ruleType,
String ruleName, IFeatureClass featureClass)
{
// 创建拓扑规则
ITopologyRule topologyRule = new TopologyRuleClass();
topologyRule.TopologyRuleType = ruleType;
topologyRule.Name = ruleName;
topologyRule.OriginClassID = featureClass.FeatureClassID;
topologyRule.AllOriginSubtypes = true;
//把topology对象强制转换到ITopologyRuleContainer对象,然后添加拓扑规则
ITopologyRuleContainer topologyRuleContainer = (ITopologyRuleContainer)topology;
if (topologyRuleContainer.get_CanAddRule(topologyRule))
{
topologyRuleContainer.AddRule(topologyRule);
}
else
{
throw new ArgumentException("Could not add specified rule to the topology.");
}
}
示例代码2:The following code example demonstrates how to create a topology rule between two feature classes, specify it at the subtype level for the destination, and add it to the topology:
public void AddRuleToTopology(ITopology topology, esriTopologyRuleType ruleType,
String ruleName, IFeatureClass originClass, int originSubtype, IFeatureClass
destinationClass, int destinationSubtype)
{
// Create a topology rule.
ITopologyRule topologyRule = new TopologyRuleClass();
topologyRule.TopologyRuleType = ruleType;
topologyRule.Name = ruleName;
topologyRule.OriginClassID = originClass.FeatureClassID;
topologyRule.AllOriginSubtypes = false;
topologyRule.OriginSubtype = originSubtype;
topologyRule.DestinationClassID = destinationClass.FeatureClassID;
topologyRule.AllDestinationSubtypes = false;
topologyRule.DestinationSubtype = destinationSubtype;
// Cast the topology to the ITopologyRuleContainer interface and add the rule.
ITopologyRuleContainer topologyRuleContainer = (ITopologyRuleContainer)topology;
if (topologyRuleContainer.get_CanAddRule(topologyRule))
{
topologyRuleContainer.AddRule(topologyRule);
}
else
{
throw new ArgumentException("Could not add specified rule to the topology.");
}
}
验证拓扑
用到的方法:
1.ITopology.ValidateTopology
示例代码1:
public void ValidateTopology(ITopology topology, IEnvelope envelope)
{
// Get the dirty area within the provided envelope.
IPolygon locationPolygon = new PolygonClass();
ISegmentCollection segmentCollection = (ISegmentCollection)locationPolygon;
segmentCollection.SetRectangle(envelope);
IPolygon polygon = topology.get_DirtyArea(locationPolygon);
// If a dirty area exists, validate the topology.
if (!polygon.IsEmpty)
{
// Define the area to validate and validate the topology.
IEnvelope areaToValidate = polygon.Envelope;
IEnvelope areaValidated = topology.ValidateTopology(areaToValidate);
}
}
示例代码2:完整的创建拓扑函数
public void CreateTopology()
{
// Open the workspace and the required datasets.
Type factoryType = Type.GetTypeFromProgID(
"esriDataSourcesGDB.FileGDBWorkspaceFactory");
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
(factoryType);
IWorkspace workspace = workspaceFactory.OpenFromFile(@
"C:\arcgis\ArcTutor\BuildingaGeodatabase\Montgomery.gdb", 0);
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
IFeatureDataset featureDataset = featureWorkspace.OpenFeatureDataset("Landbase");
IFeatureClass blocksFC = featureWorkspace.OpenFeatureClass("Blocks");
IFeatureClass parcelsFC = featureWorkspace.OpenFeatureClass("Parcels");
// Attempt to acquire an exclusive schema lock on the feature dataset.
ISchemaLock schemaLock = (ISchemaLock)featureDataset;
try
{
schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);
// 创建拓扑
ITopologyContainer2 topologyContainer = (ITopologyContainer2)featureDataset;
ITopology topology = topologyContainer.CreateTopology("Landbase_Topology",
topologyContainer.DefaultClusterTolerance, - 1, "");
//添加要素类到拓扑中
topology.AddClass(blocksFC, 5, 1, 1, false);
topology.AddClass(parcelsFC, 5, 1, 1, false);
AddRuleToTopology(topology, esriTopologyRuleType.esriTRTAreaNoOverlap,
"No Block Overlap", blocksFC);
AddRuleToTopology(topology,
esriTopologyRuleType.esriTRTAreaCoveredByAreaClass,
"ResParcels Covered by ResBlocks", parcelsFC, 1, blocksFC, 1);
// 获取验证拓扑的范围并且验证拓扑
IGeoDataset geoDataset = (IGeoDataset)topology;
IEnvelope envelope = geoDataset.Extent;
ValidateTopology(topology, envelope);
}
catch (COMException comExc)
{
throw new Exception(String.Format(
"Error creating topology: {0} Message: {1}", comExc.ErrorCode,
comExc.Message), comExc);
}
finally
{
schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
}
}
//打开拓扑
public ITopology OpenTopologyFromFeatureWorkspace(IFeatureWorkspace featureWorkspace,
String featureDatasetName, String topologyName)
{
//打开数据集
IFeatureDataset featureDataset = featureWorkspace.OpenFeatureDataset(featureDatasetName);
//获取拓扑容器
ITopologyContainer topologyContainer = (ITopologyContainer)featureDataset;
//打开拓扑
ITopology topology = topologyContainer.get_TopologyByName(topologyName);
return topology;
}
//显示拓扑规则
public void DisplayTypesForEachRule(ITopology topology)
{
ITopologyRuleContainer topologyRuleContainer = (ITopologyRuleContainer)topology;
IEnumRule enumRule = topologyRuleContainer.Rules;
// 遍历拓扑规则.
enumRule.Reset();
IRule rule = null;
while ((rule = enumRule.Next()) != null)
{
ITopologyRule topologyRule = (ITopologyRule)rule;
Console.WriteLine("Rule type: {0}", topologyRule.TopologyRuleType);
}
}
1)根据拓扑错误读取错误要素
主要利用属性:IErrorFeatureContainer.ErrorFeatures
//给定拓扑和拓扑规则,返回指定规则的错误要素
public void DisplayErrorFeaturesForRule(ITopology topology, ITopologyRule topologyRule)
{
IErrorFeatureContainer errorFeatureContainer = (IErrorFeatureContainer)topology;
IGeoDataset geoDataset = (IGeoDataset)topology;
ISpatialReference spatialReference = geoDataset.SpatialReference;
//遍历含有拓扑错误的要素
IEnumTopologyErrorFeature enumTopologyErrorFeature =
errorFeatureContainer.get_ErrorFeatures(spatialReference, topologyRule,
geoDataset.Extent, true, false);
// 显示每个错误要素的原始ID
ITopologyErrorFeature topologyErrorFeature = null;
while ((topologyErrorFeature = enumTopologyErrorFeature.Next()) != null)
{
Console.WriteLine("Origin feature OID: {0}", topologyErrorFeature.OriginOID);
}
}
2)根据几何读取错误要素
主要利用属性:IErrorFeatureContainer.ErrorFeaturesByGeometryType
//给定拓扑和几何类型,返回指定规则的错误要素
public void DisplayErrorFeatureByGeometryType(ITopology topology, esriGeometryType
geometryType)
{
//获取坐标系
IErrorFeatureContainer errorFeatureContainer = (IErrorFeatureContainer)topology;
IGeoDataset geoDataset = (IGeoDataset)topology;
ISpatialReference spatialReference = geoDataset.SpatialReference;
//遍历拓扑错误
IEnumTopologyErrorFeature enumTopologyErrorFeature =
errorFeatureContainer.get_ErrorFeaturesByGeometryType(spatialReference,
geometryType, false);
//显示错误要素的信息
ITopologyErrorFeature topologyErrorFeature = null;
while ((topologyErrorFeature = enumTopologyErrorFeature.Next()) != null)
{
Console.WriteLine("Error Feature Origin Class ID: {0}",
topologyErrorFeature.OriginClassID);
Console.WriteLine("Error Feature Origin Feature ID: {0}",
topologyErrorFeature.OriginOID);
Console.WriteLine("Error Feature Dest. Class ID: {0}",
topologyErrorFeature.DestinationClassID);
Console.WriteLine("Error Feature Dest. Feature ID: {0}",
topologyErrorFeature.DestinationOID);
}
}
3)根据拓扑规则类型读取错误要素
主要利用属性: IErrorFeatureContainer.ErrorFeaturesByRuleType
//给定拓扑和拓扑规则类型,返回指定规则的错误要素
public void DisplayErrorFeatureByRuleType(ITopology topology, esriTopologyRuleType
topologyRuleType)
{
//获取坐标系
IErrorFeatureContainer errorFeatureContainer = (IErrorFeatureContainer)topology;
IGeoDataset geoDataset = (IGeoDataset)topology;
ISpatialReference spatialReference = geoDataset.SpatialReference;
//返回指定范围内所有的错误要素,然后检索在第一个错误要素上
IEnumTopologyErrorFeature enumTopologyErrorFeature =
errorFeatureContainer.get_ErrorFeaturesByRuleType(spatialReference,
topologyRuleType, geoDataset.Extent, true, false);
// 如果存在则获取第一个错误要素并且显示它的属性
ITopologyErrorFeature topologyErrorFeature = enumTopologyErrorFeature.Next();
if (topologyErrorFeature != null)
{
Console.WriteLine("Error Feature Origin Class ID: {0}",
topologyErrorFeature.OriginClassID);
Console.WriteLine("Error Feature Origin Feature ID: {0}",
topologyErrorFeature.OriginOID);
Console.WriteLine("Error Feature Dest. Class ID: {0}",
topologyErrorFeature.DestinationClassID);
Console.WriteLine("Error Feature Dest. Feature ID: {0}",
topologyErrorFeature.DestinationOID);
}
}
4)单独访问错误要素
主要利用属性:IErrorFeatureContainer.ErrorFeature
相关参数说明:
GeometryType—Geometry type of the error feature requested
OriginClassID—Class ID of the feature class to which the rule is applied
OriginOID—ObjectID of the origin feature causing the error
DestinationClassID—Class ID of the feature class in which the rule interacts
DestinationOID—ObjectID of the destination feature causing the error
//返回面互相压盖拓扑错误的要素
public ITopologyErrorFeature GetErrorFeatureForNoOverlapRule(ITopology topology, IFeatureClass featureClass, int originFeatureOID, int destFeatureOID)
{
// 获取坐标系
IErrorFeatureContainer errorFeatureContainer = (IErrorFeatureContainer)topology;
IGeoDataset geoDataset = (IGeoDataset)topology;
ISpatialReference spatialReference = geoDataset.SpatialReference;
// 找到拓扑错误要素并返回
ITopologyErrorFeature topologyErrorFeature = errorFeatureContainer.get_ErrorFeature(spatialReference, esriTopologyRuleType.esriTRTAreaNoOverlap,
esriGeometryType.esriGeometryPolygon, featureClass.FeatureClassID, originFeatureOID, featureClass.FeatureClassID, destFeatureOID);
return topologyErrorFeature;
}
5)根据范围读取拓扑错误要素
//给定拓扑和查找的范围,返回所有的拓扑错误要素
public void FindAllErrorFeatures(ITopology topology, IEnvelope searchExtent)
{
//获取坐标系
IErrorFeatureContainer errorFeatureContainer = (IErrorFeatureContainer)topology;
IGeoDataset geoDataset = (IGeoDataset)topology;
ISpatialReference spatialReference = geoDataset.SpatialReference;
ITopologyRuleContainer topologyRuleContainer = (ITopologyRuleContainer)topology;
//遍历拓扑规则
IEnumRule enumRule = topologyRuleContainer.Rules;
enumRule.Reset();
IRule rule = null;
while ((rule = enumRule.Next()) != null)
{
//获取当前拓扑规则的拓扑错误并遍历
ITopologyRule topologyRule = (ITopologyRule)rule;
IEnumTopologyErrorFeature enumTopologyErrorFeature = errorFeatureContainer.get_ErrorFeatures(spatialReference, topologyRule, searchExtent, true, true);
ITopologyErrorFeature topologyErrorFeature = null;
while ((topologyErrorFeature = enumTopologyErrorFeature.Next()) != null)
{
//显示错误要素的信息
Console.WriteLine("Class ID: {0} Object ID: {0} IsException {0}", topologyErrorFeature.OriginClassID, topologyErrorFeature.OriginOID,
topologyErrorFeature.IsException);
}
}
}
建议使用范围
对拓扑规则要求比较多的、比较细的、图层数据量较大的优先考虑创建拓扑,个人建议仅供参考,如果异议请留言!
本文大部分内容参考AO帮助文档和ArcMap帮助文档,大家实际使用中遇到的问题也可以在本帖下方留言交流,谢谢!
欢迎浏览,拒绝转载!