ArcGIS Engine开发Geodatabase代码(五)——Relationship Class

/******************************************/
 * ESRI Developer Summit 2009
 * Developer's Guide to the Geodatabase
 * Code Samples
 * 6 April 2009
/******************************************/
偶然间整理电脑的文件夹,发现在Esri官网上曾经下载过关于Geodatabase开发的相关代码示例,赶紧跟各位共享一下
开发环境:
  • ArcGIS Engine9.3/9.3.1
  • VS2008
说明:该代码适用于ArcGIS Engine初学者,或者对Geodatabase开发感兴趣的朋友,如果你的Engine版本高于9.3.1,可能相关的接口会发生变化,这个需要用户自己来进行修改,但是聪明的用户不会局限于代码的是否允许,也许学习一下人家的接口使用方法,开发模式才是最重要的。

关于版本的接口差别参考:http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/Type_changes_between_9_3_and_10/000100000408000000/


以下示例主要是使用代码创建关系类

using System;
using System.IO;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;

namespace EditingDemo
{
	public class EditingDemo
	{
		public static void Main(string[] args)
		{
			#region Licensing
			// Set up the licencing. NOTE: This sample assumes that you are using ArcInfo Desktop.
			// You will need to adjust this code if using ArcEngine or ArcEditor.
			IAoInitialize aoInitialize = new AoInitializeClass();
			esriLicenseStatus licenseStatus = aoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo);
			if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut)
			{
				Console.WriteLine("Unable to check-out an ArcInfo license, error code is {0}", licenseStatus);
				return;
			}
			#endregion

			#region Data Setup
			// If any data is leftover from previous runs, delete it.
			if (Directory.Exists("Riverside.gdb"))
			{
				Directory.Delete("Riverside.gdb", true);
			}

			// Copy the geodatabase from the data directory to this directory.
			Type tempFactoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");
			IWorkspaceFactory tempWorkspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(tempFactoryType);
			IWorkspaceName sourceWorkspace = new WorkspaceNameClass
			{
				PathName = @"..\..\..\Data\Riverside.gdb",
				WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory"
			};
			IWorkspaceName copiedWorkspace = null;
			tempWorkspaceFactory.Copy(sourceWorkspace, Environment.CurrentDirectory, out copiedWorkspace);
			#endregion

			try
			{
				// Open the test data.
				Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");
				IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
				IWorkspace workspace = workspaceFactory.OpenFromFile("Riverside.gdb", 0);
				IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;

				// Open the feature classes that will participate in the relationship class.
				IFeatureClass polesFeatureClass = featureWorkspace.OpenFeatureClass("Utility_Poles");
				IFeatureClass transformersFeatureClass = featureWorkspace.OpenFeatureClass("Transformers");

				// Open the feature dataset where the relationship class will be stored.
				IFeatureDataset featureDataset = featureWorkspace.OpenFeatureDataset("Electric");
				IRelationshipClassContainer relClassContainer = (IRelationshipClassContainer)featureDataset;

				// Create a composite relationship class.
				IRelationshipClass relationshipClass = relClassContainer.CreateRelationshipClass("PolesToTransformers",
					polesFeatureClass, transformersFeatureClass, "supports", "is located on",
					esriRelCardinality.esriRelCardinalityOneToMany, esriRelNotification.esriRelNotificationForward,
					true, false, null, "Pole_ID", "", "Pole_ID", "");

				// Set the first relationship class rule.
				// One Pole (subtype: Wood) can hold a minimum of 0 and a maximum of 3 transformers.
				// A Transformer must be related to one and only one Utility Pole.
				IRelationshipRule relationshipRule = new RelationshipRuleClass
				{
					OriginClassID = polesFeatureClass.ObjectClassID,
					OriginSubtypeCode = 1, // Wooden pole subtype
					OriginMaximumCardinality = 1,
					OriginMinimumCardinality = 1,
					DestinationClassID = transformersFeatureClass.ObjectClassID,
					DestinationSubtypeCode = 0,
					DestinationMaximumCardinality = 3,
					DestinationMinimumCardinality = 0
				};

				// Add the rule to the relationship class.
				relationshipClass.AddRelationshipRule(relationshipRule);

				// Set the second relationship class rule.
				// One Pole (subtype: Steel) can hold a minimum of 0 and a maximum of 5 transformers,
				// a Transformer must be related to one and only one Utility Pole.
				IRelationshipRule relationshipRule2 = new RelationshipRuleClass
				{
					OriginClassID = polesFeatureClass.ObjectClassID,
					OriginSubtypeCode = 2, // Steel pole subtype
					OriginMaximumCardinality = 1,
					OriginMinimumCardinality = 1,
					DestinationClassID = transformersFeatureClass.ObjectClassID,
					DestinationSubtypeCode = 0,
					DestinationMaximumCardinality = 5,
					DestinationMinimumCardinality = 0
				};

				relationshipClass.AddRelationshipRule(relationshipRule2);

				// Validate the relationship rules to identify invalid features.
				IValidation validation = (IValidation)polesFeatureClass;
				ISelectionSet selectionSet = validation.Validate(null, null);
				Console.WriteLine("There are {0} features that are violating the relationship rules.", selectionSet.Count);

				// Display the Object IDs of poles that are violating the relationship rules.
				IEnumIDs enumIDs = selectionSet.IDs;
				enumIDs.Reset();
				int oid = -1;
				while ((oid = enumIDs.Next()) != -1)
				{
					Console.WriteLine("Invalid Pole OID = {0}", oid);
				}
			}
			catch (COMException ComEx)
			{
				Console.WriteLine("An error occurred: {0}, Error Code: {1}", ComEx.Message, ComEx.ErrorCode);
			}
			catch (Exception exc)
			{
				Console.WriteLine("An error occurred: {0}", exc.Message);
			}
			finally
			{
				Console.WriteLine("Done.");
			}

			aoInitialize.Shutdown();
		}
	}
}


相关源代码以及测试数据

你可能感兴趣的:(ArcGIS Engine开发Geodatabase代码(五)——Relationship Class)