| 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.</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[1];␊ |
| 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 | ␉}␊ |
| 244 | }␊ |
| 245 | |