예제 #1
0
        /// <summary>
        /// 检验字段合理性
        /// </summary>
        /// <param name="targetWorkspace">目标工作空间</param>
        /// <param name="isReset">是否使用检验结果重置目标字段</param>
        /// <returns>是否通过字段检验,若不通过则错误信息可通过ErrorMessage属性获取</returns>
        public bool ValidateFields(IWorkspace targetWorkspace, bool isReset)
        {
            IFieldChecker   pFieldChecker   = new FieldChecker();
            IEnumFieldError pEnumFieldError = null;
            IFields         validateFilelds = null;

            pFieldChecker.ValidateWorkspace = targetWorkspace;
            pFieldChecker.Validate(m_Fields, out pEnumFieldError, out validateFilelds);

            if (pEnumFieldError != null)
            {
                IList <string> result = new List <string>();
                IFieldError    error  = pEnumFieldError.Next();
                while (error != null)
                {
                    result.Add(string.Format("字段 {0} 出错,原因为 :{1}", m_Fields.Field[error.FieldIndex].Name, error.FieldError.ToString()));
                    error = pEnumFieldError.Next();
                }
                ErrorMessage = string.Join(";", result);

                if (isReset)
                {
                    ResetFields(validateFilelds);
                }

                return(false);
            }
            return(true);
        }
예제 #2
0
        //格式转换后,检验字段的有效性并修复字段 YH 15/4/09
        public static bool FieldsCheckAfterTransaction(IWorkspace pInputWorkspace, IWorkspace pOutPutWorkspace, IFields pInputFields, ref IFields pOoutPutFields)
        {
            bool            functionReturnValue = false;
            IFieldChecker   pFieldChecker       = default(IFieldChecker);
            IEnumFieldError pEnumFieldError     = null;

            pFieldChecker = new FieldChecker();
            pFieldChecker.InputWorkspace    = pInputWorkspace;
            pFieldChecker.ValidateWorkspace = pOutPutWorkspace;
            pFieldChecker.Validate(pInputFields, out pEnumFieldError, out pOoutPutFields);

            //处理字段中不符合语义规则的情况
            if ((pEnumFieldError != null))
            {
                pEnumFieldError.Reset();
                IFieldError pFieldError = default(IFieldError);
                pFieldError = pEnumFieldError.Next();
                //g_clsErrorHandle.DisplayInformation("导出字段中存在无效字段: " + pOoutPutFields.Field(pFieldError.FieldIndex).Name + "导出终止!!", false);
                MessageBoxEx.Show("导出字段中存在无效字段: " + pOoutPutFields.get_Field(pFieldError.FieldIndex).Name + "导出终止!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return(false);

                return(functionReturnValue);
            }
            return(true);

            return(functionReturnValue);
        }
예제 #3
0
        /// <summary>
        /// 将源表的字段放在目标数据库环境中进行检查,生成新的字段
        /// </summary>
        /// <param name="pClass"></param>
        /// <param name="pTargetWks"></param>
        /// <param name="sError"></param>
        /// <returns></returns>
        public bool CheckFields(IClass pClass, IWorkspace pTargetWks, out IFields ResultFields)
        {
            ResultFields = null;
            IFields sourceFields = pClass.Fields;
            string  sTableName   = (pClass as IDataset).Name;

            if (sTableName.IndexOf('.') > 0)
            {
                //面积字段
                string sAreaFieldName = sTableName + ".AREA";
                int    iAreaIndex     = sourceFields.FindField(sAreaFieldName);
                if (iAreaIndex >= 0)
                {
                    IFieldEdit fieldEdit = sourceFields.get_Field(iAreaIndex) as IFieldEdit;
                    fieldEdit.Name_2      = "AREA";
                    fieldEdit.AliasName_2 = "面积";
                }
                //长度字段
                string sLenFieldName = sTableName + ".LEN";
                int    iLenIndex     = sourceFields.FindField(sLenFieldName);
                if (iLenIndex >= 0)
                {
                    IFieldEdit fieldEdit = sourceFields.get_Field(iLenIndex) as IFieldEdit;
                    fieldEdit.Name_2      = "LEN";
                    fieldEdit.AliasName_2 = "长度";
                }
            }
            // 创建字段检查对象
            IFieldChecker pFieldChecker = new FieldChecker();

            pFieldChecker.InputWorkspace    = (pClass as IDataset).Workspace;
            pFieldChecker.ValidateWorkspace = pTargetWks;
            // 验证字段
            IEnumFieldError pEnumFieldError = null;

            try
            {
                ErrorMessage = "";
                IList <string> tempError = new List <string>();
                pFieldChecker.Validate(sourceFields, out pEnumFieldError, out ResultFields);
                if (pEnumFieldError != null)
                {
                    IFieldError pFieldError = null;
                    while ((pFieldError = pEnumFieldError.Next()) != null)
                    {
                        tempError.Add(sourceFields.get_Field(pFieldError.FieldIndex).Name + GetFieldNameError(pFieldError.FieldError));
                    }
                }
                ErrorMessage = string.Join(",", tempError);
            }
            finally
            {
                if (pEnumFieldError != null)
                {
                    Marshal.ReleaseComObject(pEnumFieldError);
                }
                Marshal.ReleaseComObject(pFieldChecker);
            }
            return(string.IsNullOrEmpty(ErrorMessage));
        }
예제 #4
0
        public IEnumInvalidObject ConvertTable(IDatasetName idatasetName_0, IQueryFilter iqueryFilter_0,
                                               IDatasetName idatasetName_1, IFields ifields_0, string string_0, int int_0, int int_1)
        {
            IEnumFieldError    enumFieldError;
            IFields            field;
            string             str;
            IEnumInvalidObject enumInvalidObject = null;
            IFieldChecker      fieldCheckerClass = new FieldChecker();
            IWorkspace         workspace         = (idatasetName_1.WorkspaceName as IName).Open() as IWorkspace;

            fieldCheckerClass.ValidateWorkspace = workspace;
            fieldCheckerClass.Validate(ifields_0, out enumFieldError, out field);
            fieldCheckerClass.ValidateTableName(idatasetName_1.Name, out str);
            string str1  = str;
            ITable table = null;

            table = (workspace as IFeatureWorkspace).CreateTable(str1, field, null, null, string_0);
            ITable  table1  = (idatasetName_0 as IName).Open() as ITable;
            ICursor cursor  = table1.Search(iqueryFilter_0, false);
            int     num     = 0;
            ICursor cursor1 = table.Insert(true);
            IRow    row     = cursor.NextRow();

            while (row != null)
            {
                IRowBuffer rowBuffer = table.CreateRowBuffer();
                try
                {
                    this.method_1(cursor1, rowBuffer, field, row);
                }
                catch (Exception exception)
                {
                    Logger.Current.Error("", exception, "");
                }
                num++;
                if (num == int_0)
                {
                    num = 0;
                    cursor1.Flush();
                }
                row = cursor.NextRow();
                if (this.ifeatureProgress_StepEventHandler_0 == null)
                {
                    continue;
                }
                this.ifeatureProgress_StepEventHandler_0();
            }
            if (num > 0)
            {
                cursor1.Flush();
            }
            ComReleaser.ReleaseCOMObject(cursor);
            ComReleaser.ReleaseCOMObject(cursor1);
            return(enumInvalidObject);
        }
예제 #5
0
파일: Menu.cs 프로젝트: AgentWord/SiPing
/// <summary>
/// 保存输出JointCount>1的点位图层
/// </summary>
/// <param name="apFeatureClass"></param>
        public void ExportFeatureClassToShp(IFeatureClass apFeatureClass)
        {
            if (apFeatureClass == null)
            {
                MessageBox.Show("请选择", "系统提示");
                return;
            }
//调用保存文件函数
            SaveFileDialog sa = new SaveFileDialog();

            sa.Filter = "SHP文件(.shp)|*.shp";
            sa.ShowDialog();
            sa.CreatePrompt = true;
            string ExportShapeFileName = sa.FileName;

// string StrFilter = "SHP文件(.shp)|*.shp";
// string ExportShapeFileName = SaveFileDialog(StrFilter);
            if (ExportShapeFileName == "")
            {
                return;
            }
            string ExportFileShortName = System.IO.Path.GetFileNameWithoutExtension(ExportShapeFileName);
            string ExportFilePath      = System.IO.Path.GetDirectoryName(ExportShapeFileName);

            shpPath = ExportFilePath + "\\" + ExportFileShortName + "\\" + ExportFileShortName + ".shp";
//设置导出要素类的参数
            IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass();
            IDataset          pOutDataset          = (IDataset)apFeatureClass;

            pOutFeatureClassName = (IFeatureClassName)pOutDataset.FullName;
//创建一个输出shp文件的工作空间
            IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
            IWorkspaceName    pInWorkspaceName     = new WorkspaceNameClass();

            pInWorkspaceName = pShpWorkspaceFactory.Create(ExportFilePath, ExportFileShortName, null, 0);
//创建一个要素集合
            IFeatureDatasetName pInFeatureDatasetName = null;
//创建一个要素类
            IFeatureClassName pInFeatureClassName = new FeatureClassNameClass();
            IDatasetName      pInDatasetClassName;

            pInDatasetClassName               = (IDatasetName)pInFeatureClassName;
            pInDatasetClassName.Name          = ExportFileShortName;//作为输出参数
            pInDatasetClassName.WorkspaceName = pInWorkspaceName;
//通过FIELDCHECKER检查字段的合法性,为输出SHP获得字段集合
            long            iCounter;
            IFields         pOutFields, pInFields;
            IFieldChecker   pFieldChecker;
            IField          pGeoField;
            IEnumFieldError pEnumFieldError = null;

            pInFields     = apFeatureClass.Fields;
            pFieldChecker = new FieldChecker();
            pFieldChecker.Validate(pInFields, out pEnumFieldError, out pOutFields);
//通过循环查找几何字段
            pGeoField = null;
            for (iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++)
            {
                if (pOutFields.get_Field((int)iCounter).Type == esriFieldType.esriFieldTypeGeometry)
                {
                    pGeoField = pOutFields.get_Field((int)iCounter);
                    break;
                }
            }
//得到几何字段的几何定义
            IGeometryDef     pOutGeometryDef;
            IGeometryDefEdit pOutGeometryDefEdit;

            pOutGeometryDef = pGeoField.GeometryDef;
//设置几何字段的空间参考和网格
            pOutGeometryDefEdit             = (IGeometryDefEdit)pOutGeometryDef;
            pOutGeometryDefEdit.GridCount_2 = 1;
            pOutGeometryDefEdit.set_GridSize(0, 1500000);
            try
            {
//开始导入
                IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass();
                pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, null, pInFeatureDatasetName, pInFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0);
                MessageBox.Show("导出成功", "系统提示");
            }
            catch (Exception ex)
            {
                MessageBox.Show("the following exception occurred:" + ex.ToString());
            }
        }
예제 #6
0
        private void ImportFeatureClassToNewWorkSpace(string filename, string outfilename)
        {
            //try
            //{
            string ImportShapeFileName = filename;

            if (ImportShapeFileName == "")
            {
                return;
            }
            string            ImportFileShortName = System.IO.Path.GetFileNameWithoutExtension(ImportShapeFileName);
            string            ExportFileShortName = System.IO.Path.GetFileNameWithoutExtension(outfilename);
            string            ImportFilePath      = System.IO.Path.GetDirectoryName(ImportShapeFileName);
            IWorkspaceFactory Pwf        = new FileGDBWorkspaceFactoryClass();
            IWorkspace        pWorkspace = Pwf.OpenFromFile(@comboBox1.Text, 0);

            //IWorkspaceFactory Pwf = new AccessWorkspaceFactoryClass();
            //IWorkspace pWorkspace = Pwf.OpenFromFile(@"E:\test\x.mdb", 0);

            IWorkspaceName      pInWorkspaceName;
            IFeatureDatasetName pOutFeatureDSName;
            IFeatureClassName   pInFeatureClassName;
            IDatasetName        pInDatasetName;
            IFeatureClassName   pOutFeatureClassName;
            IDatasetName        pOutDatasetName;
            long                  iCounter;
            IFields               pOutFields, pInFields;
            IFieldChecker         pFieldChecker;
            IField                pGeoField;
            IGeometryDef          pOutGeometryDef;
            IGeometryDefEdit      pOutGeometryDefEdit;
            IName                 pName;
            IFeatureClass         pInFeatureClass;
            IFeatureDataConverter pShpToClsConverter;
            IEnumFieldError       pEnumFieldError = null;

            //得到一个输入SHP文件的工作空间,
            pInWorkspaceName          = new WorkspaceNameClass();
            pInWorkspaceName.PathName = ImportFilePath;
            pInWorkspaceName.WorkspaceFactoryProgID = "esriCore.ShapefileWorkspaceFactory.1";
            //创建一个新的要素类名称,目的是为了以来PNAME接口的OPEN方法打开SHP文件
            pInFeatureClassName          = new FeatureClassNameClass();
            pInDatasetName               = (IDatasetName)pInFeatureClassName;
            pInDatasetName.Name          = ImportFileShortName;
            pInDatasetName.WorkspaceName = pInWorkspaceName;
            //打开一个SHP文件,将要读取它的字段集合
            pName           = (IName)pInFeatureClassName;
            pInFeatureClass = (IFeatureClass)pName.Open();
            //通过FIELDCHECKER检查字段的合法性,为输入要素类获得字段集合
            pInFields     = pInFeatureClass.Fields;
            pFieldChecker = new FieldChecker();
            pFieldChecker.Validate(pInFields, out pEnumFieldError, out pOutFields);
            //通过循环查找几何字段
            pGeoField = null;
            for (iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++)
            {
                if (pOutFields.get_Field((int)iCounter).Type == esriFieldType.esriFieldTypeGeometry)
                {
                    pGeoField = pOutFields.get_Field((int)iCounter);
                    break;
                }
            }
            //得到几何字段的几何定义
            pOutGeometryDef = pGeoField.GeometryDef;
            //设置几何字段的空间参考和网格
            pOutGeometryDefEdit             = (IGeometryDefEdit)pOutGeometryDef;
            pOutGeometryDefEdit.GridCount_2 = 1;
            pOutGeometryDefEdit.set_GridSize(0, 1500000);

            //创建一个新的要素类名称作为可用的参数
            pOutFeatureClassName = new FeatureClassNameClass();
            pOutDatasetName      = (IDatasetName)pOutFeatureClassName;
            pOutDatasetName.Name = ExportFileShortName;
            //创建一个新的数据集名称作为可用的参数
            pOutFeatureDSName = (IFeatureDatasetName) new FeatureDatasetName();
            //如果参数的值是NULL,说明要创建独立要素类
            //创建一个不存在的要素集合pFDN,通过它将IFeatureClassName和工作空间连接起来,而ConvertFeatureClass函数并不使用该变量作为参数,
            IFeatureDatasetName pFDN = new FeatureDatasetNameClass();
            IDatasetName        pDN  = (IDatasetName)pFDN;
            IDataset            pDS  = (IDataset)pWorkspace;

            pDN.WorkspaceName = (IWorkspaceName)pDS.FullName;
            pOutFeatureClassName.FeatureDatasetName = (IDatasetName)pFDN;
            //将pOutFeatureDSName设置为Null,将它做为参数给ConvertFeatureClass函数,因为IFeatureClassName本身已经和工作空间关联了,生成的
            //要素类在工作空间的根目录下,即独立要素类
            pOutFeatureDSName = null;


            //开始导入
            pShpToClsConverter = new FeatureDataConverterClass();
            pShpToClsConverter.ConvertFeatureClass(pInFeatureClassName, null, pOutFeatureDSName, pOutFeatureClassName, null, pOutFields, "", 1000, 0);
            MessageBox.Show("导入成功", "系统提示");
            //}
            //catch
            //{
            //    MessageBox.Show("导入失败", "系统提示");
            //}
        }
예제 #7
0
        /// <summary>
        /// 要素类转Shape
        /// </summary>
        /// <param name="apFeatureClass"></param>
        private void ExportFeatureClassToConTempShp(IFeatureClass apFeatureClass)
        {
            if (Directory.Exists(System.Windows.Forms.Application.StartupPath + "\\Convert\\DWGConvert\\ConTempShp"))
            {
                Directory.Delete(System.Windows.Forms.Application.StartupPath + "\\Convert\\DWGConvert\\ConTempShp", true);
            }

            //设置导出要素类的参数
            IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass();
            IDataset          pOutDataset          = (IDataset)apFeatureClass;

            pOutFeatureClassName = (IFeatureClassName)pOutDataset.FullName;
            //创建一个输出shp文件的工作空间
            IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
            IWorkspaceName    pInWorkspaceName     = new WorkspaceNameClass();

            pInWorkspaceName = pShpWorkspaceFactory.Create(System.Windows.Forms.Application.StartupPath + "\\Convert\\DWGConvert", "ConTempShp", null, 0);

            //创建一个要素集合
            IFeatureDatasetName pInFeatureDatasetName = null;
            //创建一个要素类
            IFeatureClassName pInFeatureClassName = new FeatureClassNameClass();
            IDatasetName      pInDatasetClassName;

            pInDatasetClassName               = (IDatasetName)pInFeatureClassName;
            pInDatasetClassName.Name          = "ConTempShp";//作为输出参数
            pInDatasetClassName.WorkspaceName = pInWorkspaceName;
            //通过FIELDCHECKER检查字段的合法性,为输出SHP获得字段集合
            long            iCounter;
            IFields         pOutFields, pInFields;
            IFieldChecker   pFieldChecker;
            IField          pGeoField;
            IEnumFieldError pEnumFieldError = null;

            pInFields     = apFeatureClass.Fields;
            pFieldChecker = new FieldChecker();
            pFieldChecker.Validate(pInFields, out pEnumFieldError, out pOutFields);
            //通过循环查找几何字段
            pGeoField = null;
            for (iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++)
            {
                if (pOutFields.get_Field((int)iCounter).Type == esriFieldType.esriFieldTypeGeometry)
                {
                    pGeoField = pOutFields.get_Field((int)iCounter);
                    break;
                }
            }
            //得到几何字段的几何定义
            IGeometryDef     pOutGeometryDef;
            IGeometryDefEdit pOutGeometryDefEdit;

            pOutGeometryDef = pGeoField.GeometryDef;

            //设置几何字段的空间参考和网格
            pOutGeometryDefEdit             = (IGeometryDefEdit)pOutGeometryDef;
            pOutGeometryDefEdit.GridCount_2 = 1;
            pOutGeometryDefEdit.set_GridSize(0, 1500000);

            //try
            //{
            IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass();

            pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, null, pInFeatureDatasetName, pInFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0);
            // MessageBox.Show("导出成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);


            //}
            //catch (Exception ex)
            //{

            //}
            /*****20130227杨漾(去掉trycatch,上层trycatch(MainForm)处理)*****/
        }
예제 #8
0
        public static void ExtractSelectFeatureFormLayer(IFeatureLayer ifeatureLayer_0,
                                                         IFeatureWorkspace ifeatureWorkspace_0, IGeometry igeometry_0)
        {
            IEnumFieldError enumFieldError;
            IFields         field;
            string          str;
            ICursor         cursor;

            try
            {
                IFeatureSelection ifeatureLayer0 = (IFeatureSelection)ifeatureLayer_0;
                if (ifeatureLayer0.SelectionSet.Count != 0)
                {
                    IFeatureClass featureClass      = ifeatureLayer_0.FeatureClass;
                    IFields       fields            = featureClass.Fields;
                    IFieldChecker fieldCheckerClass = new FieldChecker()
                    {
                        InputWorkspace    = (featureClass as IDataset).Workspace,
                        ValidateWorkspace = ifeatureWorkspace_0 as IWorkspace
                    };
                    fieldCheckerClass.Validate(fields, out enumFieldError, out field);
                    enumFieldError.Reset();
                    IFieldError fieldError = enumFieldError.Next();
                    string      str1       = "";
                    while (fieldError != null)
                    {
                        fieldError.FieldError.ToString();
                        IField   field1 = fields.Field[fieldError.FieldIndex];
                        IField   field2 = field.Field[fieldError.FieldIndex];
                        string   str2   = str1;
                        string[] name   = new string[] { str2, field2.Name, " reason: ", field1.Name, "  " };
                        str1       = string.Concat(name);
                        fieldError = enumFieldError.Next();
                    }
                    Hashtable hashtables = new Hashtable();
                    string    name1      = ifeatureLayer_0.Name;
                    char      chr        = name1[0];
                    if ((chr < '0' ? false : chr <= '9'))
                    {
                        name1 = string.Concat("A", name1);
                    }
                    fieldCheckerClass.ValidateTableName(name1, out str);
                    name1 = str;
                    string      name2       = "";
                    IFieldsEdit fieldsClass = new Fields() as IFieldsEdit;
                    for (int i = field.FieldCount - 1; i >= 0; i--)
                    {
                        IField field3 = field.Field[i];
                        if (field3.Type == esriFieldType.esriFieldTypeGeometry)
                        {
                            name2 = field3.Name;
                            IGeometryDef      geometryDef      = field3.GeometryDef;
                            ISpatialReference spatialReference =
                                (geometryDef.SpatialReference as IClone).Clone() as ISpatialReference;
                            SpatialReferenctOperator.ChangeCoordinateSystem(ifeatureWorkspace_0 as IGeodatabaseRelease,
                                                                            spatialReference, false);
                            (geometryDef as IGeometryDefEdit).SpatialReference_2 = spatialReference;
                            (field3 as IFieldEdit).GeometryDef_2 = geometryDef;
                        }
                        if ((ifeatureWorkspace_0 as IWorkspace).Type != esriWorkspaceType.esriFileSystemWorkspace)
                        {
                            fieldsClass.AddField(field3);
                        }
                        else if (field3.Type != esriFieldType.esriFieldTypeBlob)
                        {
                            fieldsClass.AddField(field3);
                        }
                    }
                    IFeatureClass featureClass1 = null;
                    try
                    {
                        featureClass1 = ifeatureWorkspace_0.CreateFeatureClass(name1, fieldsClass, null, null,
                                                                               esriFeatureType.esriFTSimple, name2, "");
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show("无法创建输出要素类!");
                        return;
                    }
                    IFeatureCursor featureCursor  = null;
                    IFeatureCursor featureCursor1 = featureClass1.Insert(true);
                    IFeatureBuffer featureBuffer  = featureClass1.CreateFeatureBuffer();
                    ifeatureLayer0.SelectionSet.Search(null, false, out cursor);
                    featureCursor = (IFeatureCursor)cursor;
                    for (IFeature j = featureCursor.NextFeature(); j != null; j = featureCursor.NextFeature())
                    {
                        if (j.Shape != null)
                        {
                            Clip.InsertFeature(featureCursor1, featureBuffer, field, j, j.Shape, igeometry_0);
                        }
                    }
                    featureCursor1.Flush();
                    Marshal.ReleaseComObject(featureBuffer);
                    featureBuffer = null;
                    Marshal.ReleaseComObject(featureCursor1);
                    featureCursor1 = null;
                }
            }
            catch
            {
            }
        }
예제 #9
0
        public static void ClipLayer(IFeatureLayer ifeatureLayer_0, IFeatureClass ifeatureClass_0,
                                     IFeatureWorkspace ifeatureWorkspace_0, IMap imap_0)
        {
            IEnumFieldError enumFieldError;
            IFields         field;
            string          str;

            try
            {
                string        name              = ifeatureLayer_0.Name;
                IFields       fields            = ifeatureLayer_0.FeatureClass.Fields;
                IFieldChecker fieldCheckerClass = new FieldChecker()
                {
                    ValidateWorkspace = ifeatureWorkspace_0 as IWorkspace
                };
                fieldCheckerClass.Validate(fields, out enumFieldError, out field);
                char chr = name[0];
                if ((chr < '0' ? false : chr <= '9'))
                {
                    name = string.Concat("A", name);
                }
                fieldCheckerClass.ValidateTableName(name, out str);
                string name1 = "";
                int    num   = 0;
                while (true)
                {
                    if (num < field.FieldCount)
                    {
                        IField field1 = field.Field[num];
                        if (field1.Type == esriFieldType.esriFieldTypeGeometry)
                        {
                            name1 = field1.Name;
                            IGeometryDef geometryDef = field1.GeometryDef;
                            (geometryDef as IGeometryDefEdit).SpatialReference_2 = geometryDef.SpatialReference;
                            (field1 as IFieldEdit).GeometryDef_2 = geometryDef;
                            break;
                        }
                        else
                        {
                            num++;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                name = str;
                IFeatureClass featureClass = ifeatureWorkspace_0.CreateFeatureClass(name, field, null, null,
                                                                                    esriFeatureType.esriFTSimple, name1, "");
                IFeatureClassName  fullName = (featureClass as IDataset).FullName as IFeatureClassName;
                IBasicGeoprocessor basicGeoprocessorClass = new BasicGeoprocessor();
                IFeatureClass      featureClass1          = basicGeoprocessorClass.Clip(ifeatureLayer_0 as ITable, false,
                                                                                        ifeatureClass_0 as ITable, false, 0, fullName);
                if (imap_0 != null)
                {
                    IFeatureLayer featureLayerClass = new FeatureLayer();
                    (featureLayerClass as IGeoFeatureLayer).Renderer = (ifeatureLayer_0 as IGeoFeatureLayer).Renderer;
                    featureLayerClass.Name         = (featureClass as IDataset).Name;
                    featureLayerClass.FeatureClass = featureClass1;
                    imap_0.AddLayer(featureLayerClass);
                }
            }
            catch
            {
            }
        }
예제 #10
0
        private IFeatureClass method_1(string string_0, int int_0, IntPtr intptr_0,
                                       IFeatureWorkspace ifeatureWorkspace_0, out bool bool_0)
        {
            IEnumFieldError enumFieldError;
            IFields         field;
            IFeatureClass   featureClass;

            bool_0 = false;
            try
            {
                IFeatureClass            featureClass1                = null;
                string                   fileNameWithoutExtension     = System.IO.Path.GetFileNameWithoutExtension(string_0);
                IFeatureClassDescription featureClassDescriptionClass =
                    new FeatureClassDescription() as IFeatureClassDescription;
                string  shapeFieldName = featureClassDescriptionClass.ShapeFieldName;
                IFields requiredFields = (featureClassDescriptionClass as IObjectClassDescription).RequiredFields;
                this.AddTabField(requiredFields as IFieldsEdit, intptr_0);
                this.AddTabField(int_0, requiredFields as IFieldsEdit);
                IFields       field1            = requiredFields;
                IFieldChecker fieldCheckerClass = new FieldChecker()
                {
                    ValidateWorkspace = ifeatureWorkspace_0 as IWorkspace
                };
                fieldCheckerClass.Validate(field1, out enumFieldError, out field);
                IFieldEdit       fieldEdit   = field.Field[field.FindField(shapeFieldName)] as IFieldEdit;
                IGeometryDefEdit geometryDef = fieldEdit.GeometryDef as IGeometryDefEdit;
                if (this.ispatialReference_0 == null)
                {
                    string str = TabRead._mitab_c_get_coordsys_xml(intptr_0);
                    if (str == null)
                    {
                        this.ispatialReference_0 = geometryDef.SpatialReference;
                    }
                    else if (str.Length <= 0)
                    {
                        this.ispatialReference_0 = geometryDef.SpatialReference;
                    }
                    else
                    {
                        string       str1         = string.Concat(System.IO.Path.GetTempPath(), "\\temp.prj");
                        StreamWriter streamWriter = new StreamWriter(str1);
                        try
                        {
                            streamWriter.Write(str);
                        }
                        finally
                        {
                            if (streamWriter != null)
                            {
                                ((IDisposable)streamWriter).Dispose();
                            }
                        }
                        try
                        {
                            this.ispatialReference_0 =
                                (new SpatialReferenceEnvironment()).CreateESRISpatialReferenceFromPRJFile(str1);
                        }
                        catch
                        {
                            this.ispatialReference_0 = geometryDef.SpatialReference;
                        }
                        try
                        {
                            File.Delete(str1);
                        }
                        catch
                        {
                        }
                    }
                    double num  = 0;
                    double num1 = 0;
                    double num2 = 0;
                    double num3 = 0;
                    TabRead._mitab_c_get_mif_bounds(intptr_0, ref num, ref num1, ref num2, ref num3);
                    SpatialReferenctOperator.ChangeCoordinateSystem(ifeatureWorkspace_0 as IGeodatabaseRelease,
                                                                    this.ispatialReference_0, false);
                    num  = num - 10;
                    num1 = num1 + 10;
                    num2 = num2 - 10;
                    num3 = num3 + 10;
                    this.ispatialReference_0.SetDomain(num, num1, num2, num3);
                }
                geometryDef.SpatialReference_2 = this.ispatialReference_0;
                TabRead._mitab_c_get_feature_count_bytype(intptr_0, int_0);
                switch (int_0)
                {
                case 1:
                case 2:
                case 3:
                {
                    fileNameWithoutExtension = string.Concat(fileNameWithoutExtension, "_Point");
                    fileNameWithoutExtension = this.method_0(ifeatureWorkspace_0 as IWorkspace,
                                                             fileNameWithoutExtension);
                    geometryDef.GeometryType_2 = esriGeometryType.esriGeometryPoint;
                    fieldEdit.GeometryDef_2    = geometryDef;
                    featureClass1 = ifeatureWorkspace_0.CreateFeatureClass(fileNameWithoutExtension, field, null,
                                                                           null, esriFeatureType.esriFTSimple, shapeFieldName, "");
                    break;
                }

                case 4:
                {
                    fileNameWithoutExtension = string.Concat(fileNameWithoutExtension, "_Anno_Point");
                    fileNameWithoutExtension = this.method_0(ifeatureWorkspace_0 as IWorkspace,
                                                             fileNameWithoutExtension);
                    geometryDef.GeometryType_2 = esriGeometryType.esriGeometryPoint;
                    fieldEdit.GeometryDef_2    = geometryDef;
                    featureClass1 = ifeatureWorkspace_0.CreateFeatureClass(fileNameWithoutExtension, field, null,
                                                                           null, esriFeatureType.esriFTSimple, shapeFieldName, "");
                    break;
                }

                case 5:
                case 6:
                {
                    fileNameWithoutExtension = string.Concat(fileNameWithoutExtension, "_Line");
                    fileNameWithoutExtension = this.method_0(ifeatureWorkspace_0 as IWorkspace,
                                                             fileNameWithoutExtension);
                    geometryDef.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
                    fieldEdit.GeometryDef_2    = geometryDef;
                    featureClass1 = ifeatureWorkspace_0.CreateFeatureClass(fileNameWithoutExtension, field, null,
                                                                           null, esriFeatureType.esriFTSimple, shapeFieldName, "");
                    break;
                }

                case 7:
                {
                    fileNameWithoutExtension = string.Concat(fileNameWithoutExtension, "_Polygon");
                    fileNameWithoutExtension = this.method_0(ifeatureWorkspace_0 as IWorkspace,
                                                             fileNameWithoutExtension);
                    geometryDef.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
                    fieldEdit.GeometryDef_2    = geometryDef;
                    featureClass1 = ifeatureWorkspace_0.CreateFeatureClass(fileNameWithoutExtension, field, null,
                                                                           null, esriFeatureType.esriFTSimple, shapeFieldName, "");
                    break;
                }

                case 8:
                case 9:
                {
                    featureClass = null;
                    return(featureClass);
                }

                case 10:
                {
                    fileNameWithoutExtension = string.Concat(fileNameWithoutExtension, "_MultiPoint");
                    fileNameWithoutExtension = this.method_0(ifeatureWorkspace_0 as IWorkspace,
                                                             fileNameWithoutExtension);
                    geometryDef.GeometryType_2 = esriGeometryType.esriGeometryMultipoint;
                    fieldEdit.GeometryDef_2    = geometryDef;
                    featureClass1 = ifeatureWorkspace_0.CreateFeatureClass(fileNameWithoutExtension, field, null,
                                                                           null, esriFeatureType.esriFTSimple, shapeFieldName, "");
                    break;
                }

                default:
                {
                    featureClass = null;
                    return(featureClass);
                }
                }
                featureClass = featureClass1;
                return(featureClass);
            }
            catch (Exception exception)
            {
                Logger.Current.Error("", exception, "");
                bool_0 = true;
            }
            featureClass = null;
            return(featureClass);
        }
예제 #11
0
        public static void Project(IFeatureClass ifeatureClass_0, ISpatialReference ispatialReference_0,
                                   IFeatureDatasetName ifeatureDatasetName_0, string string_0, double double_0)
        {
            IWorkspace            workspace            = ((IDataset)ifeatureClass_0).Workspace;
            IFeatureDataConverter featureDataConverter = new FeatureDataConverter();
            IWorkspaceName        workspaceName        = new WorkspaceName() as IWorkspaceName;

            workspaceName.ConnectionProperties   = workspace.ConnectionProperties;
            workspaceName.WorkspaceFactoryProgID = workspace.WorkspaceFactory.GetClassID().Value.ToString();
            IWorkspaceName workspaceName2 = (ifeatureDatasetName_0 as IDatasetName).WorkspaceName;
            IDatasetName   datasetName    = new FeatureClassName() as IDatasetName;
            string         text           = ifeatureClass_0.AliasName;
            int            num            = text.LastIndexOf(".");

            if (num != -1)
            {
                text = text.Substring(num + 1);
            }
            datasetName.Name          = text;
            datasetName.WorkspaceName = workspaceName;
            IWorkspace2  workspace2   = (workspaceName2 as IName).Open() as IWorkspace2;
            IDatasetName datasetName2 = new FeatureClassName() as IDatasetName;

            datasetName2.WorkspaceName = workspaceName2;
            (datasetName2 as IFeatureClassName).FeatureDatasetName = (ifeatureDatasetName_0 as IDatasetName);
            IFieldChecker fieldChecker = new FieldChecker();

            fieldChecker.ValidateWorkspace = (workspace2 as IWorkspace);
            string[] array = string_0.Split(new char[]
            {
                '.'
            });
            string_0 = array[array.Length - 1] + "_Project";
            string text2;

            fieldChecker.ValidateTableName(string_0, out text2);
            string text3 = text2;
            int    num2  = 1;

            if (workspaceName2.Type == esriWorkspaceType.esriRemoteDatabaseWorkspace ||
                workspaceName2.Type == esriWorkspaceType.esriLocalDatabaseWorkspace)
            {
                while (workspace2.get_NameExists(esriDatasetType.esriDTFeatureClass, text3))
                {
                    text3 = text2 + "_" + num2.ToString();
                    num2++;
                }
            }
            else if (workspaceName2.Type == esriWorkspaceType.esriFileSystemWorkspace)
            {
                while (System.IO.File.Exists(text3 + ".shp"))
                {
                    text3 = text2 + "_" + num2.ToString();
                    num2++;
                }
            }
            datasetName2.Name = text3;
            IFields fields = new ESRI.ArcGIS.Geodatabase.Fields() as IFields;

            num = ifeatureClass_0.Fields.FindField(ifeatureClass_0.ShapeFieldName);
            IField            field       = ifeatureClass_0.Fields.get_Field(num);
            IGeometryDef      geometryDef = field.GeometryDef;
            ISpatialReference arg_1D9_0   = geometryDef.SpatialReference;
            double            num3;
            double            num4;
            double            num5;
            double            num6;

            ispatialReference_0.GetDomain(out num3, out num4, out num5, out num6);
            ((IGeometryDefEdit)geometryDef).GridCount_2 = 1;
            ((IGeometryDefEdit)geometryDef).set_GridSize(0, double_0);
            ((IGeometryDefEdit)geometryDef).SpatialReference_2 = ispatialReference_0;
            ((IFieldEdit)field).GeometryDef_2 = geometryDef;
            for (int i = 0; i < ifeatureClass_0.Fields.FieldCount; i++)
            {
                if (i == num)
                {
                    ((IFieldsEdit)fields).AddField(field);
                }
                else
                {
                    IField field2 = ifeatureClass_0.Fields.get_Field(i);
                    ((IFieldsEdit)fields).AddField(field2);
                }
            }
            IEnumFieldError enumFieldError;
            IFields         outputFields;

            fieldChecker.Validate(fields, out enumFieldError, out outputFields);
            if (SRLibCommonFunc.m_pfrm != null)
            {
                SRLibCommonFunc.m_pfrm.FeatureProgress = featureDataConverter;
            }
            try
            {
                featureDataConverter.ConvertFeatureClass((IFeatureClassName)datasetName, null, ifeatureDatasetName_0,
                                                         (IFeatureClassName)datasetName2, geometryDef, outputFields, "", 1000, 0);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }
예제 #12
0
 public static void ExtractSelectFeatureFormLayer(IFeatureLayer ifeatureLayer_0,
                                                  IFeatureWorkspace ifeatureWorkspace_0, IGeometry igeometry_0)
 {
     try
     {
         IFeatureSelection featureSelection = (IFeatureSelection)ifeatureLayer_0;
         if (featureSelection.SelectionSet.Count != 0)
         {
             IFeatureClass featureClass = ifeatureLayer_0.FeatureClass;
             IFields       fields       = featureClass.Fields;
             IFieldChecker fieldChecker = new FieldChecker();
             fieldChecker.InputWorkspace    = (featureClass as IDataset).Workspace;
             fieldChecker.ValidateWorkspace = (ifeatureWorkspace_0 as IWorkspace);
             IEnumFieldError enumFieldError;
             IFields         fields2;
             fieldChecker.Validate(fields, out enumFieldError, out fields2);
             enumFieldError.Reset();
             IFieldError fieldError = enumFieldError.Next();
             string      text       = "";
             while (fieldError != null)
             {
                 fieldError.FieldError.ToString();
                 IField field  = fields.get_Field(fieldError.FieldIndex);
                 IField field2 = fields2.get_Field(fieldError.FieldIndex);
                 string text2  = text;
                 text = string.Concat(new string[]
                 {
                     text2,
                     field2.Name,
                     " reason: ",
                     field.Name,
                     "  "
                 });
                 fieldError = enumFieldError.Next();
             }
             new Hashtable();
             string text3 = ifeatureLayer_0.Name;
             string text4;
             fieldChecker.ValidateTableName(text3, out text4);
             text3 = text4;
             string      shapeFieldName = "";
             IFieldsEdit fieldsEdit     = new Fields() as IFieldsEdit;
             for (int i = fields2.FieldCount - 1; i >= 0; i--)
             {
                 IField field3 = fields2.get_Field(i);
                 if (field3.Type == esriFieldType.esriFieldTypeGeometry)
                 {
                     shapeFieldName = field3.Name;
                     IGeometryDef      geometryDef      = field3.GeometryDef;
                     ISpatialReference spatialReference =
                         (geometryDef.SpatialReference as IClone).Clone() as ISpatialReference;
                     SpatialReferenctOperator.ChangeCoordinateSystem(ifeatureWorkspace_0 as IGeodatabaseRelease,
                                                                     spatialReference, false);
                     (geometryDef as IGeometryDefEdit).SpatialReference_2 = spatialReference;
                     (field3 as IFieldEdit).GeometryDef_2 = geometryDef;
                 }
                 if ((ifeatureWorkspace_0 as IWorkspace).Type == esriWorkspaceType.esriFileSystemWorkspace)
                 {
                     if (field3.Type != esriFieldType.esriFieldTypeBlob)
                     {
                         fieldsEdit.AddField(field3);
                     }
                 }
                 else
                 {
                     fieldsEdit.AddField(field3);
                 }
             }
             IFeatureClass featureClass2 = null;
             try
             {
                 featureClass2 = ifeatureWorkspace_0.CreateFeatureClass(text3, fieldsEdit, null, null,
                                                                        esriFeatureType.esriFTSimple, shapeFieldName, "");
             }
             catch (Exception)
             {
                 System.Windows.Forms.MessageBox.Show("无法创建输出要素类!");
                 return;
             }
             IFeatureCursor featureCursor = featureClass2.Insert(true);
             IFeatureBuffer featureBuffer = featureClass2.CreateFeatureBuffer();
             ICursor        cursor;
             featureSelection.SelectionSet.Search(null, false, out cursor);
             IFeatureCursor featureCursor2 = (IFeatureCursor)cursor;
             for (IFeature feature = featureCursor2.NextFeature();
                  feature != null;
                  feature = featureCursor2.NextFeature())
             {
                 if (feature.Shape != null)
                 {
                     ClipHelper.InsertFeature(featureCursor, featureBuffer, fields2, feature, feature.Shape,
                                              igeometry_0);
                 }
             }
             featureCursor.Flush();
             Marshal.ReleaseComObject(featureBuffer);
             Marshal.ReleaseComObject(featureCursor);
         }
     }
     catch
     {
     }
 }
예제 #13
0
        private void method_1(IFeatureClass ifeatureClass_0, IWorkspace iworkspace_0)
        {
            IEnumFieldError enumFieldError;
            IFields         field;
            int             i;
            IField          field1;
            string          str;
            string          aliasName = ifeatureClass_0.AliasName;

            string[] strArrays = aliasName.Split(new char[] { '.' });
            aliasName = strArrays[(int)strArrays.Length - 1];
            IFields       fields            = ifeatureClass_0.Fields;
            IFieldChecker fieldCheckerClass = new FieldChecker()
            {
                ValidateWorkspace = iworkspace_0
            };

            fieldCheckerClass.Validate(fields, out enumFieldError, out field);
            List <SDEToShapefile.FieldMap> fieldMaps = new List <SDEToShapefile.FieldMap>();

            for (i = 0; i < fields.FieldCount; i++)
            {
                field1 = fields.Field[i];
                if (field1.Type != esriFieldType.esriFieldTypeBlob)
                {
                    IField field2 = field.Field[i];
                    fieldMaps.Add(new SDEToShapefile.FieldMap(field1.Name, field2.Name));
                }
            }
            for (i = field.FieldCount - 1; i >= 0; i--)
            {
                field1 = field.Field[i];
                if (field1.Type == esriFieldType.esriFieldTypeBlob)
                {
                    (field as IFieldsEdit).DeleteField(field1);
                }
            }
            fieldCheckerClass.ValidateTableName(aliasName, out str);
            aliasName = string.Concat(this.string_0, "\\", str);
            if (File.Exists(string.Concat(aliasName, ".shp")))
            {
                File.Delete(string.Concat(aliasName, ".shp"));
            }
            if (File.Exists(string.Concat(aliasName, ".shx")))
            {
                File.Delete(string.Concat(aliasName, ".shx"));
            }
            if (File.Exists(string.Concat(aliasName, ".dbf")))
            {
                File.Delete(string.Concat(aliasName, ".dbf"));
            }
            int              num         = field.FindField(ifeatureClass_0.ShapeFieldName);
            IFieldEdit       fieldEdit   = field.Field[num] as IFieldEdit;
            IGeometryDefEdit geometryDef = fieldEdit.GeometryDef as IGeometryDefEdit;

            if (ifeatureClass_0.FeatureType == esriFeatureType.esriFTAnnotation)
            {
                geometryDef.GeometryType_2 = esriGeometryType.esriGeometryPoint;
            }
            ISpatialReference spatialReference = geometryDef.SpatialReference;

            SpatialReferenctOperator.ChangeCoordinateSystem(iworkspace_0 as IGeodatabaseRelease, spatialReference, true);
            geometryDef.SpatialReference_2 = spatialReference;
            fieldEdit.GeometryDef_2        = geometryDef;
            try
            {
                IFeatureClass featureClass = (iworkspace_0 as IFeatureWorkspace).CreateFeatureClass(str, field, null,
                                                                                                    null, esriFeatureType.esriFTSimple, ifeatureClass_0.ShapeFieldName, "");
                this.method_2(ifeatureClass_0 as ITable, featureClass as ITable, fieldMaps, 800);
            }
            catch
            {
            }
        }
예제 #14
0
        /// <summary>
        /// 导出Shp文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="apFeatureClass"></param>
        /// <returns></returns>
        public static bool ExportFeatureClassToShp(string path, IFeatureClass apFeatureClass)
        {
            try
            {
                string exportFileShortName = System.IO.Path.GetFileNameWithoutExtension(path);
                if (exportFileShortName == "")
                {
                    exportFileShortName = (apFeatureClass as IDataset).Name;
                    if (exportFileShortName.LastIndexOf('.') >= 0)
                    {
                        exportFileShortName = exportFileShortName.Substring(exportFileShortName.LastIndexOf('.') + 1);
                    }
                }
                string exportFilePath = System.IO.Path.GetDirectoryName(path);
                if (exportFilePath == null)
                {
                    exportFilePath = path;
                }
                //设置导出要素类的参数
                IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass();
                IDataset          pOutDataset          = (IDataset)apFeatureClass;
                pOutFeatureClassName = (IFeatureClassName)pOutDataset.FullName;
                //创建一个输出shp文件的工作空间
                IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
                IWorkspaceName    pInWorkspaceName     = new WorkspaceNameClass();
                pInWorkspaceName = pShpWorkspaceFactory.Create(exportFilePath, exportFileShortName, null, 0);

                //创建一个要素集合
                IFeatureDatasetName pInFeatureDatasetName = null;
                //创建一个要素类
                IFeatureClassName pInFeatureClassName = new FeatureClassNameClass();
                IDatasetName      pInDatasetClassName;
                pInDatasetClassName               = (IDatasetName)pInFeatureClassName;
                pInDatasetClassName.Name          = exportFileShortName;
                pInDatasetClassName.WorkspaceName = pInWorkspaceName;
                //通过FIELDCHECKER检查字段的合法性,为输出SHP获得字段集合
                IFields pInFields = apFeatureClass.Fields;
                pInFields = apFeatureClass.Fields;
                IFieldChecker pFieldChecker = new FieldChecker();
                pFieldChecker.Validate(pInFields, out IEnumFieldError pEnumFieldError, out IFields pOutFields);
                //通过循环查找几何字段
                IField pGeoField = null;
                for (long iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++)
                {
                    if (pOutFields.get_Field((int)iCounter).Type == esriFieldType.esriFieldTypeGeometry)
                    {
                        pGeoField = pOutFields.get_Field((int)iCounter);
                        break;
                    }
                }
                //得到几何字段的几何定义
                IGeometryDef     pOutGeometryDef;
                IGeometryDefEdit pOutGeometryDefEdit;
                pOutGeometryDef = pGeoField.GeometryDef;
                //设置几何字段的空间参考和网格
                pOutGeometryDefEdit             = (IGeometryDefEdit)pOutGeometryDef;
                pOutGeometryDefEdit.GridCount_2 = 1;
                pOutGeometryDefEdit.set_GridSize(0, 1500000);

                //开始导入
                IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass();
                pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, null, pInFeatureDatasetName, pInFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0);
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
예제 #15
0
        public IEnumInvalidObject ConvertFeatureClass(IFeatureClassName ifeatureClassName_0, IQueryFilter iqueryFilter_0,
                                                      IFeatureDatasetName ifeatureDatasetName_0, IFeatureClassName ifeatureClassName_1,
                                                      IGeometryDef igeometryDef_0, IFields ifields_0, string string_0, int int_0, int int_1)
        {
            IEnumFieldError    enumFieldError;
            IFields            field;
            string             str;
            IEnumInvalidObject enumInvalidObject = null;
            IFieldChecker      fieldCheckerClass = new FieldChecker();
            IWorkspaceName     workspaceName     = null;

            workspaceName = (ifeatureDatasetName_0 == null
                ? (ifeatureClassName_1 as IDatasetName).WorkspaceName
                : (ifeatureDatasetName_0 as IDatasetName).WorkspaceName);
            IWorkspace workspace = (workspaceName as IName).Open() as IWorkspace;

            fieldCheckerClass.ValidateWorkspace = workspace;
            fieldCheckerClass.Validate(ifields_0, out enumFieldError, out field);
            string name = (ifeatureClassName_1 as IDatasetName).Name;

            fieldCheckerClass.ValidateTableName(name, out str);
            name = str;
            IField field1 = null;
            int    num    = 0;

            while (true)
            {
                if (num >= field.FieldCount)
                {
                    break;
                }
                else if (field.Field[num].Type == esriFieldType.esriFieldTypeGeometry)
                {
                    field1 = field.Field[num];
                    break;
                }
                else
                {
                    num++;
                }
            }
            IFeatureClass featureClass = null;

            if (ifeatureDatasetName_0 == null)
            {
                featureClass = (workspace as IFeatureWorkspace).CreateFeatureClass(name, field, null, null,
                                                                                   esriFeatureType.esriFTSimple, field1.Name, string_0);
            }
            else
            {
                IFeatureDataset featureDataset = (ifeatureDatasetName_0 as IName).Open() as IFeatureDataset;
                featureClass = featureDataset.CreateFeatureClass(name, field, null, null, esriFeatureType.esriFTSimple,
                                                                 field1.Name, string_0);
            }
            IFeatureClass  featureClass1  = (ifeatureClassName_0 as IName).Open() as IFeatureClass;
            IFeatureCursor featureCursor  = featureClass1.Search(iqueryFilter_0, false);
            IFeature       feature        = featureCursor.NextFeature();
            int            num1           = 0;
            IFeatureCursor featureCursor1 = featureClass.Insert(true);

            while (feature != null)
            {
                IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();
                if (feature.Shape != null)
                {
                    try
                    {
                        this.method_0(featureCursor1, featureBuffer, field, feature);
                    }
                    catch (Exception exception)
                    {
                        Logger.Current.Error("", exception, "");
                    }
                }
                num1++;
                if (num1 == int_0)
                {
                    num1 = 0;
                    featureCursor1.Flush();
                }
                feature = featureCursor.NextFeature();
                if (this.ifeatureProgress_StepEventHandler_0 == null)
                {
                    continue;
                }
                this.ifeatureProgress_StepEventHandler_0();
            }
            if (num1 > 0)
            {
                featureCursor1.Flush();
            }
            ComReleaser.ReleaseCOMObject(featureCursor);
            ComReleaser.ReleaseCOMObject(featureCursor1);
            return(enumInvalidObject);
        }
예제 #16
0
        public static void Project(IFeatureClass ifeatureClass_0, ISpatialReference ispatialReference_0,
                                   IWorkspace iworkspace_0, string string_0, double double_0)
        {
            string                str;
            double                num;
            double                num1;
            double                num2;
            double                num3;
            IEnumFieldError       enumFieldError;
            IFields               field;
            IWorkspace            workspace = ((IDataset)ifeatureClass_0).Workspace;
            IFeatureDataConverter featureDataConverterClass = new FeatureDataConverter();
            IWorkspaceName        workspaceNameClass        = new WorkspaceName() as IWorkspaceName;

            workspaceNameClass.ConnectionProperties   = workspace.ConnectionProperties;
            workspaceNameClass.WorkspaceFactoryProgID = workspace.WorkspaceFactory.GetClassID().Value.ToString();

            IWorkspaceName connectionProperties = new WorkspaceName() as IWorkspaceName;
            PropertySet    propertySetClass     = new PropertySet();

            connectionProperties.ConnectionProperties   = iworkspace_0.ConnectionProperties;
            connectionProperties.WorkspaceFactoryProgID = iworkspace_0.WorkspaceFactory.GetClassID().Value.ToString();
            IDatasetName featureClassNameClass = new FeatureClassName() as IDatasetName;
            string       aliasName             = ifeatureClass_0.AliasName;
            int          num4 = aliasName.LastIndexOf(".");

            if (num4 != -1)
            {
                aliasName = aliasName.Substring(num4 + 1);
            }
            featureClassNameClass.Name          = aliasName;
            featureClassNameClass.WorkspaceName = workspaceNameClass;
            IDatasetName datasetName = new FeatureClassName() as IDatasetName;

            {
                datasetName.WorkspaceName = connectionProperties;
            }
            ;
            IFieldChecker fieldCheckerClass = new FieldChecker()
            {
                ValidateWorkspace = iworkspace_0
            };

            string[] strArrays = string_0.Split(new char[] { '.' });
            string_0 = string.Concat(strArrays[(int)strArrays.Length - 1], "_Project");
            fieldCheckerClass.ValidateTableName(string_0, out str);
            string str1 = str;
            int    num5 = 1;

            if (
                !(iworkspace_0.Type == esriWorkspaceType.esriRemoteDatabaseWorkspace
                    ? false
                    : iworkspace_0.Type != esriWorkspaceType.esriLocalDatabaseWorkspace))
            {
                while (((IWorkspace2)iworkspace_0).NameExists[esriDatasetType.esriDTFeatureClass, str1])
                {
                    str1 = string.Concat(str, "_", num5.ToString());
                    num5++;
                }
            }
            else if (iworkspace_0.Type == esriWorkspaceType.esriFileSystemWorkspace)
            {
                while (File.Exists(string.Concat(str1, ".shp")))
                {
                    str1 = string.Concat(str, "_", num5.ToString());
                    num5++;
                }
            }
            datasetName.Name = str1;
            IFields fieldsClass = new ESRI.ArcGIS.Geodatabase.Fields();

            num4 = ifeatureClass_0.Fields.FindField(ifeatureClass_0.ShapeFieldName);
            IField            field1           = ifeatureClass_0.Fields.Field[num4];
            IGeometryDef      geometryDef      = field1.GeometryDef;
            ISpatialReference spatialReference = geometryDef.SpatialReference;

            ispatialReference_0.GetDomain(out num, out num1, out num2, out num3);
            ((IGeometryDefEdit)geometryDef).GridCount_2        = 1;
            ((IGeometryDefEdit)geometryDef).GridSize_2[0]      = double_0;
            ((IGeometryDefEdit)geometryDef).SpatialReference_2 = ispatialReference_0;
            ((IFieldEdit)field1).GeometryDef_2 = geometryDef;
            for (int i = 0; i < ifeatureClass_0.Fields.FieldCount; i++)
            {
                if (i != num4)
                {
                    IField field2 = ifeatureClass_0.Fields.Field[i];
                    ((IFieldsEdit)fieldsClass).AddField(field2);
                }
                else
                {
                    ((IFieldsEdit)fieldsClass).AddField(field1);
                }
            }
            fieldCheckerClass.Validate(fieldsClass, out enumFieldError, out field);
            if (SRLibCommonFunc.m_pfrm != null)
            {
                SRLibCommonFunc.m_pfrm.FeatureProgress = featureDataConverterClass;
            }
            try
            {
                featureDataConverterClass.ConvertFeatureClass((IFeatureClassName)featureClassNameClass, null, null,
                                                              (IFeatureClassName)datasetName, geometryDef, field, "", 1000, 0);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }
예제 #17
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            IName            internalObjectName;
            IFields          fields2;
            IEnumFieldError  error;
            string           fileNameWithoutExtension;
            string           str4;
            string           name;
            IField           field;
            int              num;
            IGeometryDefEdit geometryDef;
            Exception        exception;
            IFeatureDataset  dataset;

            if (this.txtOutName.Tag == null)
            {
                IGxFolder folder = new GxFolder();
                (folder as IGxFile).Path = Path.GetDirectoryName(this.txtOutName.Text);
                this.txtOutName.Tag      = folder;
            }
            if (this.txtOutName.Tag == null)
            {
                return;
            }
            IGxObject tag = this.txtOutName.Tag as IGxObject;

            if (tag is IGxFolder)
            {
                IWorkspaceName name2 = new WorkspaceName() as IWorkspaceName;


                name2.WorkspaceFactoryProgID = "esriDataSourcesFile.ShapefileWorkspaceFactory";
                name2.PathName = (tag.InternalObjectName as IFileName).Path;

                internalObjectName = name2 as IName;
                string text = this.txtOutName.Text;
                if (Path.GetExtension(this.txtOutName.Text) != ".shp")
                {
                    text = Path.GetFileNameWithoutExtension(this.txtOutName.Text) + ".shp";
                }
                if (File.Exists(text))
                {
                    MessageBox.Show("已经存在该shapefile文件,请重新输入文件名");
                    return;
                }
            }
            else
            {
                internalObjectName = tag.InternalObjectName;
            }
            IFields       inputField = this.m_pFeatureLayer.FeatureClass.Fields;
            IFieldChecker checker    = new FieldChecker();
            IFeatureClass class2     = null;

            if (internalObjectName is IWorkspaceName)
            {
                IFeatureWorkspace workspace = internalObjectName.Open() as IFeatureWorkspace;
                checker.ValidateWorkspace = workspace as IWorkspace;
                checker.Validate(inputField, out error, out fields2);
                fileNameWithoutExtension = Path.GetFileNameWithoutExtension(this.txtOutName.Text);
                checker.ValidateTableName(fileNameWithoutExtension, out str4);
                fileNameWithoutExtension = str4;
                ISpatialReference spatialReference = (this.m_pFeatureLayer.FeatureClass as IGeoDataset).SpatialReference;
                if (this.rdoSRType.SelectedIndex == 0)
                {
                    spatialReference = this.m_pMap.SpatialReference;
                }
                name = "";
                for (num = 0; num < fields2.FieldCount; num++)
                {
                    field = fields2.get_Field(num);
                    if (field.Type == esriFieldType.esriFieldTypeGeometry)
                    {
                        geometryDef = (field as IFieldEdit).GeometryDef as IGeometryDefEdit;
                        geometryDef.SpatialReference_2      = spatialReference;
                        (field as IFieldEdit).GeometryDef_2 = geometryDef;
                        name = field.Name;
                        break;
                    }
                }
                try
                {
                    if ((workspace is IWorkspace2) &&
                        (workspace as IWorkspace2).get_NameExists(esriDatasetType.esriDTFeatureClass,
                                                                  fileNameWithoutExtension))
                    {
                        MessageBox.Show("已经存在该要素类,请重新输入要素类名");
                        return;
                    }
                    class2 = workspace.CreateFeatureClass(fileNameWithoutExtension, fields2, null, null,
                                                          esriFeatureType.esriFTSimple, name, "");
                }
                catch (Exception exception1)
                {
                    exception = exception1;
                    MessageBox.Show("无法创建输出要素类!");
                    return;
                }
                goto Label_0400;
            }
            else
            {
                dataset = internalObjectName.Open() as IFeatureDataset;
                IWorkspace workspace2 = dataset.Workspace;
                checker.ValidateWorkspace = workspace2;
                checker.Validate(inputField, out error, out fields2);
                fileNameWithoutExtension = Path.GetFileNameWithoutExtension(this.txtOutName.Text);
                checker.ValidateTableName(fileNameWithoutExtension, out str4);
                fileNameWithoutExtension = str4;
                name = "";
                for (num = 0; num < fields2.FieldCount; num++)
                {
                    field = fields2.get_Field(num);
                    if (field.Type == esriFieldType.esriFieldTypeGeometry)
                    {
                        geometryDef = (field as IFieldEdit).GeometryDef as IGeometryDefEdit;
                        geometryDef.SpatialReference_2      = (dataset as IGeoDataset).SpatialReference;
                        (field as IFieldEdit).GeometryDef_2 = geometryDef;
                        name = field.Name;
                        break;
                    }
                }
            }
            try
            {
                class2 = dataset.CreateFeatureClass(fileNameWithoutExtension, fields2, null, null,
                                                    esriFeatureType.esriFTSimple, name, "");
            }
            catch (Exception exception2)
            {
                exception = exception2;
                MessageBox.Show("无法创建输出要素类!,原因:" + exception.Message);

                return;
            }
Label_0400:
            if (this.cboExportData.SelectedIndex == 0)
            {
                ExportDataHelper.ExportData(this.m_pFeatureLayer, class2, false);
            }
            else if (this.cboExportData.SelectedIndex == 1)
            {
                ISpatialFilter queryFilter = new SpatialFilter()
                {
                    Geometry = (this.m_pMap as IActiveView).Extent
                };
                IFeatureCursor cursor = this.m_pFeatureLayer.Search(queryFilter, false);
                ExportDataHelper.ExportData(cursor, class2);
                ComReleaser.ReleaseCOMObject(cursor);
            }
            else
            {
                ExportDataHelper.ExportData(this.m_pFeatureLayer, class2, true);
            }
            base.DialogResult = DialogResult.OK;
        }
예제 #18
0
        string m_strErr;                //错误信息提醒

        //SHP数据入库到GDB数据库的方法
        private void ImportFeatureClassToNewWorkSpace(string filename, string outfilename)
        {
            //try
            //{
            m_success = false;    //初始化
            string ImportShapeFileName = filename;
            string ExportFileShortName = outfilename;

            if (ImportShapeFileName == "")
            {
                return;
            }
            string ImportFileShortName = System.IO.Path.GetFileNameWithoutExtension(ImportShapeFileName);
            string ImportFilePath      = System.IO.Path.GetDirectoryName(ImportShapeFileName);

            //打开存在的工作空间,作为导入的空间
            IWorkspaceFactory Pwf = new FileGDBWorkspaceFactoryClass();
            //IWorkspace pWorkspace = Pwf.OpenFromFile(GetSourcePath(comboBoxSource.Text), 0);
            // IWorkspace2 pWorkspace2 =(IWorkspace2)(Pwf.OpenFromFile(GetSourcePath(comboBoxSource.Text), 0));
            IWorkspace pWorkspace = GetWorkspace(comboBoxSource.Text);

            if (pWorkspace == null)
            {
                m_strErr  = "数据源未找到";
                m_success = false;
                return;
            }
            string username = GetSourceUser(comboBoxSource.Text);

            if (username.Trim() != "")
            {
                ExportFileShortName = username + "." + ExportFileShortName;
            }
            IWorkspace2 pWorkspace2 = pWorkspace as IWorkspace2;

            //判断要素是否存在,若存在将删除源文件
            if (pWorkspace2.get_NameExists(esriDatasetType.esriDTFeatureClass, ExportFileShortName))
            {
                if (m_newfile == true)
                {
                    IFeatureClass     tmpfeatureclass;
                    IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
                    tmpfeatureclass = pFeatureWorkspace.OpenFeatureClass(ExportFileShortName);
                    IDataset set = tmpfeatureclass as IDataset;
                    fdr.DeleteSql(ExportFileShortName);
                    set.CanDelete();
                    set.Delete();
                }
                else
                {
                    //MessageBox.Show("存在相同文件名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    m_strErr  = "存在相同文件名";
                    m_success = false;
                    return;
                }
            }
            IWorkspaceName      pInWorkspaceName;
            IFeatureDatasetName pOutFeatureDSName;
            IFeatureClassName   pInFeatureClassName;
            IDatasetName        pInDatasetName;
            IFeatureClassName   pOutFeatureClassName;
            IDatasetName        pOutDatasetName;
            long                  iCounter;
            IFields               pOutFields, pInFields;
            IFieldChecker         pFieldChecker;
            IField                pGeoField;
            IGeometryDef          pOutGeometryDef;
            IGeometryDefEdit      pOutGeometryDefEdit;
            IName                 pName;
            IFeatureClass         pInFeatureClass;
            IFeatureDataConverter pShpToClsConverter;
            IEnumFieldError       pEnumFieldError = null;

            //得到一个输入SHP文件的工作空间,
            pInWorkspaceName          = new WorkspaceNameClass();
            pInWorkspaceName.PathName = ImportFilePath;
            pInWorkspaceName.WorkspaceFactoryProgID = "esriCore.ShapefileWorkspaceFactory.1";

            //创建一个新的要素类名称,目的是为了以来PNAME接口的OPEN方法打开SHP文件
            pInFeatureClassName          = new FeatureClassNameClass();
            pInDatasetName               = (IDatasetName)pInFeatureClassName;
            pInDatasetName.Name          = ImportFileShortName;
            pInDatasetName.WorkspaceName = pInWorkspaceName;

            //打开一个SHP文件,将要读取它的字段集合
            pName           = (IName)pInFeatureClassName;
            pInFeatureClass = (IFeatureClass)pName.Open();

            //通过FIELDCHECKER检查字段的合法性,为输入要素类获得字段集合
            pInFields     = pInFeatureClass.Fields;
            pFieldChecker = new FieldChecker();
            pFieldChecker.Validate(pInFields, out pEnumFieldError, out pOutFields);

            //通过循环查找几何字段
            pGeoField = null;
            for (iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++)
            {
                if (pOutFields.get_Field((int)iCounter).Type == esriFieldType.esriFieldTypeGeometry)
                {
                    pGeoField = pOutFields.get_Field((int)iCounter);
                    break;
                }
            }

            //得到几何字段的几何定义
            pOutGeometryDef = pGeoField.GeometryDef;

            //设置几何字段的空间参考和网格
            pOutGeometryDefEdit             = (IGeometryDefEdit)pOutGeometryDef;
            pOutGeometryDefEdit.GridCount_2 = 1;
            pOutGeometryDefEdit.set_GridSize(0, 1500000);

            //创建一个新的要素类名称作为可用的参数
            pOutFeatureClassName = new FeatureClassNameClass();
            pOutDatasetName      = (IDatasetName)pOutFeatureClassName;
            pOutDatasetName.Name = ExportFileShortName;

            //创建一个新的数据集名称作为可用的参数
            pOutFeatureDSName = (IFeatureDatasetName) new FeatureDatasetName();

            //如果参数的值是NULL,说明要创建独立要素类
            //创建一个不存在的要素集合pFDN,通过它将IFeatureClassName和工作空间连接起来,而ConvertFeatureClass函数并不使用该变量作为参数,
            IFeatureDatasetName pFDN = new FeatureDatasetNameClass();
            IDatasetName        pDN  = (IDatasetName)pFDN;
            IDataset            pDS  = (IDataset)pWorkspace;

            pDN.WorkspaceName = (IWorkspaceName)pDS.FullName;
            pOutFeatureClassName.FeatureDatasetName = (IDatasetName)pFDN;

            //将pOutFeatureDSName设置为Null,将它做为参数给ConvertFeatureClass函数,因为IFeatureClassName本身已经和工作空间关联了,生成的
            //要素类在工作空间的根目录下,即独立要素类
            pOutFeatureDSName = null;

            //开始导入
            if (InsertIntoDatabase(ExportFileShortName))
            {
                pShpToClsConverter = new FeatureDataConverterClass();
                pShpToClsConverter.ConvertFeatureClass(pInFeatureClassName, null, pOutFeatureDSName, pOutFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0);
                //MessageBox.Show("导入成功", "提示");
                m_success = true;
            }

            //}
            //catch
            //{
            //    m_success = false;
            //}
        }
예제 #19
0
        /// <summary>
        /// 导出FeatureClass到Shapefile文件
        /// </summary>
        /// <param name="sPath"></param>
        /// <param name="apFeatureClass"></param>
        public static bool ExportFeatureClassToShp(string sPath, IFeatureClass apFeatureClass)
        {
            try
            {
                DateTime dt             = DateTime.Now;
                string   strTime        = string.Format("{0:yyyyMMddHHmmss}", dt);
                string   fileName       = System.IO.Path.GetFileNameWithoutExtension(sPath);              //文件名称
                string   ExportFilePath = System.IO.Path.GetDirectoryName(sPath);                         //文件夹路径
                string   ExportFileName = ExportFilePath.Substring(ExportFilePath.LastIndexOf("\\") + 1); //最后选择的文件夹
                //如果是c:\\这样的根目录,那么根日期建立一个导出文件夹的名称
                if (string.IsNullOrEmpty(ExportFileName))
                {
                    ExportFileName = "导出Shape文件";
                }
                else
                {
                    ExportFilePath = ExportFilePath.Substring(0, ExportFilePath.LastIndexOf("\\"));//除了导出文件夹的路径
                }
                //设置导出要素类的参数
                IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass();
                IDataset          pOutDataset          = (IDataset)apFeatureClass;
                pOutFeatureClassName = (IFeatureClassName)pOutDataset.FullName;
                //创建一个输出shp文件的工作空间
                IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
                IWorkspaceName    pInWorkspaceName     = new WorkspaceNameClass();
                pInWorkspaceName = pShpWorkspaceFactory.Create(ExportFilePath, ExportFileName, null, 0);

                //创建一个要素集合
                IFeatureDatasetName pInFeatureDatasetName = null;
                //创建一个要素类
                IFeatureClassName pInFeatureClassName = new FeatureClassNameClass();
                IDatasetName      pInDatasetClassName;
                pInDatasetClassName               = (IDatasetName)pInFeatureClassName;
                pInDatasetClassName.Name          = fileName + strTime;//作为输出参数
                pInDatasetClassName.WorkspaceName = pInWorkspaceName;
                //通过FIELDCHECKER检查字段的合法性,为输出SHP获得字段集合
                long    iCounter;
                IFields pOutFields, pInFields;

                IField          pGeoField;
                IEnumFieldError pEnumFieldError = null;

                IQueryFilter filter = null;
                string       subset = "";

                pInFields = apFeatureClass.Fields;
                IFieldChecker pFieldChecker = new FieldChecker();
                pFieldChecker.Validate(pInFields, out pEnumFieldError, out pOutFields);

                //通过循环查找几何字段
                pGeoField = null;
                for (iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++)
                {
                    IField field = pOutFields.get_Field((int)iCounter);
                    if (field.Type == esriFieldType.esriFieldTypeGeometry)
                    {
                        pGeoField = field;
                    }
                    if (!IsBlob(field))
                    {
                        string fieldName = field.Name;
                        if (fieldName.Contains("_"))
                        {
                            fieldName = fieldName.Replace("_", ".");
                        }
                        subset += (fieldName + ",");
                    }
                }
                subset           = subset.Substring(0, subset.LastIndexOf(","));
                filter           = new QueryFilterClass();
                filter.SubFields = subset;

                //得到几何字段的几何定义
                IGeometryDef     pOutGeometryDef;
                IGeometryDefEdit pOutGeometryDefEdit;
                pOutGeometryDef = pGeoField.GeometryDef;
                //设置几何字段的空间参考和网格
                pOutGeometryDefEdit             = (IGeometryDefEdit)pOutGeometryDef;
                pOutGeometryDefEdit.GridCount_2 = 1;
                pOutGeometryDefEdit.set_GridSize(0, 1500000);

                //开始导入
                IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass();
                pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, filter, pInFeatureDatasetName, pInFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
예제 #20
0
        public static void ExtractSelectFeatureFormFeatureCursor(ILayer ilayer_0, IFeatureCursor ifeatureCursor_0,
                                                                 IFeatureWorkspace ifeatureWorkspace_0, IGeometry igeometry_0, IMap imap_0)
        {
            IEnumFieldError enumFieldError;
            IFields         field;
            string          str;
            string          name = ilayer_0.Name;

            if (ifeatureCursor_0 != null)
            {
                IFields       fields            = ifeatureCursor_0.Fields;
                IFieldChecker fieldCheckerClass = new FieldChecker()
                {
                    ValidateWorkspace = ifeatureWorkspace_0 as IWorkspace
                };
                fieldCheckerClass.Validate(fields, out enumFieldError, out field);
                char chr = name[0];
                if ((chr < '0' ? false : chr <= '9'))
                {
                    name = string.Concat("A", name);
                }
                fieldCheckerClass.ValidateTableName(name, out str);
                string name1 = "";
                int    num   = 0;
                while (true)
                {
                    if (num < field.FieldCount)
                    {
                        IField field1 = field.Field[num];
                        if (field1.Type == esriFieldType.esriFieldTypeGeometry)
                        {
                            name1 = field1.Name;
                            IGeometryDef      geometryDef      = field1.GeometryDef;
                            ISpatialReference spatialReference = geometryDef.SpatialReference;
                            SpatialReferenctOperator.ChangeCoordinateSystem(ifeatureWorkspace_0 as IGeodatabaseRelease,
                                                                            spatialReference, false);
                            (geometryDef as IGeometryDefEdit).SpatialReference_2 = spatialReference;
                            (field1 as IFieldEdit).GeometryDef_2 = geometryDef;
                            break;
                        }
                        else
                        {
                            num++;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                name = str;
                IFeatureClass featureClass = ifeatureWorkspace_0.CreateFeatureClass(name, field, null, null,
                                                                                    esriFeatureType.esriFTSimple, name1, "");
                IFeature feature = ifeatureCursor_0.NextFeature();
                if (feature != null)
                {
                    IFeatureCursor featureCursor = featureClass.Insert(true);
                    IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();
                    while (feature != null)
                    {
                        if (feature.Shape != null)
                        {
                            try
                            {
                                Clip.InsertFeature(featureCursor, featureBuffer, field, feature, feature.Shape,
                                                   igeometry_0);
                            }
                            catch (Exception exception)
                            {
                                Logger.Current.Error("", exception, "");
                            }
                        }
                        feature = ifeatureCursor_0.NextFeature();
                    }
                    featureCursor.Flush();
                    IFeatureLayer featureLayerClass = new FeatureLayer();
                    (featureLayerClass as IGeoFeatureLayer).Renderer = (ilayer_0 as IGeoFeatureLayer).Renderer;
                    featureLayerClass.Name         = (featureClass as IDataset).Name;
                    featureLayerClass.FeatureClass = featureClass;
                    imap_0.AddLayer(featureLayerClass);
                    featureBuffer = null;
                    featureCursor = null;
                }
            }
        }
예제 #21
0
        //internal void ExportFeatureClassToShp(ESRI.ArcGIS.Geodatabase.IFeatureClass pFeatureClass, string strFullPath, string filePath)
        //{
        //    //throw new NotImplementedException();
        public void ExportFeatureClassToShp(IFeatureClass apFeatureClass, string ExportShapeFileName, string ExportFilePath)
        {
            if (apFeatureClass == null)
            {
                MessageBox.Show("请选择", "系统提示");
                return;
            }
            if (ExportShapeFileName == "")
                return;

            string ExportFileShortName = System.IO.Path.GetFileNameWithoutExtension(ExportShapeFileName);
            ExportFilePath = System.IO.Path.GetDirectoryName(ExportShapeFileName);
            shpPath = ExportFilePath + "\\" + ExportFileShortName + "\\" + ExportFileShortName + ".shp";
            //设置导出要素类的参数
            IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass();
            IDataset pOutDataset = (IDataset)apFeatureClass;
            pOutFeatureClassName = (IFeatureClassName)pOutDataset.FullName;
            //创建一个输出shp文件的工作空间
            IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
            IWorkspaceName pInWorkspaceName = new WorkspaceNameClass();
            pInWorkspaceName = pShpWorkspaceFactory.Create(ExportFilePath, ExportFileShortName, null, 0);

            //创建一个要素集合
            IFeatureDatasetName pInFeatureDatasetName = null;
            //创建一个要素类
            IFeatureClassName pInFeatureClassName = new FeatureClassNameClass();
            IDatasetName pInDatasetClassName;
            pInDatasetClassName = (IDatasetName)pInFeatureClassName;
            pInDatasetClassName.Name = ExportFileShortName;//作为输出参数
            pInDatasetClassName.WorkspaceName = pInWorkspaceName;
            //通过FIELDCHECKER检查字段的合法性,为输出SHP获得字段集合
            long iCounter;
            IFields pOutFields, pInFields;
            IFieldChecker pFieldChecker;
            IField pGeoField;
            IEnumFieldError pEnumFieldError = null;
            pInFields = apFeatureClass.Fields;
            pFieldChecker = new FieldChecker();
            pFieldChecker.Validate(pInFields, out pEnumFieldError, out pOutFields);
            //通过循环查找几何字段
            pGeoField = null;
            for (iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++)
            {
                if (pOutFields.get_Field((int)iCounter).Type == esriFieldType.esriFieldTypeGeometry)
                {
                    pGeoField = pOutFields.get_Field((int)iCounter);
                    break;
                }
            }

            //得到几何字段的几何定义
            IGeometryDef pOutGeometryDef;
            IGeometryDefEdit pOutGeometryDefEdit;
            pOutGeometryDef = pGeoField.GeometryDef;
            //设置几何字段的空间参考和网格
            pOutGeometryDefEdit = (IGeometryDefEdit)pOutGeometryDef;
            pOutGeometryDefEdit.GridCount_2 = 1;
            pOutGeometryDefEdit.set_GridSize(0, 1500000);
            try
            {
                //开始导入
                IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass();
                //pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, null, pInFeatureDatasetName, pInFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0);
                pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, null, pInFeatureDatasetName, pInFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0);
                MessageBox.Show("导出成功", "系统提示");
            }
            catch (Exception ex)
            {
                MessageBox.Show("the following exception occurred:" + ex.ToString());
            }
        }
예제 #22
0
        public static void ExtractSelectFeatureFormFeatureCursor(string string_0, IFeatureCursor ifeatureCursor_0,
                                                                 IFeatureWorkspace ifeatureWorkspace_0, IGeometry igeometry_0)
        {
            IEnumFieldError enumFieldError;
            IFields         field;
            string          str;
            string          string0;
            int             num;

            if (ifeatureCursor_0 != null)
            {
                IFields       fields            = ifeatureCursor_0.Fields;
                IFieldChecker fieldCheckerClass = new FieldChecker()
                {
                    ValidateWorkspace = ifeatureWorkspace_0 as IWorkspace
                };
                fieldCheckerClass.Validate(fields, out enumFieldError, out field);
                char chr = string_0[0];
                if ((chr < '0' ? false : chr <= '9'))
                {
                    string_0 = string.Concat("A", string_0);
                }
                fieldCheckerClass.ValidateTableName(string_0, out str);
                string name = "";
                int    num1 = 0;
                while (true)
                {
                    if (num1 < field.FieldCount)
                    {
                        IField field1 = field.Field[num1];
                        if (field1.Type == esriFieldType.esriFieldTypeGeometry)
                        {
                            name = field1.Name;
                            IGeometryDef      geometryDef      = field1.GeometryDef;
                            ISpatialReference spatialReference = geometryDef.SpatialReference;
                            SpatialReferenctOperator.ChangeCoordinateSystem(ifeatureWorkspace_0 as IGeodatabaseRelease,
                                                                            spatialReference, false);
                            (geometryDef as IGeometryDefEdit).SpatialReference_2 = spatialReference;
                            (field1 as IFieldEdit).GeometryDef_2 = geometryDef;
                            break;
                        }
                        else
                        {
                            num1++;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                string_0 = str;
                if (((ifeatureWorkspace_0 as IWorkspace).Type == esriWorkspaceType.esriLocalDatabaseWorkspace
                    ? false
                    : (ifeatureWorkspace_0 as IWorkspace).Type != esriWorkspaceType.esriRemoteDatabaseWorkspace))
                {
                    string str1 = string.Concat((ifeatureWorkspace_0 as IWorkspace).PathName, "\\", string_0);
                    string0 = string.Concat(str1, ".shp");
                    num     = 1;
                    while (File.Exists(string0))
                    {
                        string0 = string.Concat(str1, "_", num.ToString(), ".shp");
                        num++;
                    }
                    string_0 = System.IO.Path.GetFileNameWithoutExtension(string0);
                }
                else
                {
                    IWorkspace2 ifeatureWorkspace0 = ifeatureWorkspace_0 as IWorkspace2;
                    string0 = string_0;
                    num     = 1;
                    while (ifeatureWorkspace0.NameExists[esriDatasetType.esriDTFeatureClass, string0])
                    {
                        string0 = string.Concat(string_0, "_", num.ToString());
                        num++;
                    }
                    string_0 = string0;
                }
                try
                {
                    IFeatureClass featureClass = ifeatureWorkspace_0.CreateFeatureClass(string_0, field, null, null,
                                                                                        esriFeatureType.esriFTSimple, name, "");
                    IFeature       feature       = ifeatureCursor_0.NextFeature();
                    IFeatureCursor featureCursor = featureClass.Insert(true);
                    IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();
                    while (feature != null)
                    {
                        if (feature.Shape != null)
                        {
                            Clip.InsertFeature(featureCursor, featureBuffer, field, feature, feature.Shape, igeometry_0);
                        }
                        feature = ifeatureCursor_0.NextFeature();
                    }
                    featureCursor.Flush();
                    Marshal.ReleaseComObject(featureClass);
                    featureClass = null;
                    Marshal.ReleaseComObject(featureBuffer);
                    featureBuffer = null;
                    Marshal.ReleaseComObject(featureCursor);
                    featureCursor = null;
                }
                catch
                {
                }
                Marshal.ReleaseComObject(ifeatureCursor_0);
                ifeatureCursor_0 = null;
            }
        }
예제 #23
0
        public static void OperateConvertToShape(string sFileName, IFeatureClass pInFeatureClass)
        {
            try
            {
                string str = sFileName;

                string str2 = str.Substring(str.LastIndexOf(@"\") + 1);         //文件名

                string str3 = str.Substring(0, (str.Length - str2.Length) - 1); //路径


                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(str3, 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 = str2;

                //作为输出参数

                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, null, null, pOutFeatureClassName, outGeometryDef, pOutFields, "", 1000, 0);

                //  IFeatureDataConverter pFeatureDataConverter =  new FeatureDataConverterClass();
                //pFeatureDataConverter.ConvertFeatureClass(sFeatureClassName, null, null, pFeatureClassName, null, null, "", 0x3e8, 0);

                MessageBox.Show("等值线计算完毕!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
예제 #24
0
        /// <summary>
        /// 要素类转Shape
        /// </summary>
        /// <param name="apFeatureClass"></param>
        private void ExportFeatureClassToConTempShp(IFeatureClass apFeatureClass)
        {
            if (Directory.Exists(System.Windows.Forms.Application.StartupPath + "\\Convert\\DWGConvert\\ConTempShp"))
            {
                Directory.Delete(System.Windows.Forms.Application.StartupPath + "\\Convert\\DWGConvert\\ConTempShp", true);
            }

            //设置导出要素类的参数
            IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass();
            IDataset pOutDataset = (IDataset)apFeatureClass;
            pOutFeatureClassName = (IFeatureClassName)pOutDataset.FullName;
            //创建一个输出shp文件的工作空间
            IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
            IWorkspaceName pInWorkspaceName = new WorkspaceNameClass();
            pInWorkspaceName = pShpWorkspaceFactory.Create(System.Windows.Forms.Application.StartupPath + "\\Convert\\DWGConvert", "ConTempShp", null, 0);

            //创建一个要素集合
            IFeatureDatasetName pInFeatureDatasetName = null;
            //创建一个要素类
            IFeatureClassName pInFeatureClassName = new FeatureClassNameClass();
            IDatasetName pInDatasetClassName;
            pInDatasetClassName = (IDatasetName)pInFeatureClassName;
            pInDatasetClassName.Name = "ConTempShp";//作为输出参数
            pInDatasetClassName.WorkspaceName = pInWorkspaceName;
            //通过FIELDCHECKER检查字段的合法性,为输出SHP获得字段集合
            long iCounter;
            IFields pOutFields, pInFields;
            IFieldChecker pFieldChecker;
            IField pGeoField;
            IEnumFieldError pEnumFieldError = null;
            pInFields = apFeatureClass.Fields;
            pFieldChecker = new FieldChecker();
            pFieldChecker.Validate(pInFields, out pEnumFieldError, out pOutFields);
            //通过循环查找几何字段
            pGeoField = null;
            for (iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++)
            {
                if (pOutFields.get_Field((int)iCounter).Type == esriFieldType.esriFieldTypeGeometry)
                {
                    pGeoField = pOutFields.get_Field((int)iCounter);
                    break;
                }
            }
            //得到几何字段的几何定义
            IGeometryDef pOutGeometryDef;
            IGeometryDefEdit pOutGeometryDefEdit;
            pOutGeometryDef = pGeoField.GeometryDef;

            //设置几何字段的空间参考和网格
            pOutGeometryDefEdit = (IGeometryDefEdit)pOutGeometryDef;
            pOutGeometryDefEdit.GridCount_2 = 1;
            pOutGeometryDefEdit.set_GridSize(0, 1500000);

            //try
            //{
                IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass();
                pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, null, pInFeatureDatasetName, pInFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0);
                // MessageBox.Show("导出成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //}
            //catch (Exception ex)
            //{

            //}
            /*****20130227杨漾(去掉trycatch,上层trycatch(MainForm)处理)*****/
        }
예제 #25
0
        //创建要素
        public void ExportFeature(IFeatureClass apFeatureClass, string ExportFilePath, string ExportFileShortName)
        {
            if (apFeatureClass == null)
            {
                MessageBox.Show("分析出错,请检查路径分析结果", "系统提示");
                return;
            }

            //设置导出要素类的参数
            IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass();
            IDataset          pOutDataset          = (IDataset)apFeatureClass;

            pOutFeatureClassName = (IFeatureClassName)pOutDataset.FullName;
            //创建一个输出shp文件的工作空间
            IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactory();
            IWorkspaceName    pInWorkspaceName     = new WorkspaceNameClass();

            pInWorkspaceName = pShpWorkspaceFactory.Create(ExportFilePath, ExportFileShortName, null, 0);

            //创建一个要素集合
            IFeatureDatasetName pInFeatureDatasetName = null;
            //创建一个要素类
            IFeatureClassName pInFeatureClassName = new FeatureClassNameClass();
            IDatasetName      pInDatasetClassName;

            pInDatasetClassName               = (IDatasetName)pInFeatureClassName;
            pInDatasetClassName.Name          = ExportFileShortName;//作为输出参数
            pInDatasetClassName.WorkspaceName = pInWorkspaceName;
            //自定义字段
            AddField(apFeatureClass, "Elevation", "", esriFieldType.esriFieldTypeInteger);
            //通过FIELDCHECKER检查字段的合法性,为输出SHP获得字段集合
            long            iCounter;
            IFields         pOutFields, pInFields;
            IFieldChecker   pFieldChecker;
            IField          pGeoField;
            IEnumFieldError pEnumFieldError = null;

            pInFields     = apFeatureClass.Fields;
            pFieldChecker = new FieldChecker();
            pFieldChecker.Validate(pInFields, out pEnumFieldError, out pOutFields);
            //通过循环查找几何字段
            pGeoField = null;
            for (iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++)
            {
                if (pOutFields.get_Field((int)iCounter).Type == esriFieldType.esriFieldTypeGeometry)
                {
                    pGeoField = pOutFields.get_Field((int)iCounter);
                    break;
                }
            }
            //得到几何字段的几何定义
            IGeometryDef     pOutGeometryDef;
            IGeometryDefEdit pOutGeometryDefEdit;

            pOutGeometryDef = pGeoField.GeometryDef;
            //设置几何字段的空间参考和网格
            pOutGeometryDefEdit             = (IGeometryDefEdit)pOutGeometryDef;
            pOutGeometryDefEdit.GridCount_2 = 1;
            pOutGeometryDefEdit.set_GridSize(0, 1500000);
            try
            {
                //开始导入
                IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass();
                pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, null, pInFeatureDatasetName, pInFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0);
                MessageBox.Show("导出成功!");
            }
            catch
            {
            }
        }