我的獨(dú)立博客網(wǎng)址是: http://wuyouqiang.sinaapp.com/ 。
我的新浪微博: http://weibo.com/freshairbrucewoo 。
歡迎大家相互交流,共同提高技術(shù)。
前面有幾篇文章專門介紹了空間數(shù)據(jù)的導(dǎo)入,導(dǎo)入的目的是為了統(tǒng)一管理。今天介紹空間數(shù)據(jù)導(dǎo)出,導(dǎo)出的格式支持和導(dǎo)入的格式一樣,導(dǎo)出的目的是為了方便數(shù)據(jù)的遷移。其實(shí)導(dǎo)入和導(dǎo)出用到的技術(shù)基本上都是相同的,不過為了介紹的完整性還是單獨(dú)拿出來,因?yàn)檫@一部分的功能也是很重要而且是必不可少的!
1.首先定義一個(gè)用于操作SDE數(shù)據(jù)庫的工作空間并且在構(gòu)造函數(shù)中初始化(調(diào)用工具類里面提供的靜態(tài)方法初始化):
1 private IFeatureWorkspace pWorkspaceSDE; // 定義SDE工作空間
2 public FrmDataExport()
3 {
4 InitializeComponent();
5 if (pWorkspaceSDE == null )
6 {
7 pWorkspaceSDE = MapOperation.GetFeatrueWorkspace();
8 }
9 }
?
2.列出所有數(shù)據(jù)表:供用戶選擇需要導(dǎo)出的數(shù)據(jù),每一個(gè)表是一個(gè)可選項(xiàng),這樣用戶可以一次導(dǎo)出多個(gè)需要的數(shù)據(jù)表。
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='矢量數(shù)據(jù)' " ;
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.根據(jù)選擇的導(dǎo)出數(shù)據(jù)格式打開相應(yīng)的文件
1 /// <summary>
2 /// 根據(jù)選擇的導(dǎo)出數(shù)據(jù)格式打開相應(yīng)的文件
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void selectPathBtn_Click( object sender, EventArgs e)
7 {
8 // 根據(jù)導(dǎo)出數(shù)據(jù)格式打開相應(yīng)的文件
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.執(zhí)行具體的導(dǎo)出功能:一起準(zhǔn)備工作都做好了就開始執(zhí)行具體的導(dǎo)出功能了,根據(jù)不同的格式執(zhí)行相應(yīng)導(dǎo)出格式的功能。
1 /// <summary>
2 /// 執(zhí)行具體的導(dǎo)出功能
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( " 請(qǐng)選擇導(dǎo)出路勁 " );
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 // 創(chuàng)建一個(gè)輸出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( " 屬性表不能夠?qū)С鰹镾hape文件 " );
40 }
41 }
42 }
43 MessageBox.Show( " 導(dǎo)出數(shù)據(jù)完成! " );
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( " 導(dǎo)出數(shù)據(jù)完成! " );
77 }
78 break ;
79 default :
80 break ;
81 }
82 }
?
5.如果導(dǎo)出的數(shù)據(jù)表或文件已經(jīng)存在就以追加的方式導(dǎo)出數(shù)據(jù)
1 /// <summary>
2 /// 如果目的數(shù)據(jù)庫中已經(jīng)有表,則將新的記錄追加進(jìn)去
3 /// </summary>
4 /// <param name="FromTable"> 導(dǎo)出表 </param>
5 /// <param name="ToTable"> 導(dǎo)入表 </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.總結(jié):這里用到的大部分技術(shù)在前面都介紹過了,這里不過是不同的業(yè)務(wù)邏輯而已,其實(shí)很多的時(shí)候高深的技術(shù)并不會(huì)用到很多,主要是處理好各個(gè)功能的業(yè)務(wù)邏輯,至于用什么樣的技術(shù)實(shí)現(xiàn)都是可以的!
基于ArcGIS10.0和Oracle10g的空間數(shù)據(jù)管理平臺(tái)十三(C#開發(fā))-空間數(shù)據(jù)導(dǎo)出
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
