ArcGIS Engine开发Geodatabase代码(四)——Join
/******************************************/
* 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/
在我们使用过程中,经常使用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();
}
}
}
注意:其实也可以使用sdetable -o create_view来创建关联视图,这样的话,用户可以操作视图(跟普通要素类一样)来进行查询,但是视图是只读对象,不能编辑。
另外还可以使用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");
}
转载自:https://blog.csdn.net/linghe301/article/details/8485959