public void Save_ThrowsExceptionIfPathIsNull() { string path = null; var target = new GpxDocument(); Assert.Throws <ArgumentNullException>(() => target.Save(path)); }
public void Save_IGpxWriter_ThrowsExceptionIfWriterIsNull() { IGpxWriter writer = null; var target = new GpxDocument(); Assert.Throws <ArgumentNullException>(() => target.Save(writer)); }
public void GpxWritev1_0Test() { // instantiate and load the gpx test document. XmlStreamSource source = new XmlStreamSource( Assembly.GetExecutingAssembly().GetManifestResourceStream("OsmSharp.UnitTests.test.v1.0.gpx")); GpxDocument document = new GpxDocument(source); object gpx = document.Gpx; if (gpx is OsmSharp.Xml.Gpx.v1_0.gpx) { // all ok here! // get the target file. MemoryStream write_file = new MemoryStream(); // create a new xml source. XmlStreamSource write_source = new XmlStreamSource(write_file); GpxDocument gpx_target = new GpxDocument(write_source); // set the target data the same as the source document. gpx_target.Gpx = gpx; // save the data. gpx_target.Save(); // close the old document. document.Close(); source.Close(); // check to see if the data was written correctly. // instantiate and load the gpx test document. source = new XmlStreamSource(write_file); document = new GpxDocument(source); gpx = document.Gpx; if (gpx is OsmSharp.Xml.Gpx.v1_0.gpx) { // all ok here! OsmSharp.Xml.Gpx.v1_0.gpx gpx_type = (gpx as OsmSharp.Xml.Gpx.v1_0.gpx); // test the gpx test file content. Assert.IsNotNull(gpx_type.trk, "Gpx has not track!"); Assert.AreEqual(gpx_type.trk[0].trkseg.Length, 424, "Not the correct number of track segments found!"); } else { Assert.Fail("No gpx data was read, or data was of the incorrect type!"); } } else { Assert.Fail("No gpx data was read, or data was of the incorrect type!"); } document.Close(); source.Close(); }
internal static void Save(Stream stream, Route route) { GpxDocument gpxDocument = new GpxDocument((IXmlSource) new XmlStreamSource(stream)); gpxType gpxType = new gpxType(); gpxType.trk = new trkType[1]; List <wptType> wptTypeList1 = new List <wptType>(); trkType trkType = new trkType(); List <wptType> wptTypeList2 = new List <wptType>(); trkType.trkseg = new trksegType[1]; trksegType trksegType = new trksegType(); for (int index1 = 0; index1 < route.Segments.Count; ++index1) { RouteSegment segment = route.Segments[index1]; if (segment.Points != null) { for (int index2 = 0; index2 < segment.Points.Length; ++index2) { RouteStop point = segment.Points[index2]; RouteTags routeTags = point.Tags == null ? (RouteTags)null : ((IEnumerable <RouteTags>)point.Tags).FirstOrDefault <RouteTags>((Func <RouteTags, bool>)(x => x.Value == "name")); wptTypeList2.Add(new wptType() { lat = (Decimal)point.Latitude, lon = (Decimal)point.Longitude, name = routeTags == null ? string.Empty : routeTags.Value }); } } wptTypeList1.Add(new wptType() { lat = (Decimal)segment.Latitude, lon = (Decimal)segment.Longitude }); } trksegType.trkpt = wptTypeList1.ToArray(); trkType.trkseg[0] = trksegType; gpxType.trk[0] = trkType; gpxType.wpt = wptTypeList2.ToArray(); gpxDocument.Gpx = (object)gpxType; gpxDocument.Save(); }
public void Save_IGpxWriter_WritesDataToWriter() { var waypoint = new GpxPoint(); var route = new GpxRoute(); var track = new GpxTrack(); Mock <IGpxWriter> writerM = new Mock <IGpxWriter>(); writerM.Setup(w => w.Write(waypoint)).Verifiable(); writerM.Setup(w => w.Write(route)).Verifiable(); writerM.Setup(w => w.Write(track)).Verifiable(); var target = new GpxDocument(new[] { waypoint }, new[] { route }, new[] { track }); target.Save(writerM.Object); writerM.Verify(w => w.Write(waypoint), Times.Once()); writerM.Verify(w => w.Write(route), Times.Once()); writerM.Verify(w => w.Write(track), Times.Once()); }
/// <summary> /// Saves the route to a gpx file. /// </summary> /// <param name="file"></param> /// <param name="route"></param> internal static void Save(Stream stream, Route route) { XmlStreamSource source = new XmlStreamSource(stream); GpxDocument output_document = new GpxDocument(source); gpxType output_gpx = new gpxType(); output_gpx.trk = new trkType[1]; // initialize all objects. List <wptType> segments = new List <wptType>(); trkType track = new trkType(); List <wptType> poi_gpx = new List <wptType>(); track.trkseg = new trksegType[1]; // ============= CONSTRUCT TRACK SEGMENT ============== trksegType track_segment = new trksegType(); // loop over all points. for (int idx = 0; idx < route.Entries.Length; idx++) { // get the current entry. RoutePointEntry entry = route.Entries[idx]; // ================== INITIALIZE A NEW SEGMENT IF NEEDED! ======== wptType waypoint; if (entry.Points != null) { // loop over all points and create a waypoint for each. for (int p_idx = 0; p_idx < entry.Points.Length; p_idx++) { RoutePoint point = entry.Points[p_idx]; waypoint = new wptType(); waypoint.lat = (decimal)point.Latitude; waypoint.lon = (decimal)point.Longitude; waypoint.name = point.Name; poi_gpx.Add(waypoint); } } // insert poi's. double longitde = entry.Longitude; double latitude = entry.Latitude; waypoint = new wptType(); waypoint.lat = (decimal)entry.Latitude; waypoint.lon = (decimal)entry.Longitude; segments.Add(waypoint); } // put the segment in the track. track_segment.trkpt = segments.ToArray(); track.trkseg[0] = track_segment; // set the track to the output. output_gpx.trk[0] = track; output_gpx.wpt = poi_gpx.ToArray(); // save the ouput. output_document.Gpx = output_gpx; output_document.Save(); }