赞
踩
上一篇博客介绍了如何将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; } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。