ArcEngine中数据集创建、保存方法总结

包括FeatureClassFeatureLayer创建、保存,临时图层创建,聚合图元创建等。

一、将查询到的要素集输出为SHP文件,涉及到FeatureClass的创建方法,IFeatureDataConverter
的使用。利用IFeatureDataConverter2根据此思路还可以将选择集输出为SHP文件。

private IEnumInvalidObject SaveFeatureClassToFile(IFeatureClass pInFeatureClass, QueryFilter queryFilter, string outputDirectory, string outputFileName)

{

 //设置inputFclassname

IDataset pInDataset = pInFeatureClass as IDataset;

 IDatasetName pInputDatasetName = (IDatasetName)pInDataset.FullName;

 IFeatureClassName pInputFClassName = (IFeatureClassName)pInputDatasetName;

 ///////////////////////////////////////////// //创建一个输出shp文件的工作空间
IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass() as IWorkspaceFactory;

 IWorkspace pWorkspace = pShpWorkspaceFactory.OpenFromFile(outputDirectory, 0);

 IWorkspaceName pOutWorkspaceName = new WorkspaceNameClass();

 IDataset pDataset = pWorkspace as IDataset;

 pOutWorkspaceName = (IWorkspaceName)pDataset.FullName;

 //创建一个要素集合

IFeatureDatasetName pOutFeatureDatasetName = null;

 //创建一个要素类

IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass();

 IDatasetName pOutDatasetClassName =(IDatasetName)pOutFeatureClassName;

 pOutDatasetClassName.Name = outputFileName;

//作为输出参数

pOutDatasetClassName.WorkspaceName = pOutWorkspaceName;

 //////////////////////////////////////////////////////////////// //创建输出文件属性字段

 IFields pOutFields, pInFields;

 IFieldChecker pFieldChecker;

 IField pGeoField;

 IEnumFieldError pEnumFieldError = null;

 pInFields = pInFeatureClass.Fields;

 pFieldChecker = new FieldChecker();

 pFieldChecker.Validate(pInFields, out pEnumFieldError, out pOutFields);

 ///设置输出文件的几何定义

String shapeFieldName = pInFeatureClass.ShapeFieldName;

 int shapeFieldIndex = pInFeatureClass.FindField(shapeFieldName);

 IField shapeField = pInFeatureClass.Fields.get_Field(shapeFieldIndex);

 IGeometryDef geometryDef = shapeField.GeometryDef;

 IClone geometryDefClone = (IClone)geometryDef;

 IClone outGeometryDefClone = geometryDefClone.Clone();

 IGeometryDef outGeometryDef = (IGeometryDef)outGeometryDefClone;

 IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass();

 IEnumInvalidObject enumInvalidObject = featureDataConverter.ConvertFeatureClass (pInputFClassName, queryFilter, null, pOutFeatureClassName, outGeometryDef, pOutFields, “”, 1000, 0);

 return enumInvalidObject;

 }

另外,完成此功能时,曾想到使用IQueryDef接口,代码如下,后来发现IQueryDef并不能在Shapefile
Coverage
数据中使用,而仅仅可以使用在GeoDatabase数据库中。

 IDataset dataset = pRegLayer.FeatureClass as IDataset;

 IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)dataset.Workspace;

 IQueryDef queryDef = featureWorkspace.CreateQueryDef();

 queryDef.Tables = dataset.Name;

 queryDef.SubFields = “*”;

 queryDef.WhereClause = dataset.Name+”.BEDCD=’COG'”;

// “STATE_NAME = ‘California'”;

 IFeatureDataset featureDataset = featureWorkspace.OpenFeatureQuery(“Filtted”, queryDef);

 IFeatureClassContainer featureClassContainer = (IFeatureClassContainer)featureDataset;

 IFeatureClass pFeatureClass = featureClassContainer.get_ClassByName(“Filtted”);

 二、创建要素图层,并关联要素类添加要素类到Map
对象
,此法可将内存中的FeatureClass添加到地图。
IFeatureLayer pFeatureLayer;

 pFeatureLayer = New FeatureLayerClass ;

 pFeatureLayer.FeatureClass = pFeatureClass ;

 pFeatureLayer.Name = pFeatureClass.AliasName ;

 pMap.AddLayer (pFeatureLayer) ;

 三、用选择集创建临时图层 IFeatureLayerDefinition
pFDefinition = pSelectionSet as IFeatureLayerDefinition;

 pFDefinition.CreateSelectionLayer(“newlayerName”, true, null, null);

 IFeatureLayer pFeatureLayer=pFDefinition as IFeatureLayer;

 四、用选择集创建聚合图元,并获取图元范围 IEnumGeometry
pEnumGeom=new EnumFeatureGeometryClass();

 IEnumGeometryBind pEnumGeomBind = pEnumGeom as IEnumGeometryBind;

 pEnumGeomBind.BindGeometrySource(null, pFeatureSelection.SelectionSet);

 IGeometryFactory pGeomFactory = new GeometryEnvironmentClass();

 IGeometry pGeom = pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom);

 m_pMapControl.ActiveView.Extent = pGeom.Envelope;

 参考网址: http://bbs.esrichina-bj.cn/ESRI/thread-50618-1-1.html
http://bbs.esrichina-bj.cn/esri/thread-69689-1-1.html http://bbs.esrichina-bj.cn/ESRI/thread-65661-1-1.html http://www.cnblogs.com/zhangjun1130/archive/2009/04/11/1433947.html

 

 

转载自:https://blog.csdn.net/lk103852503/article/details/6567507

You may also like...