PageRenderTime 221ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/Renci.Wwt.DataManager.NetCDF/Models/ShapeDataSourceInfo.cs

#
C# | 267 lines | 186 code | 31 blank | 50 comment | 16 complexity | 2d5cd22549cb0786396f2e4e27441796 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Renci.Wwt.DataManager.Common.Models;
  6. using System.Xml.Linq;
  7. using System.ComponentModel;
  8. using Renci.Wwt.DataManager.Common.Editors;
  9. using Microsoft.Windows.Controls.PropertyGrid;
  10. using Renci.Wwt.Core;
  11. using System.IO;
  12. using Renci.Wwt.Core.Frames.Layers.Spreadsheet;
  13. using System.Windows.Media;
  14. namespace Renci.Wwt.DataManager.NetCDF.Models
  15. {
  16. public class ShapeDataSourceInfo : DataSourceInfo
  17. {
  18. private List<DataItem> _dataItems;
  19. [CategoryAttribute("Shape File"),
  20. DisplayName("File Path"),
  21. DescriptionAttribute("Shape file path")]
  22. [PropertyEditor(typeof(PathSelectionEditor))]
  23. public string Path
  24. {
  25. get { return this.Get(() => this.Path); }
  26. set { this.Set(() => this.Path, value); }
  27. }
  28. [CategoryAttribute("Shape File"),
  29. DisplayName("Color"),
  30. DescriptionAttribute("Elements variable name, must be double array of integers.")]
  31. public Color Color
  32. {
  33. get { return this.Get(() => this.Color); }
  34. set { this.Set(() => this.Color, value); }
  35. }
  36. public ShapeDataSourceInfo()
  37. {
  38. }
  39. /// <summary>
  40. /// Initializes a new instance of the <see cref="VariableNetCDFDataSourceInfo"/> class.
  41. /// </summary>
  42. /// <param name="id">The id.</param>
  43. /// <param name="name">The name.</param>
  44. public ShapeDataSourceInfo(Guid id, string name)
  45. : base(id, name)
  46. {
  47. }
  48. public override int GetDataCount()
  49. {
  50. if (!File.Exists(this.Path))
  51. return 0;
  52. this._dataItems = this.readShapeFile(this.Path);
  53. return this._dataItems.Count;
  54. }
  55. public override IEnumerable<Core.DataItem> GetData()
  56. {
  57. return this._dataItems;
  58. }
  59. protected override System.Xml.Linq.XElement SaveAttributes()
  60. {
  61. return new XElement("shape",
  62. new XAttribute("path", this.Path),
  63. new XAttribute("color", this.Color)
  64. );
  65. }
  66. protected override void LoadAttributes(System.Xml.Linq.XElement element)
  67. {
  68. if (!element.Name.LocalName.Equals("shape", StringComparison.InvariantCulture))
  69. throw new InvalidOperationException("'shape' element expected.");
  70. if (element.Attribute("path") != null)
  71. this.Path = element.Attribute("path").Value;
  72. if (element.Attribute("color") != null)
  73. this.Color = (Color)ColorConverter.ConvertFromString(element.Attribute("color").Value);
  74. }
  75. public List<DataItem> readShapeFile(string filename)
  76. {
  77. List<DataItem> dataItems = new List<DataItem>();
  78. FileStream fs = new FileStream(filename, FileMode.Open);
  79. long fileLength = fs.Length;
  80. Byte[] data = new Byte[fileLength];
  81. fs.Read(data, 0, (int)fileLength);
  82. fs.Close();
  83. var filecode = readIntBig(data, 0);
  84. var filelength = readIntBig(data, 24);
  85. var version = readIntLittle(data, 28);
  86. var shapetype = readIntLittle(data, 32);
  87. var xMin = ReadSignedDouble(data, 36);
  88. var yMin = ReadSignedDouble(data, 44);
  89. yMin = 0 - yMin;
  90. var xMax = ReadSignedDouble(data, 52);
  91. var yMax = ReadSignedDouble(data, 60);
  92. yMax = 0 - yMax;
  93. var zMin = ReadSignedDouble(data, 68);
  94. var zMax = ReadSignedDouble(data, 76);
  95. var mMin = ReadSignedDouble(data, 84);
  96. var mMax = ReadSignedDouble(data, 92);
  97. int currentPosition = 100;
  98. while (currentPosition < fileLength)
  99. {
  100. DataItem dataItem = null;
  101. int recordStart = currentPosition;
  102. int recordNumber = readIntBig(data, recordStart);
  103. int contentLength = readIntBig(data, recordStart + 4);
  104. int recordContentStart = recordStart + 8;
  105. if (shapetype == 1)
  106. {
  107. int recordShapeType = readIntLittle(data, recordContentStart);
  108. var lon = (float)ReadSignedDouble(data, recordContentStart + 4);
  109. var lat = (float)ReadSignedDouble(data, recordContentStart + 12);
  110. dataItem = new SpreadsheetDataItem(string.Empty, DateTime.Now.AddHours(-1), DateTime.Now.AddHours(-1 + 1),
  111. lat,
  112. lon,
  113. 0,
  114. 1,
  115. System.Drawing.Color.FromArgb(this.Color.A, this.Color.R, this.Color.G, this.Color.B),
  116. string.Empty,
  117. string.Empty
  118. );
  119. }
  120. else if (shapetype == 3)
  121. {
  122. var geometry = new StringBuilder();
  123. geometry.Append("LINESTRING ((");
  124. //Line line = new Line();
  125. int recordShapeType = readIntLittle(data, recordContentStart);
  126. //line.box = new Double[4];
  127. //line.box[0] = readDoubleLittle(data, recordContentStart + 4);
  128. //line.box[1] = readDoubleLittle(data, recordContentStart + 12);
  129. //line.box[2] = readDoubleLittle(data, recordContentStart + 20);
  130. //line.box[3] = readDoubleLittle(data, recordContentStart + 28);
  131. ReadSignedDouble(data, recordContentStart + 4);
  132. ReadSignedDouble(data, recordContentStart + 12);
  133. ReadSignedDouble(data, recordContentStart + 20);
  134. ReadSignedDouble(data, recordContentStart + 28);
  135. //line.numParts = readIntLittle(data, recordContentStart + 36);
  136. var numberOfParts = readIntLittle(data, recordContentStart + 36);
  137. //line.parts = new int[line.numParts];
  138. //line.numPoints = readIntLittle(data, recordContentStart + 40);
  139. var numberOfPoints = readIntLittle(data, recordContentStart + 40);
  140. //line.points = new PointF[line.numPoints];
  141. int partStart = recordContentStart + 44;
  142. for (int i = 0; i < numberOfParts; i++)
  143. {
  144. //line.parts[i] = readIntLittle(data, partStart + i * 4);
  145. readIntLittle(data, partStart + i * 4);
  146. }
  147. int pointStart = recordContentStart + 44 + 4 * numberOfParts;
  148. //for (int i = 0; i < line.numPoints; i++)
  149. //{
  150. // line.points[i].X = (float)readDoubleLittle(data, pointStart + (i * 16));
  151. // line.points[i].Y = (float)readDoubleLittle(data, pointStart + (i * 16) + 8);
  152. // line.points[i].Y = 0 - line.points[i].Y;
  153. //}
  154. //lines.Add(line);
  155. for (int i = 0; i < numberOfPoints; i++)
  156. {
  157. var lon = (float)ReadSignedDouble(data, pointStart + (i * 16));
  158. var lat = (float)ReadSignedDouble(data, pointStart + (i * 16) + 8);
  159. var alt = 0f;
  160. geometry.AppendFormat("{0} {1} {2},", lon, lat, alt);
  161. }
  162. geometry.Append(") 0)");
  163. dataItem = new SpreadsheetDataItem(string.Empty, DateTime.Now.AddHours(-1), DateTime.Now.AddHours(-1 + 1),
  164. geometry.ToString(),
  165. System.Drawing.Color.FromArgb(this.Color.A, this.Color.R, this.Color.G, this.Color.B),
  166. string.Empty,
  167. string.Empty
  168. );
  169. }
  170. //else if (shapetype == 5)
  171. //{
  172. // Polygon polygon = new Polygon();
  173. // int recordShapeType = readIntLittle(data, recordContentStart);
  174. // polygon.box = new Double[4];
  175. // polygon.box[0] = readDoubleLittle(data, recordContentStart + 4);
  176. // polygon.box[1] = readDoubleLittle(data, recordContentStart + 12);
  177. // polygon.box[2] = readDoubleLittle(data, recordContentStart + 20);
  178. // polygon.box[3] = readDoubleLittle(data, recordContentStart + 28);
  179. // polygon.numParts = readIntLittle(data, recordContentStart + 36);
  180. // polygon.parts = new int[polygon.numParts];
  181. // polygon.numPoints = readIntLittle(data, recordContentStart + 40);
  182. // polygon.points = new PointF[polygon.numPoints];
  183. // int partStart = recordContentStart + 44;
  184. // for (int i = 0; i < polygon.numParts; i++)
  185. // {
  186. // polygon.parts[i] = readIntLittle(data, partStart + i * 4);
  187. // }
  188. // int pointStart = recordContentStart + 44 + 4 * polygon.numParts;
  189. // for (int i = 0; i < polygon.numPoints; i++)
  190. // {
  191. // polygon.points[i].X = (float)readDoubleLittle(data, pointStart + (i * 16));
  192. // polygon.points[i].Y = (float)readDoubleLittle(data, pointStart + (i * 16) + 8);
  193. // polygon.points[i].Y = 0 - polygon.points[i].Y;
  194. // }
  195. // polygons.Add(polygon);
  196. //}
  197. currentPosition = recordStart + (4 + contentLength) * 2;
  198. if (dataItem != null)
  199. dataItems.Add(dataItem);
  200. }
  201. return dataItems;
  202. }
  203. public int readIntBig(byte[] data, int pos)
  204. {
  205. byte[] bytes = new byte[4];
  206. bytes[0] = data[pos];
  207. bytes[1] = data[pos + 1];
  208. bytes[2] = data[pos + 2];
  209. bytes[3] = data[pos + 3];
  210. Array.Reverse(bytes);
  211. return BitConverter.ToInt32(bytes, 0);
  212. }
  213. public int readIntLittle(byte[] data, int pos)
  214. {
  215. byte[] bytes = new byte[4];
  216. bytes[0] = data[pos];
  217. bytes[1] = data[pos + 1];
  218. bytes[2] = data[pos + 2];
  219. bytes[3] = data[pos + 3];
  220. return BitConverter.ToInt32(bytes, 0);
  221. }
  222. public double ReadSignedDouble(byte[] data, int pos)
  223. {
  224. byte[] bytes = new byte[8];
  225. bytes[0] = data[pos];
  226. bytes[1] = data[pos + 1];
  227. bytes[2] = data[pos + 2];
  228. bytes[3] = data[pos + 3];
  229. bytes[4] = data[pos + 4];
  230. bytes[5] = data[pos + 5];
  231. bytes[6] = data[pos + 6];
  232. bytes[7] = data[pos + 7];
  233. return BitConverter.ToDouble(bytes, 0);
  234. }
  235. }
  236. }