在我们使用过程中,经常使用Join进行连接,以下代码就是来实现join功能
using System; using System.Runtime.InteropServices; using ESRI.ArcGIS.ADF; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Geometry; namespace JoinDemo { public class JoinDemo { 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 // Open the File Geodatabase and the Parcels feature class. Type fgdbFactoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory"); IWorkspaceFactory fgdbWorkspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(fgdbFactoryType); IWorkspace fgdbWorkspace = fgdbWorkspaceFactory.OpenFromFile(@"..\..\..\Data\JoinDemo.gdb", 0); IFeatureWorkspace fgdbFeatureWorkspace = (IFeatureWorkspace)fgdbWorkspace; IFeatureClass featureClass = fgdbFeatureWorkspace.OpenFeatureClass("Parcels"); // Open the Shapefile workspace and the Owners DBF file. Type shpFactoryType = Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory"); IWorkspaceFactory shpWorkspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(shpFactoryType); IWorkspace shpWorkspace = shpWorkspaceFactory.OpenFromFile(@"..\..\..\Data", 0); IFeatureWorkspace shpFeatureWorkspace = (IFeatureWorkspace)shpWorkspace; IObjectClass objectClass = (IObjectClass)shpFeatureWorkspace.OpenTable("Owners"); // Create a memory relationship class between the two datasets. IMemoryRelationshipClassFactory memRelClassFactory = new MemoryRelationshipClassFactoryClass(); IRelationshipClass relationshipClass = memRelClassFactory.Open("ParcelsOwners", featureClass, "PROPERTY_I", objectClass, "PROPERTY_I", "Is Owned By", "Owns", esriRelCardinality.esriRelCardinalityOneToOne); // Create a RelQueryTable using the memory relationship class. IRelQueryTableFactory relQueryTableFactory = new RelQueryTableFactoryClass(); ITable relQueryTable = (ITable)relQueryTableFactory.Open(relationshipClass, true, null, null, "", true, false); // Create a query filter that finds the names of the owners of large parcels. IQueryFilter queryFilter = new QueryFilterClass { SubFields = "Parcels.PROPERTY_I, Owners.OWNER_NAME", WhereClause = "Parcels.SHAPE_AREA > 300000" }; // Find the indexes of the Property ID and Owner Name fields. int propertyIdIndex = relQueryTable.FindField("Parcels.PROPERTY_I"); int ownerNameIndex = relQueryTable.FindField("Owners.OWNER_NAME"); // Execute a query, displaying the names of the owners. using (ComReleaser comReleaser = new ComReleaser()) { ICursor cursor = relQueryTable.Search(queryFilter, true); comReleaser.ManageLifetime(cursor); IRow row = null; while ((row = cursor.NextRow()) != null) { Console.WriteLine("Owner of Property {0} is: {1}", row.get_Value(propertyIdIndex), row.get_Value(ownerNameIndex)); } } // Shutdown the licensing. aoInitialize.Shutdown(); } } }
另外还可以使用IQueryDef接口来进行关联
public void IQueryDef_Example(IWorkspace workspace) { IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace; //create query definition IQueryDef queryDef = featureWorkspace.CreateQueryDef(); //provide list of tables to join queryDef.Tables = "datesjoin,dudatest"; //retrieve the fields from all tables queryDef.SubFields = "sde.datesjoin.dt_field = sde.dudates.dt_field"; //set up join queryDef.WhereClause = "datesjoin.dt_field = dudates.dt_field"; //Create FeatureDataset. Note the use of .OpenFeatureQuery. //The name "MyJoin" is the name of the restult of the query def and //is used in place of a feature class name. IFeatureDataset featureDataset = featureWorkspace.OpenFeatureQuery("MyJoin", queryDef); //open layer to test against IFeatureClassContainer featureClassContainer = (IFeatureClassContainer)featureDataset; IFeatureClass featureClass = featureClassContainer.get_ClassByName("MyJoin"); }
源代码以及数据下载