/// <summary> /// Exports track by asking user for destination path and then using GpxWriter to write /// track to file /// </summary> /// <param name="track">track to export</param> /// <returns>task to wait on</returns> public static async Task ExportTrackAsync(Track track) { string exportFilename = await AskUserExportFilenameAsync(track.Name + ".gpx"); if (exportFilename == null) { return; } string exportPath = Path.Combine(FileSystem.CacheDirectory, exportFilename); try { GpxWriter.WriteTrack(exportPath, track); await ShareFile(exportPath, "application/gpx+xml"); } catch (Exception ex) { App.LogError(ex); await App.Current.MainPage.DisplayAlert( Constants.AppTitle, "Error while exporting track: " + ex.Message, "OK"); } }
private void newGpxFile(string Name, string Path) { FileStream readFile = File.Open(Path, FileMode.Open); FileStream writeFile = File.OpenWrite("../../Files/" + Name); GpxReader reader = new GpxReader(readFile); GpxWriter writer = new GpxWriter(writeFile); while (reader.Read()) { switch (reader.ObjectType) { case GpxObjectType.Metadata: writer.WriteMetadata(reader.Metadata); break; case GpxObjectType.WayPoint: writer.WriteWayPoint(reader.WayPoint); break; case GpxObjectType.Route: writer.WriteRoute(reader.Route); break; case GpxObjectType.Track: writer.WriteTrack(reader.Track); break; } } readFile.Close(); writeFile.Close(); }
GpxWriter loadTrack(List <Track> tracks, Stream stream, string link_Href = "https://rutoteca.es/", string link_Text = "Rutoteca", string mimeType = "text/html", string name = "Rutoteca", string descripcion = "Visita Rutoteca el mayor almacen de rutas homologadas de España") { GpxWriter newGpx = new GpxWriter(stream); tracks.ForEach(t => { var newMetadata = new Gpx.GpxMetadata(); newMetadata.Link = new GpxLink(); newMetadata.Link.Href = link_Href; newMetadata.Link.MimeType = mimeType; newMetadata.Link.Text = link_Text; newMetadata.Name = name; newMetadata.Description = descripcion; newGpx.WriteMetadata(newMetadata); GpxTrack newTrack = new GpxTrack(); var newSegment = new Gpx.GpxTrackSegment(); var inicio = t.GpxPoint.First().Time; t.GpxPoint.ToList().ForEach(pt => { newSegment.TrackPoints.Add(SetGpxPoint(pt, inicio)); }); newTrack.Segments.Add(newSegment); newGpx.WriteTrack(newTrack); } ); return(newGpx); }
public void TestWriteTrack() { // set up const string TrackName = "Test Track"; var track = new Track { Name = TrackName, TrackPoints = new System.Collections.Generic.List <TrackPoint> { new TrackPoint(47.754076, 12.352277, 1234.0, null) { Time = DateTime.Today + TimeSpan.FromHours(1.0) }, new TrackPoint(46.017779, 11.900711, 778.2, null) { Time = DateTime.Today + TimeSpan.FromHours(2.0) }, } }; // run string text = string.Empty; using (var stream = new MemoryStream()) { GpxWriter.WriteTrack(stream, track); text = Encoding.UTF8.GetString(stream.GetBuffer(), 0, (int)stream.Length); } // check Debug.WriteLine("xml = " + text); Assert.IsTrue(text.Contains(TrackName), "gpx must contain track name"); }
static void Main(string[] args) { var input = new FileStream(@"C:\perso\Night ride\Night ride 2nd part.gpx", FileMode.Open); var output = new FileStream(@"C:\perso\Night ride\Night ride 2nd part_with_timespamp.gpx", FileMode.Create); DateTime startTime = DateTime.Parse("2020-07-06T23:35:28"); DateTime endTime = DateTime.Parse("2020-07-07T00:35:28"); double totalPoints = 1379; int p = 0; using (GpxReader reader = new GpxReader(input)) { using (GpxWriter writer = new GpxWriter(output)) { while (reader.Read()) { switch (reader.ObjectType) { case GpxObjectType.Metadata: writer.WriteMetadata(reader.Metadata); break; case GpxObjectType.WayPoint: writer.WriteWayPoint(reader.WayPoint); break; case GpxObjectType.Route: writer.WriteRoute(reader.Route); break; case GpxObjectType.Track: var track = reader.Track; foreach (var seg in track.Segments) { foreach (var point in seg.TrackPoints) { point.Time = startTime.AddSeconds(p * (endTime - startTime).TotalSeconds / totalPoints); Console.WriteLine($"time = {point.Time}"); p++; } } writer.WriteTrack(track); break; } } } } }
public static IList <Coordinates> GetGPXPoints() { IList <Coordinates> lst = new List <Coordinates>(); //Reading the GOX Debug.Log(("Reading GPX")); StreamReader input = new StreamReader(@"C:\Users\Chris\Documents\UnityProjects\CreateObject\Assets\gpxfiles\Heffers.gpx", Encoding.Default); // StreamReader input = new StreamReader(@"C:\Users\Chris\Documents\UnityProjects\CreateObject\Assets\chop.gpx", Encoding.Default); // StreamReader input = new StreamReader(@"C:\Users\Chris\Documents\UnityProjects\CreateObject\Assets\Perth worlds qualifier.gpx", Encoding.Default); MemoryStream m = new MemoryStream(); using (GpxReader reader = new GpxReader(input.BaseStream)) { using (GpxWriter writer = new GpxWriter(m)) { while (reader.Read()) { switch (reader.ObjectType) { case GpxObjectType.Metadata: Debug.Log(("Metadata")); writer.WriteMetadata(reader.Metadata); break; case GpxObjectType.WayPoint: Debug.Log(("Point")); writer.WriteWayPoint(reader.WayPoint); break; case GpxObjectType.Route: Debug.Log(("Route")); writer.WriteRoute(reader.Route); break; case GpxObjectType.Track: Debug.Log(("Track")); writer.WriteTrack(reader.Track); //Set a local origin //GPSEncoder.SetLocalOrigin(new Vector2((float)originCoords.latitude, (float)originCoords.longitude)); //Coordinates origin = new Coordinates(1.289639, -3532648, 1); IList <GpxTrackSegment> segments = reader.Track.Segments; IList <GpxTrackPoint> tp = segments[0].TrackPoints; foreach (GpxTrackPoint item in tp) { Debug.Log(("lat" + item.Latitude)); Debug.Log(("lng" + item.Longitude)); Debug.Log(("e" + item.Elevation)); //Coordinates c = new Coordinates(item.Latitude, item.Longitude, item.Elevation.Value); //Vector3 v = c.convertCoordinateToVector(); Coordinates currentLocation; currentLocation = new Coordinates(item.Latitude, item.Longitude, item.Elevation.Value); lst.Add(currentLocation); // UnityEngine.GameObject o = (GameObject)Instantiate(prefab, new Vector3(v.x, v.y, v.z), Quaternion.identity); //UnityEngine.GameObject o = (GameObject)Instantiate(prefab, new Vector3(i * 2.0f, 0, 0), Quaternion.identity); // o.transform.parent = sourceGO.transform; } break; } } } } return(lst); }