MainForm.cs
上传用户:sex100000
上传日期:2013-11-09
资源大小:1377k
文件大小:13k
源码类别:

GIS编程

开发平台:

C#

  1. // Copyright 2007 - Rory Plaire (codekaizen@gmail.com)
  2. //
  3. // This file is part of SharpMap.
  4. // SharpMap is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation; either version 2 of the License, or
  7. // (at your option) any later version.
  8. // 
  9. // SharpMap is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. // GNU Lesser General Public License for more details.
  13. // You should have received a copy of the GNU Lesser General Public License
  14. // along with SharpMap; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Collections.ObjectModel;
  19. using System.ComponentModel;
  20. using System.Configuration;
  21. using System.Data;
  22. using System.Drawing;
  23. using System.IO;
  24. using System.Text;
  25. using System.Windows.Forms;
  26. using GeoPoint = SharpMap.Geometries.Point;
  27. using SharpMap.Geometries;
  28. using SharpMap.Layers;
  29. using SharpMap.Forms;
  30. using SharpMap.Data.Providers;
  31. using DemoWinForm.Properties;
  32. namespace DemoWinForm
  33. {
  34. public partial class MainForm : Form
  35. {
  36. Dictionary<string, ILayerFactory> _layerFactoryCatalog = new Dictionary<string, ILayerFactory>();
  37. Dictionary<string, Bitmap> _symbolTable = new Dictionary<string, Bitmap>();
  38. Dictionary<string, Color> _colorTable = new Dictionary<string, Color>();
  39. public MainForm()
  40. {
  41. InitializeComponent();
  42. registerSymbols();
  43. registerKnownColors(_colorTable);
  44. registerLayerFactories();
  45. MainMapImage.MapQueried += new MapImage.MapQueryHandler(MainMapImage_MapQueried);
  46. }
  47. private void registerSymbols()
  48. {
  49. _symbolTable["Notices"] = Resources.Chat;
  50. _symbolTable["Radioactive Fuel Rods"] = Resources.DATABASE;
  51. _symbolTable["Bases"] = Resources.Flag;
  52. _symbolTable["Houses"] = Resources.Home;
  53. _symbolTable["Measures"] = Resources.PIE_DIAGRAM;
  54. _symbolTable["Contacts"] = Resources.Women;
  55. _symbolTable["Prospects"] = Resources.Women_1;
  56. }
  57. private static void registerKnownColors(Dictionary<string, Color> colorTable)
  58. {
  59. foreach (string colorName in Enum.GetNames(typeof(KnownColor)))
  60. {
  61. KnownColor color = (KnownColor)Enum.Parse(typeof(KnownColor), colorName);
  62. colorTable[colorName] = Color.FromKnownColor(color);
  63. }
  64. }
  65. private void registerLayerFactories()
  66. {
  67. // ConfigurationManager.GetSection("LayerFactories");
  68. _layerFactoryCatalog[".shp"] = new ShapeFileLayerFactory();
  69. }
  70. private void addLayer(ILayer layer)
  71. {
  72. MainMapImage.Map.Layers.Add(layer);
  73. LayersDataGridView.Rows.Insert(0, true, getLayerTypeIcon(layer.GetType()), layer.LayerName);
  74. }
  75. private void addNewRandomGeometryLayer()
  76. {
  77. Random rndGen = new Random();
  78. Collection<Geometry> geometry = new Collection<Geometry>();
  79. VectorLayer layer = new VectorLayer(String.Empty);
  80. switch (rndGen.Next(3))
  81. {
  82. case 0:
  83. {
  84. generatePoints(geometry, rndGen);
  85. KeyValuePair<string, Bitmap> symbolEntry = getSymbolEntry(rndGen.Next(_symbolTable.Count));
  86. layer.Style.Symbol = symbolEntry.Value;
  87. layer.LayerName = symbolEntry.Key;
  88. }
  89. break;
  90. case 1:
  91. {
  92. generateLines(geometry, rndGen);
  93. KeyValuePair<string, Color> colorEntry = getColorEntry(rndGen.Next(_colorTable.Count));
  94. layer.Style.Line = new Pen(colorEntry.Value);
  95. layer.LayerName = String.Format("{0} lines", colorEntry.Key);
  96. }
  97. break;
  98. case 2:
  99. {
  100. generatePolygons(geometry, rndGen);
  101. KeyValuePair<string, Color> colorEntry = getColorEntry(rndGen.Next(_colorTable.Count));
  102. layer.Style.Fill = new SolidBrush(colorEntry.Value);
  103. layer.LayerName = String.Format("{0} squares", colorEntry.Key);
  104. }
  105. break;
  106. default:
  107. throw new NotSupportedException();
  108. }
  109. GeometryProvider provider = new GeometryProvider(geometry);
  110. layer.DataSource = provider;
  111. addLayer(layer);
  112. }
  113. private KeyValuePair<string, Bitmap> getSymbolEntry(int index)
  114. {
  115. foreach (KeyValuePair<string, Bitmap> entry in _symbolTable)
  116. {
  117. if (index-- == 0)
  118. return entry;
  119. }
  120. throw new InvalidOperationException();
  121. }
  122. private KeyValuePair<string, Color> getColorEntry(int index)
  123. {
  124. foreach (KeyValuePair<string, Color> entry in _colorTable)
  125. {
  126. if (index-- == 0)
  127. return entry;
  128. }
  129. throw new InvalidOperationException();
  130. }
  131. private Color getRandomColor(Random rndGen)
  132. {
  133. return Color.FromArgb(rndGen.Next(255), rndGen.Next(255), rndGen.Next(255));
  134. }
  135. private void generatePolygons(Collection<Geometry> geometry, Random rndGen)
  136. {
  137. int numPolygons = rndGen.Next(10, 100);
  138. for (int polyIndex = 0; polyIndex < numPolygons; polyIndex++)
  139. {
  140. Polygon polygon = new Polygon();
  141. Collection<GeoPoint> verticies = new Collection<GeoPoint>();
  142. GeoPoint upperLeft = new GeoPoint(rndGen.NextDouble() * 1000, rndGen.NextDouble() * 1000);
  143. double sideLength = rndGen.NextDouble() * 50;
  144. // Make a square
  145. verticies.Add(upperLeft);
  146. verticies.Add(new GeoPoint(upperLeft.X + sideLength, upperLeft.Y));
  147. verticies.Add(new GeoPoint(upperLeft.X + sideLength, upperLeft.Y - sideLength));
  148. verticies.Add(new GeoPoint(upperLeft.X, upperLeft.Y - sideLength));
  149. polygon.ExteriorRing = new LinearRing(verticies);
  150. geometry.Add(polygon);
  151. }
  152. }
  153. private void generateLines(Collection<Geometry> geometry, Random rndGen)
  154. {
  155. int numLines = rndGen.Next(10, 100);
  156. for (int lineIndex = 0; lineIndex < numLines; lineIndex++)
  157. {
  158. LineString line = new LineString();
  159. Collection<GeoPoint> verticies = new Collection<GeoPoint>();
  160. int numVerticies = rndGen.Next(4, 15);
  161. GeoPoint lastPoint = new GeoPoint(rndGen.NextDouble() * 1000, rndGen.NextDouble() * 1000);
  162. verticies.Add(lastPoint);
  163. for (int vertexIndex = 0; vertexIndex < numVerticies; vertexIndex++)
  164. {
  165. GeoPoint nextPoint = new GeoPoint(lastPoint.X + rndGen.Next(-50, 50), lastPoint.Y + rndGen.Next(-50, 50));
  166. verticies.Add(nextPoint);
  167. lastPoint = nextPoint;
  168. }
  169. line.Vertices = verticies;
  170. geometry.Add(line);
  171. }
  172. }
  173. private void generatePoints(Collection<Geometry> geometry, Random rndGen)
  174. {
  175. int numPoints = rndGen.Next(10, 100);
  176. for (int pointIndex = 0; pointIndex < numPoints; pointIndex++)
  177. {
  178. GeoPoint point = new GeoPoint(rndGen.NextDouble() * 1000, rndGen.NextDouble() * 1000);
  179. geometry.Add(point);
  180. }
  181. }
  182. private void loadLayer()
  183. {
  184. DialogResult result = AddLayerDialog.ShowDialog(this);
  185. if (result == DialogResult.OK)
  186. {
  187. foreach (string fileName in AddLayerDialog.FileNames)
  188. {
  189. string extension = Path.GetExtension(fileName);
  190. ILayerFactory layerFactory = null;
  191. if (!_layerFactoryCatalog.TryGetValue(extension, out layerFactory))
  192. continue;
  193. ILayer layer = layerFactory.Create(Path.GetFileNameWithoutExtension(fileName), fileName);
  194. addLayer(layer);
  195. }
  196. changeUIOnLayerSelectionChange();
  197. MainMapImage.Refresh();
  198. }
  199. }
  200. private void removeLayer()
  201. {
  202. if (LayersDataGridView.SelectedRows.Count == 0)
  203. return;
  204. string layerName = LayersDataGridView.SelectedRows[0].Cells[2].Value as string;
  205. ILayer layerToRemove = null;
  206. foreach (ILayer layer in MainMapImage.Map.Layers)
  207. if (layer.LayerName == layerName)
  208. {
  209. layerToRemove = layer;
  210. break;
  211. }
  212. MainMapImage.Map.Layers.Remove(layerToRemove);
  213. LayersDataGridView.Rows.Remove(LayersDataGridView.SelectedRows[0]);
  214. }
  215. private void zoomToExtents()
  216. {
  217. MainMapImage.Map.ZoomToExtents();
  218. MainMapImage.Refresh();
  219. }
  220. private void changeMode(MapImage.Tools tool)
  221. {
  222. MainMapImage.ActiveTool = tool;
  223. ZoomInModeToolStripButton.Checked = (tool == MapImage.Tools.ZoomIn);
  224. ZoomOutModeToolStripButton.Checked = (tool == MapImage.Tools.ZoomOut);
  225. PanToolStripButton.Checked = (tool == MapImage.Tools.Pan);
  226. QueryModeToolStripButton.Checked = (tool == MapImage.Tools.Query);
  227. }
  228. private object getLayerTypeIcon(Type type)
  229. {
  230. if (type == typeof(VectorLayer))
  231. {
  232. return Resources.polygon;
  233. }
  234. return Resources.Raster;
  235. }
  236. private void changeUIOnLayerSelectionChange()
  237. {
  238. bool isLayerSelected = false;
  239. int layerIndex = -1;
  240. if (LayersDataGridView.SelectedRows.Count > 0)
  241. {
  242. isLayerSelected = true;
  243. layerIndex = LayersDataGridView.SelectedRows[0].Index;
  244. }
  245. RemoveLayerToolStripButton.Enabled = isLayerSelected;
  246. RemoveLayerToolStripMenuItem.Enabled  = isLayerSelected;
  247. if (layerIndex < 0)
  248. {
  249. MoveUpToolStripMenuItem.Visible = false;
  250. MoveDownToolStripMenuItem.Visible = false;
  251. LayerContextMenuSeparator.Visible = false;
  252. return;
  253. }
  254. else
  255. {
  256. MoveUpToolStripMenuItem.Visible = true;
  257. MoveDownToolStripMenuItem.Visible = true;
  258. LayerContextMenuSeparator.Visible = true;
  259. }
  260. if (layerIndex == 0)
  261. {
  262. MoveUpToolStripMenuItem.Enabled = false;
  263. }
  264. else
  265. {
  266. MoveUpToolStripMenuItem.Enabled = true;
  267. }
  268. if (layerIndex == LayersDataGridView.Rows.Count - 1)
  269. {
  270. MoveDownToolStripMenuItem.Enabled = false;
  271. }
  272. else
  273. {
  274. MoveDownToolStripMenuItem.Enabled = true;
  275. }
  276. }
  277. void MainMapImage_MapQueried(SharpMap.Data.FeatureDataTable data)
  278. {
  279. FeaturesDataGridView.DataSource = data;
  280. }
  281. private void AddLayerToolStripButton_Click(object sender, EventArgs e)
  282. {
  283. BeginInvoke((MethodInvoker)delegate() { loadLayer(); });
  284. }
  285. private void RemoveLayerToolStripButton_Click(object sender, EventArgs e)
  286. {
  287. BeginInvoke((MethodInvoker)delegate() { removeLayer(); });
  288. }
  289. private void AddLayerToolStripMenuItem_Click(object sender, EventArgs e)
  290. {
  291. BeginInvoke((MethodInvoker)delegate() { loadLayer(); });
  292. }
  293. private void RemoveLayerToolStripMenuItem_Click(object sender, EventArgs e)
  294. {
  295. BeginInvoke((MethodInvoker)delegate() { removeLayer(); });
  296. }
  297. private void ZoomToExtentsToolStripButton_Click(object sender, EventArgs e)
  298. {
  299. BeginInvoke((MethodInvoker)delegate() { zoomToExtents(); });
  300. }
  301. private void PanToolStripButton_Click(object sender, EventArgs e)
  302. {
  303. BeginInvoke((MethodInvoker)delegate() { changeMode(MapImage.Tools.Pan); });
  304. }
  305. private void QueryModeToolStripButton_Click(object sender, EventArgs e)
  306. {
  307. BeginInvoke((MethodInvoker)delegate() { changeMode(MapImage.Tools.Query); });
  308. }
  309. private void ZoomInModeToolStripButton_Click(object sender, EventArgs e)
  310. {
  311. BeginInvoke((MethodInvoker)delegate() { changeMode(MapImage.Tools.ZoomIn); });
  312. }
  313. private void ZoomOutModeToolStripButton_Click(object sender, EventArgs e)
  314. {
  315. BeginInvoke((MethodInvoker)delegate() { changeMode(MapImage.Tools.ZoomOut); });
  316. }
  317. private void MoveUpToolStripMenuItem_Click(object sender, EventArgs e)
  318. {
  319. if (LayersDataGridView.SelectedRows.Count == 0)
  320. return;
  321. if (LayersDataGridView.SelectedRows[0].Index == 0)
  322. return;
  323. int rowIndex = LayersDataGridView.SelectedRows[0].Index;
  324. DataGridViewRow row = LayersDataGridView.Rows[rowIndex];
  325. LayersDataGridView.Rows.RemoveAt(rowIndex);
  326. LayersDataGridView.Rows.Insert(rowIndex - 1, row);
  327. int layerIndex = MainMapImage.Map.Layers.Count - rowIndex - 1;
  328. ILayer layer = MainMapImage.Map.Layers[layerIndex];
  329. MainMapImage.Map.Layers.RemoveAt(layerIndex);
  330. MainMapImage.Map.Layers.Insert(layerIndex + 1, layer);
  331. }
  332. private void MoveDownToolStripMenuItem_Click(object sender, EventArgs e)
  333. {
  334. if (LayersDataGridView.SelectedRows.Count == 0)
  335. return;
  336. if (LayersDataGridView.SelectedRows[0].Index == LayersDataGridView.Rows.Count - 1)
  337. return;
  338. int rowIndex = LayersDataGridView.SelectedRows[0].Index;
  339. DataGridViewRow row = LayersDataGridView.Rows[rowIndex];
  340. LayersDataGridView.Rows.RemoveAt(rowIndex);
  341. LayersDataGridView.Rows.Insert(rowIndex + 1, row);
  342. int layerIndex = MainMapImage.Map.Layers.Count - rowIndex - 1;
  343. ILayer layer = MainMapImage.Map.Layers[layerIndex];
  344. MainMapImage.Map.Layers.RemoveAt(layerIndex);
  345. MainMapImage.Map.Layers.Insert(layerIndex - 1, layer);
  346. }
  347. private void LayersDataGridView_SelectionChanged(object sender, EventArgs e)
  348. {
  349. BeginInvoke((MethodInvoker)delegate() 
  350. changeUIOnLayerSelectionChange();
  351. if (LayersDataGridView.SelectedRows.Count > 0)
  352. {
  353. MainMapImage.QueryLayerIndex = LayersDataGridView.SelectedRows[0].Index;
  354. }
  355. });
  356. }
  357. private void MainMapImage_MouseMove(SharpMap.Geometries.Point WorldPos, MouseEventArgs ImagePos)
  358. {
  359. CoordinatesLabel.Text = String.Format("Coordinates: {0:N5}, {1:N5}", WorldPos.X, WorldPos.Y);
  360. }
  361. private void AddNewRandomGeometryLayer_Click(object sender, EventArgs e)
  362. {
  363. BeginInvoke((MethodInvoker)delegate() { addNewRandomGeometryLayer(); });
  364. }
  365. }
  366. }