arcengine 要素类的复制

转自原文arcengine 要素类的复制

using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
namespace MyGISClass
{
    /// <summary>
    /// 该类主要包含了要素类的复制以及同要素类数据的加载
    /// 函数主要用于SDE与Personal GDB之间数据的处理
    /// </summary>
    class FeatureClassDataManager
    {
        /// <summary>
        /// 根据传入的源要素类OldFeatureClass,新空间范围,要素存储工作空间,新要素类名
        /// 产生具有相同字段结构和不同空间范围的要素类
        /// </summary>
        /// <param name="OldFeatureClass">源要素类</param>
        /// <param name="SaveFeatWorkspace">存储工作空间</param>
        /// <param name="FeatClsName">新要素类名</param>
        /// <param name="pDomainEnv">新空间范围,可为null</param>
        /// <returns></returns>
        public IFeatureClass CloneFeatureClassInWorkspace(IFeatureClass OldFeatureClass, IFeatureWorkspace SaveFeatWorkspace, string FeatClsName, IEnvelope pDomainEnv)
        {
            IFields pFields = CloneFeatureClassFields(OldFeatureClass, pDomainEnv);
            return SaveFeatWorkspace.CreateFeatureClass(FeatClsName, pFields, null, null, esriFeatureType.esriFTSimple, OldFeatureClass.ShapeFieldName, "");
        }
        /// <summary>
        /// 复制AnnotationClass,未完待续
        /// </summary>
        /// <param name="OldFeatureClass"></param>
        /// <param name="SaveFeatWorkspace"></param>
        /// <param name="FeatClsName"></param>
        /// <param name="pDomainEnv"></param>
        /// <returns></returns>
        public IFeatureClass CloneAnnotationClassInWorkspace(IFeatureClass OldFeatureClass, IFeatureWorkspace SaveFeatWorkspace, string FeatClsName, IEnvelope pDomainEnv)
        {
            IFeatureWorkspaceAnno pFWSAnno = (IFeatureWorkspaceAnno)SaveFeatWorkspace;
            IAnnoClass pAnnoClass = (IAnnoClass)OldFeatureClass.Extension;
            return null;
        }
        /// <summary>
        /// 将inFeatureClass要素类中所有符合pQueryFilter的要素复制到saveFeatureClass中,仅复制不做任何修改
        /// </summary>
        /// <param name="inFeatureClass">源要素类</param>
        /// <param name="saveFeatureClass">存储要素类</param>
        /// <param name="pQueryFilter">过滤参数</param>
        /// <returns></returns>
        public bool LoadFeatureClass(IFeatureClass inFeatureClass, IFeatureClass saveFeatureClass, IQueryFilter pQueryFilter)
        {
            //生成两个要素类字段的对应表
            Dictionary<int, int> pFieldsDict = new Dictionary<int, int>();
            this.GetFCFieldsDirectory(inFeatureClass, saveFeatureClass, ref pFieldsDict);
            IFeatureCursor pinFeatCursor = inFeatureClass.Search(pQueryFilter, false);
            long nCount = inFeatureClass.FeatureCount(pQueryFilter);
            IFeature pinFeat = pinFeatCursor.NextFeature();
            IFeatureCursor psaveFeatCursor = saveFeatureClass.Insert(true);
            //使用IFeatureBuffer在内存中产生缓存避免多次打开,关闭数据库
            IFeatureBuffer psaveFeatBuf = null;
            IFeature psaveFeat = null;
            long n = 0;
            while (pinFeat != null)
            {
                try
                {
                    psaveFeatBuf = saveFeatureClass.CreateFeatureBuffer();
                    psaveFeat = psaveFeatBuf as IFeature;
                    if (inFeatureClass.FeatureType == esriFeatureType.esriFTAnnotation)
                    {
                        IAnnotationFeature pAF = (IAnnotationFeature)pinFeat;
                        IAnnotationFeature pNAF = (IAnnotationFeature)psaveFeat;
                        if (pAF.Annotation != null)
                        {
                            pNAF.Annotation = pAF.Annotation;
                        }
                    }
                    psaveFeat.Shape = pinFeat.Shape;
                    foreach (KeyValuePair<int, int> keyvalue in pFieldsDict)
                    {
                        if (pinFeat.get_Value(keyvalue.Key).ToString() == "")
                        {
                            if (psaveFeat.Fields.get_Field(keyvalue.Value).Type == esriFieldType.esriFieldTypeString)
                            {
                                psaveFeat.set_Value(keyvalue.Value, "");
                            }
                            else
                            {
                                psaveFeat.set_Value(keyvalue.Value, 0);
                            }
                        }
                        else
                        {
                            psaveFeat.set_Value(keyvalue.Value, pinFeat.get_Value(keyvalue.Key));
                        }
                    }
                    psaveFeatCursor.InsertFeature(psaveFeatBuf);
                }
                catch (Exception ex) { }
                finally
                {
                    psaveFeat = null;
                    n++;
                    if (n % 2000 == 0)
                    {
                        psaveFeatCursor.Flush();
                    }
                    pinFeat = pinFeatCursor.NextFeature();
                }
            }
            psaveFeatCursor.Flush();
            return true;
        }
        private IFields CloneFeatureClassFields(IFeatureClass pFeatureClass, IEnvelope pDomainEnv)
        {
            IFields pFields = new FieldsClass();
            IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;
            //根据传入的要素类,将除了shape字段之外的字段复制
            long nOldFieldsCount = pFeatureClass.Fields.FieldCount;
            long nOldGeoIndex = pFeatureClass.Fields.FindField(pFeatureClass.ShapeFieldName);
            for (int i = 0; i < nOldFieldsCount; i++)
            {
                if (i != nOldGeoIndex)
                {
                    pFieldsEdit.AddField(pFeatureClass.Fields.get_Field(i));
                }
                else
                {
                    IGeometryDef pGeomDef = new GeometryDefClass();
                    IGeometryDefEdit pGeomDefEdit = (IGeometryDefEdit)pGeomDef;
                    ISpatialReference pSR = null;
                    if (pDomainEnv != null)
                    {
                        pSR = new UnknownCoordinateSystemClass();
                        pSR.SetDomain(pDomainEnv.XMin, pDomainEnv.XMax, pDomainEnv.YMin, pDomainEnv.YMax);
                    }
                    else
                    {
                        IGeoDataset pGeoDataset = pFeatureClass as IGeoDataset;
                        pSR = CloneSpatialReference(pGeoDataset.SpatialReference);
                    }
                    //设置新要素类Geometry的参数
                    pGeomDefEdit.GeometryType_2 = pFeatureClass.ShapeType;
                    pGeomDefEdit.GridCount_2 = 1;
                    pGeomDefEdit.set_GridSize(0, 10);
                    pGeomDefEdit.AvgNumPoints_2 = 2;
                    pGeomDefEdit.SpatialReference_2 = pSR;
                    //产生新的shape字段
                    IField pField = new FieldClass();
                    IFieldEdit pFieldEdit = (IFieldEdit)pField;
                    pFieldEdit.Name_2 = "shape";
                    pFieldEdit.AliasName_2 = "shape";
                    pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
                    pFieldEdit.GeometryDef_2 = pGeomDef;
                    pFieldsEdit.AddField(pField);
                }
            }
            return pFields;
        }
        private ISpatialReference CloneSpatialReference(ISpatialReference pSrcSpatialReference)
        {
            double xmin, xmax, ymin, ymax;
            pSrcSpatialReference.GetDomain(out xmin, out xmax, out ymin, out ymax);
            ISpatialReference pSR = new UnknownCoordinateSystemClass();
            pSR.SetDomain(xmin, xmax, ymin, ymax);
            return pSR;
        }
        private void GetFCFieldsDirectory(IFeatureClass pFCold,IFeatureClass pFCnew,ref Dictionary<int,int> FieldsDictionary)
        {
            for(int i=0;i<pFCold.Fields.FieldCount;i++)
            {
                string tmpstrold=pFCold.Fields.get_Field(i).Name.ToUpper();
                switch(tmpstrold)
                {
                    case "OBJECTID":
                    case "SHAPE":
                    case "SHAPE_LENGTH":
                    case "SHAPE_AREA":
                    case "FID":
                        {
                            //以上字段由系统自动生成
                            break;
                        }
                    default:
                        {
                            for(int j=0;j<pFCnew.Fields.FieldCount;j++)
                            {
                                string tmpstrnew=pFCnew.Fields.get_Field(j).Name.ToUpper();
                                if(tmpstrold==tmpstrnew)
                                {
                                    FieldsDictionary.Add(i,j);
                                    break;
                                }
                            }
                            break;
                        }
                }
            }
        }
    }
}  
时间: 2024-10-02 11:23:52

arcengine 要素类的复制的相关文章

ArcGIS模型构建器案例教程-批量复制工作空间所有要素类

目的:批量复制工作空间所有要素类 工具名称:WorkspaceCopyFeatureClasses 使用方法:输入工作空间,指定输出工作空间 结果: 联系方式:谢老师,I35_4855_4328,xiexiaokui#139.com 原文地址:https://www.cnblogs.com/xiexiaokui/p/10165055.html

ArcEngine创建要素类01

单独生成要素类: public IFeatureClass CreateStandaloneFeatureClass(IWorkspace workspace, String featureClassName, IFields fieldsCollection, String shapeFieldName) { IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace; IFeatureClassDescription f

Arcgis engine 要素集的复制和删除

一. 要素集的删除 1.对于一般的要素类删除 可直接用IDataset.Delete()方法进行删除 2.对于无网络的要素集删除 可用IFeatureDataset.Delete()方法进行删除 3.对有网络的要素集删除时,需用IFeatureWorkspaceManage.DeleteByName(IDatasetName name)方法删除 二.要素集的复制 IGeoDBDataTansfer接口用于实现要素集的复制,注意的是从gdb将要素集复制到sde,且gdb要素集中含有网络时,用此接口

ArcGIS Engine问答:为什么地理数据库中不能产生同名要素类

之所以产生这样的问题,其原因是无论一个要素类是直接放在工作空问中,还是放在工作空问的一个要素数据集中,这些差别仅仅是逻辑上的,而它们的物理组成都是数据库中的一张二维表,并目表名就是要素类的名字,在一个数据库中不能出现两个同名的二维表,因此也就不能产生两个同名的要素类. 也就是说如果在工作空问中存在一个名为A的要素类和B的要素数据集,B中如果再产生一个名为A的要素类是不会成功的. 因此可以使用IFeatureWorkspace::OpenFeatureClass方法可以打开工作空问中的任何一个要素

要素类的复制方法

转自原文 要素类的复制方法 如何根据一个要素类(源要素类)在另一个工作空间中产生一个同字段,但空间范围可能不一致的要素类(目标要素类)?本文提供了完备的方法. public ESRI.ArcGIS.Geodatabase.IFeatureClass CloneFeatureClass(ESRI.ArcGIS.Geodatabase.IFeatureClass srcFeatureClass, ESRI.ArcGIS.Geodatabase.IFeatureWorkspace SaveFeatWo

获取mdb下要素类FeatureClass,独立要素类,没有dataset获取要素类

转载自:http://blog.sina.com.cn/s/blog_6faf711d0100za4x.html 获取mdb数据库要素类的名称 整体思路如下:1.通过IWorkspace的Datasets属性获取工作空间中的所有Dataset对象(IEnumDataset)2.枚举EnumDataset,获取Dataset对象3.如果该Dataset是FeatureDataset4.QI到IFeatureClassContainer接口for(int i=0;i < pFClContainer.

Geodatabase - 创建要素类.

在NET中,会遇到以“_2”结尾的属性,这些属性是可写的. 以下代码在已有工作空间下,创建一个新的点要素类: 1 //例如,[email protected]"G:\doc\gis\1.400\data\pdb.mdb", featureClassName="newF" 2 public void CreateFeatureClass(string personalDBPath, string featureClassName) { 3 //创建点类型. 4 //空

Geodatabase - 打开要素类

1 string dbPath = @"G:\doc\gis\1.400\data\pdb.mdb"; 2 ESRI.ArcGIS.Geodatabase.IWorkspaceFactory wsf = new ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactoryClass(); 3 ESRI.ArcGIS.Geodatabase.IWorkspace ws = wsf.OpenFromFile(dbPath, 0); 4 ESRI.Arc

ArcGIS Engine检索要素集、要素类和要素

转自原文 ArcGIS Engine检索要素集.要素类和要素 [csharp] view plain copy print? /// <summary> /// 获取所有要素集 /// </summary> /// <param name="workspace">工作空间对象</param> /// <returns>要素集列表</returns> public static List<IFeatureDat