MapObjects2中加载瓦片地图的相关内容(C#)

在使用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应用程序,界面如下

 

MapObjects2中加载瓦片地图的相关内容(C#)_第1张图片

 

结合下面的代码,相信你可以很容易的理解界面上各所填项的意义

 

窗体设计器生成的代码

 

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显示瓦片地图,可以根据各个缩放比例来控制加载具体使用哪个比例尺的瓦片地图,这样就很容易达到显示地图的清晰化和效率化了。

 

好,内容基本上也就写完了。至于如果有不懂或者有什么问题欢迎交流

 

 

 

 

你可能感兴趣的:(sql,sql,数据库,String,server,C#,sqlserver)