MapObjects2中載入瓦片地圖的相關內容(C#)

來源:互聯網
上載者:User

在使用MapObjects2顯示瓦片地圖時,可以用一下代碼

 

 

通過MapObjects可以顯示瓦片地圖(ImageCatalog),瓦片地圖資訊儲存在一個資料庫表中,其格式為
Image String
xmin  地圖左下角x座標
ymin  地圖左下角y座標
xmax  地圖左上方x座標
ymax  地圖左上方y座標

我採用的是在Access資料庫儲存地圖位置資訊(我試過 Sql Server,但是以失敗告終,不知道是不支援Sql Server,還是我的t.Database寫錯了,如果有人知道,請不吝賜教)
資料庫名稱為map.mdb,瓦片表名稱為map_pic1。

 

Table t = new TableClass(); t.Database = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/房地產/MapToDB/MapToDB/map.mdb;Persist Security Info=False"; t.Name = "map_pic1"; ImageLayer imgLyr = new ImageLayerClass(); imgLyr.OpenCatalog(t, @"C:/arcgisserver/arcgiscache/nj/Layers/_alllayers/L00/", ImageCatalogConstants.moOpenImagesInDisplay);

 瓦片地圖儲存在"c:/map"檔案夾下。
這樣就可以在Map地圖中增加並顯示圖片資料了,並且當圖片資料非常大是,顯示效果也很好。

 

得到以上效果有兩點需要注意

 

(一) 首先資料表的格式必須是包含五個欄位:image,xmin,ymin,xmax,ymax。切欄位的名稱是規定了的

(二)map.mdb中的資料如何準備呢,下面著重講一下這一點

 

首先要明白ArcGis Server切圖的原理,可以參考我之前的一片部落格  ArcGis Server建立緩衝(切圖)原理分析  ,然後我們現在做的事是需要把每張瓦片地圖的路經和經緯度寫到資料庫map.mdb中,我寫了個工具,是一個基於C#的Window應用程式,介面如下

 

 

結合下面的代碼,相信你可以很容易的理解介面上各所填項的意義

 

表單設計器產生的程式碼

 

namespace MapToDB { partial class Form1 { /// <summary> /// 必需的設計器變數。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的資源。 /// </summary> /// <param name="disposing">如果應釋放託管資源,為 true;否則為 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 表單設計器產生的程式碼 /// <summary> /// 設計器支援所需的方法 - 不要 /// 使用代碼編輯器修改此方法的內容。 /// </summary> private void InitializeComponent() { this.lblConn = new System.Windows.Forms.Label(); this.txtConnStr = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.txtTable = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.txtMapPath = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.txtResolution = new System.Windows.Forms.TextBox(); this.label4 = new System.Windows.Forms.Label(); this.txtOriginX = new System.Windows.Forms.TextBox(); this.label5 = new System.Windows.Forms.Label(); this.txtOriginY = new System.Windows.Forms.TextBox(); this.txtMapHeight = new System.Windows.Forms.TextBox(); this.txtMapWidth = new System.Windows.Forms.TextBox(); this.btnImport = new System.Windows.Forms.Button(); this.btnExit = new System.Windows.Forms.Button(); this.access = new System.Windows.Forms.RadioButton(); this.sqlServer = new System.Windows.Forms.RadioButton(); this.SuspendLayout(); // // lblConn // this.lblConn.AutoSize = true; this.lblConn.Location = new System.Drawing.Point(12, 23); this.lblConn.Name = "lblConn"; this.lblConn.Size = new System.Drawing.Size(113, 12); this.lblConn.TabIndex = 0; this.lblConn.Text = "資料庫連接字串:"; // // txtConnStr // this.txtConnStr.Location = new System.Drawing.Point(128, 20); this.txtConnStr.Name = "txtConnStr"; this.txtConnStr.Size = new System.Drawing.Size(549, 21); this.txtConnStr.TabIndex = 1; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(12, 63); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(41, 12); this.label1.TabIndex = 2; this.label1.Text = "表名:"; // // txtTable // this.txtTable.Location = new System.Drawing.Point(128, 54); this.txtTable.Name = "txtTable"; this.txtTable.Size = new System.Drawing.Size(118, 21); this.txtTable.TabIndex = 3; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(12, 102); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(65, 12); this.label2.TabIndex = 4; this.label2.Text = "圖片路經:"; // // txtMapPath // this.txtMapPath.Location = new System.Drawing.Point(128, 93); this.txtMapPath.Name = "txtMapPath"; this.txtMapPath.Size = new System.Drawing.Size(547, 21); this.txtMapPath.TabIndex = 5; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(12, 141); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(77, 12); this.label3.TabIndex = 6; this.label3.Text = "Resolution:"; // // txtResolution // this.txtResolution.Location = new System.Drawing.Point(128, 138); this.txtResolution.Name = "txtResolution"; this.txtResolution.Size = new System.Drawing.Size(547, 21); this.txtResolution.TabIndex = 7; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(12, 183); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(77, 12); this.label4.TabIndex = 8; this.label4.Text = "原點經緯度:"; // // txtOriginX // this.txtOriginX.Location = new System.Drawing.Point(128, 180); this.txtOriginX.Name = "txtOriginX"; this.txtOriginX.Size = new System.Drawing.Size(118, 21); this.txtOriginX.TabIndex = 9; // // label5 // this.label5.AutoSize = true; this.label5.Location = new System.Drawing.Point(12, 230); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(65, 12); this.label5.TabIndex = 10; this.label5.Text = "圖片像素:"; // // txtOriginY // this.txtOriginY.Location = new System.Drawing.Point(268, 180); this.txtOriginY.Name = "txtOriginY"; this.txtOriginY.Size = new System.Drawing.Size(118, 21); this.txtOriginY.TabIndex = 11; // // txtMapHeight // this.txtMapHeight.Location = new System.Drawing.Point(268, 221); this.txtMapHeight.Name = "txtMapHeight"; this.txtMapHeight.Size = new System.Drawing.Size(118, 21); this.txtMapHeight.TabIndex = 13; // // txtMapWidth // this.txtMapWidth.Location = new System.Drawing.Point(128, 221); this.txtMapWidth.Name = "txtMapWidth"; this.txtMapWidth.Size = new System.Drawing.Size(118, 21); this.txtMapWidth.TabIndex = 12; // // btnImport // this.btnImport.Location = new System.Drawing.Point(494, 283); this.btnImport.Name = "btnImport"; this.btnImport.Size = new System.Drawing.Size(75, 23); this.btnImport.TabIndex = 14; this.btnImport.Text = "導 入"; this.btnImport.UseVisualStyleBackColor = true; this.btnImport.Click += new System.EventHandler(this.btnImport_Click); // // btnExit // this.btnExit.Location = new System.Drawing.Point(589, 283); this.btnExit.Name = "btnExit"; this.btnExit.Size = new System.Drawing.Size(75, 23); this.btnExit.TabIndex = 15; this.btnExit.Text = "退 出"; this.btnExit.UseVisualStyleBackColor = true; this.btnExit.Click += new System.EventHandler(this.btnExit_Click); // // access // this.access.AutoSize = true; this.access.Location = new System.Drawing.Point(318, 59); this.access.Name = "access"; this.access.Size = new System.Drawing.Size(59, 16); this.access.TabIndex = 16; this.access.TabStop = true; this.access.Text = "Access"; this.access.UseVisualStyleBackColor = true; this.access.CheckedChanged += new System.EventHandler(this.access_CheckedChanged); // // sqlServer // this.sqlServer.AutoSize = true; this.sqlServer.Location = new System.Drawing.Point(423, 59); this.sqlServer.Name = "sqlServer"; this.sqlServer.Size = new System.Drawing.Size(83, 16); this.sqlServer.TabIndex = 17; this.sqlServer.TabStop = true; this.sqlServer.Text = "Sql Server"; this.sqlServer.UseVisualStyleBackColor = true; this.sqlServer.CheckedChanged += new System.EventHandler(this.sqlServer_CheckedChanged); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(687, 318); this.Controls.Add(this.sqlServer); this.Controls.Add(this.access); this.Controls.Add(this.btnExit); this.Controls.Add(this.btnImport); this.Controls.Add(this.txtMapHeight); this.Controls.Add(this.txtMapWidth); this.Controls.Add(this.txtOriginY); this.Controls.Add(this.label5); this.Controls.Add(this.txtOriginX); this.Controls.Add(this.label4); this.Controls.Add(this.txtResolution); this.Controls.Add(this.label3); this.Controls.Add(this.txtMapPath); this.Controls.Add(this.label2); this.Controls.Add(this.txtTable); this.Controls.Add(this.label1); this.Controls.Add(this.txtConnStr); this.Controls.Add(this.lblConn); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "地圖匯入工具"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label lblConn; private System.Windows.Forms.TextBox txtConnStr; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox txtTable; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox txtMapPath; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox txtResolution; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox txtOriginX; private System.Windows.Forms.Label label5; private System.Windows.Forms.TextBox txtOriginY; private System.Windows.Forms.TextBox txtMapHeight; private System.Windows.Forms.TextBox txtMapWidth; private System.Windows.Forms.Button btnImport; private System.Windows.Forms.Button btnExit; private System.Windows.Forms.RadioButton access; private System.Windows.Forms.RadioButton sqlServer; } }  

 

業務代碼如下

 

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 System.Data.SqlClient; using System.IO; using System.Data.OleDb; namespace MapToDB { public partial class Form1 : Form { //private string connStr = "data source=192.168.1.119;initial catalog=estartmanage;user id=sa;password=123;"; private string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/房地產/MapToDB/MapToDB/map.mdb;Persist Security Info=False"; private string table = "map_pic1"; //一個比例尺對應一個資料表 private string path = @"C:/arcgisserver/arcgiscache/nj/Layers/_alllayers/L00"; //一個比例尺圖片的主目錄 private double res = 2.37946096144484E-05; //一個像素顯示的經度 private double originX = -400; //切圖開始緯度 private double originY = 400; //切圖開始經度 private int width = 256; //每張瓦片地圖的寬度 private int height = 256; //每張瓦片地圖的高度 private int dbType = 0; //0-access,1-sql server private SqlConnection sqlConn = null; private OleDbConnection oleConn = null; public Form1() { InitializeComponent(); InitUI(); } private void InitUI() { this.txtConnStr.Text=connStr; this.txtTable.Text=table; this.txtMapPath.Text=path; this.txtResolution.Text = "2.37946096144484E-05"; this.txtOriginX.Text=originX.ToString(); this.txtOriginY.Text=originY.ToString(); this.txtMapWidth.Text=width.ToString(); this.txtMapHeight.Text=height.ToString(); this.access.Checked = true; } private void btnImport_Click(object sender, EventArgs e) { string msg = CheckData(); if (string.IsNullOrEmpty(msg)) { Import(); } else { MessageBox.Show(msg); } } private string CheckData() { string msg = ""; string value = ""; value = txtConnStr.Text; if (string.IsNullOrEmpty(value.Trim())) { msg += "資料庫連接字串不可為空/n"; } else { connStr = value.Trim(); } value = txtTable.Text; if (string.IsNullOrEmpty(value.Trim())) { msg += "資料表名不可為空/n"; } else { table = value.Trim(); } value = txtMapPath.Text; if (string.IsNullOrEmpty(value.Trim())) { msg += "地圖圖片路經不可為空/n"; } else { path = value.Trim(); } value = txtResolution.Text; if (string.IsNullOrEmpty(value.Trim())) { msg += "地圖的Resolution不可為空/n"; } else { if (!Double.TryParse(value, out res)) { msg += "地圖的Resolution必須為浮點數類型/n"; } } value = txtOriginX.Text; if (string.IsNullOrEmpty(value.Trim())) { msg += "原點經緯度的緯度不可為空/n"; } else { if (!Double.TryParse(value, out originX)) { msg += "原點經緯度的緯度必須是數字/n"; } } value = txtOriginY.Text; if (string.IsNullOrEmpty(value.Trim())) { msg += "原點經緯度的經度不可為空/n"; } else { if (!Double.TryParse(value, out originY)) { msg += "原點經緯度的經度必須是數字/n"; } } value = txtMapWidth.Text; if (string.IsNullOrEmpty(value.Trim())) { msg += "圖片的寬度不可為空/n"; } else { if (!Int32.TryParse(value, out width)) { msg += "圖片的寬度必須是整數/n"; } } value = txtMapHeight.Text; if (string.IsNullOrEmpty(value.Trim())) { msg += "圖片的高度不可為空/n"; } else { if (!Int32.TryParse(value, out height)) { msg += "圖片的高度必須是整數/n"; } } return msg; } /// <summary> /// 匯入資料 /// </summary> /// <returns></returns> private bool Import() { string sql = ""; double x = 0f; double y = 0f; int success = 0; try { OpenConn(); //匯入資料前,先刪除原來的所有資料 DeleteTable(); DirectoryInfo root = Directory.CreateDirectory(path); //遍曆主目錄下的所有目錄,主目錄下的所有目錄下的圖片為同一比例尺下的圖片切圖 foreach (string str in Directory.GetDirectories(path)) { DirectoryInfo dir = Directory.CreateDirectory(str); string dirName = dir.Name.Substring(1); //擷取行號,目錄的命名規則為:"R"+ROWNUM,當然ROWNUM是十六進位的,需要轉換為十進位的 int row = Int32.Parse(dirName, System.Globalization.NumberStyles.AllowHexSpecifier); FileInfo[] files = dir.GetFiles(); //遍曆所有檔案,也就是圖片 foreach (FileInfo file in files) { string fileName = file.Name.Substring(1, 8); int column = 0; try { //擷取列號,圖片檔案的命名規則為:"C"+COLNUM,當然COLNUM是十六進位的,需要轉換為十進位的 column = Int32.Parse(fileName, System.Globalization.NumberStyles.AllowHexSpecifier); } catch (Exception ex) { continue; } double xMin = 0f; double yMin = 0f; double xMax = 0f; double yMax = 0f; //*************此處為經緯度演算法************// xMin = column * width * res+originX; xMax = xMin + width * res; //yMin = originY - row * height * res; //yMax = yMin + height * res; yMax = originY - row * height * res; yMin = yMax - height * res; //****************************************// sql = "Insert into " + table; sql += " values('" + file.DirectoryName + "//" + file.ToString() + "'"; sql += "," + xMin; sql += "," + yMin; sql += "," + xMax; sql += "," + yMax; sql += ")"; int result = 0; if (dbType == 1) { SqlCommand sqlComm = new SqlCommand(sql, sqlConn); result = sqlComm.ExecuteNonQuery(); } else if (dbType == 0) { OleDbCommand oleComm = new OleDbCommand(sql, oleConn); result = oleComm.ExecuteNonQuery(); } if (result > 0) { success++; } } //目錄名為行號 } } catch (Exception ex) { MessageBox.Show(ex.Message); return false; } MessageBox.Show("成功匯入資料" + success + "條"); return true; } /// <summary> /// 根據資料庫,選擇開啟的連線類型 /// </summary> private void OpenConn() { if (dbType == 1) { sqlConn = new SqlConnection(connStr); sqlConn.Open(); } else if (dbType == 0) { oleConn = new OleDbConnection(connStr); oleConn.Open(); } } private void CloseConn() { if (dbType == 1) { sqlConn.Close(); sqlConn = null; } else if (dbType == 0) { oleConn.Close(); oleConn = null; } } private void DeleteTable() { string sql = "delete from " + table; if (dbType == 1) { SqlCommand sqlComm = new SqlCommand(sql, sqlConn); sqlComm.ExecuteNonQuery(); } else { OleDbCommand oleComm = new OleDbCommand(sql, oleConn); oleComm.ExecuteNonQuery(); } } private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } private void access_CheckedChanged(object sender, EventArgs e) { this.sqlServer.Checked = false; this.txtConnStr.Text = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/房地產/MapToDB/MapToDB/map.mdb;Persist Security Info=False"; dbType = 0; } private void sqlServer_CheckedChanged(object sender, EventArgs e) { this.access.Checked = false; this.txtConnStr.Text = @"data source=192.168.1.119;initial catalog=estartmanage;user id=sa;password=123;"; dbType = 1; } } }

 

 

當執行完上述程式後,我們的圖片資訊就已經寫入了map.mdb資料庫中了。當然地圖圖片應該是分為多個比例尺的,我們這裡採用一個比例尺一張資料表,當匯入第二個比例尺資料時,更改介面上的填的內容,如表明改為“map_pic2”,並修改圖片路經和Resolution,會很容就把資料導進去的。

 

資料準備完了,在MapObjects2中適用ImageLayer顯示瓦片地圖,可以根據各個縮放比例來控制載入具體使用哪個比例尺的瓦片地圖,這樣就很容易達到顯示地圖的清晰化和效率化了。

 

好,內容基本上也就寫完了。至於如果有不懂或者有什麼問題歡迎交流

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.