基于ArcGIS10.0和Oracle10g的空间数据管理平台十三(C#开发)-空间数据导出

我的独立博客网址是:http://wuyouqiang.sinaapp.com/

我的新浪微博:http://weibo.com/freshairbrucewoo

欢迎大家相互交流,共同提高技术。

前面有几篇文章专门介绍了空间数据的导入,导入的目的是为了统一管理。今天介绍空间数据导出,导出的格式支持和导入的格式一样,导出的目的是为了方便数据的迁移。其实导入和导出用到的技术基本上都是相同的,不过为了介绍的完整性还是单独拿出来,因为这一部分的功能也是很重要而且是必不可少的!

1.首先定义一个用于操作SDE数据库的工作空间并且在构造函数中初始化(调用工具类里面提供的静态方法初始化):

1         private IFeatureWorkspace pWorkspaceSDE;//定义SDE工作空间
2 public FrmDataExport()
3 {
4 InitializeComponent();
5 if (pWorkspaceSDE == null)
6 {
7 pWorkspaceSDE = MapOperation.GetFeatrueWorkspace();
8 }
9 }

 

2.列出所有数据表:供用户选择需要导出的数据,每一个表是一个可选项,这样用户可以一次导出多个需要的数据表。

 1         /// <summary>
2 /// 列出所有的表信息
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void FrmDataExport_Load(object sender, EventArgs e)
7 {
8 SqlHelper sh = new SqlHelper();
9 string sql = string.Empty;
10 sql = "select table_name,table_mapname,type from layer l,element e where "
11 + "e.id=l.pid and e.category='矢量数据'";
12 OracleDataReader odr = sh.ReturnDataReader(sql);
13 object[] obj = new object[4];
14 while (odr.Read())
15 {
16 obj[0] = false;
17 obj[1] = odr[0].ToString();
18 obj[2] = odr[2].ToString();
19 obj[3] = odr[1].ToString();
20 dataGridViewX1.Rows.Add(obj);
21 }
22 comboBoxEx1.SelectedIndex = 0;
23 }

 

3.根据选择的导出数据格式打开相应的文件

 1         /// <summary>
2 /// 根据选择的导出数据格式打开相应的文件
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void selectPathBtn_Click(object sender, EventArgs e)
7 {
8 //根据导出数据格式打开相应的文件
9 switch (comboBoxEx1.SelectedIndex)
10 {
11 case 0:
12 {
13 FolderBrowserDialog folder = new FolderBrowserDialog();
14 if (folder.ShowDialog() == DialogResult.OK)
15 {
16 if (folder.SelectedPath != "")
17 {
18 selectPathTxt.Text = folder.SelectedPath;
19 }
20 }
21 }
22 break;
23 case 1:
24 {
25 OpenFileDialog ofd = new OpenFileDialog();
26 ofd.Filter = "MDB文件(.mdb) | *.mdb";
27 ofd.CheckFileExists = false;
28
29 if (ofd.ShowDialog() == DialogResult.OK)
30 {
31
32 if (ofd.FileName != "")
33 {
34 selectPathTxt.Text = ofd.FileName;
35 }
36 }
37 }
38 break;
39 default:
40 break;
41 }
42 }

 

4.执行具体的导出功能:一起准备工作都做好了就开始执行具体的导出功能了,根据不同的格式执行相应导出格式的功能。

 1         /// <summary>
2 /// 执行具体的导出功能
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void exportBtn_Click(object sender, EventArgs e)
7 {
8 if (selectPathTxt.Text == "")
9 {
10 MessageBox.Show("请选择导出路劲");
11 return;
12 }
13 IWorkspaceFactory pWF = null;
14 switch (comboBoxEx1.SelectedIndex)
15 {
16 case 0:
17 {
18 if (!File.Exists(selectPathTxt.Text))
19 {
20
21 }
22 //创建一个输出shp文件的工作空间
23 pWF = new ShapefileWorkspaceFactoryClass();
24 IFeatureWorkspace pFW = pWF.OpenFromFile(selectPathTxt.Text, 0) as IFeatureWorkspace;
25 IWorkspace pW = pFW as IWorkspace;
26
27 for (int i = 0; i < dataGridViewX1.Rows.Count; ++i)
28 {
29 if (bool.Parse(dataGridViewX1.Rows[i].Cells[0].Value.ToString()))
30 {
31 if (dataGridViewX1.Rows[i].Cells[2].Value.ToString() != "PA")
32 {
33 string str = dataGridViewX1.Rows[i].Cells[1].Value.ToString();
34 MapOperation.ConvertFeatureClass(pWorkspaceSDE as IWorkspace,
35 pW, str, str, 4326);
36 }
37 else
38 {
39 MessageBox.Show("属性表不能够导出为Shape文件");
40 }
41 }
42 }
43 MessageBox.Show("导出数据完成!");
44 }
45 break;
46 case 1:
47 {
48 // Instantiate an Access workspace factory and create a new personal geodatabase.
49 pWF = new AccessWorkspaceFactoryClass();
50 IWorkspaceName pWN = pWF.Create(Path.GetDirectoryName(selectPathTxt.Text),
51 Path.GetFileName(selectPathTxt.Text),null, 0);
52
53 // Cast the workspace name object to the IName interface and open the workspace.
54 IName pN = (IName)pWN;
55 IWorkspace pW = (IWorkspace)pN.Open();
56
57 for (int i = 0; i < dataGridViewX1.Rows.Count; ++i)
58 {
59 if (bool.Parse(dataGridViewX1.Rows[i].Cells[0].Value.ToString()))
60 {
61 string str = dataGridViewX1.Rows[i].Cells[1].Value.ToString();
62 if (dataGridViewX1.Rows[i].Cells[2].Value.ToString() != "PA")
63 {
64 MapOperation.ConvertFeatureClass(pWorkspaceSDE as IWorkspace,
65 pW, str, str, 4326);
66 }
67 else
68 {
69 ITable pSourceT = pWorkspaceSDE.OpenTable(str);
70 IFeatureWorkspace pFW = pW as IFeatureWorkspace;
71 ITable pTargetT = pFW.CreateTable(str, pSourceT.Fields, null, null, "");
72 FusedIndexTable(ref pSourceT, ref pTargetT);
73 }
74 }
75 }
76 MessageBox.Show("导出数据完成!");
77 }
78 break;
79 default:
80 break;
81 }
82 }

 

5.如果导出的数据表或文件已经存在就以追加的方式导出数据

 1         /// <summary>
2 /// 如果目的数据库中已经有表,则将新的记录追加进去
3 /// </summary>
4 /// <param name="FromTable">导出表</param>
5 /// <param name="ToTable">导入表</param>
6 private void FusedIndexTable(ref ITable FromTable, ref ITable ToTable)
7 {
8 if (FromTable == null || ToTable == null)
9 {
10 return;
11 }
12 IRow pFromRow;
13 ICursor pToCursor, pFromCursor;
14 IRowBuffer pToRowBuffer;
15 int pIndex;
16
17 pToRowBuffer = ToTable.CreateRowBuffer();
18 pToCursor = ToTable.Insert(true);
19 pFromCursor = FromTable.Search(null, false);
20 pFromRow = pFromCursor.NextRow();
21 while (pFromRow != null)
22 {
23 for (int i = 0; i < pFromRow.Fields.FieldCount; i++)
24 {
25 pIndex = pToRowBuffer.Fields.FindField(pFromRow.Fields.get_Field(i).Name.Trim());
26 if (pFromRow.Fields.get_Field(i).Editable && pIndex > -1)
27 {
28 pToRowBuffer.set_Value(pIndex, pFromRow.get_Value(i));
29 }
30 }
31
32 pToCursor.InsertRow(pToRowBuffer);
33 pFromRow = pFromCursor.NextRow();
34 }
35 System.Runtime.InteropServices.Marshal.ReleaseComObject(pToCursor);
36 pFromRow = null;
37 pFromCursor = null;
38 pToRowBuffer = null;
39 }
40 }

 

6.总结:这里用到的大部分技术在前面都介绍过了,这里不过是不同的业务逻辑而已,其实很多的时候高深的技术并不会用到很多,主要是处理好各个功能的业务逻辑,至于用什么样的技术实现都是可以的!

posted @ 2011-12-11 16:37  蔷薇理想人生  阅读(701)  评论(0编辑  收藏  举报