PageRenderTime 35ms CodeModel.GetById 4ms RepoModel.GetById 1ms app.codeStats 0ms

/Report7.0.6/EpiDashboard/MapControl.xaml.cs

#
C# | 292 lines | 251 code | 37 blank | 4 comment | 27 complexity | a38f1b30e06688563a4510f04295382c MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Runtime.Serialization;
  8. using System.Text;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. using ESRI.ArcGIS.Client;
  19. using ESRI.ArcGIS.Client.Layers;
  20. using ESRI.ArcGIS.Client.Geometry;
  21. using ESRI.ArcGIS.Client.Symbols;
  22. using Epi;
  23. using Epi.Data;
  24. namespace EpiDashboard
  25. {
  26. public delegate void RecordSelectedHandler(int id);
  27. /// <summary>
  28. /// Interaction logic for MapControl.xaml
  29. /// </summary>
  30. public partial class MapControl : UserControl
  31. {
  32. public event RecordSelectedHandler RecordSelected;
  33. private BackgroundWorker worker;
  34. private Map myMap;
  35. private delegate void RenderMapDelegate(string url);
  36. private delegate void SimpleDelegate();
  37. private delegate void DebugDelegate(string debugMsg);
  38. private const string SatelliteViewURL = "http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_Imagery_World_2D/MapServer";
  39. private const string StreetViewURL = "http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer";
  40. public MapControl()
  41. {
  42. InitializeComponent();
  43. worker = new System.ComponentModel.BackgroundWorker();
  44. worker.DoWork += new DoWorkEventHandler(worker_DoWork);
  45. worker.RunWorkerAsync();
  46. }
  47. void worker_DoWork(object sender, DoWorkEventArgs e)
  48. {
  49. if (InternetAvailable())
  50. {
  51. this.Dispatcher.BeginInvoke(new SimpleDelegate(PrepareToLoad));
  52. System.Threading.Thread.Sleep(6000);
  53. this.Dispatcher.BeginInvoke(new SimpleDelegate(RenderMap));
  54. }
  55. }
  56. private void PrepareToLoad()
  57. {
  58. txtInternet.Visibility = Visibility.Collapsed;
  59. txtLoading.Visibility = Visibility.Visible;
  60. waitCursor.Visibility = Visibility.Visible;
  61. }
  62. private void RenderMap()
  63. {
  64. txtLoading.Visibility = Visibility.Collapsed;
  65. waitCursor.Visibility = Visibility.Collapsed;
  66. LayerTypeSelector.Visibility = Visibility.Visible;
  67. ArcGISTiledMapServiceLayer layer = new ArcGISTiledMapServiceLayer()
  68. {
  69. Url = SatelliteViewURL
  70. };
  71. myMap = new Map()
  72. {
  73. Background = Brushes.White,
  74. Extent = new Envelope(-154.16, 82.44, -17.05, -50)
  75. };
  76. myMap.Layers.Add(layer);
  77. myMap.MouseMove += new MouseEventHandler(myMap_MouseMove);
  78. MapContainer.Children.Add(myMap);
  79. }
  80. public void UnSubscribe()
  81. {
  82. if(myMap != null)
  83. {
  84. myMap.MouseMove -= new MouseEventHandler(myMap_MouseMove);
  85. }
  86. this.myMap = null;
  87. this.worker = null;
  88. }
  89. private void RadioButton_Click(object sender, RoutedEventArgs e)
  90. {
  91. if (myMap != null)
  92. {
  93. if (myMap.Layers.Count > 0)
  94. {
  95. if (ImageryRadioButton.IsChecked.Value)
  96. {
  97. ((ArcGISTiledMapServiceLayer)myMap.Layers[0]).Url = SatelliteViewURL;
  98. }
  99. else
  100. {
  101. ((ArcGISTiledMapServiceLayer)myMap.Layers[0]).Url = StreetViewURL;
  102. }
  103. }
  104. }
  105. }
  106. private Symbol YellowMarkerSymbol
  107. {
  108. get
  109. {
  110. SimpleMarkerSymbol symbol = new SimpleMarkerSymbol();
  111. symbol.Color = Brushes.Yellow;
  112. symbol.Size = 15;
  113. symbol.Style = SimpleMarkerSymbol.SimpleMarkerStyle.Circle;
  114. return symbol;
  115. }
  116. }
  117. public void RenderClusterMap(View view, IDbDriver db, string latVar, string longVar)
  118. {
  119. CustomCoordinateList coordinateList = GetCoordinates(view, db, latVar, longVar);
  120. GraphicsLayer graphicsLayer = new GraphicsLayer();
  121. for (int i = 0; i < coordinateList.Coordinates.Count; i++)
  122. {
  123. ExtendedGraphic graphic = new ExtendedGraphic()
  124. {
  125. Geometry = new MapPoint(coordinateList.Coordinates[i].X, coordinateList.Coordinates[i].Y),
  126. RecordId = coordinateList.Coordinates[i].RecordId,
  127. Symbol = YellowMarkerSymbol
  128. };
  129. graphic.MouseLeftButtonUp += new MouseButtonEventHandler(graphic_MouseLeftButtonUp);
  130. graphicsLayer.Graphics.Add(graphic);
  131. }
  132. FlareClusterer clusterer = new FlareClusterer()
  133. {
  134. FlareBackground = Brushes.Yellow,
  135. FlareForeground = new SolidColorBrush(Color.FromArgb(0x99, 0, 0, 0)),
  136. MaximumFlareCount = 10,
  137. Radius = 15,
  138. Gradient = ClustererGradient
  139. };
  140. graphicsLayer.Clusterer = clusterer;
  141. myMap.Layers.Add(graphicsLayer);
  142. object objMinX = db.ExecuteScalar(db.CreateQuery("SELECT MIN(" + longVar + ") FROM " + view.TableName));
  143. object objMinY = db.ExecuteScalar(db.CreateQuery("SELECT MIN(" + latVar + ") FROM " + view.TableName));
  144. object objMaxX = db.ExecuteScalar(db.CreateQuery("SELECT MAX(" + longVar + ") FROM " + view.TableName));
  145. object objMaxY = db.ExecuteScalar(db.CreateQuery("SELECT MAX(" + latVar + ") FROM " + view.TableName));
  146. if (objMaxX != DBNull.Value && objMaxY != DBNull.Value && objMinX != DBNull.Value && objMinY != DBNull.Value)
  147. {
  148. double minX = double.Parse(objMinX.ToString());
  149. double minY = double.Parse(objMinY.ToString());
  150. double maxX = double.Parse(objMaxX.ToString());
  151. double maxY = double.Parse(objMaxY.ToString());
  152. myMap.Extent = new Envelope(minX - 0.01, minY - 0.01, maxX + 0.01, maxY + 0.01);
  153. }
  154. }
  155. private CustomCoordinateList GetCoordinates(View view, IDbDriver db, string latVar, string longVar)
  156. {
  157. DataTable data = db.Select(db.CreateQuery("SELECT UniqueKey, " + latVar + ", " + longVar + " FROM " + view.TableName));
  158. CustomCoordinateList coordinateList = new CustomCoordinateList();
  159. foreach (DataRow row in data.Rows)
  160. {
  161. if (row[latVar] != DBNull.Value && row[longVar] != DBNull.Value)
  162. {
  163. double latitude = double.Parse(row[latVar].ToString());
  164. double longitude = double.Parse(row[longVar].ToString());
  165. if (latitude <= 180 && latitude >= -180 && longitude <= 90 && longitude >= -90)
  166. coordinateList.Coordinates.Add(new CustomCoordinate((int)row["UniqueKey"], latitude, longitude));
  167. }
  168. }
  169. return coordinateList;
  170. }
  171. void graphic_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  172. {
  173. int recordId = ((ExtendedGraphic)sender).RecordId;
  174. if (RecordSelected != null)
  175. {
  176. RecordSelected(recordId);
  177. }
  178. }
  179. void myMap_MouseMove(object sender, MouseEventArgs e)
  180. {
  181. Map myMap = (Map)e.Source;
  182. if (!myMap.IsFocused)
  183. myMap.Focus();
  184. }
  185. private LinearGradientBrush ClustererGradient
  186. {
  187. get
  188. {
  189. LinearGradientBrush brush = new LinearGradientBrush();
  190. brush.MappingMode = BrushMappingMode.RelativeToBoundingBox;
  191. brush.GradientStops.Add(new GradientStop(Color.FromArgb(0x99, 0xFF, 0x11, 0), 0));
  192. brush.GradientStops.Add(new GradientStop(Color.FromArgb(0x99, 0xFF, 0x55, 0), 0.25));
  193. brush.GradientStops.Add(new GradientStop(Color.FromArgb(0x99, 0xFF, 0x99, 0), 0.5));
  194. brush.GradientStops.Add(new GradientStop(Color.FromArgb(0x99, 0xFF, 0xCC, 0), 0.75));
  195. brush.GradientStops.Add(new GradientStop(Color.FromArgb(0x99, 0xFF, 0xFF, 0), 1));
  196. return brush;
  197. }
  198. }
  199. private bool InternetAvailable()
  200. {
  201. bool retval = false;
  202. try
  203. {
  204. HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://www.google.com");
  205. HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  206. retval = true;
  207. //this.Dispatcher.Invoke(new DebugDelegate(ShowDebugMessage), retval);
  208. }
  209. catch (Exception ex)
  210. {
  211. }
  212. return retval;
  213. }
  214. private void ShowDebugMessage(string debugMsg)
  215. {
  216. MessageBox.Show(debugMsg);
  217. }
  218. private void ColorBlendCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
  219. {
  220. }
  221. private void ClassCountCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
  222. {
  223. }
  224. private void LoadShapeButton_Click(object sender, RoutedEventArgs e)
  225. {
  226. }
  227. private void LoadDataButton_Click(object sender, RoutedEventArgs e)
  228. {
  229. }
  230. void Triangle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  231. {
  232. }
  233. }
  234. public class CustomCoordinateList
  235. {
  236. public List<CustomCoordinate> Coordinates = new List<CustomCoordinate>();
  237. }
  238. public class CustomCoordinate
  239. {
  240. public CustomCoordinate() { }
  241. public CustomCoordinate(int recordId, double y, double x)
  242. {
  243. this.RecordId = recordId;
  244. this.X = x;
  245. this.Y = y;
  246. }
  247. public int RecordId { get; set; }
  248. public double X { get; set; }
  249. public double Y { get; set; }
  250. }
  251. public class ExtendedGraphic : Graphic
  252. {
  253. public int RecordId { get; set; }
  254. }
  255. }