Loading tile map in MapObjects2 (C #)

Source: Internet
Author: User
Tags mdb database naming convention access database

When using MAPOBJECTS2 to display tile maps, you can use the code

Tile maps (Imagecatalog) can be displayed through mapobjects, and tile map information is stored in a database table in the format
Image String
Xmin map lower left corner x coordinates
Ymin map left Lower corner y-coordinate
Xmax map upper left corner x coordinate
Ymax map y-coordinate in upper left corner

I used to save map location information in an Access database (I tried SQL Server, but failed, not knowing whether to support SQL Server or my T. Database is wrong, if someone knows, please do not hesitate to enlighten me.
The database name is Map.mdb and the tile table name is Map_pic1.

Table t = new Tableclass (); T.database = @ "Provider=Microsoft.Jet.OLEDB.4.0;Data source=d:/real Estate/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);

Tile maps are kept under the "C:/map" folder.
This allows you to add and display picture data in map maps, and when the image data is very large, the display works well.

The above effect has two points to note

(a) The format of the datasheet must first contain five fields: Image,xmin,ymin,xmax,ymax. The name of the tangent field is specified.

(b) How to prepare the data in the Map.mdb, which is highlighted below

First of all to understand the ArcGis server Chettu principle, you can refer to my previous blog ArcGIS server to establish a cache (cut graph) principle analysis, And then what we're doing now is we need to write the path and latitude of each tile map to the database Map.mdb, I wrote a tool that is a C # based window application, the interface is as follows

Combined with the following code, I believe you can easily understand the meaning of each item on the interface

Code generated by the form designer

Namespace Maptodb {Partial class Form1 {///<summary>///required designer variables. </summary> private System.ComponentModel.IContainer components = null; <summary>///Clean up all the resources that are in use. </summary>///<param name= "disposing" > if the managed resource should be freed, true; otherwise, false. </param> protected override void Dispose (bool disposing) {if (disposing && (components!= null)) {Componen Ts. Dispose (); Base. Dispose (disposing); #region the code generated by the Windows Forms Designer///<summary>///Designer supports the method you want-do not///use the Code Editor to modify the contents of this method. </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 = "Database connection string:"; 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 = "table name:"; 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 = "Picture road warp:"; 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 = "The origin of latitude and longitude:"; 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 = "picture pixel:"; 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 = "Import"; 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 = "Exit"; 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); SQL Server//this.sqlServer.AutoSize = true; This.sqlServer.Location = new System.Drawing.Point (423, 59); This.sqlServer.Name = "SQL Server"; 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 = "Map Import Tool"; 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 SQL Server; }}  

The business code is as follows

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 cata Log=estartmanage;user id=sa;password=123; "; private string connstr = @ "Provider=Microsoft.Jet.OLEDB.4.0;Data source=d:/real Estate/maptodb/maptodb/map.mdb; Persist security Info=false "; private string table = "Map_pic1"; A scale corresponds to a datasheet private string path = @ "C:/arcgisserver/arcgiscache/nj/layers/_alllayers/l00"; The home directory of a scale picture private double res = 2.37946096144484E-05; A pixel-displayed longitude private double Originx =-400; Chettu starting latitude private double originy = 400; Chettu start longitude private int width = 256; Width of each tile map private int height = 256; The height of each tile map is private int dbType = 0; 0-access,1-sql server private SqlConnection sqlconn = null; PrivateOleDbConnection 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 + = "Database connection string cannot be null/n";} else {connstr = value. Trim (); } value = Txttable.text; if (string. IsNullOrEmpty (value. Trim ()) {msg + = "Datasheet name cannot be null/n";} else {table = value.) Trim (); } value = Txtmappath.text; if (string. IsNullOrEmpty (value. Trim ()) {msg = "Map picture path cannot be empty/n";} else {path = value. Trim (); } value = Txtresolution.text; if (string. IsNullOrEmpty (value. Trim ()) {msg = "Map resolution cannot be empty/n";} else {if (! Double.tryparse (value, out res)) {msg = "Map resolution must be floating-point type/n";}} value = Txtoriginx.text; if (string. IsNullOrEmpty (value. Trim ()) {msg + = "The latitude of the origin of the longitude must not be null/n";} else {if (! Double.tryparse (value, out Originx)) {msg + = "The latitude of the origin longitude must be numeric/n";}} value = Txtoriginy.text; if (string. IsNullOrEmpty (value. Trim ()) {msg = "Longitude of origin longitude cannot be null/n";} else {if (! Double.tryparse (value, out originy)) {msg = "The longitude of the origin longitude must be numeric/n";}} value = Txtmapwidth.text; if (string. IsNullOrEmpty (value. Trim ())) {msg = "Picture's width cannot be empty/n";} else {if (! Int32.TryParse (value, out width)) {msg = "The width of the picture must be integer/n";}} value = Txtmapheight.text; if (string. IsNullOrEmpty (value. Trim ())) {msg = "Picture's height cannot be null/n";} else {if (! Int32.TryParse (value, out height)) {msg = "picture must be of a height of integer/n";}} return msg; ///<summary>///Import data///</summary>///<returns></returns&Gt private bool Import () {String sql = ""; Double x = 0f; Double y = 0f; int success = 0; try {openconn ();///Before data is imported, delete all previous data deletetable (); DirectoryInfo root = directory.createdirectory (path); Traverse all directories in the home directory, the picture under all directories under the home directory is tangent to the picture under the same scale foreach (String str in directory.getdirectories (path)) {DirectoryInfo dir = Directory.CreateDirectory (str); String dirname = dir. Name.substring (1); Gets the row number, the directory naming convention is: "R" +rownum, of course rownum is hexadecimal, need to convert to decimal int row = Int32.Parse (dirname, System.Globalization.NumberStyles.AllowHexSpecifier); fileinfo[] files = dir. GetFiles (); Traverse all files, i.e. picture foreach (FileInfo file in files) {string fileName = file. Name.substring (1, 8); int column = 0; try {//Get column number, picture file naming rule is: "C" +colnum, of course colnum is hexadecimal, need to convert to decimal column = Int32.Parse (FileName, System.Globalization.NumberStyles.AllowHexSpecifier); The catch (Exception ex) {continue;} double xmin = 0f; Double ymin = 0f; Double xmax = 0f; Double ymax = 0f; Here is the latitude algorithm ************//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 (Dbtyp E = = 0) {OleDbCommand Olecomm = new OleDbCommand (sql, oleconn); result = Olecomm.executenonquery ();} if (Result > 0) {success++;} }//directory name is line number} catch (Exception ex) {MessageBox.Show Ex. message); return false; } MessageBox.Show ("Import data successfully" + Success + "Bar"); return true; ///<summary>///Depending on the database, select Open Connection type///</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 ole Comm = new OleDbCommand (sql, oleconn); Olecomm.executenonquery (); } private void Btnexit_click (object sender, EventArgs e) {application.exit ();} private void Access_checkedchanged (Obje CT sender, EventArgs e) {this.sqlServer.Checked = false; This.txtConnStr.Text = @ "Provider=Microsoft.Jet.OLEDB.4.0;Data source=d:/Real Estate/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; } } }

When the above program is executed, our picture information has been written to the Map.mdb database. Of course, the map image should be divided into multiple scales, here we use a scale of a data table, when the second scale data import, change the content of the interface, such as "Map_pic2", and modify the picture of the road and resolution, will be very content to guide the data into.

The data is ready, in the MAPOBJECTS2 imagelayer display tile map, can be based on the scale to control the specific use of the scale of the tile map, so it is easy to achieve the clarity and efficiency of the display map.

Well, the content is basically finished. As for if you don't understand or have any questions, welcome to the exchange.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.