PageRenderTime 32ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/GeoRouting/branches/SharpMapUITest/SharpMapUITest/GoogleEarthKML.cs

http://georouting-hyperpath.googlecode.com/
C# | 233 lines | 180 code | 28 blank | 25 comment | 12 complexity | 44293a22e74fdd39ded342865457dfdb MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Google.KML;
  6. using ProjNet.CoordinateSystems;
  7. using Npgsql;
  8. using System.Data;
  9. using System.IO;
  10. using ProjNet.CoordinateSystems.Transformations;
  11. namespace SharpMapUITest
  12. {
  13. class GoogleEarthKML
  14. {
  15. geDocument doc = new geDocument();
  16. string ConnStr;
  17. string[] lyrNames;
  18. string path;
  19. public GoogleEarthKML(string _ConnStr, string[] _lyrName,string _path)
  20. {
  21. ConnStr = _ConnStr;
  22. lyrNames = _lyrName;
  23. path = _path;
  24. }
  25. public bool WriteKMLFile()
  26. {
  27. byte[] bytesToWrite = GenKML().ToKML();
  28. try
  29. {
  30. File.WriteAllBytes(path, bytesToWrite);
  31. return true;
  32. }
  33. catch (IOException){ return false; }
  34. }
  35. private geKML GenKML()
  36. {
  37. List<geCoordinates> linepoints = null;
  38. using (NpgsqlConnection conn = new NpgsqlConnection(ConnStr))
  39. {
  40. //???????Style????
  41. // p >= 0.5
  42. //Lets add a Line somewhere too...
  43. geStyle myLineStyle1 = new geStyle("myLineStyle1");
  44. myLineStyle1.LineStyle = new geLineStyle();
  45. myLineStyle1.LineStyle.Color.SysColor = System.Drawing.Color.Green;
  46. myLineStyle1.LineStyle.Width = 8; //This may or may not work, depends on the end user's video card
  47. doc.StyleSelectors.Add(myLineStyle1);
  48. // 0.2 =< p < 0.5
  49. geStyle myLineStyle2 = new geStyle("myLineStyle2");
  50. myLineStyle2.LineStyle = new geLineStyle();
  51. myLineStyle2.LineStyle.Color.SysColor = System.Drawing.Color.Blue;
  52. myLineStyle2.LineStyle.Width = 5; //This may or may not work, depends on the end user's video card
  53. doc.StyleSelectors.Add(myLineStyle2);
  54. // p < 0.2
  55. geStyle myLineStyle3 = new geStyle("myLineStyle3");
  56. myLineStyle3.LineStyle = new geLineStyle();
  57. myLineStyle3.LineStyle.Color.SysColor = System.Drawing.Color.Red;
  58. myLineStyle3.LineStyle.Width = 3; //This may or may not work, depends on the end user's video card
  59. doc.StyleSelectors.Add(myLineStyle3);
  60. geStyle normalLightBlue = new geStyle("normalLightBlue");
  61. normalLightBlue.LineStyle = new geLineStyle();
  62. normalLightBlue.LineStyle.Color.SysColor = System.Drawing.Color.LightBlue;
  63. normalLightBlue.LineStyle.Width = 1.5f; //This may or may not work, depends on the end user's video card
  64. doc.StyleSelectors.Add(normalLightBlue);
  65. geStyle normalRed = new geStyle("normalRed");
  66. normalRed.LineStyle = new geLineStyle();
  67. normalRed.LineStyle.Color.SysColor = System.Drawing.Color.Red;
  68. normalRed.LineStyle.Width = 3; //This may or may not work, depends on the end user's video card
  69. doc.StyleSelectors.Add(normalRed);
  70. NpgsqlCommand cmd = null;
  71. NpgsqlDataReader reader = null;
  72. //????????????????kml?
  73. foreach (string lyrName in lyrNames)
  74. {
  75. if (conn.State == ConnectionState.Closed) conn.Open();
  76. cmd = new NpgsqlCommand("select gid,ST_ASEWKT(ST_Transform(the_geom,4326)),choice_possibility from " + lyrName + ";", conn);
  77. reader = cmd.ExecuteReader();
  78. while (reader.Read())
  79. {
  80. int gid = reader.GetInt32(reader.GetOrdinal("gid"));
  81. string geomwkt = reader.GetString(reader.GetOrdinal("st_asewkt"));
  82. string[] points = geomwkt.Substring(geomwkt.IndexOf("((") + 2, geomwkt.Length - geomwkt.IndexOf("((") - 4).Split(',');
  83. linepoints = new List<geCoordinates>();
  84. foreach (string pointstr in points)
  85. {
  86. double[] point = new double[2];
  87. var pointcor = pointstr.Split(' ');
  88. //??
  89. point[0] = Convert.ToDouble(pointcor[0]);
  90. //??
  91. point[1] = Convert.ToDouble(pointcor[1]);
  92. linepoints.Add(new geCoordinates(new geAngle90(point[1]), new geAngle180(point[0])));
  93. }
  94. gePlacemark pmLine = new gePlacemark();
  95. if (lyrName == "hyperpath_lyr")
  96. {
  97. double choice_possibility = reader.GetDouble(reader.GetOrdinal("choice_possibility"));
  98. if (choice_possibility >= 0.5)
  99. pmLine.StyleUrl = "myLineStyle1";
  100. else if (choice_possibility >= 0.2)
  101. pmLine.StyleUrl = "myLineStyle2";
  102. else pmLine.StyleUrl = "myLineStyle3";
  103. pmLine.Name = "ID: " + gid.ToString() + " Possibility: " + Math.Round(choice_possibility, 2).ToString();
  104. pmLine.Description = "This is a " + lyrNames + "link";
  105. }
  106. else
  107. {
  108. if(lyrName=="popath_lyr")
  109. pmLine.StyleUrl = "normalLightBlue";
  110. else if (lyrName=="regretpath_lyr")
  111. pmLine.StyleUrl = "normalRed";
  112. pmLine.Name = "ID: " + gid.ToString();
  113. pmLine.Description = "This is a " + lyrName + " link";
  114. }
  115. //gePlacemark hyperpath = new gePlacemark();
  116. pmLine.Geometry = new geLineString(linepoints);
  117. pmLine.ID = (doc.Features.Count + 1).ToString();
  118. doc.Features.Add(pmLine);
  119. }
  120. }
  121. reader.Close();
  122. cmd.Dispose();
  123. geKML kml = new geKML(doc);
  124. return kml;
  125. }
  126. }
  127. private geKML GetKML(int gid, string wkt, double p)
  128. {
  129. // Use a Document as the root of the KML
  130. geDocument doc = new geDocument();
  131. doc.Name = "My Root Document";
  132. //Create a Placemark to put in the document
  133. //This placemark is going to be a point
  134. //but it could be anything in the Geometry class
  135. gePlacemark pm = new gePlacemark();
  136. //Create some coordinates for the point at which
  137. //this placemark will sit. (Lat / Lon)
  138. geCoordinates coords = new geCoordinates(
  139. new geAngle90(37.422067),
  140. new geAngle180(-122.084437));
  141. //Create a point with these new coordinates
  142. gePoint point = new gePoint(coords);
  143. //Assign the point to the Geometry property of the
  144. //placemark.
  145. pm.Geometry = point;
  146. //Now lets add some other properties to our placemark
  147. pm.Name = "My Placemark";
  148. pm.Snippet = "This is where I put my Placemark";
  149. pm.Description =
  150. "I wonder where this is...";
  151. //Finally, add the placemark to the document
  152. doc.Features.Add(pm);
  153. //Now that we have our document, lets create our KML
  154. geKML kml = new geKML(doc);
  155. return kml;
  156. }
  157. #region ???????Proj4??????????????????????????????postgis??????
  158. private IProjectedCoordinateSystem CreateUtmProjection(int utmZone)
  159. {
  160. CoordinateSystemFactory cFac = new ProjNet.CoordinateSystems.CoordinateSystemFactory();
  161. IEllipsoid ellipsoid = cFac.CreateFlattenedSphere("WGS 84",
  162. 6378137, 298.257223563, LinearUnit.Metre);
  163. IHorizontalDatum datum = cFac.CreateHorizontalDatum("WGS_1984",
  164. DatumType.HD_Geocentric, ellipsoid, null);
  165. IGeographicCoordinateSystem gcs = cFac.CreateGeographicCoordinateSystem(
  166. "WGS 84", AngularUnit.Degrees, datum,
  167. PrimeMeridian.Greenwich,
  168. new AxisInfo("Lon", AxisOrientationEnum.East),
  169. new AxisInfo("Lat", AxisOrientationEnum.North));
  170. //Create UTM projection
  171. List<ProjectionParameter> parameters = new List<ProjectionParameter>(5);
  172. parameters.Add(new ProjectionParameter("latitude_of_origin", 0));
  173. parameters.Add(new ProjectionParameter("central_meridian", -183 + 6 * utmZone));
  174. parameters.Add(new ProjectionParameter("scale_factor", 0.9996));
  175. parameters.Add(new ProjectionParameter("false_easting", 500000));
  176. parameters.Add(new ProjectionParameter("false_northing", 0.0));
  177. IProjection projection = cFac.CreateProjection(
  178. "Transverse Mercator", "Transverse_Mercator", parameters);
  179. return cFac.CreateProjectedCoordinateSystem(
  180. "WGS 84 / UTM zone " + utmZone.ToString() + "N", gcs,
  181. projection, LinearUnit.Metre,
  182. new AxisInfo("East", AxisOrientationEnum.East),
  183. new AxisInfo("North", AxisOrientationEnum.North));
  184. }
  185. private ICoordinateSystem CreateCoordinateSystemFromWKT(string wkt)
  186. {
  187. CoordinateSystemFactory cFac = new CoordinateSystemFactory();
  188. return cFac.CreateFromWkt(wkt);
  189. }
  190. private double[] Proj2WGS(int utm, double[] point)
  191. {
  192. //Create zone UTM 32N projection
  193. IProjectedCoordinateSystem utmProj = CreateUtmProjection(54);
  194. //Create geographic coordinate system (lets just reuse the CS from the projection)
  195. IGeographicCoordinateSystem geoCS = utmProj.GeographicCoordinateSystem;
  196. //Create transformation
  197. CoordinateTransformationFactory ctFac = new CoordinateTransformationFactory();
  198. ICoordinateTransformation transform =
  199. ctFac.CreateFromCoordinateSystems(utmProj, geoCS);
  200. double[] CRSCoor = transform.MathTransform.Transform(point);
  201. return CRSCoor;
  202. }
  203. #endregion
  204. }
  205. }