当前位置:   article > 正文

ArcEngine将GDB中的要素类导出为shp文件_gdb数据集要素导出

gdb数据集要素导出

上一篇博客介绍了如何将shp文件导入GDB中,现在反过来介绍如何将GDB中的要素类导出为shp文件,其实方法大同小异,就不多介绍了,直接上代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;

namespace WindowsFormsApplication1
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        // 要素类导出为shp
        private void btnExport_Click(object sender, EventArgs e)
        {
            // 属性过滤器
            IQueryFilter pQueryFilter = new QueryFilter();
            pQueryFilter.AddField("OBJECTID");
            pQueryFilter.WhereClause = "OBJECTID>=1 and OBJECTID<=3";

            // 导入数据
            IFeatureClass pFeatureClass = GetFeatureClass(@"C:\Users\DSF\Desktop\test.gdb", "区域");
            bool ok = Export(pFeatureClass, pQueryFilter, @"C:\Users\DSF\Desktop\data\区域.shp");

            // 提示用户信息
            if (ok)
            {
                MessageBox.Show("导出成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
                MessageBox.Show("导出失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // 获取要素类
        private IFeatureClass GetFeatureClass(string directory, string className)
        {
            IWorkspaceFactory pWorkspaceFactory = new FileGDBWorkspaceFactory();
            IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(directory, 0);
            IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
            IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(className);
            return pFeatureClass;
        }

        // 导出要素类
        private bool Export(IFeatureClass pSourceFeatureClass, IQueryFilter pQueryFilter, string filePath)
        {
            // 源数据工作空间(GDB)
            IDataset pSourceDataset = pSourceFeatureClass as IDataset;
            IFeatureClassName pSourceFeatureClassName = pSourceDataset.FullName as IFeatureClassName;
            IWorkspace pSourceWorkspace = pSourceDataset.Workspace;

            // 目标数据工作空间(SHP)
            IWorkspaceFactory pTargetWorkspaceFactory = new ShapefileWorkspaceFactory();
            IWorkspace pTargetWorkspace = pTargetWorkspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(filePath), 0);
            IDataset pTargetDataSet = pTargetWorkspace as IDataset;

            // 设置目标数据属性
            IWorkspaceName pTargetWorkspaceName = pTargetDataSet.FullName as IWorkspaceName;
            IFeatureClassName pTargetFeatureClassName = new FeatureClassName() as IFeatureClassName;
            IDatasetName pTargetDatasetName = pTargetFeatureClassName as IDatasetName;
            pTargetDatasetName.WorkspaceName = pTargetWorkspaceName;
            pTargetDatasetName.Name = System.IO.Path.GetFileName(filePath);

            // 检查字段
            IFieldChecker pFieldChecker = new FieldChecker();
            pFieldChecker.InputWorkspace = pSourceWorkspace;
            pFieldChecker.ValidateWorkspace = pTargetWorkspace;

            // 字段转换
            IFields pSourceFields = pSourceFeatureClass.Fields;
            IFields pTargetFields = null;
            IEnumFieldError pEnumFieldError = null;
            pFieldChecker.Validate(pSourceFields, out pEnumFieldError, out pTargetFields);

            // 数据转换
            IFeatureDataConverter pFeatureDataConverter = new FeatureDataConverter();
            try
            {
                pFeatureDataConverter.ConvertFeatureClass(pSourceFeatureClassName, pQueryFilter, null, pTargetFeatureClassName, null, pTargetFields, "", 1000, 0);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/341100
推荐阅读
相关标签
  

闽ICP备14008679号