GIS二次开发第7课教程课件

上传人:博****1 文档编号:567924557 上传时间:2024-07-22 格式:PPT 页数:117 大小:2.34MB
返回 下载 相关 举报
GIS二次开发第7课教程课件_第1页
第1页 / 共117页
GIS二次开发第7课教程课件_第2页
第2页 / 共117页
GIS二次开发第7课教程课件_第3页
第3页 / 共117页
GIS二次开发第7课教程课件_第4页
第4页 / 共117页
GIS二次开发第7课教程课件_第5页
第5页 / 共117页
点击查看更多>>
资源描述

《GIS二次开发第7课教程课件》由会员分享,可在线阅读,更多相关《GIS二次开发第7课教程课件(117页珍藏版)》请在金锄头文库上搜索。

1、GIS二次开发二次开发主讲:张云鹏主讲:张云鹏第七课第七课 空间数据管理空间数据管理nGeoDatabase的基的基础知知识nWorkspace及相关及相关对象象nDataset对象象n表、表、对象象类和要素和要素类nROW,Object和和Feature对象象7.1 GeoDatabase的基础知识n在在AO(AE)中,各种类型的空间数据都采用)中,各种类型的空间数据都采用统一的对象模型统一的对象模型Geodatabase数据模型数据模型进行操作。进行操作。7.2 Workspace及相关对象n在在Geodatabase数据模型中,数据模型中,一个工作空间一个工作空间对应于对应于一个一个ge

2、odatabase或或一个一个ArcInfocoverage工作空间工作空间或或一个文一个文件夹(内有地理数据文件)。工作空间件夹(内有地理数据文件)。工作空间是空间数据集与是空间数据集与非空间数据集的容器。非空间数据集的容器。n由由esriWorkspaceType枚举类型指定的枚举类型指定的Workspace有三种类型:有三种类型:esriFileSystemWorkspaceesriFileSystemWorkspace:基于文件类型的基于文件类型的ShapefilesShapefiles和和ArcInfo ArcInfo 的工作空间的工作空间esriLocalDatabaseWorks

3、paceesriLocalDatabaseWorkspace:Personal/File geodatabase Personal/File geodatabase esriRemoteDatabaseWorkspaceesriRemoteDatabaseWorkspace: ArcSDE geodatabase ArcSDE geodatabase n其它其它workspace类型包括:类型包括:Raster Workspace:Raster Workspace: A workspace containing Grids and A workspace containing Grids an

4、d Images. Images. Tin Workspace:Tin Workspace: A workspace containing TINs. A workspace containing TINs. CAD Workspace:CAD Workspace: A workspace containing CAD drawings. A workspace containing CAD drawings. VPF Workspace:VPF Workspace: A workspace containing VPF data. A workspace containing VPF dat

5、a. n要操作各种类型的空间数据,首先要获得空间数据所在的工要操作各种类型的空间数据,首先要获得空间数据所在的工作空间。作空间。Workspace(工作空间)是一个普通类(工作空间)是一个普通类(Class),),这意味着用户不能直接新建它。这意味着用户不能直接新建它。为了获得一个工作空间,需为了获得一个工作空间,需要使用要使用WorkspaceFactory对象来对象来创建创建或或打开打开一个一个Workspace。nWorkspaceFactory是是GeoDatabase的入口。它是一个抽象类,的入口。它是一个抽象类,派生了很多的子类,例如派生了很多的子类,例如SdeWorkspaceF

6、actory,AccessWorkspaceFactory,ShapefileWorkspaceFactory,FileGDBWorkspaceFactory等。等。不同类型的空间数据需要不不同类型的空间数据需要不同的工作空间工厂对象来打开一个工作空间。同的工作空间工厂对象来打开一个工作空间。Classes that implement IWorkspaceFactoryClassesClassesAccessWorkspaceFactory (esriDataSourcesGDB)PlugInWorkspaceFactoryAMSWorkspaceFactory (esriTrackingA

7、nalyst)RasterWorkspaceFactory (esriDataSourcesRaster)ArcInfoWorkspaceFactory (esriDataSourcesFile)SDCWorkspaceFactory (esriDataSourcesFile)CadWorkspaceFactory (esriDataSourcesFile)SdeWorkspaceFactory (esriDataSourcesGDB)ExcelWorkspaceFactory (esriDataSourcesOleDB)ShapefileWorkspaceFactory (esriDataS

8、ourcesFile)FileGDBWorkspaceFactory (esriDataSourcesGDB)SqlWorkspaceFactory (esriDataSourcesGDB)GeoRSSWorkspaceFactory (esriDataSourcesFile)StreetMapWorkspaceFactory (esriDataSourcesFile)IMSWorkspaceFactory (esriGISClient)TextFileWorkspaceFactory (esriDataSourcesOleDB)InMemoryWorkspaceFactory (esriDa

9、taSourcesGDB)TinWorkspaceFactory (esriDataSourcesFile)NetCDFWorkspaceFactory (esriDataSourcesNetCDF)ToolboxWorkspaceFactory (esriGeoprocessing)OLEDBWorkspaceFactory (esriDataSourcesOleDB) VpfWorkspaceFactory (esriDataSourcesFile)PCCoverageWorkspaceFactory (esriDataSourcesFile)nIWorkspaceFactory接口定接口

10、定义了义了所有工作空间工厂对所有工作空间工厂对象的一般属性和方法象的一般属性和方法,用,用户可以通过它管理不同类户可以通过它管理不同类型的工作空间。型的工作空间。IWorkspaceFactoryn使用使用IWorkspaceFactory:Create方法可以用于产生一个新的工作空间方法可以用于产生一个新的工作空间名称对象名称对象。public public IWorkspaceNameIWorkspaceName Create ( Create (string string parentDirectory,parentDirectory, string string Name,Name,

11、IPropertySet IPropertySet ConnectionProperties,ConnectionProperties, int int hWndhWnd ););nIWorkspaceFactory:Open方法和方法和IWorkspaceFactory:OpenFromFile方法可以方法可以用于打开一个已经存在的工作空间。用于打开一个已经存在的工作空间。public public IWorkspaceIWorkspace Open ( Open ( IPropertySetIPropertySet ConnectionProperties, ConnectionPrope

12、rties, int hWnd);int hWnd);public public IWorkspaceIWorkspace OpenFromFile ( string fileName, int hWnd); OpenFromFile ( string fileName, int hWnd);nIWorkspaceFactory:WorkspaceType属性可以返回工作空间的类型。属性可以返回工作空间的类型。7.2.1 How to create new geodatabasesnCreatingapersonalgeodatabaseworkspacestoredinAccesspriva

13、te private IWorkspaceIWorkspace CreateAccessWorkspace ()CreateAccessWorkspace () IWorkspaceFactoryIWorkspaceFactory pAccessWorkspaceFactory = new pAccessWorkspaceFactory = new AccessWorkspaceFactoryAccessWorkspaceFactoryClass();Class();IWorkspaceNamepWorkspaceName=pAccessWorkspaceFactory.Create(d:te

14、mp,test.mdb,null,0);ESRI.ArcGIS.esriSystem.INamename=(IName)pWorkspaceName;IWorkspacepGDB_Wor=(IWorkspace)name.Open();returnpGDB_Wor; Creating a file geodatabase workspace / Create a file geodatabase workspace factory. / Create a file geodatabase workspace factory. IWorkspaceFactoryIWorkspaceFactory

15、 workspaceFactory = new workspaceFactory = new FileGDBWorkspaceFactoryFileGDBWorkspaceFactoryClassClass(); (); / Create a new file geodatabase. / Create a new file geodatabase. IWorkspaceNameIWorkspaceName workspaceName = workspaceFactory. workspaceName = workspaceFactory.CreateCreate(C:temp, (C:tem

16、p, MyFGDB.gdb, null, 0); MyFGDB.gdb, null, 0); INameIName name = (IName)workspaceName; name = (IName)workspaceName; /Open a reference to the file geodatabase workspace through the name /Open a reference to the file geodatabase workspace through the name object. object. IWorkspaceIWorkspace fGDB_Wor

17、= (IWorkspace)name. fGDB_Wor = (IWorkspace)name.OpenOpen(); (); npublicstaticIWorkspaceCreateFileGdbWorkspace(Stringpath) TypeType factoryType = factoryType = Type.GetTypeFromProgID(esriDataSourcesGDB.FileGDBWorkspaceFactoryType.GetTypeFromProgID(esriDataSourcesGDB.FileGDBWorkspaceFactory);); IWorks

18、paceFactoryIWorkspaceFactory workspaceFactory = ( workspaceFactory = (IWorkspaceFactoryIWorkspaceFactory) ) Activator.CreateInstance(factoryType);Activator.CreateInstance(factoryType); IWorkspaceNameIWorkspaceName workspaceName = workspaceFactory.Create(path, workspaceName = workspaceFactory.Create(

19、path, Sample.gdb, null, 0);Sample.gdb, null, 0); INameIName name = ( name = (INameIName) workspaceName;) workspaceName; IWorkspaceIWorkspace workspace = ( workspace = (IWorkspaceIWorkspace) name.Open();) name.Open(); return workspace; return workspace; Creating a connection file (.sde) to an Enterpr

20、ise ArcSDE workspace npublicIWorkspaceNameopen_ArcSDE_Workspace(stringserver,stringinstance,stringuser,stringpassword,stringdatabase,stringversion) IPropertySetIPropertySet propertySet = new PropertySetClass(); propertySet = new PropertySetClass(); propertySet.SetProperty( propertySet.SetProperty(SE

21、RVERSERVER, server);, server); propertySet.SetProperty( propertySet.SetProperty(INSTANCEINSTANCE, instance);, instance); propertySet.SetProperty( propertySet.SetProperty(DATABASEDATABASE, database);, database); propertySet.SetProperty( propertySet.SetProperty(USERUSER, user);, user); propertySet.Set

22、Property( propertySet.SetProperty(PASSWORDPASSWORD, password);, password); propertySet.SetProperty( propertySet.SetProperty(VERSIONVERSION, version);, version); IWorkspaceFactory2IWorkspaceFactory2 workspaceFactory = (IWorkspaceFactory2) workspaceFactory = (IWorkspaceFactory2) new new SdeWorkspaceFa

23、ctoryClassSdeWorkspaceFactoryClass();(); return workspaceFactory.Create(d:temp, myconnection.sde, return workspaceFactory.Create(d:temp, myconnection.sde, propertySet, 0);propertySet, 0); Creating a geodatabase in a personal or workgroup ArcSDE workspace / Create a data server manager object./ Creat

24、e a data server manager object.IDataServerManagerIDataServerManager dataserverManager = new DataServerManagerClass(); dataserverManager = new DataServerManagerClass();/ Set the server name and connect to the server./ Set the server name and connect to the server.dataserverManager.ServerName = tivosq

25、lexpress;dataserverManager.ServerName = tivosqlexpress;dataserverManager.Connect();dataserverManager.Connect();/ Open one of the geodatabases in the database server./ Open one of the geodatabases in the database server.IDataServerManagerAdminIDataServerManagerAdmin dataservermanagerAdmin = dataserve

26、rmanagerAdmin = (IDataServerManagerAdmin)dataserverManager;(IDataServerManagerAdmin)dataserverManager;IWorkspaceNameIWorkspaceName workspaceName = workspaceName = dataservermanagerAdmin.CreateWorkspaceName(sewer, VERSION, dataservermanagerAdmin.CreateWorkspaceName(sewer, VERSION, dbo.Default);dbo.De

27、fault);INameIName name = (IName)workspaceName; name = (IName)workspaceName;IWorkspaceIWorkspace GDB_wor = (IWorkspace)name.Open(); GDB_wor = (IWorkspace)name.Open();7.2.2 How to connect to a geodatabasen有有3种不同的方法打开工作空间:种不同的方法打开工作空间:IWorkspaceFactory.OpenIWorkspaceFactory.Open npublicIWorkspaceOpen(I

28、PropertySetConnectionProperties,inthWnd);IWorkspaceFactory.OpenFromFileIWorkspaceFactory.OpenFromFile npublicIWorkspaceOpenFromFile(stringfileName,inthWnd);IWorkspaceFactory2.OpenFromStringIWorkspaceFactory2.OpenFromStringnpublicIWorkspaceOpenFromString(stringconnectStr,inthWnd);nTheconnectstringisa

29、collectionofnamevaluepairsseparatedbyasemicolon(;).the pathname of a file or directory the pathname of a file or directory that represents either a that represents either a FileSystemWorkspaceFileSystemWorkspace or or LocalDatabaseWorkspaceLocalDatabaseWorkspace, or , or a a connection file to a Rem

30、oteDatabase connection file to a RemoteDatabase workspaceworkspaceThe The hWndhWnd argument is the parent argument is the parent window or applications window. window or applications window. The The hWnd will guarantee that the hWnd will guarantee that the connection dialog, if presented to connecti

31、on dialog, if presented to you because of insufficient you because of insufficient properties, has the correct parent.properties, has the correct parent.(1)打开Personal GeoDatabae数据库n/Forexample,database=C:myDatamypGDB.mdb.public public IWorkspaceIWorkspace open_pGDB_Workspace(string database) open_pG

32、DB_Workspace(string database) IPropertySetIPropertySet propertySet = new PropertySetClass(); propertySet = new PropertySetClass(); propertySet.SetProperty(DATABASE, database); propertySet.SetProperty(DATABASE, database); IWorkspaceFactoryIWorkspaceFactory workspaceFactory = new workspaceFactory = ne

33、w AccessWorkspaceFactoryClass(); AccessWorkspaceFactoryClass(); return workspaceFactory. return workspaceFactory.OpenOpen(propertySet, 0); (propertySet, 0); public public IWorkspaceIWorkspace OpenOpen ( ( IPropertySetIPropertySet ConnectionProperties,ConnectionProperties, int int hWndhWnd ); ); n/Fo

34、rexample,fileName=C:myDatamypGDB.mdb.public public IWorkspaceIWorkspace openFromFilepGDBWorkspace(string openFromFilepGDBWorkspace(string fileNamefileName) ) IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass(); IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass()

35、; return workspaceFactory.return workspaceFactory.OpenFromFileOpenFromFile( (fileNamefileName, 0); , 0); public IWorkspace public IWorkspace OpenFromFileOpenFromFile ( string ( string fileName,fileName, int int hWndhWnd ); ); n/Forexample,connectionString=DATABASE=C:myDatamypGDB.mdb.public public IW

36、orkspaceIWorkspace openFromStringpGDBWorkspace(string connectionString) openFromStringpGDBWorkspace(string connectionString) IWorkspaceFactory2IWorkspaceFactory2 workspaceFactory = (IWorkspaceFactory2)new workspaceFactory = (IWorkspaceFactory2)new AccessWorkspaceFactoryClass(); AccessWorkspaceFactor

37、yClass(); return workspaceFactory. return workspaceFactory.OpenFromStringOpenFromString(connectionString, 0); (connectionString, 0); public IWorkspace public IWorkspace OpenFromStringOpenFromString ( string ( string connectStr,connectStr, int int hWndhWnd ); ); SERVER=myServer;DATABASE=sde;INSTANCE=

38、5151;USER=Editor;PASSWORD=Editor;VERSSERVER=myServer;DATABASE=sde;INSTANCE=5151;USER=Editor;PASSWORD=Editor;VERSION=SDE.DEFAULTION=SDE.DEFAULT(2)Connecting to a file geodatabase workspace n/Forexample,database=C:myDatamyfGDB.gdb.public public IWorkspaceIWorkspace open_fGDB_Workspace(string database)

39、 open_fGDB_Workspace(string database) IPropertySetIPropertySet propertySet = new PropertySetClass(); propertySet = new PropertySetClass(); propertySet.SetProperty(DATABASE, database); propertySet.SetProperty(DATABASE, database); IWorkspaceFactory IWorkspaceFactory workspaceFactory = new workspaceFac

40、tory = new FileGDBWorkspaceFactoryFileGDBWorkspaceFactoryClass(); Class(); return workspaceFactory. return workspaceFactory.OpenOpen(propertySet, 0); (propertySet, 0); public public IWorkspaceIWorkspace Open ( Open ( IPropertySetIPropertySet ConnectionProperties,ConnectionProperties, int int hWndhWn

41、d ); ); n/Forexample,fileName=C:myDatamyfGDB.gdb.public public IWorkspaceIWorkspace openFromFile_fGDB_Workspace(String openFromFile_fGDB_Workspace(String fileNamefileName ) ) IWorkspaceFactoryIWorkspaceFactory workspaceFactory = new workspaceFactory = new FileGDBWorkspaceFactoryClass(); FileGDBWorks

42、paceFactoryClass(); return workspaceFactory. return workspaceFactory.OpenFromFileOpenFromFile( (fileNamefileName , 0); , 0); public IWorkspace OpenFromFile ( string public IWorkspace OpenFromFile ( string fileName,fileName, int int hWndhWnd ); );n/Forexample,connectionString=DATABASE=C:myDatamyfGDB.

43、gdb.public public IWorkspaceIWorkspace openFromString_fGDB_Workspace(string connectionString) openFromString_fGDB_Workspace(string connectionString) IWorkspaceFactory2 workspaceFactory = (IWorkspaceFactory2)new IWorkspaceFactory2 workspaceFactory = (IWorkspaceFactory2)new FileGDBWorkspaceFactoryClas

44、s(); FileGDBWorkspaceFactoryClass(); return workspaceFactory. return workspaceFactory.OpenFromStringOpenFromString(connectionString, 0); (connectionString, 0); public IWorkspace OpenFromString ( string public IWorkspace OpenFromString ( string connectStr,connectStr, int int hWndhWnd ); ); SERVER=myS

45、erver;DATABASE=sde;INSTANCE=5151;USER=Editor;PASSWORD=Editor;VESERVER=myServer;DATABASE=sde;INSTANCE=5151;USER=Editor;PASSWORD=Editor;VERSION=SDE.DEFAULTRSION=SDE.DEFAULT(3)Connecting to an enterprise ArcSDE geodatabase workspace npublicIWorkspaceopen_ArcSDE_Workspace(stringserver,stringinstance,str

46、inguser,stringpassword,stringdatabase,stringversion) IPropertySetIPropertySet propertySet = new PropertySetClass(); propertySet = new PropertySetClass(); propertySet.SetProperty( propertySet.SetProperty(SERVERSERVER, server); , server); propertySet.SetProperty( propertySet.SetProperty(INSTANCEINSTAN

47、CE, instance); , instance); propertySet.SetProperty( propertySet.SetProperty(DATABASEDATABASE, database); , database); propertySet.SetProperty( propertySet.SetProperty(USERUSER, user); , user); propertySet.SetProperty( propertySet.SetProperty(PASSWORDPASSWORD, password); , password); propertySet.Set

48、Property( propertySet.SetProperty(VERSIONVERSION, version);, version); IWorkspaceFactory workspaceFactory = new IWorkspaceFactory workspaceFactory = new SdeWorkspaceFactoryClass(); return SdeWorkspaceFactoryClass(); return workspaceFactory.workspaceFactory.OpenOpen(propertySet, 0); (propertySet, 0);

49、 (4)打开Shapefile工作空间npublicIWorkspaceOpenShapfileWorkspace(stringLocation) IWorkspaceFactoryIWorkspaceFactory wsf = new wsf = new ShapefileWorkspaceFactoryClass();ShapefileWorkspaceFactoryClass();IWorkspaceIWorkspace ws = wsf.OpenFromFile(Location,0); ws = wsf.OpenFromFile(Location,0);return ws;retur

50、n ws; 7.2.3 Workspace主要接口WorkspaceWorkspace实现的主要接口如下表所示实现的主要接口如下表所示. .InterfacesDescriptionIDatasetContainer3Provides access to adding datasets to the dataset container.IFeatureWorkspaceProvides access to members that create and open various types of datasets and other workspace level objects.IFeatu

51、reWorkspaceSchemaEditProvides access to altering the instance CLSID and the class extension CLSID.ITopologyWorkspaceProvides access to members that access the topologies in a workspace.ITransactionsProvides access to members that control Transaction management.ITransactionsOptionsProvides access to

52、members that control Transaction options.IWorkspace2Provides access to members that have information about the workspace.IWorkspaceConfigurationProvides access to configuration keywords.IWorkspaceDomainsProvides access to members that return information about domains and allows you to add or delete

53、domains.IWorkspaceDomains2Provides access to members that allow you to alter a domain.IWorkspaceEditProvides access to members that control Workspace Editing.IWorkspaceReplicaDatasetsProvides access to members that return a list of replica datasets.IWorkspaceReplicasProvides access to members that r

54、eturn objects containing information about replicas.IWorkspaceReplicasAdmin3Provides access to members that modify information about a replica.IWorkspace 接口nIWorkspace接口定义了一个工接口定义了一个工作空间最普通的属性和方法。作空间最普通的属性和方法。 ConnectionPropertiesConnectionProperties返回工作返回工作空间的连接属性集对象;空间的连接属性集对象;DatasetsDatasets属性可以

55、按照数据集属性可以按照数据集(Dataset)(Dataset)的类型而返回一个数据的类型而返回一个数据集枚举对象;集枚举对象;TypeType和和WorkspaceFactoryWorkspaceFactory属性则可属性则可以分别返回工作空间的类型和工作以分别返回工作空间的类型和工作空间工厂的种类。空间工厂的种类。 npublicIEnumDatasetget_Datasets(esriDatasetTypeDatasetType);The The get_Datasets get_Datasets method returns an enumerator over the method

56、returns an enumerator over the datasets in the workspace of the specified dataset datasets in the workspace of the specified dataset type.type.Using a dataset type of esriDTAny will return all the Using a dataset type of esriDTAny will return all the datasets or datasets in the workspace, across all

57、 datasets or datasets in the workspace, across all dataset types. dataset types. nNotethatonlythetop-leveldatasetsintheworkspacearereturnedbythesemethods.Inparticular,ifaworkspacecontainsbothstand-alonefeatureclassesandadditionalfeatureclassesthatarepartofafeaturedataset,thencallingtheget_Datasetson

58、theworkspacewithadatasettypeofesriDTFeatureClasswillreturnonlythestandalonefeatureclasses.n/拷贝数据库模式:包括属性域、要素数据集、要素类(包括要素拷贝数据库模式:包括属性域、要素数据集、要素类(包括要素数据集中的要素类和独立的要素类)、表数据集中的要素类和独立的要素类)、表 private void CreateSchema()private void CreateSchema() CopyDomains(sourceWorkspace, targetWorkspace); CopyDomains(s

59、ourceWorkspace, targetWorkspace); IEnumDatasetIEnumDataset sourceFeatureDatasets = sourceFeatureDatasets = sourceWorkspace.sourceWorkspace.get_Datasetsget_Datasets(esriDatasetType.(esriDatasetType.esriDTFeatureDatasetesriDTFeatureDataset);); CreateFeatureDatasetsAndFeatureClasses(sourceFeatureDatase

60、ts); CreateFeatureDatasetsAndFeatureClasses(sourceFeatureDatasets); IEnumDatasetIEnumDataset featureClasses = featureClasses = sourceWorkspace.sourceWorkspace.get_Datasetsget_Datasets(esriDatasetType.(esriDatasetType.esriDTFeatureClassesriDTFeatureClass);); CreateFeatureClassesInWorkspace(featureCla

61、sses); CreateFeatureClassesInWorkspace(featureClasses); IEnumDatasetIEnumDataset tables = tables = sourceWorkspace.sourceWorkspace.get_Datasetsget_Datasets(esriDatasetType.(esriDatasetType.esriDTTableesriDTTable);); CreateTablesInWorkspace(tables); CreateTablesInWorkspace(tables); IFeatureWorkspace接

62、口n主要用于管理基于要素的数主要用于管理基于要素的数据集,如表(据集,如表(Table)、对象)、对象类(类(ObjectClass)、要素类)、要素类(FeatureClass)、要素数据)、要素数据集(集(FeatureDataset)和关系)和关系类(类(RelationshipClass)等。)等。nprivatevoidmnuAddFeatureClassToMap_Click(objectsender,System.EventArgse) IWorkspaceFactoryIWorkspaceFactory wsf = new ShapefileWorkspaceFactoryCl

63、ass(); wsf = new ShapefileWorkspaceFactoryClass(); IFeatureWorkspaceIFeatureWorkspace pFeatWorkspace = pFeatWorkspace = wsf.OpenFromFile(D:ArcTutorusa,0) as IFeatureWorkspace;wsf.OpenFromFile(D:ArcTutorusa,0) as IFeatureWorkspace;IFeatureClassIFeatureClass pFeatClass = pFeatClass = pFeatWorkspace.Op

64、enFeatureClass(counties);pFeatWorkspace.OpenFeatureClass(counties);IFeatureLayerIFeatureLayer pFeatLyr = new FeatureLayerClass(); pFeatLyr = new FeatureLayerClass();pFeatLyr.FeatureClass = pFeatClass;pFeatLyr.FeatureClass = pFeatClass;pFeatLyr.Name = pFeatClass.AliasName;pFeatLyr.Name = pFeatClass.A

65、liasName;IMapIMap pMap = axMapControl1.Map; pMap = axMapControl1.Map;pMap.AddLayer(pFeatLyr);pMap.AddLayer(pFeatLyr); 7.2.4 PropertySetnPropertyset对象是一个专门用于设置属性的对象,它是对象是一个专门用于设置属性的对象,它是一种一种name-value对的集合。属性名必须是字符串,属性对的集合。属性名必须是字符串,属性值可以是字符串、数值或日期,也可以是一个对象。属值可以是字符串、数值或日期,也可以是一个对象。属性值支持通过名字来查找属性的方法。性值

66、支持通过名字来查找属性的方法。nIWorkspaceFactory:Open方法要求用方法要求用Propertyset(属(属性集合)来打开一个性集合)来打开一个Workspace。n如打开一个如打开一个SDE数据库的时候,进行如下配置:数据库的时候,进行如下配置:IPropertySet pPropertyset = new PropertySetClass();IPropertySet pPropertyset = new PropertySetClass();pPropertyset.pPropertyset.SetPropertySetProperty(Server, data) ;

67、/(Server, data) ; /服务器服务器pPropertyset.pPropertyset.SetPropertySetProperty(Instance, esri_sde); /SDE(Instance, esri_sde); /SDE实例实例pPropertyset.pPropertyset.SetPropertySetProperty(user, sde) ; /SDE(user, sde) ; /SDE数据库的用户名数据库的用户名pPropertyset.pPropertyset.SetPropertySetProperty(password, sde); /SDE(pas

68、sword, sde); /SDE数据库的密码数据库的密码pPropertyset.pPropertyset.SetPropertySetProperty(version, sde.DEFAULT); (version, sde.DEFAULT); /默认版本默认版本7.2.5 名称对象n名称(名称(Name)对象标识并且定义了)对象标识并且定义了GeoDatabase对象(如数据对象(如数据集或者工作空间)或地图对象(如图层)。集或者工作空间)或地图对象(如图层)。尽管名称对象只是尽管名称对象只是它代表的对象的一个它代表的对象的一个“代理代理”,但它支持程序员使用实例化的,但它支持程序员使用

69、实例化的特定对象的特定对象的Open方法。方法。n下面是一个使用下面是一个使用Open方法实例化的例子:方法实例化的例子: INameIName pName = pFeatureClassName as IName; pName = pFeatureClassName as IName; IFeatureClassIFeatureClass pFeatureClass = pName.Open () as IFeatureClass; pFeatureClass = pName.Open () as IFeatureClass;n名称对象的子类众多,名称对象的子类众多,如如TableName、

70、FeatureClassName、ObjectClassName等。等。7.3 Dataset对象nDataset(数据集)是一个代表了(数据集)是一个代表了Workspace中所谓的中所谓的数据集合的抽象类,它是一个高级别的数据容器。数据集合的抽象类,它是一个高级别的数据容器。所所有放在工作空间的对象都是一种数据集对象。有放在工作空间的对象都是一种数据集对象。nDataset对象分为两大类对象分为两大类一种是一种是TableTable,可以简单看成是一张二维表,它是由一,可以简单看成是一张二维表,它是由一条条记录组成的,是保存条条记录组成的,是保存RowRow的容器;的容器;一种是一种是Ge

71、oDatasetGeoDataset,地理数据集,如要素数据集、栅格,地理数据集,如要素数据集、栅格数据集。数据集。nIDataset接口定义的属性、方法如右图所示:接口定义的属性、方法如右图所示:esriDatasetType Constants7.3.1 GeoDataset类nGeoDataset(地理数据集)是一个抽象类,它代表了(地理数据集)是一个抽象类,它代表了拥有空间属性的数据集。拥有空间属性的数据集。GeoDataset的例子包括的例子包括要素要素数据集数据集FeatureDataset、要素类、要素类FeatureClass、TIN和和栅格数据集栅格数据集RasterData

72、set等等。n非非GeoDataset的数据集的数据集包括包括Table、对象类、对象类ObjectClass和关系类和关系类RelationshipClass等。等。nIGeoDataset接口定义了接口定义了GeoDataset对象的空间信息,包括空间对象的空间信息,包括空间参考和范围属性。通过参考和范围属性。通过IGeoDataset:SpatialRefrence可以获得可以获得一个一个GeoDataset对象的空间参考,对象的空间参考,IGeoDataset:Extent则可以则可以获得要素集的定义范围。获得要素集的定义范围。nIGeoDatasetSchemaEdit接口可以让程序

73、员去改变一个接口可以让程序员去改变一个GeoDataset的空间参考,的空间参考,IGeoDatasetSchemaEdit:AlterSpatialRefrence方法重新设置与数据集关联的空间参考。方法重新设置与数据集关联的空间参考。这个方法多使用于给一个空间参考为这个方法多使用于给一个空间参考为Unknown的地理数据集设的地理数据集设置空间参考。置空间参考。IGeoDatasetSchemaEdit:CanAlterSpatialReferenceIndicatesifthespatialreferenceofthedatasetcanbealtered.改变一个图层的空间参考npri

74、vatevoidAlterSpatialReferrenc(IMappMap,IFeatureLayerfeaturelayer) IActiveViewIActiveView pActiveView = pMap as IActiveView; pActiveView = pMap as IActiveView; IFeatureClassIFeatureClass pFeatureClass = featurelayer.FeatureClass; pFeatureClass = featurelayer.FeatureClass; IGeoDatasetIGeoDataset pGeoD

75、ataset = pFeatureClass as pGeoDataset = pFeatureClass as IGeoDatasetIGeoDataset; ; IGeoDatasetSchemaEditIGeoDatasetSchemaEdit pGeoDatasetEdit = pGeoDataset as pGeoDatasetEdit = pGeoDataset as IGeoDatasetSchemaEditIGeoDatasetSchemaEdit; ; ? if (pGeoDatasetEdit.CanAlterSpatialReference = true) if (pGe

76、oDatasetEdit.CanAlterSpatialReference = true) ISpatialReferenceFactory2ISpatialReferenceFactory2 pSpatRefFact = new pSpatRefFact = new SpatialReferenceEnvironmentClass();SpatialReferenceEnvironmentClass(); IGeographicCoordinateSystemIGeographicCoordinateSystem pGeoSys = pGeoSys = pSpatRefFact.Create

77、GeographicCoordinateSystem(4214);/esriSRGeoCSType pSpatRefFact.CreateGeographicCoordinateSystem(4214);/esriSRGeoCSType .esriSRGeoCS_Beijing1954.esriSRGeoCS_Beijing1954 pGeoDatasetEdit. pGeoDatasetEdit.AlterSpatialReferenceAlterSpatialReference(pGeoSys);(pGeoSys); pActiveView.Refresh(); pActiveView.R

78、efresh(); 7.3.2 FeatureDataset对象n要素数据集对象是要素数据集对象是具有相同空间参考的具有相同空间参考的要素类的容器要素类的容器。使用要使用要素数据集的情况十分广泛,例如几何网络、拓扑关系必须建立素数据集的情况十分广泛,例如几何网络、拓扑关系必须建立在一个要素数据集中。在一个要素数据集中。n在工作空间中对一个要素类进行编程的时候,需要注意这个要在工作空间中对一个要素类进行编程的时候,需要注意这个要素类放在什么地方,是直接放在工作空间中(这种要素类被称素类放在什么地方,是直接放在工作空间中(这种要素类被称为为standalonefeatureclass),还是放在一

79、个要素数据集中。),还是放在一个要素数据集中。当当使用使用IWorkspace:Datasets属性来遍历一个工作空间内的数据属性来遍历一个工作空间内的数据集时,返回的只是直接放在工作空间的数据集,而保存在一个集时,返回的只是直接放在工作空间的数据集,而保存在一个要素数据集中的要素类则不会被遍历。要素数据集中的要素类则不会被遍历。n使用使用IFeatureWorkspace:OpenFeatureClass打开工作空间中的打开工作空间中的任何一个要素类任何一个要素类,无论它是直接存放在工作空间还是存放在工,无论它是直接存放在工作空间还是存放在工作空间中的一个要素数据集中。作空间中的一个要素数据

80、集中。IFeatureDatasetn这个接口是从这个接口是从IDataset接口继承而来的。接口继承而来的。n使用使用IFeatureDataset:CreateFeatureClass方法可以用来方法可以用来在要素数据集中创建一个新的要素类。这个方法和在要素数据集中创建一个新的要素类。这个方法和IFeatureWorkspace:CreateFeaureClass的方法类似。的方法类似。n新建要素类的空间参考新建要素类的空间参考是通过它的几何字段来设置的。是通过它的几何字段来设置的。 IEnumDatasetIEnumDataset sourceFeatureDatasets = sour

81、ceWorkspace.get_Datasets( sourceFeatureDatasets = sourceWorkspace.get_Datasets( esriDatasetType.esriDTFeatureDatasetesriDatasetType.esriDTFeatureDataset ););nprivatevoidCreateFeatureDatasetsAndFeatureClasses(IEnumDatasetsourceFeatureDatasets) if (sourceWorkspace = null | targetWorkspace = null) retu

82、rn; if (sourceWorkspace = null | targetWorkspace = null) return; ISpatialReferenceISpatialReference spatialRference; spatialRference; sourceFeatureDatasets.Reset(); sourceFeatureDatasets.Reset(); IDatasetIDataset dataset = sourceFeatureDatasets.Next(); dataset = sourceFeatureDatasets.Next(); while (

83、dataset != null) while (dataset != null) IGeoDatasetIGeoDataset geoDataset = dataset as IGeoDataset; geoDataset = dataset as IGeoDataset; spatialRference = geoDataset.SpatialReference; spatialRference = geoDataset.SpatialReference; IFeatureDatasetIFeatureDataset sourceFeatureDataset = dataset as sou

84、rceFeatureDataset = dataset as IFeatureDataset;IFeatureDataset; IFeatureWorkspaceIFeatureWorkspace featureWorkspace = targetWorkspace as featureWorkspace = targetWorkspace as IFeatureWorkspace; IFeatureWorkspace; if (if (IWorkspace2IWorkspace2)targetWorkspace).)targetWorkspace).get_NameExistsget_Nam

85、eExists (esriDatasetType.(esriDatasetType. esriDTFeatureDataset, (esriDTFeatureDataset, (IDatasetIDataset)sourceFeatureDataset).Name)sourceFeatureDataset).Name) dataset = sourceFeatureDatasets.Next(); dataset = sourceFeatureDatasets.Next();IFeatureDatasetIFeatureDataset targetFeatureDataset = target

86、FeatureDataset = featureWorkspace.featureWorkspace.CreateFeatureDatasetCreateFeatureDataset(dataset.Name, spatialRference); (dataset.Name, spatialRference); CreateFeatureClassesInDataset(sourceFeatureDataset, targetFeatureDataset);CreateFeatureClassesInDataset(sourceFeatureDataset, targetFeatureData

87、set);dataset = sourceFeatureDatasets.Next();dataset = sourceFeatureDatasets.Next(); public bool get_NameExists (esriDatasetTypeType,stringName);n/创建要素数据集中的要素类:要素类、拓扑关系创建要素数据集中的要素类:要素类、拓扑关系public void CreateFeatureClassesInDataset(public void CreateFeatureClassesInDataset(IFeatureDataset IFeatureData

88、set sourceFeatureDataset, sourceFeatureDataset, IFeatureDataset IFeatureDataset targetFeatureDataset)targetFeatureDataset) if (sourceFeatureDataset = null | targetFeatureDataset = null) if (sourceFeatureDataset = null | targetFeatureDataset = null) return;return; IEnumDatasetIEnumDataset datasets =

89、sourceFeatureDataset. datasets = sourceFeatureDataset.SubsetsSubsets; ; IDatasetIDataset sourceDataset = datasets.Next(); sourceDataset = datasets.Next(); while (sourceDataset != null)while (sourceDataset != null) switch (sourceDataset.Type)switch (sourceDataset.Type) casecase esriDatasetType.esriDT

90、FeatureClassesriDatasetType.esriDTFeatureClass: : if (IWorkspace2)targetWorkspace).get_NameExists if (IWorkspace2)targetWorkspace).get_NameExists (esriDatasetType.esriDTFeatureClass, sourceDataset.Name)(esriDatasetType.esriDTFeatureClass, sourceDataset.Name) breakbreak; ; IFeatureClassIFeatureClass

91、sourceFeatureClass = sourceDataset as sourceFeatureClass = sourceDataset as IFeatureClass;IFeatureClass; CreateFeatureClassInThisDataset(sourceFeatureClass, CreateFeatureClassInThisDataset(sourceFeatureClass, targetFeatureDataset);targetFeatureDataset); break; break; casecase esriDatasetType.esriDTN

92、etworkDatasetesriDatasetType.esriDTNetworkDataset: : INetworkDatasetINetworkDataset sourceNetworkDataset = sourceDataset as INetworkDataset; sourceNetworkDataset = sourceDataset as INetworkDataset; CreateNetworkDatasetCreateNetworkDataset( (sourceFeatureDataset, targetFeatureDatasetsourceFeatureData

93、set, targetFeatureDataset);); break;break;casecase esriDatasetType.esriDTRelationshipClassesriDatasetType.esriDTRelationshipClass: : CreateRelationshipClassSchema(sourceFeatureDataset, CreateRelationshipClassSchema(sourceFeatureDataset, targetFeatureDataset);targetFeatureDataset); break; break;casec

94、ase esriDatasetType.esriDTTopologyesriDatasetType.esriDTTopology: : CreateTopology(CreateTopology( sourceFeatureDataset, targetFeatureDataset);sourceFeatureDataset, targetFeatureDataset); break; break;default: break; default: break; sourceDataset = datasets.Next(); sourceDataset = datasets.Next(); I

95、FeatureClassContainernIFeatureClassContainer接口用于管理要接口用于管理要素数据集中的素数据集中的要素类要素类。该接口的。该接口的ClassByName和和Class(index)等属性都可以等属性都可以用来获取数据集中的特定的要素类。用来获取数据集中的特定的要素类。nIFeatureClassContainer:ClassCount和和Classes属性分别可以获得要素数据集中的属性分别可以获得要素数据集中的要素类的数目和得到一个要素类的枚举对要素类的数目和得到一个要素类的枚举对象。象。nIFeatureClassContainer:ClassByI

96、D属性属性可以让程序员通过对象类的可以让程序员通过对象类的ID值返回一个值返回一个特定的对象类。特定的对象类。npublicvoidCreateFeatureClassesInDataset(IFeatureDatasetsourceFeatureDataset,IFeatureDatasettargetFeatureDataset) IFeatureClassContainerIFeatureClassContainer sourceFeatureClassContainer = sourceFeatureClassContainer = sourceFeatureDataset as IF

97、eatureClassContainer;sourceFeatureDataset as IFeatureClassContainer; for (int i = 0; i for (int i = 0; i sourceFeatureClassContainer.sourceFeatureClassContainer.ClassCountClassCount; i+); i+) IFeatureClassIFeatureClass sourceFeatureClass = sourceFeatureClass = sourceFeatureClassContainer.sourceFeatu

98、reClassContainer.get_Classget_Class(i);(i); CreateFeatureClassInThisDataset(sourceFeatureClass, CreateFeatureClassInThisDataset(sourceFeatureClass, targetFeatureDataset); targetFeatureDataset); IRelationshipClassContainer接口n要素数据集中还可以存储关系类对象,可以通过这个接口来添加、要素数据集中还可以存储关系类对象,可以通过这个接口来添加、新建和获得要素数据集中的关系类对象。

99、新建和获得要素数据集中的关系类对象。nIRelationshipClassContainer.CreateRelationshipClass方法可以让方法可以让程序员在要素数据集中产生一个新的关系类。程序员在要素数据集中产生一个新的关系类。public public IRelationshipClassIRelationshipClass CreateRelationshipClassCreateRelationshipClass ( ( stringstring relClassName,relClassName, IObjectClassIObjectClass OriginClass,O

100、riginClass, IObjectClassIObjectClass DestinationClass,DestinationClass, stringstring forwardLabel,forwardLabel, stringstring backwardLabel,backwardLabel, esriRelCardinalityesriRelCardinality Cardinality,Cardinality, esriRelNotificationesriRelNotification Notification,Notification, boolbool IsComposi

101、te,IsComposite, boolbool IsAttributed,IsAttributed, IFieldsIFields relAttrFields,relAttrFields, stringstring OriginPrimaryKey,OriginPrimaryKey, stringstring destPrimaryKey,destPrimaryKey, stringstring OriginForeignKey,OriginForeignKey, stringstring destForeignKeydestForeignKey ); ); nprivatevoidCrea

102、teRelationshipClassSchema(IFeatureDatasetsourceFeatureDataset,IFeatureDatasettargetFeatureDataset) IRelationshipClassContainerIRelationshipClassContainer fromRelClsContainer = fromRelClsContainer = sourceFeatureDataset as sourceFeatureDataset as IRelationshipClassContainerIRelationshipClassContainer

103、; ; IRelationshipClassContainerIRelationshipClassContainer toRelClsContainer = toRelClsContainer = targetFeatureDataset as targetFeatureDataset as IRelationshipClassContainerIRelationshipClassContainer; ; IFeatureWorkspaceIFeatureWorkspace targetFeatureWorkspace = targetWorkspace targetFeatureWorksp

104、ace = targetWorkspace as as IFeatureWorkspaceIFeatureWorkspace; ; IEnumRelationshipClassIEnumRelationshipClass fromRelClasses = fromRelClasses = fromRelClsContainer.fromRelClsContainer.RelationshipClassesRelationshipClasses; ; fromRelClasses.ResetfromRelClasses.Reset();(); IRelationshipClassIRelatio

105、nshipClass fromRelClass = fromRelClasses.Next(); fromRelClass = fromRelClasses.Next();while (fromRelClass != null)while (fromRelClass != null) stringstring fromRelClsName = (fromRelClass as IDataset). fromRelClsName = (fromRelClass as IDataset).NameName; ; esriRelCardinalityesriRelCardinality fromRe

106、lCardinality = fromRelCardinality = fromRelClass.fromRelClass.CardinalityCardinality; ; stringstring fromFwdPathLabel = fromRelClass. fromFwdPathLabel = fromRelClass.ForwardPathLabelForwardPathLabel; ; stringstring fromBwdPathLabel = fromRelClass. fromBwdPathLabel = fromRelClass.BackwardPathLabelBac

107、kwardPathLabel; ; IObjectClassIObjectClass fromOriginCls = fromRelClass. fromOriginCls = fromRelClass.OriginClassOriginClass; ; stringstring fromOriginPK = fromRelClass. fromOriginPK = fromRelClass.OriginPrimaryKeyOriginPrimaryKey; ; stringstring fromOriginFK = fromRelClass. fromOriginFK = fromRelCl

108、ass.OriginForeignKeyOriginForeignKey; ; IObjectClassIObjectClass fromDesCls = fromRelClass. fromDesCls = fromRelClass.DestinationClassDestinationClass; ; stringstring fromDesPK = fromRelClass. fromDesPK = fromRelClass.DestinationPrimaryKeyDestinationPrimaryKey; ; stringstring fromDesFK = fromRelClas

109、s. fromDesFK = fromRelClass.DestinationForeignKeyDestinationForeignKey; ; boolbool fromIsAttributed = fromRelClass. fromIsAttributed = fromRelClass.IsAttributedIsAttributed; ; boolbool fromIsComposite = fromRelClass. fromIsComposite = fromRelClass.IsCompositeIsComposite; ; esriRelNotificationesriRel

110、Notification fromRelNotif = fromRelNotif = fromRelClass.fromRelClass.NotificationNotification; ; IObjectClassIObjectClass toOriginCls = null; toOriginCls = null; if (fromOriginCls is if (fromOriginCls is IFeatureClassIFeatureClass) ) toOriginCls = toOriginCls = targetFeatureWorkspace.OpenFeatureClas

111、stargetFeatureWorkspace.OpenFeatureClass(IDataset)fromOriginCls).Name(IDataset)fromOriginCls).Name); ); else if (fromOriginCls is else if (fromOriginCls is ITableITable) ) toOriginCls = toOriginCls = targetFeatureWorkspace.OpenTabletargetFeatureWorkspace.OpenTable(IDataset)fromOriginCls).Name) as (I

112、Dataset)fromOriginCls).Name) as IObjectClassIObjectClass; ; IObjectClassIObjectClass toDesCls = null; toDesCls = null; if (fromDesCls is if (fromDesCls is IFeatureClassIFeatureClass) ) toDesCls = toDesCls = targetFeatureWorkspace.OpenFeatureClasstargetFeatureWorkspace.OpenFeatureClass(IDataset)fromD

113、esCls).Name);(IDataset)fromDesCls).Name); else if (fromOriginCls is else if (fromOriginCls is ITableITable) ) toDesCls = toDesCls = targetFeatureWorkspace.OpenTabletargetFeatureWorkspace.OpenTable(IDataset)fromDesCls).Name) as (IDataset)fromDesCls).Name) as IObjectClassIObjectClass; ; toRelClsContai

114、ner.toRelClsContainer.CreateRelationshipClassCreateRelationshipClass (fromRelClsName, (fromRelClsName, toOriginClstoOriginCls, , toDesClstoDesCls, fromFwdPathLabel, fromBwdPathLabel, , fromFwdPathLabel, fromBwdPathLabel, fromRelCardinality, fromRelNotif, fromRelCardinality, fromRelNotif, fromIsCompo

115、site, fromIsAttributed,null, fromOriginPK, , fromIsComposite, fromIsAttributed,null, fromOriginPK, , fromOriginFK, );fromOriginFK, ); fromRelClass = fromRelClasses.Next(); fromRelClass = fromRelClasses.Next(); ITopologyContainer2 拓扑关系npublicITopologyCreateTopology(stringstring Name,Name, doubledoubl

116、e ClusterTolerance,ClusterTolerance, intint maxGeneratedErrorCount,maxGeneratedErrorCount, stringstring ConfigurationKeywordConfigurationKeyword ) ); ; ITopology npublicvoidAddClass(IClass (IClass classToAdd,classToAdd, double double Weight,Weight, int int XYRank,XYRank, int int ZRank,ZRank, bool bo

117、ol EventNotificationOnValidateEventNotificationOnValidate ); ); n/拷贝拓扑关系拷贝拓扑关系 private void CreateTopology(private void CreateTopology(IFeatureDatasetIFeatureDataset sourceFeatureDataset, sourceFeatureDataset, IFeatureDatasetIFeatureDataset targetFeatureDataset) targetFeatureDataset) IFeatureWorkspa

118、ceManage2IFeatureWorkspaceManage2 sourceWsManage = sourceWorkspace as sourceWsManage = sourceWorkspace as IFeatureWorkspaceManage2;IFeatureWorkspaceManage2; IFeatureWorkspaceIFeatureWorkspace targetFeatureWorkspace = targetWorkspace as targetFeatureWorkspace = targetWorkspace as IFeatureWorkspace;IF

119、eatureWorkspace; ITopologyContainerITopologyContainer toTopoContainer = (ITopologyContainer) toTopoContainer = (ITopologyContainer) targetFeatureDataset;targetFeatureDataset; ITopologyContainerITopologyContainer fromTopoContainer = fromTopoContainer = (ITopologyContainer)sourceFeatureDataset;(ITopol

120、ogyContainer)sourceFeatureDataset; intint topoCount = fromTopoContainer.TopologyCount; topoCount = fromTopoContainer.TopologyCount; ITopologyITopology fromTopology; fromTopology; ITopologyITopology toTopology; toTopology; for (int i = 0; i topoCount; i+)for (int i = 0; i = 10000; queryFilter.WhereCl

121、ause = Pop1996 = 10000; queryFilter.SubFields = TERM; queryFilter.SubFields = TERM; intint termPosition = table.FindField(TERM); termPosition = table.FindField(TERM); IRowBufferIRowBuffer rowBuffer = table.CreateRowBuffer(); rowBuffer = table.CreateRowBuffer(); rowBuffer.set_Value(termPosition, City

122、); rowBuffer.set_Value(termPosition, City); / Update the matching rows. / Update the matching rows. table.UpdateSearchedRows(queryFilter, rowBuffer); table.UpdateSearchedRows(queryFilter, rowBuffer); 7.4.2 ObjectClass对象nObjectClass(对象类)是(对象类)是Table对象的子类,对象的子类,它也是由它也是由Row组成的表;是组成的表;是Table的一种扩展,的一种扩展,

123、它它就是一张就是一张“智能化智能化”的表,拥有属性和行为。的表,拥有属性和行为。n一个对象类和其他对象类之间可以存在任意数一个对象类和其他对象类之间可以存在任意数目的关系(目的关系(Relationship),这些关系可以用),这些关系可以用于控制对象的行为。于控制对象的行为。n一个对象类可以使用一个字段将它自身划分为一个对象类可以使用一个字段将它自身划分为几个子类(几个子类(Subtypes)。)。IObjectClass接口n所有的对象类都支持所有的对象类都支持IObjectClass接口,它是从接口,它是从IClass接口继承而来的。接口继承而来的。IObjectClass:IObjec

124、tClass:AliasNameAliasName属性可以返回对象类的别名,如果一个对象类属性可以返回对象类的别名,如果一个对象类没有设置别名属性,那么这个别名将和对象类的没有设置别名属性,那么这个别名将和对象类的NameName属性一样。属性一样。IObjectClass:ObjectClassIDIObjectClass:ObjectClassID属性则可以返回这个对象类的属性则可以返回这个对象类的 OIDOID值。其值。其实一个没有被注册的对象类也是有实一个没有被注册的对象类也是有 OIDOID值的,但是这个值总是为值的,但是这个值总是为-1-1,而,而且通过且通过 IClass:Has

125、OIDIClass:HasOID属性来查询的时候,返回值为属性来查询的时候,返回值为falsefalse。IObjectClass: RelationshipClassesIObjectClass: RelationshipClasses 列出了该对象类参与的关系类列出了该对象类参与的关系类(按关系类的角色:(按关系类的角色: origin, destination, or any origin, destination, or any)。)。publicIEnumRelationshipClassget_RelationshipClasses(esriRelRolerole);IClassS

126、chemaEdit3接口nIClassSchemaEdit、IClassSchemaEdit2和和IClassSchemaEdit3接口都可以用于修接口都可以用于修改一个对象类的属性。如对象类的别名,改一个对象类的属性。如对象类的别名,对象类某个字段的缺省值、值域,改变对象类某个字段的缺省值、值域,改变对象类字段的别名等。对象类字段的别名等。n使用使用IClassschemaEdit中的方法时,用户中的方法时,用户必须取得这个对象类的完全控制权,以必须取得这个对象类的完全控制权,以免在修改的过程中有别的程序使用这个免在修改的过程中有别的程序使用这个对象类,对象类,为此需要使用为此需要使用ISc

127、hemaLock接口接口来给数据集(对象类也是一个数据集)来给数据集(对象类也是一个数据集)设置一个独占型的锁定。设置一个独占型的锁定。nprivatevoidChangeSchema() IFeatureWorkspaceIFeatureWorkspace pFeatWorkspace = pWorkspace as pFeatWorkspace = pWorkspace as IFeatureWorkspaceIFeatureWorkspace; ; IClassSchemaEditIClassSchemaEdit pOcSchemaEdit = pOcSchemaEdit = pFeat

128、Workspace.OpenFeatureClass(Parcels) as pFeatWorkspace.OpenFeatureClass(Parcels) as IClassSchemaEditIClassSchemaEdit; ;ISchemaLockISchemaLock pSchLock = pOcSchemaEdit as pSchLock = pOcSchemaEdit as ISchemaLockISchemaLock; ;pSchLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemapSchLock.ChangeSch

129、emaLock(esriSchemaLock.esriExclusiveSchemaLock);Lock);pOcSchemaEdit.AlterFieldAliasName(Res, ResOrNon);pOcSchemaEdit.AlterFieldAliasName(Res, ResOrNon);pSchLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLocpSchLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);k); ISubtypes接口nISubtypes

130、接口用于管理和查询对象接口用于管理和查询对象类的子类、属性域(类的子类、属性域(Domain)和缺)和缺省值(省值(defaultvalue)。)。public void public void AddSubtypeAddSubtype ( int SubtypeCode, string SubtypeName); ( int SubtypeCode, string SubtypeName);public int public int DefaultSubtypeCodeDefaultSubtypeCode get; set; get; set;public public objectobje

131、ct get_DefaultValueget_DefaultValue ( int SubtypeCode, string ( int SubtypeCode, string FieldName);FieldName);public void public void set_DefaultValueset_DefaultValue ( int SubtypeCode, string ( int SubtypeCode, string FieldName, object Value);FieldName, object Value);public public IDomainIDomain ge

132、t_Domainget_Domain ( int SubtypeCode, string ( int SubtypeCode, string FieldName);FieldName);public void public void set_Domainset_Domain ( int SubtypeCode, string FieldName, ( int SubtypeCode, string FieldName, IDomain Domain);IDomain Domain);public string public string get_SubtypeNameget_SubtypeNa

133、me ( int SubtypeCode); ( int SubtypeCode);public public IEnumSubtypeIEnumSubtype SubtypesSubtypes get; get;7.4.3 FeatureClass对象nFeatureClass(要素类)是可以存储空间数据的对(要素类)是可以存储空间数据的对象类,它是象类,它是ObjectClass的扩展。的扩展。n在一个要素类中的所有的要素都使用同样的字段在一个要素类中的所有的要素都使用同样的字段结构,要素类与表、对象类的最大的区别在于它结构,要素类与表、对象类的最大的区别在于它拥有一个拥有一个几何字段几何

134、字段,即,即Shape字段,用于存储要素字段,用于存储要素的几何信息,这使得用户可以在地图上看到一个的几何信息,这使得用户可以在地图上看到一个要素的形状和位置。要素的形状和位置。IFeatureClass接口nTheIFeatureClassinterfaceinheritsfromtheIObjectClassandIClassinterfaces.ShapeFieldNameShapeFieldName属性可以返回要素类的几何字段属性可以返回要素类的几何字段的名称,的名称,AreaFieldAreaField和和LengthFieldLengthField则可以返回要则可以返回要素类中用于

135、存储要素面积和长度的字段对象。素类中用于存储要素面积和长度的字段对象。ShapeTypeShapeType返回的是返回的是 ShapeShape字段的类型,即要素类字段的类型,即要素类存储的是什么类型的几何对象。存储的是什么类型的几何对象。IFeatureClass:IFeatureClass:FeatureTypeFeatureType可以让程序员知道可以让程序员知道要素类的类型,如简单要素类、标注类、注记类要素类的类型,如简单要素类、标注类、注记类还是复杂要素类等。还是复杂要素类等。FeatureClassIDFeatureClassID返回的是要素类在地理数据库中返回的是要素类在地理数据

136、库中的唯一标识符,它和的唯一标识符,它和IObiectClass.IObiectClass.ObiectClassIDObiectClassID属性是一样属性是一样的。的。使用使用 SelectSelect方法可以在一个要素类中得到要素选择集,而获方法可以在一个要素类中得到要素选择集,而获得的要素选择集的数目和种类取决于使用的过滤器对象得的要素选择集的数目和种类取决于使用的过滤器对象(QueryFilterQueryFilter)。)。CreateFeatureCreateFeature方法可以新建一个新的要素对象,用户在新建方法可以新建一个新的要素对象,用户在新建一个一个FeatureFea

137、ture对象后,需要对它的值进行设定,然后使用对象后,需要对它的值进行设定,然后使用IFeature:StoreIFeature:Store方法将新的要素存储到要素类中去。方法将新的要素存储到要素类中去。CreateFeatureBufferCreateFeatureBuffer方法则用于产生一个方法则用于产生一个FeatureBufferFeatureBuffer对象,对象,它并不是一个真正的要素,而是一个暂时存在的对象,它需要它并不是一个真正的要素,而是一个暂时存在的对象,它需要使用使用“插入插入”游标,将新建的游标,将新建的“缓冲要素缓冲要素”保存到要素类中去。保存到要素类中去。npri

138、vatevoidCreateFeatureClassInThisDataset(IFeatureClasssourceFeatureClass,IFeatureDatasettargetFeatureDataset) IFieldsIFields fields = sourceFeatureClass.Fields; fields = sourceFeatureClass.Fields; if (fields = null) return;if (fields = null) return; IFieldsEditIFieldsEdit fieldsEdit = (IFieldsEdit)fi

139、elds; fieldsEdit = (IFieldsEdit)fields; /定义几何字段定义几何字段 IFieldIField fieldUserDefined = new FieldClass (); fieldUserDefined = new FieldClass (); IFieldEditIFieldEdit fieldEdit = (IFieldEdit)fieldUserDefined; fieldEdit = (IFieldEdit)fieldUserDefined; IGeometryDefIGeometryDef geometryDef = new GeometryD

140、efClass(); geometryDef = new GeometryDefClass(); IGeometryDefEditIGeometryDefEdit geometryDefEdit = geometryDefEdit = (IGeometryDefEdit)geometryDef;(IGeometryDefEdit)geometryDef; geometryDefEdit.GeometryType_2 = geometryDefEdit.GeometryType_2 = sourceFeatureClass.ShapeType;sourceFeatureClass.ShapeTy

141、pe; geometryDefEdit.GridCount_2 = 1;geometryDefEdit.GridCount_2 = 1; geometryDefEdit.set_GridSize(0, 0); geometryDefEdit.set_GridSize(0, 0); geometryDefEdit.HasM_2 = bHasM; geometryDefEdit.HasM_2 = bHasM; geometryDefEdit.HasZ_2 = bHasZ; geometryDefEdit.HasZ_2 = bHasZ; / Set standard field properties

142、./ Set standard field properties. fieldEdit.Name_2 = SHAPE; fieldEdit.Name_2 = SHAPE; fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; fieldEdit.GeometryDef_2 = geometryDef; fieldEdit.GeometryDef_2 = geometryDef; fieldEdit.IsNullable_2 =

143、 true; fieldEdit.IsNullable_2 = true; fieldEdit.Required_2 = true; fieldEdit.Required_2 = true; fieldsEdit.fieldsEdit.set_Field(fields.FindField(SHAPE), set_Field(fields.FindField(SHAPE), fieldUserDefined);fieldUserDefined); UID CLSID = new UIDClass(); UID CLSID = new UIDClass(); CLSID.Value = esriG

144、eoDatabase.Feature; CLSID.Value = esriGeoDatabase.Feature; string featureClassName = (IDataset)sourceFeatureClass).Name; string featureClassName = (IDataset)sourceFeatureClass).Name; IFeatureClassIFeatureClass targetClass = targetClass = targetFeatureDataset.targetFeatureDataset.CreateFeatureClassCr

145、eateFeatureClass(featureClassName, fields, CLSID, (featureClassName, fields, CLSID, null, esriFeatureType.esriFTSimple, Shape, );null, esriFeatureType.esriFTSimple, Shape, ); / /设置要素类子类型设置要素类子类型 SetSubtypes(sourceFeatureClass, targetClass);SetSubtypes(sourceFeatureClass, targetClass); n/拷贝要素类的子类型定义拷

146、贝要素类的子类型定义 private void SetSubtypes(private void SetSubtypes(IFeatureClassIFeatureClass fromFeatureClass, fromFeatureClass, IFeatureClassIFeatureClass toFeatureClass) toFeatureClass) IWorkspaceDomainsIWorkspaceDomains targetWorkspaceDomains = targetWorkspaceDomains = (IWorkspaceDomains)targetWorkspa

147、ce;(IWorkspaceDomains)targetWorkspace; ISubtypesISubtypes toSubtypes = (ISubtypes)toFeatureClass;toSubtypes = (ISubtypes)toFeatureClass; ISubtypesISubtypes fromSubtypes = fromSubtypes = (ISubtypes)fromFeatureClass(ISubtypes)fromFeatureClass; ; IFields fromFields = IFields fromFields = fromFeatureCla

148、ssfromFeatureClass.Fields.Fields IEnumSubtypeIEnumSubtype fromEnumSubtype; fromEnumSubtype; string subtypeFieldName; string subtypeFieldName; int subtypeCode; int subtypeCode; string subtypeName; string subtypeName; IDomainIDomain fromDomain; fromDomain; if (fromSubtypes.HasSubtype)if (fromSubtypes.

149、HasSubtype) fromEnumSubtype = fromSubtypes.Subtypes;fromEnumSubtype = fromSubtypes.Subtypes;subtypeFieldName = fromSubtypes.SubtypeFieldName;subtypeFieldName = fromSubtypes.SubtypeFieldName; toSubtypes.SubtypeFieldName = subtypeFieldName;toSubtypes.SubtypeFieldName = subtypeFieldName; subtypeName =

150、fromEnumSubtype.Next(out subtypeCode);subtypeName = fromEnumSubtype.Next(out subtypeCode); while (subtypeName != null)while (subtypeName != null) toSubtypes.AddSubtype(subtypeCode, subtypeName);toSubtypes.AddSubtype(subtypeCode, subtypeName); for (int i=0;ifromFields.for (int i=0;ifromFields.FieldCo

151、untFieldCount;i+);i+) string fieldName = fromFields.get_Field(i).Name; string fieldName = fromFields.get_Field(i).Name; fromDomain = fromSubtypes.get_Domain(subtypeCode, fromDomain = fromSubtypes.get_Domain(subtypeCode, fieldName fieldName );); if (fromDomain != null) if (fromDomain != null) toSubty

152、pes.set_Domain(subtypeCode, toSubtypes.set_Domain(subtypeCode, fieldName fieldName , , targetWorkspaceDomains.get_DomainByName(fromDomain.Name);targetWorkspaceDomains.get_DomainByName(fromDomain.Name); subtypeName = fromEnumSubtype.Next(out subtypeCode);subtypeName = fromEnumSubtype.Next(out subtype

153、Code); public IDomain get_Domain (intSubtypeCode,stringFieldName);public void set_Domain (intSubtypeCode,stringFieldName,IDomainDomain);nprivatevoidCopyFeatureClass(IFeatureClasssourceClass,IFeatureClasstargetClass) IFeatureBufferIFeatureBuffer targetFeatureBuffer = targetClass.CreateFeatureBuffer()

154、;targetFeatureBuffer = targetClass.CreateFeatureBuffer();IFeatureCursorIFeatureCursor targetFeatureCursor = targetClass.Insert(true);targetFeatureCursor = targetClass.Insert(true); IFieldsIFields sourceFields = sourceClass.Fields; sourceFields = sourceClass.Fields;IFieldsIFields targetFields = targe

155、tClass.Fields; targetFields = targetClass.Fields;IFeatureCursorIFeatureCursor sourceFeatureCursor = sourceClass.Search(null, false);sourceFeatureCursor = sourceClass.Search(null, false);IFeatureIFeature sourceFeature = sourceFeatureCursor.NextFeature();sourceFeature = sourceFeatureCursor.NextFeature

156、();int j = 0;int j = 0;Create a feature buffer that can be used with an insert cursor.Create a feature buffer that can be used with an insert cursor.InsertInsert creates an insert cursor on the feature class which can be used creates an insert cursor on the feature class which can be used to insert

157、new features into it.to insert new features into it. while (sourceFeature != null)while (sourceFeature != null) IGeometryIGeometry geometry = sourceFeature.ShapeCopy; geometry = sourceFeature.ShapeCopy; if (geometry = null) goto nextFeature; if (geometry = null) goto nextFeature; if (geometry is if

158、(geometry is ITopologicalOperator2ITopologicalOperator2) ) ITopologicalOperator2ITopologicalOperator2 topo = geometry as topo = geometry as ITopologicalOperator2;ITopologicalOperator2; topo.IsKnownSimple_2 = false; topo.IsKnownSimple_2 = false; topo.Simplify(); topo.Simplify(); IZAwareIZAware za = g

159、eometry as IZAware; za = geometry as IZAware; za.ZAware = bHasZ; za.ZAware = bHasZ; IMAwareIMAware ma = geometry as IMAware; ma = geometry as IMAware; ma.MAware = bHasM; ma.MAware = bHasM; targetFeatureBuffer.Shape = geometry; targetFeatureBuffer.Shape = geometry;for (int i = 0; i targetFields.Field

160、Count; i+)for (int i = 0; i targetFields.FieldCount; i+) IFieldIField field = new FieldClass(); field = new FieldClass(); field = targetFields.get_Field(i);field = targetFields.get_Field(i); if (field.Type = esriFieldType.esriFieldTypeOID) continue; if (field.Type = esriFieldType.esriFieldTypeOID) c

161、ontinue; if (field.Type = esriFieldType.esriFieldTypeGeometry) continue;if (field.Type = esriFieldType.esriFieldTypeGeometry) continue; if (field.Name = Shape_Length) continue; if (field.Name = Shape_Length) continue; if (field.Name = Shape_Area) continue; if (field.Name = Shape_Area) continue; inti

162、nt fieldIdx = sourceFields.FindField(field.Name); fieldIdx = sourceFields.FindField(field.Name); objectobject fieldValue = sourceFeature.get_Value(fieldIdx); fieldValue = sourceFeature.get_Value(fieldIdx); targetFeatureBuffer.set_Value(i, fieldValue); targetFeatureBuffer.set_Value(i, fieldValue); ta

163、rgetFeatureCursor.InsertFeature(targetFeatureBuffer);targetFeatureCursor.InsertFeature(targetFeatureBuffer); sourceFeature = null; j+; sourceFeature = null; j+; nextFeature: sourceFeature = sourceFeatureCursor.NextFeature();nextFeature: sourceFeature = sourceFeatureCursor.NextFeature(); targetFeatur

164、eCursor.Flush();targetFeatureCursor.Flush(); 7.4.4 字段集与字段n字段集(字段集(Fields)是表的列(是表的列(Column)属性的集合。一张表必须)属性的集合。一张表必须有一个有一个字段集字段集,而一个字段集中至少有一个字段,而一个字段集中至少有一个字段(Field)。字。字段集是一个组件类。段集是一个组件类。在新建一个字段后,必须将它加入到一个字在新建一个字段后,必须将它加入到一个字段集中供新建的表或要素类使用。段集中供新建的表或要素类使用。n设置字段集的过程包括设置字段集的过程包括设定字段集中字段(设定字段集中字段(Field)的)的

165、类型(类型(Type)和值域()和值域(Domain),缺省值,别名),缺省值,别名(Alias)和是否允许为空()和是否允许为空(Allownulls)等)等属性。属性。IFields接口n字段集是与一个表相关联的,如果需要获取一个表对象的字段集,字段集是与一个表相关联的,如果需要获取一个表对象的字段集,可以使用可以使用ITable:Fields属性来实现。程序员使用属性来实现。程序员使用IFields接口定义接口定义的属性和方法,可以非常容易地得到字段集中的某个字段,继而的属性和方法,可以非常容易地得到字段集中的某个字段,继而获得某个特定字段的值。获得某个特定字段的值。nIFields有两

166、个属性,其中一个是有两个属性,其中一个是Field(index),它通过传入不同的,它通过传入不同的字段索引值可以返回某个字段,如:字段索引值可以返回某个字段,如: IField pField = pFields.get_Field(0);IField pField = pFields.get_Field(0);n而而IFields:FieldCount属性可以返回字段集合的字段数目。属性可以返回字段集合的字段数目。nIFields定义了两种方法,一种是定义了两种方法,一种是FindField,另一种是,另一种是FindFieldByAliasName,前者是根据字段名、后者是根据字段别前者是

167、根据字段名、后者是根据字段别名找到字段在字段集中的索引号名找到字段在字段集中的索引号。n下面是获取一个名为下面是获取一个名为“City_name”字段的代码段:字段的代码段: IFieldsIFields pFields = pFeatClass.Fields; pFields = pFeatClass.Fields; int i = pFields.FindField(City_name); int i = pFields.FindField(City_name); IFieldIField pField = pFields.get_Field(i); pField = pFields.ge

168、t_Field(i);IFieldsEdit接口n字段集中的字段需要能够修改、更新字段集中的字段需要能够修改、更新和删除,实现这些要求的方法由和删除,实现这些要求的方法由IFieldsEdit接口定义,这个接口从接口定义,这个接口从IFields继承而来。继承而来。nIFieldsEdit:AddField方法,可以添加方法,可以添加一个新的字段到字段集中。当需要修一个新的字段到字段集中。当需要修改一个表或者要素类的结构时,这个改一个表或者要素类的结构时,这个方法非常有用。方法非常有用。DeleteAllFields方法可方法可以一次删除表中所有的字段。以一次删除表中所有的字段。IField与

169、IFieldEdit接口esriFieldType ConstantsnprivateITableCreateTable(IFeatureWorkspacepFeatWorkspace,stringstrName) IFieldsIFields pFields = new FieldsClass(); pFields = new FieldsClass();IFieldsEditIFieldsEdit pFieldsEdit = pFields as IFieldsEdit; pFieldsEdit = pFields as IFieldsEdit;pFieldsEdit.FieldCount

170、_2 = 2; /pFieldsEdit.FieldCount_2 = 2; /设定字段数目设定字段数目IFieldIField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit;IFieldEdit;pFieldEdit.Name_2 = OBJECTID; pFieldEdit.AliasName_2 = FID;pFieldEdit.Name_2 = OBJECTID; pF

171、ieldEdit.AliasName_2 = FID;pFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;pFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;pFieldsEdit.set_Field(0, pField);pFieldsEdit.set_Field(0, pField);pField = new FieldClass(); pFieldEdit = pField as IFieldEdit;pField = new FieldClass(); pFieldEdit = pFie

172、ld as IFieldEdit;pFieldEdit.Length_2 = 30; pFieldEdit.Name_2 = Owner;pFieldEdit.Length_2 = 30; pFieldEdit.Name_2 = Owner;pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.set_Field(1, pField); pFieldsEdit.set_Field(1, pField);IT

173、ableITable pTable = pFeatWorkspace.CreateTable(strName, pFields, null, null, pTable = pFeatWorkspace.CreateTable(strName, pFields, null, null, ););return pTable; return pTable; public public ITable CreateTable ( CreateTable ( string Name, IFields Fields, UID CLSID, UID EXTCLSID, string ConfigKeyword

174、 ););nprivatevoidAddFieldToFeatureClass() IWorkspaceFactoryIWorkspaceFactory pWSF = new AccessWorkspaceFactoryClass(); pWSF = new AccessWorkspaceFactoryClass();IFeatureWorkspaceIFeatureWorkspace pWS = pWSF.OpenFromFile(d:temptest.mdb, pWS = pWSF.OpenFromFile(d:temptest.mdb, 0) as IFeatureWorkspace;0

175、) as IFeatureWorkspace;IFeatureClassIFeatureClass pFeatClass = pWS.OpenFeatureClass(parcels); pFeatClass = pWS.OpenFeatureClass(parcels); IFieldIField pField = new FieldClass(); pField = new FieldClass();IFieldEditIFieldEdit pFieldEdit = pField as IFieldEdit; pFieldEdit = pField as IFieldEdit;pField

176、Edit.Type_2 = esriFieldType.esriFieldTypeDouble;pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;pFieldEdit.Name_2 = tax;pFieldEdit.Name_2 = tax;pFeatClass.AddField(pField);pFeatClass.AddField(pField); 7.4.5 与字段相关的对象 1.1.设计几何字段设计几何字段2.2.设置表的索引设置表的索引3.3.使用子类型使用子类型4.4.使用属性域使用属性域7.4.5.1 几何字段的设计n几何

177、字段用于存储要素的几何形状,几何字段没有缺省值。几何字段用于存储要素的几何形状,几何字段没有缺省值。n设计一个几何字段的时候,有一个属性是必须设置的,即设计一个几何字段的时候,有一个属性是必须设置的,即IField:GeometryDef。它是一个。它是一个GeometryDef对象,用于预定义对象,用于预定义一个几何字段的几何属性。一个几何字段的几何属性。GeometryDef对象的两个接口分别是对象的两个接口分别是IGeometryDef和和IGeometryDefEdit,由于两个接口定义的属性都,由于两个接口定义的属性都是可读写的,如是可读写的,如GeometryType为可读,为可读

178、,GeometryType_2为可写。为可写。nIGeometryDef:GeometryType属性用于设置几何字段的几何类型,属性用于设置几何字段的几何类型,如点、点集、多边形、环等;如点、点集、多边形、环等;SpatialRefrence属性则用于设置几属性则用于设置几何字段的空间参考。对于一个几何字段而言,这两个属性是必需何字段的空间参考。对于一个几何字段而言,这两个属性是必需的。的。IGeometryDef和IGeometryDefEditnprivatevoidDefineGeometryField(ISpatialReferencepSpatRef) IGeometryDefIG

179、eometryDef pGeoDef = new GeometryDefClass(); pGeoDef = new GeometryDefClass();IGeometryDefEditIGeometryDefEdit pGeoDefEdit = pGeoDef as IGeometryDefEdit; pGeoDefEdit = pGeoDef as IGeometryDefEdit;pGeoDefEdit.AvgNumPoints_2 = 5;pGeoDefEdit.AvgNumPoints_2 = 5;pGeoDefEdit.GeometryType_2 = esriGeometryT

180、ype.esriGeometryPolyline; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline; pGeoDefEdit.GridCount_2 = 2;pGeoDefEdit.GridCount_2 = 2;pGeoDefEdit.set_GridSize(0, 200); pGeoDefEdit.set_GridSize(0, 200); pGeoDefEdit.set_GridSize(1, 500);pGeoDefEdit.set_GridSize(1, 500);pGeoDefEdit.HasM

181、_2 = false; pGeoDefEdit.HasM_2 = false; pGeoDefEdit.HasZ_2 = true;pGeoDefEdit.HasZ_2 = true;pGeoDefEdit.SpatialReference_2 = pSpatRef;pGeoDefEdit.SpatialReference_2 = pSpatRef; IFieldIField pField = new FieldClass(); pField = new FieldClass();IFieldEditIFieldEdit pFieldEdit = pField as IFieldEdit; p

182、FieldEdit = pField as IFieldEdit;pFieldEdit.Name_2 = SHAPE;pFieldEdit.Name_2 = SHAPE;pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;pFieldEdit.GeometryDef_2 = pGeoDef; pFieldEdit.GeometryDef_2 = pGeoDef; pFieldEdit.IsNullable_2 = true;

183、pFieldEdit.IsNullable_2 = true; 7.4.5.2 索引集和索引n索引集(索引集(Indexes)对象和字段集对象相似,它用于管理被设置为)对象和字段集对象相似,它用于管理被设置为索引的字段,索引的字段,IIndexes接口用于管理索引集对象,如接口用于管理索引集对象,如IIndexes:index属性可以按照索引值得到一个表中的某个特定的索属性可以按照索引值得到一个表中的某个特定的索引。引。n索引对象代表了一个表的索引。一个要素类存在两种形式的索引:索引对象代表了一个表的索引。一个要素类存在两种形式的索引:空间索引和属性索引。空间索引存在一个要素类的几何字段中,当

184、空间索引和属性索引。空间索引存在一个要素类的几何字段中,当一个要素类被创建的时候,系统会自动给这个要素类创建一个空间一个要素类被创建的时候,系统会自动给这个要素类创建一个空间索引。对于索引。对于GeoDatabase而言,它也会给要素类自动建立一个属性而言,它也会给要素类自动建立一个属性索引,这个索引是基于索引,这个索引是基于OID字段的。字段的。nIndex对象支持对象支持IIndex接口和接口和IIndexEdit接口,使用这两个接口可以接口,使用这两个接口可以产生一个索引并设置它的属性。产生一个索引并设置它的属性。npublicvoidIIndexes_FindIndexesByFiel

185、dName_Example(IFeatureClassfeatureClass,stringnameOfField) IIndexes indexes = featureClass.Indexes; IIndexes indexes = featureClass.Indexes; Console.WriteLine(The feature class has an index count of: 0, Console.WriteLine(The feature class has an index count of: 0, indexes.IndexCount);indexes.IndexCo

186、unt);int indexPosition; int indexPosition; indexes.FindIndex(Temp_Index, out indexPosition); indexes.FindIndex(Temp_Index, out indexPosition); Iindex index = indexes.get_Index(indexPosition); Iindex index = indexes.get_Index(indexPosition); /or /or IEnumIndex enumIndex = IEnumIndex enumIndex = index

187、es.FindIndexesByFieldName(nameOfField); indexes.FindIndexesByFieldName(nameOfField); index = enumIndex.Next(); index = enumIndex.Next(); while (index != null) while (index != null) if (index.Name = Temp_Index) if (index.Name = Temp_Index) featureClass.DeleteIndex(index); break; featureClass.DeleteIn

188、dex(index); break; index = enumIndex.Next(); index = enumIndex.Next(); npublicvoidAddIndexToFeatureClass(IFeatureClassfeatureClass,StringindexName,StringnameOfField) intint fieldIndex = featureClass.FindField(nameOfField); fieldIndex = featureClass.FindField(nameOfField); if (fieldIndex = -1) if (fi

189、eldIndex = -1) IFieldsIFields featureClassFields = featureClass.Fields; featureClassFields = featureClass.Fields; IFieldIField field = featureClassFields.get_Field(fieldIndex); field = featureClassFields.get_Field(fieldIndex); IFieldsIFields fields = new FieldsClass(); fields = new FieldsClass(); IF

190、ieldsEditIFieldsEdit fieldsEdit = (IFieldsEdit)fields; fieldsEdit = (IFieldsEdit)fields; fieldsEdit.FieldCount_2 = 1; fieldsEdit.FieldCount_2 = 1; fieldsEdit.set_Field(0, field);fieldsEdit.set_Field(0, field); IIndexIIndex index = new IndexClass(); index = new IndexClass(); IIndexEditIIndexEdit inde

191、xEdit = (IIndexEdit)index; indexEdit = (IIndexEdit)index; indexEdit.Fields_2 = fields; indexEdit.Fields_2 = fields; indexEdit.IsAscending_2 = false;indexEdit.IsAscending_2 = false; indexEdit.IsUnique_2 = false; indexEdit.IsUnique_2 = false; indexEdit.Name_2 = indexName;indexEdit.Name_2 = indexName;

192、ISchemaLockISchemaLock schemaLock = (ISchemaLock)featureClass; schemaLock = (ISchemaLock)featureClass; trytry schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLschemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);ock); featureClass.AddIndex(index); featureClass.AddIndex(i

193、ndex); catch (COMException comExc) catch (COMException comExc) /提示出错信息提示出错信息 finally finally schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLockschemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);); 7.4.5.3 子类型n子类型(子类型(Subtypes)是一种用于给要素类或者对象类中的要素或对)是一种用于给要素类或者对象类中的要素或对象分组的方式

194、。通过给定不同子类的不同的缺省值和有效性规则,象分组的方式。通过给定不同子类的不同的缺省值和有效性规则,用户可以定制要素类中不同子类要素的行为。用户可以定制要素类中不同子类要素的行为。如:在一个要素数据集中,有如:在一个要素数据集中,有LandLand和和buildingbuilding要素类,用户希望两个要素类,用户希望两个要素类之间的要素是满足严格的拓扑关系的,比如商业建筑必须在商要素类之间的要素是满足严格的拓扑关系的,比如商业建筑必须在商业土地区内,工业建筑不能在居住土地区等。业土地区内,工业建筑不能在居住土地区等。这时可以将一个这时可以将一个LandLand的的要素类分为三个子类要素类

195、分为三个子类businessbusiness、residentialresidential和和industryindustry;将;将buildingbuilding的要素也分为三种:的要素也分为三种:businessbusiness要素、要素、residentialresidential要素和要素和 industryindustry要素。要素。将两个要素类各自分成子类后,可以在地理数据集中将两个要素类各自分成子类后,可以在地理数据集中建立拓扑关系,并用这些拓扑规则来保证要素类的设计要求。建立拓扑关系,并用这些拓扑规则来保证要素类的设计要求。n为了建立一个对象类的子为了建立一个对象类的子类型和

196、设置子类型字段的类型和设置子类型字段的缺省值,程序员可以使用缺省值,程序员可以使用ISubtypes接口。接口。nprivatevoidCreateSubtypes() IWorkspaceFactoryIWorkspaceFactory pFact = new FileGDBWorkspaceFactoryClass(); pFact = new FileGDBWorkspaceFactoryClass();IFeatureWorkspaceIFeatureWorkspace pFeatws = pFact.OpenFromFile(d:temptest.gdb, 0) pFeatws =

197、pFact.OpenFromFile(d:temptest.gdb, 0) as as IFeatureWorkspaceIFeatureWorkspace; ;IFeatureClassIFeatureClass pFeatcls = pFeatws.OpenFeatureClass(parcels); pFeatcls = pFeatws.OpenFeatureClass(parcels);ISubtypesISubtypes pSubType = pFeatcls as pSubType = pFeatcls as ISubtypesISubtypes; ;pSubType.pSubTy

198、pe.SubtypeFieldNameSubtypeFieldName = Res;= Res;pSubType.pSubType.AddSubtypeAddSubtype(1, Non_Residential); (1, Non_Residential); pSubType.pSubType.AddSubtypeAddSubtype(2, Residential);(2, Residential);pSubType.pSubType.DefaultSubtypeCodeDefaultSubtypeCode = 1; = 1;pSubType.pSubType.set_DefaultValue

199、set_DefaultValue(1, ZONING_S, B);(1, ZONING_S, B);pSubType.pSubType.set_DefaultValueset_DefaultValue(2, (2, “ZONING_SZONING_S”, ,“R R”);); /子类代码,字段名,值子类代码,字段名,值 public void set_DefaultValue (intSubtypeCode,stringFieldName,objectValue);“parcels”要素类分为2个子类:Non_Residential,子类代码为1;Residential,代码值为2。7.4.5

200、.4 属性域 nDomain(属性域)是一个抽象类,它可(属性域)是一个抽象类,它可以用于限制一个对象类或一个要素类的某以用于限制一个对象类或一个要素类的某个字段的允许值。值域可以用于探测一个个字段的允许值。值域可以用于探测一个字段值的有效性,它是子类的基础。字段值的有效性,它是子类的基础。n属性域与一个工作空间相关,因而一个值属性域与一个工作空间相关,因而一个值域可以给多个字段使用。在域可以给多个字段使用。在ArcObjects中中可以通过可以通过IWorkspaceDomains.AddDomain来添加一个属性域对象。来添加一个属性域对象。nRangeDomain和和CodedValue

201、Domain是是Domain的子类,它们是组件类,并且都支的子类,它们是组件类,并且都支持持lDomain接口。接口。nRangedomain是范围值域对象,它确定某个字段的值在一个范是范围值域对象,它确定某个字段的值在一个范围内,这个对象规定的值围内,这个对象规定的值只能是数值型或日期型只能是数值型或日期型,如考试的分,如考试的分数在数在0到到100的范围内,日期在的范围内,日期在2005-1-1到到2006-1-1的范围内。的范围内。IRangeDomain接口规定了范围的最大和最小值属性。接口规定了范围的最大和最小值属性。nCodedValueDomain是代码值域对象,它是一个是代码值

202、域对象,它是一个“值值-名称名称”对象的集对象的集合,它用于确定一个字段所有可能的取值。代码值域一般都是字符类合,它用于确定一个字段所有可能的取值。代码值域一般都是字符类型。型。如用户将土地分为三种使用类型如用户将土地分为三种使用类型( (“工业土地工业土地”、“农业土地农业土地”、“居住土地居住土地) )后,这个土地要素类中的类型字段可以出现的值后,这个土地要素类中的类型字段可以出现的值就是用户规定的三种,而不能出现别的。就是用户规定的三种,而不能出现别的。nICodedValueDomain规定了代码值域对象的特定的属性和方法,如规定了代码值域对象的特定的属性和方法,如AddCode方法可

203、以用于添加一个方法可以用于添加一个“值值-名称名称对象,而使用对象,而使用Name和和Value可以通过一个索引值来得到一个可以通过一个索引值来得到一个“值值-名称名称”对象。对象。n/拷贝数据库的属性域拷贝数据库的属性域public void CopyDomains(public void CopyDomains(IWorkspaceIWorkspace sourceWorkspace, sourceWorkspace, IWorkspaceIWorkspace targetWorkspace) targetWorkspace) IWorkspaceDomainsIWorkspaceDoma

204、ins targetWorkspaceDomains = targetWorkspaceDomains = (IWorkspaceDomains)targetWorkspace;(IWorkspaceDomains)targetWorkspace; IWorkspaceDomainsIWorkspaceDomains sourceWorkspaceDomains = sourceWorkspaceDomains = (IWorkspaceDomains)sourceWorkspace;(IWorkspaceDomains)sourceWorkspace; IEnumDomainIEnumDom

205、ain sourceDomains = sourceDomains = sourceWorkspaceDomains.Domains;sourceWorkspaceDomains.Domains; if (sourceDomains = null) return; if (sourceDomains = null) return; IDomainIDomain sourceDomain = sourceDomains.Next(); sourceDomain = sourceDomains.Next(); IDomainIDomain targetDomain = null; targetDo

206、main = null; while (sourceDomain != null)while (sourceDomain != null) if (sourceDomain is if (sourceDomain is IRangeDomainIRangeDomain) ) IRangeDomainIRangeDomain rangeDomain = new RangeDomainClass(); rangeDomain = new RangeDomainClass(); rangeDomain. rangeDomain.MinValue MinValue = (sourceDomain as

207、 IRangeDomain).MinValue;= (sourceDomain as IRangeDomain).MinValue; rangeDomain. rangeDomain.MaxValueMaxValue = (sourceDomain as IRangeDomain).MaxValue; = (sourceDomain as IRangeDomain).MaxValue; targetDomain = rangeDomain as IDomain;targetDomain = rangeDomain as IDomain; if (sourceDomain is if (sour

208、ceDomain is ICodedValueDomainICodedValueDomain) ) ICodedValueDomainICodedValueDomain sourceCodedDomain =sourceDomain as sourceCodedDomain =sourceDomain as ICodedValueDomain;ICodedValueDomain; ICodedValueDomainICodedValueDomain codedDomain = new CodedValueDomainClass(); codedDomain = new CodedValueDo

209、mainClass(); for (int i = 0; i sourceCodedDomain.CodeCount; i+) for (int i = 0; i sourceCodedDomain.CodeCount; i+) codedDomain.codedDomain.AddCodeAddCode(sourceCodedDomain.get_Value(i),sourceCodedDomain.get_N(sourceCodedDomain.get_Value(i),sourceCodedDomain.get_Name(i); ame(i); targetDomain = codedD

210、omain as IDomain;targetDomain = codedDomain as IDomain; if (targetDomain = null) return;if (targetDomain = null) return; targetDomain. targetDomain.NameName = sourceDomain.Name; = sourceDomain.Name; targetDomain. targetDomain.FieldType FieldType = sourceDomain.FieldType;= sourceDomain.FieldType; tar

211、getDomain. targetDomain.MergePolicy MergePolicy = sourceDomain.MergePolicy;= sourceDomain.MergePolicy; targetDomain. targetDomain.SplitPolicy SplitPolicy = sourceDomain.SplitPolicy;= sourceDomain.SplitPolicy; targetWorkspaceDomains.AddDomain(targetDomain);targetWorkspaceDomains.AddDomain(targetDomai

212、n); sourceDomain = sourceDomains.Next(); sourceDomain = sourceDomains.Next(); nprivatevoidAddDomain() IWorkspaceFactoryIWorkspaceFactory pWorkspaceFactory = new pWorkspaceFactory = new AccessWorkspaceFactoryClass();AccessWorkspaceFactoryClass();IWorkspaceIWorkspace pWorkspace = pWorkspace = pWorkspa

213、ceFactory.OpenFromFile(d:temptest.mdb, 0);pWorkspaceFactory.OpenFromFile(d:temptest.mdb, 0);IWorkspaceDomainsIWorkspaceDomains pWorkspaceDomains = pWorkspace as pWorkspaceDomains = pWorkspace as IWorkspaceDomains;IWorkspaceDomains;IRangeDomainIRangeDomain pRangepRange = new = new RangeDomainClassRan

214、geDomainClass();();pRange.MinValue = 9.0; pRange.MinValue = 9.0; pRange.MaxValue = 600000.0;pRange.MaxValue = 600000.0;IDomainIDomain pDomain = pRange as IDomain; pDomain = pRange as IDomain;pDomain.Name = Area constraint; pDomain.Name = Area constraint; pDomain.FieldType = esriFieldType.esriFieldTy

215、peDouble;pDomain.FieldType = esriFieldType.esriFieldTypeDouble;pDomain.Description = Constrains the area Of buildings;pDomain.Description = Constrains the area Of buildings;pDomain.MergePolicy = esriMergePolicyType.esriMPTAreaWeighted;pDomain.MergePolicy = esriMergePolicyType.esriMPTAreaWeighted;pDo

216、main.SplitPolicy = esriSplitPolicyType.esriSPTGeometryRatio;pDomain.SplitPolicy = esriSplitPolicyType.esriSPTGeometryRatio;pWorkspaceDomains.AddDomain(pDomain);pWorkspaceDomains.AddDomain(pDomain);ICodedValueDomainICodedValueDomain pCodeValuepCodeValue = new = new CodedValueDomainClassCodedValueDoma

217、inClass();();pCodeValue.AddCode(RES, Residential);pCodeValue.AddCode(RES, Residential);pCodeValue.AddCode(COM, Commercial);pCodeValue.AddCode(COM, Commercial);pCodeValue.AddCode(IND, Industrial);pCodeValue.AddCode(IND, Industrial);pCodeValue.AddCode(BLD, Building);pCodeValue.AddCode(BLD, Building);/

218、QI/QI到到IDomainIDomain,设置属性,设置属性IDomainIDomain pDomainCodeValue = pCodeValue as IDomain; pDomainCodeValue = pCodeValue as IDomain;pDomainCodeValue.FieldType = esriFieldType.esriFieldTypeString;pDomainCodeValue.FieldType = esriFieldType.esriFieldTypeString;pDomainCodeValue.Name = Building types;pDomai

219、nCodeValue.Name = Building types;pDomainCodeValue.Description = Valid building type codes;pDomainCodeValue.Description = Valid building type codes;pDomainCodeValue.MergePolicy = pDomainCodeValue.MergePolicy = esriMergePolicyType.esriMPTDefaultValue;esriMergePolicyType.esriMPTDefaultValue;pDomainCode

220、Value.SplitPolicy = pDomainCodeValue.SplitPolicy = esriSplitPolicyType.esriSPTDuplicate;esriSplitPolicyType.esriSPTDuplicate;/添加到工作空间添加到工作空间pWorkspaceDomains.AddDomain(pDomainCodeValue);pWorkspaceDomains.AddDomain(pDomainCodeValue); 7.5 ROW,Object 和 Feature 对象n表、对象类和要素类是一个数表、对象类和要素类是一个数据集,作为它们的组成部分,

221、据集,作为它们的组成部分,Row(行行)、Object(对象对象)和和Feature(要素要素)就是最小的数据就是最小的数据单元。单元。它们的物理组成就是地它们的物理组成就是地理数据库中的一行记录。理数据库中的一行记录。n在在ArcObjects开发中,插入、更开发中,插入、更新和删除数据库中的一条记录新和删除数据库中的一条记录是非常普遍的操作。是非常普遍的操作。7.5.1 RowBuffer和Row对象nRowBuffer(缓冲行缓冲行)对象可以以一个行对象可以以一个行(Row)对象的状态出现,对象的状态出现,但是它并不是一个对象实体,即它是临时存在内存中的对象。但是它并不是一个对象实体,即

222、它是临时存在内存中的对象。这个对象主要被使用在一个插入游标的这个对象主要被使用在一个插入游标的InsertRow方法中,作方法中,作为一个参数使用。用户可以使用为一个参数使用。用户可以使用ITable:CreateRowBuffer方法方法来获得一个缓冲行对象。来获得一个缓冲行对象。nIRowBuffer接口用于获得接口用于获得RowBuffer对象的属性,如对象的属性,如Fields用用于获得缓冲行对象的字段集,而于获得缓冲行对象的字段集,而Value属性可以通过字段的索属性可以通过字段的索引值得到某个字段的值,通常用于被设置字段值。引值得到某个字段的值,通常用于被设置字段值。nRow是一个

223、组件类对象,它代表了一个表对象中的一行记录。程是一个组件类对象,它代表了一个表对象中的一行记录。程序员可以根据一个表的游标来获得序员可以根据一个表的游标来获得Row对象,如对象,如ICursor:Row,当然,用户可以根据它的当然,用户可以根据它的OID值来获得,如值来获得,如ITable:GetRow。nArcObjects中一般都使用中一般都使用ITable:CreateRow来产生一个新的来产生一个新的Row对象,在得到这个对象后,需要给这个对象不同字段的值进行设对象,在得到这个对象后,需要给这个对象不同字段的值进行设置,置,Row对象也有字段集,而且这个字段集和它所属于的表的字对象也有

224、字段集,而且这个字段集和它所属于的表的字段集是一样的,因而用户还需要通过段集是一样的,因而用户还需要通过IFields对象的各种方法和属对象的各种方法和属性来获得某个具体的字段,然后赋值给它。性来获得某个具体的字段,然后赋值给它。nRow对象主要支持对象主要支持IRow接口,这个接口从接口,这个接口从IRowBuffer继继承而来。承而来。int i = pTable.FindField(Name);int i = pTable.FindField(Name);/添加一条记录添加一条记录IRowIRow pRow = pTable.CreateRow(); pRow = pTable.Crea

225、teRow();pRow.set_Value(i, esri);pRow.set_Value(i, esri);pRow.Store();pRow.Store();/更新一条记录更新一条记录pRow.set_Value(i,esrichina);pRow.set_Value(i,esrichina);pRow.Store();pRow.Store();/删除一条记录删除一条记录pRow.Delete();pRow.Delete();7.5.2 Object和Feature对象nObjectClass对象是对象是Object(对象对象)的集合,的集合,Object代表了一个具有代表了一个具有属性

226、和行为的实体对象。在地理数据库中,对象类其实也是一张属性和行为的实体对象。在地理数据库中,对象类其实也是一张表,所以表,所以Object对象也就是表中的一条记录。对象也就是表中的一条记录。nIObject接口是接口是Object对象的主要接口,它和对象的主要接口,它和IRow接口非常相似,接口非常相似,且是从且是从IRow继承的。继承的。IObject:Class属性可以获得对象的容器属性可以获得对象的容器-对对象类。象类。nFeature(要素要素)是要素类中的一条记录,是从对象派生出来的既具是要素类中的一条记录,是从对象派生出来的既具有属性和行为,而且还能保存空间数据的对象。要素中的几何形

227、有属性和行为,而且还能保存空间数据的对象。要素中的几何形体对象定义了要素类的类型。一个要素中可能保存的几何形体有体对象定义了要素类的类型。一个要素中可能保存的几何形体有点、点集、多边形和多义线。点、点集、多边形和多义线。nFeature对象的主要接口是对象的主要接口是IFeature,它从,它从IRowBuffer、IRow和和IObject继承而来,继承而来,定义了要素对象特有的属性和方法。定义了要素对象特有的属性和方法。nIFeatureBuffer接口从接口从IRowBuffer继承而来,并增加了一个继承而来,并增加了一个Shape属性。属性。nprivatevoidAddFeature

228、ToFeatureClass(IFeatureClasspFeatClass,IGeometrypGeom) IFeatureCursorIFeatureCursor pFeatCur = pFeatClass.Insert(true); pFeatCur = pFeatClass.Insert(true);IFeatureBufferIFeatureBuffer pFeatBuf = pFeatClass.CreateFeatureBuffer(); pFeatBuf = pFeatClass.CreateFeatureBuffer();IPolygonIPolygon pPolygon =

229、 pGeom as IPolygon; pPolygon = pGeom as IPolygon;IFieldsIFields pFlds = pFeatClass.Fields; pFlds = pFeatClass.Fields; IField pFld ;IField pFld ;for (int i = 1 ; i = pFlds.FieldCount - 1; i+)for (int i = 1 ; i = pFlds.FieldCount - 1; i+) pFld = pFlds.get_Field(i); pFld = pFlds.get_Field(i);if (pFld.T

230、ype = esriFieldType.esriFieldTypeGeometry) pFeatBuf.set_Value(i,pGeom);if (pFld.Type = esriFieldType.esriFieldTypeGeometry) pFeatBuf.set_Value(i,pGeom);else else if (pFld.Type = esriFieldType.esriFieldTypeInteger) pFeatBuf.set_Value(i, 0);if (pFld.Type = esriFieldType.esriFieldTypeInteger) pFeatBuf.

231、set_Value(i, 0);if (pFld.Type = esriFieldType.esriFieldTypeDouble ) pFeatBuf.set_Value(i,0.0); if (pFld.Type = esriFieldType.esriFieldTypeDouble ) pFeatBuf.set_Value(i,0.0); if (pFld.Type = esriFieldType.esriFieldTypeSmallInteger) pFeatBuf.set_Value(i, if (pFld.Type = esriFieldType.esriFieldTypeSmal

232、lInteger) pFeatBuf.set_Value(i, 0); 0); if (pFld.Type = esriFieldType.esriFieldTypeString) if (pFld.Type = esriFieldType.esriFieldTypeString) pFeatBuf.set_Value(i,noname);pFeatBuf.set_Value(i,noname); pFeatCur.InsertFeature(pFeatBuf);pFeatCur.InsertFeature(pFeatBuf); nIFeatureEdit、IFeatureEdit2接口定义的

233、方法可以用于编辑单个的要素接口定义的方法可以用于编辑单个的要素或者多个要素组成的要素集。或者多个要素组成的要素集。n这个接口定义的这个接口定义的MoveSet、RotateSet和和IRow:DeleteSet方法既可以使用方法既可以使用在单个要素上,也可以使用在多个要素组成的要素集上。在单个要素上,也可以使用在多个要素组成的要素集上。nIFeatureEdit:Split和和IFeatureEdit:SplitAttributes操作是使用在一个操作是使用在一个单一的要素上,前者可以通过一个或多个点分割多义线,或者使用多单一的要素上,前者可以通过一个或多个点分割多义线,或者使用多义线分割多边

234、形。在分割后,新的要素会产生,而旧的要素会被删除义线分割多边形。在分割后,新的要素会产生,而旧的要素会被删除掉。在分割一个或者多个要素时,用户需要通过一定的机制来分割要掉。在分割一个或者多个要素时,用户需要通过一定的机制来分割要素的属性字段中的值,素的属性字段中的值,SplitAttributes方法将会执行这个过程,而属性方法将会执行这个过程,而属性分割机制是由字段的值域来决定的。但程序员并不需要在分割机制是由字段的值域来决定的。但程序员并不需要在Split要素后要素后执行执行SplitAttributes方法,因为后者会自动执行。方法,因为后者会自动执行。7.5.3 更新要素n在在GIS数

235、据库中使用数据库中使用GeoDatabase模型更新一个要素的数据,就模型更新一个要素的数据,就如同在普通数据库中更新一行记录一样方便。如同在普通数据库中更新一行记录一样方便。ArcObjects提供了提供了多种方法:如多种方法:如IFeature:Store和和IFeatureClass:Update都可以达都可以达到目的,但前一种方法一次只能够改变一个要素的值,而后者到目的,但前一种方法一次只能够改变一个要素的值,而后者可以一次更新多个要素的值。可以一次更新多个要素的值。npublicvoidUpdateTypeValues(IFeatureClassfeatureClass) IQuer

236、yFilterIQueryFilter queryFilter = new QueryFilterClass(); queryFilter = new QueryFilterClass(); queryFilter.WhereClause =Type = COM; queryFilter.WhereClause =Type = COM; queryFilter.SubFields = FID, Type; queryFilter.SubFields = FID, Type; IFeatureCursorIFeatureCursor featureCursor = featureClass. f

237、eatureCursor = featureClass.UpdateUpdate(queryFilter, false); (queryFilter, false); IFieldsIFields fields = featureCursor.Fields; fields = featureCursor.Fields; intint fieldIndex = fields.FindField(Type); fieldIndex = fields.FindField(Type); IFeature feature = null; IFeature feature = null; while (f

238、eature = featureCursor.NextFeature() != null) while (feature = featureCursor.NextFeature() != null) Console.WriteLine(The old type: 0, Console.WriteLine(The old type: 0, feature.get_Value(fieldIndex);feature.get_Value(fieldIndex); feature.set_Value(fieldIndex, ABC); feature.set_Value(fieldIndex, ABC); featureCursor.UpdateFeature(feature); featureCursor.UpdateFeature(feature); Console.WriteLine(The new type: 0, Console.WriteLine(The new type: 0, feature.get_Value(fieldIndex);feature.get_Value(fieldIndex); Marshal.ReleaseComObject(featureCursor);Marshal.ReleaseComObject(featureCursor);

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 教学/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号