自己动手用c#读取绘制kml数据格式的黄河
这是以前初学c#时,拿来练手的东西。功能很简单,就是读取kml格式的路径坐标(在ge上,我自己手工用鼠标拾取,并保存为kml文件),经过那个经纬度的什么变换后,显示出平面图形。
我的目标是自己独立(可以和其他朋友一起)完成一套实用的,可扩展的gis系统。呵呵,目标是不是有点过大了,不过就是定个目标督促自己好好干而已。本着一腔热血,来向各位高人前辈学习。大家请批评指正,能给出建议当然更好了。
图种红蓝两色分别是变换前后的对比(变换前为经纬度坐标)。
Form1.Designer.cs代码:
Code
1namespace WindowsTest
2{
3 partial class Form1
4 {
5 /**//// <summary>
6 /// Required designer variable.
7 /// </summary>
8 private System.ComponentModel.IContainer components = null;
9
10 /**//// <summary>
11 /// Clean up any resources being used.
12 /// </summary>
13 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
14 protected override void Dispose(bool disposing)
15 {
16 if (disposing && (components != null))
17 {
18 components.Dispose();
19 }
20 base.Dispose(disposing);
21 }
22
23 Windows Form Designer generated code#region Windows Form Designer generated code
24
25 /**//// <summary>
26 /// Required method for Designer support - do not modify
27 /// the contents of this method with the code editor.
28 /// </summary>
29 private void InitializeComponent()
30 {
31 this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
32 this.button1 = new System.Windows.Forms.Button();
33 this.button2 = new System.Windows.Forms.Button();
34 this.label1 = new System.Windows.Forms.Label();
35 this.CoorX = new System.Windows.Forms.Label();
36 this.CoorY = new System.Windows.Forms.Label();
37 this.panel1 = new System.Windows.Forms.Panel();
38 this.SuspendLayout();
39 //
40 // openFileDialog1
41 //
42 this.openFileDialog1.FileName = "openFileDialog1";
43 this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
44 //
45 // button1
46 //
47 this.button1.Location = new System.Drawing.Point(21, 12);
48 this.button1.Name = "button1";
49 this.button1.Size = new System.Drawing.Size(75, 23);
50 this.button1.TabIndex = 0;
51 this.button1.Text = "打开";
52 this.button1.UseVisualStyleBackColor = true;
53 this.button1.Click += new System.EventHandler(this.button1_Click);
54 //
55 // button2
56 //
57 this.button2.Location = new System.Drawing.Point(21, 52);
58 this.button2.Name = "button2";
59 this.button2.Size = new System.Drawing.Size(75, 23);
60 this.button2.TabIndex = 1;
61 this.button2.Text = "显示";
62 this.button2.UseVisualStyleBackColor = true;
63 this.button2.Click += new System.EventHandler(this.button2_Click);
64 //
65 // label1
66 //
67 this.label1.AutoSize = true;
68 this.label1.Location = new System.Drawing.Point(146, 57);
69 this.label1.Name = "label1";
70 this.label1.Size = new System.Drawing.Size(41, 12);
71 this.label1.TabIndex = 2;
72 this.label1.Text = "label1";
73 //
74 // CoorX
75 //
76 this.CoorX.AutoSize = true;
77 this.CoorX.Location = new System.Drawing.Point(146, 17);
78 this.CoorX.Name = "CoorX";
79 this.CoorX.Size = new System.Drawing.Size(41, 12);
80 this.CoorX.TabIndex = 3;
81 this.CoorX.Text = "label2";
82 //
83 // CoorY
84 //
85 this.CoorY.AutoSize = true;
86 this.CoorY.Location = new System.Drawing.Point(241, 17);
87 this.CoorY.Name = "CoorY";
88 this.CoorY.Size = new System.Drawing.Size(41, 12);
89 this.CoorY.TabIndex = 4;
90 this.CoorY.Text = "label3";
91 //
92 // panel1
93 //
94 this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
95 this.panel1.Location = new System.Drawing.Point(243, 57);
96 this.panel1.Name = "panel1";
97 this.panel1.Size = new System.Drawing.Size(300, 300);
98 this.panel1.TabIndex = 5;
99 this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
100 //
101 // Form1
102 //
103 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
104 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
105 this.ClientSize = new System.Drawing.Size(565, 376);
106 this.Controls.Add(this.panel1);
107 this.Controls.Add(this.CoorY);
108 this.Controls.Add(this.CoorX);
109 this.Controls.Add(this.label1);
110 this.Controls.Add(this.button2);
111 this.Controls.Add(this.button1);
112 this.Name = "Form1";
113 this.Text = "Form1";
114 this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
115 this.ResumeLayout(false);
116 this.PerformLayout();
117
118 }
119
120 #endregion
121
122 private System.Windows.Forms.OpenFileDialog openFileDialog1;
123 private System.Windows.Forms.Button button1;
124 private System.Windows.Forms.Button button2;
125 private System.Windows.Forms.Label label1;
126 private System.Windows.Forms.Label CoorX;
127 private System.Windows.Forms.Label CoorY;
128 private System.Windows.Forms.Panel panel1;
129 }
130}
131
132
Form1.cs代码:1namespace WindowsTest
2{
3 partial class Form1
4 {
5 /**//// <summary>
6 /// Required designer variable.
7 /// </summary>
8 private System.ComponentModel.IContainer components = null;
9
10 /**//// <summary>
11 /// Clean up any resources being used.
12 /// </summary>
13 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
14 protected override void Dispose(bool disposing)
15 {
16 if (disposing && (components != null))
17 {
18 components.Dispose();
19 }
20 base.Dispose(disposing);
21 }
22
23 Windows Form Designer generated code#region Windows Form Designer generated code
24
25 /**//// <summary>
26 /// Required method for Designer support - do not modify
27 /// the contents of this method with the code editor.
28 /// </summary>
29 private void InitializeComponent()
30 {
31 this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
32 this.button1 = new System.Windows.Forms.Button();
33 this.button2 = new System.Windows.Forms.Button();
34 this.label1 = new System.Windows.Forms.Label();
35 this.CoorX = new System.Windows.Forms.Label();
36 this.CoorY = new System.Windows.Forms.Label();
37 this.panel1 = new System.Windows.Forms.Panel();
38 this.SuspendLayout();
39 //
40 // openFileDialog1
41 //
42 this.openFileDialog1.FileName = "openFileDialog1";
43 this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
44 //
45 // button1
46 //
47 this.button1.Location = new System.Drawing.Point(21, 12);
48 this.button1.Name = "button1";
49 this.button1.Size = new System.Drawing.Size(75, 23);
50 this.button1.TabIndex = 0;
51 this.button1.Text = "打开";
52 this.button1.UseVisualStyleBackColor = true;
53 this.button1.Click += new System.EventHandler(this.button1_Click);
54 //
55 // button2
56 //
57 this.button2.Location = new System.Drawing.Point(21, 52);
58 this.button2.Name = "button2";
59 this.button2.Size = new System.Drawing.Size(75, 23);
60 this.button2.TabIndex = 1;
61 this.button2.Text = "显示";
62 this.button2.UseVisualStyleBackColor = true;
63 this.button2.Click += new System.EventHandler(this.button2_Click);
64 //
65 // label1
66 //
67 this.label1.AutoSize = true;
68 this.label1.Location = new System.Drawing.Point(146, 57);
69 this.label1.Name = "label1";
70 this.label1.Size = new System.Drawing.Size(41, 12);
71 this.label1.TabIndex = 2;
72 this.label1.Text = "label1";
73 //
74 // CoorX
75 //
76 this.CoorX.AutoSize = true;
77 this.CoorX.Location = new System.Drawing.Point(146, 17);
78 this.CoorX.Name = "CoorX";
79 this.CoorX.Size = new System.Drawing.Size(41, 12);
80 this.CoorX.TabIndex = 3;
81 this.CoorX.Text = "label2";
82 //
83 // CoorY
84 //
85 this.CoorY.AutoSize = true;
86 this.CoorY.Location = new System.Drawing.Point(241, 17);
87 this.CoorY.Name = "CoorY";
88 this.CoorY.Size = new System.Drawing.Size(41, 12);
89 this.CoorY.TabIndex = 4;
90 this.CoorY.Text = "label3";
91 //
92 // panel1
93 //
94 this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
95 this.panel1.Location = new System.Drawing.Point(243, 57);
96 this.panel1.Name = "panel1";
97 this.panel1.Size = new System.Drawing.Size(300, 300);
98 this.panel1.TabIndex = 5;
99 this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
100 //
101 // Form1
102 //
103 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
104 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
105 this.ClientSize = new System.Drawing.Size(565, 376);
106 this.Controls.Add(this.panel1);
107 this.Controls.Add(this.CoorY);
108 this.Controls.Add(this.CoorX);
109 this.Controls.Add(this.label1);
110 this.Controls.Add(this.button2);
111 this.Controls.Add(this.button1);
112 this.Name = "Form1";
113 this.Text = "Form1";
114 this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
115 this.ResumeLayout(false);
116 this.PerformLayout();
117
118 }
119
120 #endregion
121
122 private System.Windows.Forms.OpenFileDialog openFileDialog1;
123 private System.Windows.Forms.Button button1;
124 private System.Windows.Forms.Button button2;
125 private System.Windows.Forms.Label label1;
126 private System.Windows.Forms.Label CoorX;
127 private System.Windows.Forms.Label CoorY;
128 private System.Windows.Forms.Panel panel1;
129 }
130}
131
132
Code
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Drawing.Drawing2D;
7using System.Text;
8using System.Windows.Forms;
9using System.IO;
10using System.Collections;
11namespace WindowsTest
12{
13 public partial class Form1 : Form
14 {
15 //double MinX = 0;
16 //double MinY = 0;
17 //double MaxX = 0;
18 //double MaxY = 0;
19 //LBXY lb2xy = new LBXY(121, 32);
20
21 int ZoneWidth;
22 int ZoneHeight;
23 bool IsDraw = false;
24 ArrayList data;
25 ArrayList data2DPoint1;
26 ArrayList data2DPoint2;
27 string filePath;
28 //Stream ss;
29 string[] fileLines;
30 public Form1()
31 {
32
33 InitializeComponent();
34 ZoneWidth = this.panel1.Width;
35 ZoneHeight = this.panel1.Height;
36 }
37
38 private void button1_Click(object sender, EventArgs e)
39 {
40 openFileDialog1.ShowDialog();
41 }
42
43 private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
44 {
45 filePath = openFileDialog1.FileName;
46 //ss = openFileDialog1.OpenFile();
47 }
48
49 private void button2_Click(object sender, EventArgs e)
50 {
51 //ss = openFileDialog1.OpenFile();
52 //long fl=ss.Length;
53
54 //StreamWriter sw = new StreamWriter(ss);
55 //StreamReader sr = new StreamReader(ss);
56 //sr.ReadLine();
57 if (filePath == null || filePath == "")
58 return;
59 try
60 {
61 fileLines = File.ReadAllLines(filePath);
62 string currentLine = fileLines[0];
63 label1.Text = "共有:" + fileLines.Length + "行";
64 StringBuilder sb = new StringBuilder(currentLine);
65 sb.Replace("<coordinates>", "");
66 sb.Replace("</coordinates>", "");
67 currentLine = sb.ToString();
68 string[] Coordinates = currentLine.Split(' ');
69 int PointCount = Coordinates.Length;
70 //string[][] Points = new string[PointCount][];
71 data = new ArrayList();
72 data2DPoint1 = new ArrayList();
73 data2DPoint2 = new ArrayList();
74
75 for (int i = 0; i < PointCount; i++)
76 {
77 if (Coordinates[i] != null && Coordinates[i] != "")
78 {
79 PointCoordinate dpc = new PointCoordinate(Coordinates[i]);
80 data.Add(dpc);
81 }
82 }
83 int tempX,tempY;
84 PointCount = data.Count;
85
86 for (int i = 0; i < PointCount; i++)
87 {
88 new LBXY((PointCoordinate)data[i]);
89 }
90 for (int i = 0; i < PointCount; i++)
91 {
92 tempX =(int)(((PointCoordinate)data[i]).xRatio * ZoneWidth);
93 tempY =ZoneHeight-(int)(((PointCoordinate)data[i]).yRatio * ZoneHeight);
94
95 Point dp = new Point(tempX, tempY);
96 data2DPoint1.Add(dp);
97
98 tempX = (int)(((PointCoordinate)data[i]).lonRatio * ZoneWidth);
99 tempY = ZoneHeight - (int)(((PointCoordinate)data[i]).latRatio * ZoneHeight);
100
101 dp = new Point(tempX, tempY);
102 data2DPoint2.Add(dp);
103 }
104 //dataGridView1.DataSource = data;
105 IsDraw = true;
106 this.Refresh();
107
108
109
110 }
111 catch (Exception)
112 {
113 throw new Exception("读文件失败!");
114 }
115
116 //ss.Close();
117 }
118
119 private void Form1_MouseMove(object sender, MouseEventArgs e)
120 {
121 CoorX.Text = e.X.ToString();
122 CoorY.Text = e.Y.ToString();
123 }
124
125 private void panel1_Paint(object sender, PaintEventArgs e)
126 {
127 Graphics g = e.Graphics;
128 Pen BluePen = new Pen(Color.Blue, 2);
129 Pen RedPen = new Pen(Color.Red, 2);
130 Brush RedBrush = new SolidBrush(Color.Red);
131 Brush GreenBrush = new SolidBrush(Color.Green);
132 //g.DrawLine(BluePen, new Point(0, 0), new Point(180, 100));
133
134 if (IsDraw)
135 {
136
137 int pCount = data2DPoint1.Count;
138 if (pCount < 1)
139 return;
140
141 for (int i = 1; i < pCount; i++)
142 {
143 //g.DrawLine(BluePen, data2DPoint[i - 1], data2DPoint[i]);
144 g.DrawLine(BluePen, (Point)data2DPoint1[i - 1], (Point)data2DPoint1[i]);
145 }
146
147
148
149 pCount = data2DPoint2.Count;
150 if (pCount < 1)
151 return;
152
153 for (int i = 1; i < pCount; i++)
154 {
155 //g.DrawLine(BluePen, data2DPoint[i - 1], data2DPoint[i]);
156 g.DrawLine(RedPen, (Point)data2DPoint2[i - 1], (Point)data2DPoint2[i]);
157 }
158 }
159
160 }
161
162
163 }
164
165 public class PointCoordinate
166 {
167 private double lon;
168 private double lat;
169 private double alt;
170 public double x;
171 public double y;
172 private static double minX = double.MaxValue;
173 private static double maxX = double.MinValue;
174 private static double minY = double.MaxValue;
175 private static double maxY = double.MinValue;
176 public static double minlon = double.MaxValue;
177 public static double minlat = double.MaxValue;
178 public static double minalt = double.MaxValue;
179 public static double maxlon = double.MinValue;
180 public static double maxlat = double.MinValue;
181 public static double maxalt = double.MinValue;
182
183 Attribute#region Attribute
184
185 public double Lon
186 {
187 get
188 {
189 return lon;
190 }
191 set
192 {
193 lon = value;
194 }
195 }
196 public double Lat
197 {
198 get
199 {
200 return lat;
201 }
202 set
203 {
204 lat = value;
205 }
206 }
207 public double Alt
208 {
209 get
210 {
211 return alt;
212 }
213 set
214 {
215 alt = value;
216 }
217 }
218
219 #endregion
220
221 ConStructions#region ConStructions
222
223 public PointCoordinate(double lon, double lat, double alt)
224 {
225 init(lon, lat, alt);
226 }
227 public PointCoordinate(string lon, string lat, string alt)
228 {
229 init(lon, lat, alt);
230 }
231 public PointCoordinate(string[] OnePoint)
232 {
233 init(OnePoint);
234 }
235
236 public PointCoordinate(string tuples)
237 {
238 init(tuples);
239 }
240
241 #endregion
242
243 Initilation#region Initilation
244
245 private void init(double lon, double lat, double alt)
246 {
247 this.lon = lon;
248 this.lat = lat;
249 this.alt = alt;
250 //LBXY lbxy = new LBXY(this.lon, this.lat);
251 //this.x = lbxy.x;
252 //this.y = lbxy.y;
253 //CheckRange();
254 CheckEarthRange();
255 }
256 private void init(string lon, string lat, string alt)
257 {
258 init(Convert.ToDouble(lon), Convert.ToDouble(lat), Convert.ToDouble(alt));
259 }
260 private void init(string[] OnePoint)
261 {
262 int CheckLen = OnePoint.Length;
263
264 if (CheckLen == 3)
265 init(OnePoint[0], OnePoint[1], OnePoint[2]);
266 else if(CheckLen==2)
267 init(OnePoint[0], OnePoint[1], "0.0");
268 }
269 private void init(string tuples)
270 {
271 string[] OnePoint = tuples.Split(',');
272 init(OnePoint);
273 }
274
275 #endregion
276
277 private void CheckEarthRange()
278 {
279 if (lon < minlon) minlon = lon;
280 if (lon > maxlon) maxlon = lon;
281 if (lat < minlat) minlat = lat;
282 if (lat > maxlat) maxlat = lat;
283 if (alt < minalt) minalt = alt;
284 if (alt > maxalt) maxalt = alt;
285 }
286 public void CheckRange()
287 {
288 if (x < minX) minX = x;
289 if (x > maxX) maxX = x;
290 if (y < minY) minY = y;
291 if (y > maxY) maxY = y;
292 }
293
294 //public double LonRatio
295 //{
296 // get
297 // {
298 // if((maxlon - minlon)!=0)
299 // return (lon - minlon) / (maxlon - minlon);
300 // else
301 // return 0.0;
302 // }
303 //}
304 //public double LatRatio
305 //{
306 // get
307 // {
308 // if((maxlat - minlat)!=0)
309 // return (lat - minlat) / (maxlat - minlat);
310 // else
311 // return 0.0;
312 // }
313 //}
314 public double xRatio
315 {
316 get
317 {
318 if ((maxX - minX) != 0)
319 return (x - minX) / (maxX - minX);
320 else
321 return 0.0;
322 }
323 }
324 public double yRatio
325 {
326 get
327 {
328 if ((maxY - minY) != 0)
329 return (y - minY) / (maxY - minY);
330 else
331 return 0.0;
332 }
333 }
334 public double lonRatio
335 {
336 get
337 {
338 if ((maxlon - minlon) != 0)
339 return (lon - minlon) / (maxlon - minlon);
340 else
341 return 0.0;
342 }
343 }
344 public double latRatio
345 {
346 get
347 {
348 if ((maxlat - minlat) != 0)
349 return (lat - minlat) / (maxlat - minlat);
350 else
351 return 0.0;
352 }
353 }
354
355 }
356
357 public class LBXY
358 {
359 public double l;//角度
360 public double b;//角度
361 private double L;//弧度
362 private double B;//弧度
363 private double l0;
364 private double L0;
365 public double x;
366 public double y;
367
368 public LBXY(double l, double b)
369 {
370 this.l = l;
371 this.b = b;
372 l0 = ML(l);
373 L0 = Deg2Arc(l0);
374 L = Deg2Arc(l);
375 B = Deg2Arc(b);
376 LB2XY();
377 }
378
379 public LBXY(PointCoordinate pc)
380 {
381 this.l = pc.Lon;
382 this.b = pc.Lat;
383 l0 = ML((PointCoordinate.maxlon + PointCoordinate.minlon) / 2.0);
384 L0 = Deg2Arc(l0);
385 L = Deg2Arc(l);
386 B = Deg2Arc(b);
387 LB2XY();
388 pc.x = this.y;
389 pc.y = this.x;
390 pc.CheckRange();
391 }
392
393 public double Arc2Deg(double arc)
394 {
395 return arc * 180 / Math.PI;
396 }
397 public double Deg2Arc(double deg)
398 {
399 return deg * Math.PI / 180;
400 }
401 public double ML(double lon)
402 {
403 int ZoneID=(int)(lon / 6) + 1;
404 double rev = (double)(ZoneID * 6 - 3);
405 return rev;
406 }
407
408 public void LB2XY()
409 {
410 double e2 = 0.006739501819;
411 double c = 6399596.65198801;//单位:m
412 //double e2 = 0.006738525415;
413 //double c = 6399698.9018;//单位:m
414
415 double t = Math.Tan(B);
416 double eta2 = e2 * Math.Cos(B) * Math.Cos(B);
417 double N = c / Math.Sqrt(1 + eta2);
418 double m = Math.Cos(B)*(L-L0);
419 double X = 111134.0047 * b
420 - (
421 32009.8575 * Math.Sin(B)
422 + 133.9602 * Math.Pow(Math.Sin(B), 3)
423 + 0.6976 * Math.Pow(Math.Sin(B), 5)
424 + 0.0039 * Math.Pow(Math.Sin(B), 7)
425 ) * Math.Cos(B);
426 double q1, q2, q3, q4;
427 q1 = t*t;
428 q2 = Math.Pow(t, 4);
429 q3 = m * m;
430 q4 = Math.Pow(m, 4);
431
432 x=X+N*t*q3*(0.5+(5-q1+9*eta2+4*eta2*eta2)*q3/24+(61-58*q1+q2)*q4/720);//纵轴,经线方向
433 y=N*m*(1+(1-q1+eta2)*q3/6+(5-18*q1+q2+14*eta2-58*eta2*q1)*q4/720);//横轴,赤道方向
434 }
435 }
436
437}
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Drawing.Drawing2D;
7using System.Text;
8using System.Windows.Forms;
9using System.IO;
10using System.Collections;
11namespace WindowsTest
12{
13 public partial class Form1 : Form
14 {
15 //double MinX = 0;
16 //double MinY = 0;
17 //double MaxX = 0;
18 //double MaxY = 0;
19 //LBXY lb2xy = new LBXY(121, 32);
20
21 int ZoneWidth;
22 int ZoneHeight;
23 bool IsDraw = false;
24 ArrayList data;
25 ArrayList data2DPoint1;
26 ArrayList data2DPoint2;
27 string filePath;
28 //Stream ss;
29 string[] fileLines;
30 public Form1()
31 {
32
33 InitializeComponent();
34 ZoneWidth = this.panel1.Width;
35 ZoneHeight = this.panel1.Height;
36 }
37
38 private void button1_Click(object sender, EventArgs e)
39 {
40 openFileDialog1.ShowDialog();
41 }
42
43 private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
44 {
45 filePath = openFileDialog1.FileName;
46 //ss = openFileDialog1.OpenFile();
47 }
48
49 private void button2_Click(object sender, EventArgs e)
50 {
51 //ss = openFileDialog1.OpenFile();
52 //long fl=ss.Length;
53
54 //StreamWriter sw = new StreamWriter(ss);
55 //StreamReader sr = new StreamReader(ss);
56 //sr.ReadLine();
57 if (filePath == null || filePath == "")
58 return;
59 try
60 {
61 fileLines = File.ReadAllLines(filePath);
62 string currentLine = fileLines[0];
63 label1.Text = "共有:" + fileLines.Length + "行";
64 StringBuilder sb = new StringBuilder(currentLine);
65 sb.Replace("<coordinates>", "");
66 sb.Replace("</coordinates>", "");
67 currentLine = sb.ToString();
68 string[] Coordinates = currentLine.Split(' ');
69 int PointCount = Coordinates.Length;
70 //string[][] Points = new string[PointCount][];
71 data = new ArrayList();
72 data2DPoint1 = new ArrayList();
73 data2DPoint2 = new ArrayList();
74
75 for (int i = 0; i < PointCount; i++)
76 {
77 if (Coordinates[i] != null && Coordinates[i] != "")
78 {
79 PointCoordinate dpc = new PointCoordinate(Coordinates[i]);
80 data.Add(dpc);
81 }
82 }
83 int tempX,tempY;
84 PointCount = data.Count;
85
86 for (int i = 0; i < PointCount; i++)
87 {
88 new LBXY((PointCoordinate)data[i]);
89 }
90 for (int i = 0; i < PointCount; i++)
91 {
92 tempX =(int)(((PointCoordinate)data[i]).xRatio * ZoneWidth);
93 tempY =ZoneHeight-(int)(((PointCoordinate)data[i]).yRatio * ZoneHeight);
94
95 Point dp = new Point(tempX, tempY);
96 data2DPoint1.Add(dp);
97
98 tempX = (int)(((PointCoordinate)data[i]).lonRatio * ZoneWidth);
99 tempY = ZoneHeight - (int)(((PointCoordinate)data[i]).latRatio * ZoneHeight);
100
101 dp = new Point(tempX, tempY);
102 data2DPoint2.Add(dp);
103 }
104 //dataGridView1.DataSource = data;
105 IsDraw = true;
106 this.Refresh();
107
108
109
110 }
111 catch (Exception)
112 {
113 throw new Exception("读文件失败!");
114 }
115
116 //ss.Close();
117 }
118
119 private void Form1_MouseMove(object sender, MouseEventArgs e)
120 {
121 CoorX.Text = e.X.ToString();
122 CoorY.Text = e.Y.ToString();
123 }
124
125 private void panel1_Paint(object sender, PaintEventArgs e)
126 {
127 Graphics g = e.Graphics;
128 Pen BluePen = new Pen(Color.Blue, 2);
129 Pen RedPen = new Pen(Color.Red, 2);
130 Brush RedBrush = new SolidBrush(Color.Red);
131 Brush GreenBrush = new SolidBrush(Color.Green);
132 //g.DrawLine(BluePen, new Point(0, 0), new Point(180, 100));
133
134 if (IsDraw)
135 {
136
137 int pCount = data2DPoint1.Count;
138 if (pCount < 1)
139 return;
140
141 for (int i = 1; i < pCount; i++)
142 {
143 //g.DrawLine(BluePen, data2DPoint[i - 1], data2DPoint[i]);
144 g.DrawLine(BluePen, (Point)data2DPoint1[i - 1], (Point)data2DPoint1[i]);
145 }
146
147
148
149 pCount = data2DPoint2.Count;
150 if (pCount < 1)
151 return;
152
153 for (int i = 1; i < pCount; i++)
154 {
155 //g.DrawLine(BluePen, data2DPoint[i - 1], data2DPoint[i]);
156 g.DrawLine(RedPen, (Point)data2DPoint2[i - 1], (Point)data2DPoint2[i]);
157 }
158 }
159
160 }
161
162
163 }
164
165 public class PointCoordinate
166 {
167 private double lon;
168 private double lat;
169 private double alt;
170 public double x;
171 public double y;
172 private static double minX = double.MaxValue;
173 private static double maxX = double.MinValue;
174 private static double minY = double.MaxValue;
175 private static double maxY = double.MinValue;
176 public static double minlon = double.MaxValue;
177 public static double minlat = double.MaxValue;
178 public static double minalt = double.MaxValue;
179 public static double maxlon = double.MinValue;
180 public static double maxlat = double.MinValue;
181 public static double maxalt = double.MinValue;
182
183 Attribute#region Attribute
184
185 public double Lon
186 {
187 get
188 {
189 return lon;
190 }
191 set
192 {
193 lon = value;
194 }
195 }
196 public double Lat
197 {
198 get
199 {
200 return lat;
201 }
202 set
203 {
204 lat = value;
205 }
206 }
207 public double Alt
208 {
209 get
210 {
211 return alt;
212 }
213 set
214 {
215 alt = value;
216 }
217 }
218
219 #endregion
220
221 ConStructions#region ConStructions
222
223 public PointCoordinate(double lon, double lat, double alt)
224 {
225 init(lon, lat, alt);
226 }
227 public PointCoordinate(string lon, string lat, string alt)
228 {
229 init(lon, lat, alt);
230 }
231 public PointCoordinate(string[] OnePoint)
232 {
233 init(OnePoint);
234 }
235
236 public PointCoordinate(string tuples)
237 {
238 init(tuples);
239 }
240
241 #endregion
242
243 Initilation#region Initilation
244
245 private void init(double lon, double lat, double alt)
246 {
247 this.lon = lon;
248 this.lat = lat;
249 this.alt = alt;
250 //LBXY lbxy = new LBXY(this.lon, this.lat);
251 //this.x = lbxy.x;
252 //this.y = lbxy.y;
253 //CheckRange();
254 CheckEarthRange();
255 }
256 private void init(string lon, string lat, string alt)
257 {
258 init(Convert.ToDouble(lon), Convert.ToDouble(lat), Convert.ToDouble(alt));
259 }
260 private void init(string[] OnePoint)
261 {
262 int CheckLen = OnePoint.Length;
263
264 if (CheckLen == 3)
265 init(OnePoint[0], OnePoint[1], OnePoint[2]);
266 else if(CheckLen==2)
267 init(OnePoint[0], OnePoint[1], "0.0");
268 }
269 private void init(string tuples)
270 {
271 string[] OnePoint = tuples.Split(',');
272 init(OnePoint);
273 }
274
275 #endregion
276
277 private void CheckEarthRange()
278 {
279 if (lon < minlon) minlon = lon;
280 if (lon > maxlon) maxlon = lon;
281 if (lat < minlat) minlat = lat;
282 if (lat > maxlat) maxlat = lat;
283 if (alt < minalt) minalt = alt;
284 if (alt > maxalt) maxalt = alt;
285 }
286 public void CheckRange()
287 {
288 if (x < minX) minX = x;
289 if (x > maxX) maxX = x;
290 if (y < minY) minY = y;
291 if (y > maxY) maxY = y;
292 }
293
294 //public double LonRatio
295 //{
296 // get
297 // {
298 // if((maxlon - minlon)!=0)
299 // return (lon - minlon) / (maxlon - minlon);
300 // else
301 // return 0.0;
302 // }
303 //}
304 //public double LatRatio
305 //{
306 // get
307 // {
308 // if((maxlat - minlat)!=0)
309 // return (lat - minlat) / (maxlat - minlat);
310 // else
311 // return 0.0;
312 // }
313 //}
314 public double xRatio
315 {
316 get
317 {
318 if ((maxX - minX) != 0)
319 return (x - minX) / (maxX - minX);
320 else
321 return 0.0;
322 }
323 }
324 public double yRatio
325 {
326 get
327 {
328 if ((maxY - minY) != 0)
329 return (y - minY) / (maxY - minY);
330 else
331 return 0.0;
332 }
333 }
334 public double lonRatio
335 {
336 get
337 {
338 if ((maxlon - minlon) != 0)
339 return (lon - minlon) / (maxlon - minlon);
340 else
341 return 0.0;
342 }
343 }
344 public double latRatio
345 {
346 get
347 {
348 if ((maxlat - minlat) != 0)
349 return (lat - minlat) / (maxlat - minlat);
350 else
351 return 0.0;
352 }
353 }
354
355 }
356
357 public class LBXY
358 {
359 public double l;//角度
360 public double b;//角度
361 private double L;//弧度
362 private double B;//弧度
363 private double l0;
364 private double L0;
365 public double x;
366 public double y;
367
368 public LBXY(double l, double b)
369 {
370 this.l = l;
371 this.b = b;
372 l0 = ML(l);
373 L0 = Deg2Arc(l0);
374 L = Deg2Arc(l);
375 B = Deg2Arc(b);
376 LB2XY();
377 }
378
379 public LBXY(PointCoordinate pc)
380 {
381 this.l = pc.Lon;
382 this.b = pc.Lat;
383 l0 = ML((PointCoordinate.maxlon + PointCoordinate.minlon) / 2.0);
384 L0 = Deg2Arc(l0);
385 L = Deg2Arc(l);
386 B = Deg2Arc(b);
387 LB2XY();
388 pc.x = this.y;
389 pc.y = this.x;
390 pc.CheckRange();
391 }
392
393 public double Arc2Deg(double arc)
394 {
395 return arc * 180 / Math.PI;
396 }
397 public double Deg2Arc(double deg)
398 {
399 return deg * Math.PI / 180;
400 }
401 public double ML(double lon)
402 {
403 int ZoneID=(int)(lon / 6) + 1;
404 double rev = (double)(ZoneID * 6 - 3);
405 return rev;
406 }
407
408 public void LB2XY()
409 {
410 double e2 = 0.006739501819;
411 double c = 6399596.65198801;//单位:m
412 //double e2 = 0.006738525415;
413 //double c = 6399698.9018;//单位:m
414
415 double t = Math.Tan(B);
416 double eta2 = e2 * Math.Cos(B) * Math.Cos(B);
417 double N = c / Math.Sqrt(1 + eta2);
418 double m = Math.Cos(B)*(L-L0);
419 double X = 111134.0047 * b
420 - (
421 32009.8575 * Math.Sin(B)
422 + 133.9602 * Math.Pow(Math.Sin(B), 3)
423 + 0.6976 * Math.Pow(Math.Sin(B), 5)
424 + 0.0039 * Math.Pow(Math.Sin(B), 7)
425 ) * Math.Cos(B);
426 double q1, q2, q3, q4;
427 q1 = t*t;
428 q2 = Math.Pow(t, 4);
429 q3 = m * m;
430 q4 = Math.Pow(m, 4);
431
432 x=X+N*t*q3*(0.5+(5-q1+9*eta2+4*eta2*eta2)*q3/24+(61-58*q1+q2)*q4/720);//纵轴,经线方向
433 y=N*m*(1+(1-q1+eta2)*q3/6+(5-18*q1+q2+14*eta2-58*eta2*q1)*q4/720);//横轴,赤道方向
434 }
435 }
436
437}
效果显示: