c#,arcengine,Arcobjects建立拓扑的完整例子

 

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);

    }

}

public void AddRuleToTopology(ITopology topology, esriTopologyRuleType ruleType,

String ruleName, IFeatureClass originClass, int originSubtype, IFeatureClass

destinationClass, int destinationSubtype)

{

    // Create a new 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.")

        ;

    }

}

public void AddRuleToTopology(ITopology topology, esriTopologyRuleType ruleType,

String ruleName, IFeatureClass featureClass)

{

    // Create a new topology rule.

    ITopologyRule topologyRule = new TopologyRuleClass();

    topologyRule.TopologyRuleType = ruleType;

    topologyRule.Name = ruleName;

    topologyRule.OriginClassID = featureClass.FeatureClassID;

    topologyRule.AllOriginSubtypes = true;

 

    // 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.")

        ;

    }

}

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);

    }

}

public void CreateTopology()

{

    // Open the workspace and the required datasets.

    IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();

    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);

 

        // Create the topology.

        ITopologyContainer2 topologyContainer = (ITopologyContainer2)featureDataset;

        ITopology topology = topologyContainer.CreateTopology("Landbase_Topology",

        topologyContainer.DefaultClusterTolerance, - 1, "");

 

        // Add feature classes and rules to the topology.

        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);

 

        // Get an envelope with the topology's extents and validate the topology.

        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);

    }

}

 

 来自:arcengine的帮助

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值