仿照ArcGIS的数据导入功能做了个简易的数据导入界面:
需要注意的问题:上篇博文中的要素类导入函数要变成静态函数,不然会报错。原因我想可能是因为非静态函数导入时,workspace与workspacefactory等类型变量未释放,希望了解的童鞋留言讨论哈。
下面给出全部源码:
public partial class ImportDataToSDE:DevComponents.DotNetBar.Office2007Form { #region 类成员变量 public IGlobeControl globeControl = null; public IScene scene = null; public IWorkspace sdeWorkspace = null; public IFeatureWorkspace featureWorkspace = null; //矢量工作空间 public IRasterWorkspace rasterWorkspace = null;//栅格工作空间 public IFeatureDataset featureDataset = null; //矢量数据集 public IRasterDataset rasterDataset = null; //栅格数据集 public List<IFeatureLayer> featurelayerList; public List<IRasterLayer> rasterlayerList; public string layerType = "";//图层类型 public string outPutLayerName = "";//导出图层名称 public string selectedDataSet_list = "";//listbox选中的图层 #endregion //构造 public ImportDataToSDE (IWorkspace ws,IGlobeControl globeControl) { this.sdeWorkspace = ws; this.globeControl = globeControl; this.scene = globeControl.GlobeDisplay.Scene; this.featurelayerList = new List<IFeatureLayer>(); this.rasterlayerList = new List<IRasterLayer>(); InitializeComponent(); } //load private void ImportDataToSDE_Load (object sender,EventArgs e) { IAoInitialize initAO = new AoInitializeClass(); initAO.Initialize((esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB)); } //填充combox private void comboBox_DataType_SelectedIndexChanged (object sender,EventArgs e) { try { layerType = comboBox_DataType.Text.ToString(); featurelayerList.Clear(); rasterlayerList.Clear(); loadAllLayers(); if(layerType == "矢量数据") { for(int i = 0;i < featurelayerList.Count;i++) { comboBox_DataSet.Items.Add(featurelayerList[i].Name); } } else if(layerType == "栅格数据") { for(int i = 0;i < rasterlayerList.Count;i++) { comboBox_DataSet.Items.Add(rasterlayerList[i].Name); } } } catch(System.Exception ex) { MessageBox.Show(ex.Message); } } //加载所有图层 public void loadAllLayers () { try { for(int i = 0;i < scene.LayerCount;i++) { ILayer layer = scene.get_Layer(i); if(layer is IFeatureLayer) { featurelayerList.Add(layer as IFeatureLayer); } else { rasterlayerList.Add(layer as IRasterLayer); } } } catch(System.Exception ex) { MessageBox.Show(ex.Message); } } //选择导出图层 private void comboBox_DataSet_SelectedIndexChanged (object sender,EventArgs e) { outPutLayerName = comboBox_DataSet.Text.ToString(); } // 根据名称获取图层 private ILayer GetLayerByName (string strLayerName) { ILayer pLayer = null; for(int i = 0;i < scene.LayerCount;i++) { pLayer = scene.get_Layer(i); if(strLayerName == pLayer.Name) { break; } } return pLayer; } //添加导出数据集 private void button_AddDS_Click (object sender,EventArgs e) { try { if(comboBox_DataSet.Text != "") { listBox_SelectDataSet.Items.Add(comboBox_DataSet.Text); } else { MessageBox.Show("未选中导出数据集"); } } catch(System.Exception ex) { MessageBox.Show(ex.Message + " 添加失败!"); } } private void listBox_SelectDataSet_SelectedIndexChanged (object sender,EventArgs e) { if(listBox_SelectDataSet.SelectedIndex != -1) { selectedDataSet_list = listBox_SelectDataSet.Items[listBox_SelectDataSet.SelectedIndex].ToString(); } } //移除数据集 private void button_RemoveDS_Click (object sender,EventArgs e) { try { listBox_SelectDataSet.Items.Remove(listBox_SelectDataSet.SelectedIndex); listBox_SelectDataSet.Refresh(); } catch(System.Exception ex) { MessageBox.Show("移除数据集失败!"); } } //导入 private void button_import_Click (object sender,EventArgs e) { try { if(layerType == "矢量数据") { for(int i = 0;i < listBox_SelectDataSet.Items.Count;i++) { IFeatureLayer featurelayer = new FeatureLayerClass(); featurelayer = GetLayerByName(listBox_SelectDataSet.Items[i].ToString()) as IFeatureLayer; IFeatureClass outputFeatureClass = featurelayer.FeatureClass; DataExportHelper.FeatureClassToFeatureClass(outputFeatureClass,sdeWorkspace); } this.Close(); MessageBox.Show("导入完成!"); } else if(layerType == "栅格数据") { } } catch(System.Exception ex) { MessageBox.Show(ex.Message); } } private void button_cancle_Click (object sender,EventArgs e) { this.Close(); this.Dispose(); } }现在只是实现了矢量数据的批量导入,栅格数据的导入类似,就不再赘述了。希望和大家多多交流,谢谢关注!