| 1 | //␊ |
| 2 | // csmapcontrol - a C# map control - http://projects.ciarang.com/p/csmapcontrol␊ |
| 3 | // Copyright (C) 2010-11, Ciaran Gultnieks␊ |
| 4 | //␊ |
| 5 | // This program is free software: you can redistribute it and/or modify␊ |
| 6 | // it under the terms of the GNU Affero General Public License as published by␊ |
| 7 | // the Free Software Foundation, either version 3 of the License, or␊ |
| 8 | // (at your option) any later version.␊ |
| 9 | //␊ |
| 10 | // This program is distributed in the hope that it will be useful,␊ |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of␊ |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the␊ |
| 13 | // GNU Affero General Public License for more details.␊ |
| 14 | //␊ |
| 15 | // You should have received a copy of the GNU Affero General Public License␊ |
| 16 | // along with this program. If not, see <http://www.gnu.org/licenses/>. ␊ |
| 17 | ␊ |
| 18 | using System;␊ |
| 19 | using System.Collections.Generic;␊ |
| 20 | using System.Xml;␊ |
| 21 | using System.Drawing;␊ |
| 22 | using System.Text;␊ |
| 23 | using System.IO;␊ |
| 24 | ␊ |
| 25 | namespace csmapcontrol␊ |
| 26 | {␊ |
| 27 | ␊ |
| 28 | ␉public class TrackPoint : ICloneable␊ |
| 29 | ␉{␊ |
| 30 | ␉␉public double Lat;␊ |
| 31 | ␉␉public double Lon;␊ |
| 32 | ␉␉public double Elevation;␊ |
| 33 | ␉␉public DateTime Time;␊ |
| 34 | ␊ |
| 35 | ␉␉public object Clone()␊ |
| 36 | ␉␉{␊ |
| 37 | ␉␉␉TrackPoint copy=new TrackPoint();␊ |
| 38 | ␉␉␉copy.Lat=Lat;␊ |
| 39 | ␉␉␉copy.Lon=Lon;␊ |
| 40 | ␉␉␉copy.Elevation=Elevation;␊ |
| 41 | ␉␉␉copy.Time=Time;␊ |
| 42 | ␉␉␉return copy;␊ |
| 43 | ␉␉}␊ |
| 44 | ␊ |
| 45 | ␉}␊ |
| 46 | ␊ |
| 47 | ␉public class Track␊ |
| 48 | ␉{␊ |
| 49 | ␊ |
| 50 | ␉␉public List<TrackPoint> Points;␊ |
| 51 | ␉␉public string Name;␊ |
| 52 | ␊ |
| 53 | ␉␉/// <summary>␊ |
| 54 | ␉␉/// Create a new empty Track.␊ |
| 55 | ␉␉/// </summary>␊ |
| 56 | ␉␉public Track()␊ |
| 57 | ␉␉{␊ |
| 58 | ␉␉␉Points=new List<TrackPoint>();␊ |
| 59 | ␉␉}␊ |
| 60 | ␊ |
| 61 | ␉␉/// <summary>␊ |
| 62 | ␉␉/// Create a new Track by loading from a GPX file.␊ |
| 63 | ␉␉/// </summary>␊ |
| 64 | ␉␉/// <param name="filespec">The filespec of a GPX file.</param>␊ |
| 65 | ␉␉public Track(string gpxfile)␊ |
| 66 | ␉␉{␊ |
| 67 | ␉␉␉Points=new List<TrackPoint>();␊ |
| 68 | ␉␉␉XmlDocument x=new XmlDocument();␊ |
| 69 | ␉␉␉x.Load(gpxfile);␊ |
| 70 | ␉␉␉if(x.DocumentElement.Name!="gpx")␊ |
| 71 | ␉␉␉␉throw new ApplicationException("Document element should be 'gpx'");␊ |
| 72 | ␉␉␉string ns="http://www.topografix.com/GPX/1/0";␊ |
| 73 | ␉␉␉string ver=x.DocumentElement.GetAttribute("version");␊ |
| 74 | ␉␉␉if(ver!=null && ver=="1.1")␊ |
| 75 | ␉␉␉␉ns="http://www.topografix.com/GPX/1/1";␊ |
| 76 | ␉␉␉XmlNamespaceManager nsmgr=new XmlNamespaceManager(x.NameTable);␊ |
| 77 | ␉␉␉nsmgr.AddNamespace("gpx", ns);␊ |
| 78 | ␉␉␉if(x.GetElementsByTagName("name").Count>0)␊ |
| 79 | ␉␉␉␉Name=x.GetElementsByTagName("name")[0].InnerText;␊ |
| 80 | ␉␉␉else␊ |
| 81 | ␉␉␉␉Name=Path.GetFileNameWithoutExtension(gpxfile);␊ |
| 82 | ␉␉␉foreach(XmlNode point in x.SelectNodes("//gpx:trkpt",nsmgr))␊ |
| 83 | ␉␉␉{␊ |
| 84 | ␉␉␉␉TrackPoint p=new TrackPoint();␊ |
| 85 | ␉␉␉␉p.Lat=double.Parse(point.SelectSingleNode("@lat").Value);␊ |
| 86 | ␉␉␉␉p.Lon=double.Parse(point.SelectSingleNode("@lon").Value);␊ |
| 87 | ␉␉␉␉p.Elevation=double.Parse(point.SelectSingleNode("gpx:ele",nsmgr).InnerText);␊ |
| 88 | ␉␉␉␉p.Time=DateTime.Parse(point.SelectSingleNode("gpx:time",nsmgr).InnerText).ToUniversalTime();␊ |
| 89 | ␉␉␉␉Points.Add(p);␊ |
| 90 | ␉␉␉}␊ |
| 91 | ␉␉}␊ |
| 92 | ␊ |
| 93 | ␉␉/// <summary>␊ |
| 94 | ␉␉/// Get a GPX representation of the track data.␊ |
| 95 | ␉␉/// </summary>␊ |
| 96 | ␉␉/// <returns>GPX data. (UTF-16 encoded)</returns>␊ |
| 97 | ␉␉public string ToGPX()␊ |
| 98 | ␉␉{␊ |
| 99 | ␉␉␉const string gpx="http://www.topografix.com/GPX/1/1";␊ |
| 100 | ␉␉␉StringBuilder sb=new StringBuilder();␊ |
| 101 | ␉␉␉using(XmlWriter writer=XmlWriter.Create(sb))␊ |
| 102 | ␉␉␉{␊ |
| 103 | ␉␉␉␉writer.WriteStartElement("gpx",gpx);␊ |
| 104 | ␉␉␉␉writer.WriteAttributeString("version","1.1");␊ |
| 105 | ␉␉␉␉writer.WriteAttributeString("creator","CSMapControl");␊ |
| 106 | ␊ |
| 107 | ␉␉␉␉Bounds b=CalculateBounds();␊ |
| 108 | ␉␉␉␉writer.WriteStartElement("bounds",gpx);␊ |
| 109 | ␉␉␉␉writer.WriteAttributeString("minlat",b.MinLat.ToString());␊ |
| 110 | ␉␉␉␉writer.WriteAttributeString("maxlat",b.MaxLat.ToString());␉␉␉␉␊ |
| 111 | ␉␉␉␉writer.WriteAttributeString("minlon",b.MinLon.ToString());␊ |
| 112 | ␉␉␉␉writer.WriteAttributeString("maxlon",b.MaxLon.ToString());␊ |
| 113 | ␊ |
| 114 | ␉␉␉␉writer.WriteStartElement("trk",gpx);␊ |
| 115 | ␉␉␉␉writer.WriteStartElement("name",gpx);␊ |
| 116 | ␉␉␉␉writer.WriteValue(Name);␊ |
| 117 | ␉␉␉␉writer.WriteEndElement();␊ |
| 118 | ␊ |
| 119 | ␉␉␉␉writer.WriteStartElement("trkseg",gpx);␊ |
| 120 | ␉␉␉␉foreach(TrackPoint p in Points)␊ |
| 121 | ␉␉␉␉{␊ |
| 122 | ␉␉␉␉␉writer.WriteStartElement("trkpt",gpx);␊ |
| 123 | ␉␉␉␉␉writer.WriteAttributeString("lat",p.Lat.ToString());␊ |
| 124 | ␉␉␉␉␉writer.WriteAttributeString("lon",p.Lon.ToString());␊ |
| 125 | ␉␉␉␉␉writer.WriteStartElement("ele",gpx);␊ |
| 126 | ␉␉␉␉␉writer.WriteValue(p.Elevation.ToString());␊ |
| 127 | ␉␉␉␉␉writer.WriteEndElement();␊ |
| 128 | ␉␉␉␉␉writer.WriteStartElement("time",gpx);␊ |
| 129 | ␉␉␉␉␉writer.WriteValue(p.Time.ToString("s")+"Z");␊ |
| 130 | ␉␉␉␉␉writer.WriteEndElement();␉␉␉␉␉␊ |
| 131 | ␉␉␉␉␉writer.WriteEndElement();␊ |
| 132 | ␉␉␉␉}␊ |
| 133 | ␉␉␉␉␊ |
| 134 | ␉␉␉␉writer.WriteEndElement();␊ |
| 135 | ␉␉␉}␊ |
| 136 | ␉␉␉return sb.ToString();␊ |
| 137 | ␉␉}␊ |
| 138 | ␉␉␊ |
| 139 | ␉␉/// <summary>␊ |
| 140 | ␉␉/// The start time of the track. Returns DateTime.MinValue if the␊ |
| 141 | ␉␉/// track has no points.␊ |
| 142 | ␉␉/// </summary>␊ |
| 143 | ␉␉public DateTime StartTime␊ |
| 144 | ␉␉{␊ |
| 145 | ␉␉␉get␊ |
| 146 | ␉␉␉{␊ |
| 147 | ␉␉␉␉if(Points.Count==0)␊ |
| 148 | ␉␉␉␉␉return DateTime.MinValue;␊ |
| 149 | ␉␉␉␉return Points[0].Time;␊ |
| 150 | ␉␉␉}␊ |
| 151 | ␉␉}␊ |
| 152 | ␊ |
| 153 | ␉␉/// <summary>␊ |
| 154 | ␉␉/// The end time of the track. Returns DateTime.MinValue if the␊ |
| 155 | ␉␉/// track has no points.␊ |
| 156 | ␉␉/// </summary>␊ |
| 157 | ␉␉public DateTime EndTime␊ |
| 158 | ␉␉{␊ |
| 159 | ␉␉␉get␊ |
| 160 | ␉␉␉{␊ |
| 161 | ␉␉␉␉if(Points.Count==0)␊ |
| 162 | ␉␉␉␉␉return DateTime.MinValue;␊ |
| 163 | ␉␉␉␉return Points[Points.Count-1].Time;␊ |
| 164 | ␉␉␉}␊ |
| 165 | ␉␉}␊ |
| 166 | ␊ |
| 167 | ␉␉/// <summary>␊ |
| 168 | ␉␉/// Split this Track at the specified time.␊ |
| 169 | ␉␉/// </summary>␊ |
| 170 | ␉␉/// <param name="splittime">The time to split at.</param>␊ |
| 171 | ␉␉/// <returns>An array containing two new Track objects, the first being everything␊ |
| 172 | ␉␉/// from this Track up to and including splittime, and the second being the␊ |
| 173 | ␉␉/// remainder.</returns>␊ |
| 174 | ␉␉public Track[] Split(DateTime splittime)␊ |
| 175 | ␉␉{␊ |
| 176 | ␉␉␉Track[] retval=new Track[2];␊ |
| 177 | ␉␉␉retval[0]=new Track();␊ |
| 178 | ␉␉␉retval[1]=new Track();␊ |
| 179 | ␉␉␉retval[0].Name=Name+" Part 1";␊ |
| 180 | ␉␉␉retval[1].Name=Name+" Part 2";␊ |
| 181 | ␉␉␉foreach(TrackPoint p in Points)␊ |
| 182 | ␉␉␉␉if(p.Time<=splittime)␊ |
| 183 | ␉␉␉␉␉retval[0].Points.Add((TrackPoint)p.Clone());␊ |
| 184 | ␉␉␉␉else␊ |
| 185 | ␉␉␉␉␉retval[1].Points.Add((TrackPoint)p.Clone());␊ |
| 186 | ␉␉␉return retval;␊ |
| 187 | ␉␉}␊ |
| 188 | ␊ |
| 189 | ␉␉/// <summary>␊ |
| 190 | ␉␉/// Remove the trackpoint at the given time.␊ |
| 191 | ␉␉/// </summary>␊ |
| 192 | ␉␉/// <param name="removetime">The time at which the point will be removed. Any␊ |
| 193 | ␉␉/// points at this exact time will be removed.</param>␊ |
| 194 | ␉␉public void RemovePoint(DateTime removetime)␊ |
| 195 | ␉␉{␊ |
| 196 | ␉␉␉List<TrackPoint> toremove=new List<TrackPoint>();␊ |
| 197 | ␉␉␉foreach(TrackPoint p in Points)␊ |
| 198 | ␉␉␉␉if(p.Time==removetime)␊ |
| 199 | ␉␉␉␉␉toremove.Add(p);␊ |
| 200 | ␉␉␉foreach(TrackPoint p in toremove)␊ |
| 201 | ␉␉␉␉Points.Remove(p);␊ |
| 202 | ␉␉}␊ |
| 203 | ␊ |
| 204 | ␉␉public class Bounds␊ |
| 205 | ␉␉{␊ |
| 206 | ␉␉␉public double MinLat;␊ |
| 207 | ␉␉␉public double MinLon;␊ |
| 208 | ␉␉␉public double MaxLat;␊ |
| 209 | ␉␉␉public double MaxLon;␊ |
| 210 | ␉␉}␊ |
| 211 | ␊ |
| 212 | ␉␉/// <summary>␊ |
| 213 | ␉␉/// Calculate the bounds of this track.␊ |
| 214 | ␉␉/// </summary>␊ |
| 215 | ␉␉/// <returns>The bounds of the track, or null if the track contains no␊ |
| 216 | ␉␉/// data at all.</returns>␊ |
| 217 | ␉␉public Bounds CalculateBounds()␊ |
| 218 | ␉␉{␊ |
| 219 | ␉␉␉Bounds bounds=null;␊ |
| 220 | ␉␉␉foreach(TrackPoint p in Points)␊ |
| 221 | ␉␉␉{␊ |
| 222 | ␉␉␉␉if(bounds==null)␊ |
| 223 | ␉␉␉␉{␊ |
| 224 | ␉␉␉␉␉bounds=new Bounds();␊ |
| 225 | ␉␉␉␉␉bounds.MinLat=bounds.MaxLat=p.Lat;␊ |
| 226 | ␉␉␉␉␉bounds.MinLon=bounds.MaxLon=p.Lon;␊ |
| 227 | ␉␉␉␉}␊ |
| 228 | ␉␉␉␉else␊ |
| 229 | ␉␉␉␉{␊ |
| 230 | ␉␉␉␉␉if(p.Lat<bounds.MinLat)␊ |
| 231 | ␉␉␉␉␉␉bounds.MinLat=p.Lat;␊ |
| 232 | ␉␉␉␉␉if(p.Lat>bounds.MaxLat)␊ |
| 233 | ␉␉␉␉␉␉bounds.MaxLat=p.Lat;␊ |
| 234 | ␉␉␉␉␉if(p.Lon<bounds.MinLon)␊ |
| 235 | ␉␉␉␉␉␉bounds.MinLon=p.Lon;␊ |
| 236 | ␉␉␉␉␉if(p.Lon>bounds.MaxLon)␊ |
| 237 | ␉␉␉␉␉␉bounds.MaxLon=p.Lon;␊ |
| 238 | ␉␉␉␉}␊ |
| 239 | ␉␉␉}␊ |
| 240 | ␉␉␉return bounds;␊ |
| 241 | ␉␉}␊ |
| 242 | ␊ |
| 243 | ␉␉/// <summary>␊ |
| 244 | ␉␉/// Calculate the length of the track.␊ |
| 245 | ␉␉/// </summary>␊ |
| 246 | ␉␉/// <returns>The length in metres.</returns>␊ |
| 247 | ␉␉public double CalculateLength ()␊ |
| 248 | ␉␉{␊ |
| 249 | ␉␉␉TrackPoint last = null;␊ |
| 250 | ␉␉␉double dist = 0;␊ |
| 251 | ␉␉␉foreach (TrackPoint p in Points) {␊ |
| 252 | ␉␉␉␉if (last != null) {␊ |
| 253 | ␉␉␉␉␉dist += distance (last.Lat, last.Lon, p.Lat, p.Lon);␊ |
| 254 | ␉␉␉␉}␊ |
| 255 | ␉␉␉␉last = p;␊ |
| 256 | ␉␉␉}␊ |
| 257 | ␉␉␉return dist;␊ |
| 258 | ␉␉}␊ |
| 259 | ␊ |
| 260 | ␉␉/// <summary>␊ |
| 261 | ␉␉/// Calculate geodesic distance (in m) between two points specified by␊ |
| 262 | ␉␉/// latitude/longitude (in numeric degrees) using Vincenty inverse␊ |
| 263 | ␉␉/// formula for ellipsoids. ␊ |
| 264 | ␉␉///␊ |
| 265 | ␉␉/// Code based on a Javascript version by © 2002-2008 Chris Veness. ␊ |
| 266 | ␉␉///␊ |
| 267 | ␉␉/// That code contains the following: "You are welcome to re-use these scripts ␊ |
| 268 | ␉␉/// [without any warranty express or implied] provided you retain my copyright notice ␊ |
| 269 | ␉␉/// and when possible a link to my website (under a LGPL license)."␊ |
| 270 | ␉␉///␊ |
| 271 | ␉␉/// The originating web page: http://www.movable-type.co.uk/scripts/latlong-vincenty.html␊ |
| 272 | ␉␉///␊ |
| 273 | ␉␉/// In turn, this python conversion was done by Maarten Sneep and released␊ |
| 274 | ␉␉/// under the GPL V2 at http://www.xs4all.nl/~msneep/software/whereabouts-readme.html␊ |
| 275 | ␉␉/// ␊ |
| 276 | ␉␉/// In turn, it was converted to C# by Ciaran Gultnieks.␊ |
| 277 | ␉␉/// </summary>␊ |
| 278 | ␉␉/// <param name="lat1">Latitude of first point.</param>␊ |
| 279 | ␉␉/// <param name="lon1">Longitude of first point.</param>␊ |
| 280 | ␉␉/// <param name="lat2">Latitude of second point.</param>␊ |
| 281 | ␉␉/// <param name="lon2">Longitude of second point.</param>␊ |
| 282 | ␉␉/// <returns>The distance in metres.</returns>␊ |
| 283 | ␉␉private double distance (double lat1, double lon1, double lat2, double lon2)␊ |
| 284 | ␉␉{␊ |
| 285 | ␉␉␉␊ |
| 286 | ␉␉␉double a = 6378137;␊ |
| 287 | ␉␉␉// in m␊ |
| 288 | ␉␉␉double b = 6356752.3142;␊ |
| 289 | ␉␉␉// in m␊ |
| 290 | ␉␉␉double f = 1 / 298.257223563;␊ |
| 291 | ␉␉␉double deg2rad = Math.PI / 180;␊ |
| 292 | ␉␉␉lat1 *= deg2rad;␊ |
| 293 | ␉␉␉lon1 *= deg2rad;␊ |
| 294 | ␉␉␉lat2 *= deg2rad;␊ |
| 295 | ␉␉␉lon2 *= deg2rad;␊ |
| 296 | ␉␉␉␊ |
| 297 | ␉␉␉double L = lon2 - lon1;␊ |
| 298 | ␉␉␉double U1 = Math.Atan ((1 - f) * Math.Tan (lat1));␊ |
| 299 | ␉␉␉double U2 = Math.Atan ((1 - f) * Math.Tan (lat2));␊ |
| 300 | ␉␉␉double sinU1 = Math.Sin (U1);␊ |
| 301 | ␉␉␉double cosU1 = Math.Cos (U1);␊ |
| 302 | ␉␉␉double sinU2 = Math.Sin (U2);␊ |
| 303 | ␉␉␉double cosU2 = Math.Cos (U2);␊ |
| 304 | ␉␉␉double Lambda = L;␊ |
| 305 | ␉␉␉double LambdaP = 2 * Math.PI;␊ |
| 306 | ␉␉␉int iterLimit = 20;␊ |
| 307 | ␉␉␉␊ |
| 308 | ␉␉␉double sinLambda, cosLambda, sinSigma = 0, cosSigma = 0;␊ |
| 309 | ␉␉␉double cosSqAlpha = 0, sinAlpha, cos2SigmaM = 0;␊ |
| 310 | ␉␉␉double C, sigma = 0;␊ |
| 311 | ␉␉␉␊ |
| 312 | ␉␉␉while (Math.Abs (Lambda - LambdaP) > 1e-12 && iterLimit > 0) {␊ |
| 313 | ␉␉␉␉sinLambda = Math.Sin (Lambda);␊ |
| 314 | ␉␉␉␉cosLambda = Math.Cos (Lambda);␊ |
| 315 | ␉␉␉␉sinSigma = Math.Sqrt (Math.Pow (cosU2 * sinLambda, 2) + Math.Pow (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda, 2));␊ |
| 316 | ␉␉␉␉if (sinSigma == 0)␊ |
| 317 | ␉␉␉␉␉return 0;␊ |
| 318 | ␉␉␉␉cosSigma = sinU1 * sinU2 + cosU1 * cosU2 * cosLambda;␊ |
| 319 | ␉␉␉␉sigma = Math.Atan2 (sinSigma, cosSigma);␊ |
| 320 | ␉␉␉␉sinAlpha = cosU1 * cosU2 * sinLambda / sinSigma;␊ |
| 321 | ␉␉␉␉cosSqAlpha = 1 - Math.Pow (sinAlpha, 2);␊ |
| 322 | ␉␉␉␉try {␊ |
| 323 | ␉␉␉␉␉␊ |
| 324 | ␉␉␉␉␉cos2SigmaM = cosSigma - 2 * sinU1 * sinU2 / cosSqAlpha;␊ |
| 325 | ␉␉␉␉} catch (Exception) {␊ |
| 326 | ␉␉␉␉␉cos2SigmaM = 0;␊ |
| 327 | ␉␉␉␉}␊ |
| 328 | ␉␉␉␉C = f / 16 * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha));␊ |
| 329 | ␉␉␉␉LambdaP = Lambda;␊ |
| 330 | ␉␉␉␉Lambda = L + (1 - C) * f * sinAlpha * (sigma + C * sinSigma * (cos2SigmaM + C * cosSigma * (-1 + 2 * Math.Pow (cos2SigmaM, 2))));␊ |
| 331 | ␉␉␉␉iterLimit -= 1;␊ |
| 332 | ␉␉␉␉␊ |
| 333 | ␉␉␉}␊ |
| 334 | ␉␉␉␊ |
| 335 | ␉␉␉if (iterLimit == 0)␊ |
| 336 | ␉␉␉␉return 0;␊ |
| 337 | ␉␉␉␊ |
| 338 | ␉␉␉double uSq = cosSqAlpha * (Math.Pow (a, 2) - Math.Pow (b, 2)) / Math.Pow (b, 2);␊ |
| 339 | ␉␉␉double A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));␊ |
| 340 | ␉␉␉double B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));␊ |
| 341 | ␉␉␉double deltaSigma = B * sinSigma * (cos2SigmaM + B / 4 * (cosSigma * (-1 + 2 * Math.Pow (cos2SigmaM, 2)) - B / 6 * cos2SigmaM * (-3 + 4 * Math.Pow (sinSigma, 2)) * (-3 + 4 * Math.Pow (cos2SigmaM, 2))));␊ |
| 342 | ␉␉␉return b * A * (sigma - deltaSigma);␊ |
| 343 | ␉␉␉␊ |
| 344 | ␉␉}␊ |
| 345 | ␉␉␊ |
| 346 | ␉}␊ |
| 347 | }␊ |
| 348 | |