/
GpxTrack.cs
95 lines (79 loc) · 3.49 KB
/
GpxTrack.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
namespace NetTopologySuite.IO
{
public sealed class GpxTrack
{
public GpxTrack(string name, string comment, string description, string source, ImmutableArray<GpxWebLink> links, uint? number, string classification, ImmutableArray<GpxTrackSegment> segments, object extensions)
{
this.Name = name;
this.Comment = comment;
this.Description = description;
this.Source = source;
this.Links = links;
this.Number = number;
this.Classification = classification;
this.Segments = segments;
this.Extensions = extensions;
}
public string Name { get; }
public string Comment { get; }
public string Description { get; }
public string Source { get; }
public ImmutableArray<GpxWebLink> Links { get; }
public uint? Number { get; }
public string Classification { get; }
public ImmutableArray<GpxTrackSegment> Segments { get; }
public object Extensions { get; }
public static GpxTrack Load(XElement element, GpxReaderSettings settings)
{
if (element is null)
{
return null;
}
if (settings is null)
{
throw new ArgumentNullException(nameof(settings));
}
var extensionsElement = element.GpxElement("extensions");
return new GpxTrack(
name: element.GpxElement("name")?.Value,
comment: element.GpxElement("cmt")?.Value,
description: element.GpxElement("desc")?.Value,
source: element.GpxElement("src")?.Value,
links: ImmutableArray.CreateRange(element.GpxElements("link").Select(GpxWebLink.Load)),
number: Helpers.ParseUInt32(element.GpxElement("number")?.Value),
classification: element.GpxElement("type")?.Value,
segments: ImmutableArray.CreateRange(element.GpxElements("trkseg").Select(el => GpxTrackSegment.LoadNoValidation(el, settings))),
extensions: extensionsElement is null ? null : settings.ExtensionReader.ConvertTrackExtensionElement(extensionsElement.Elements()));
}
public void Save(XmlWriter writer, GpxWriterSettings settings)
{
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
if (settings is null)
{
throw new ArgumentNullException(nameof(settings));
}
writer.WriteOptionalGpxElementValue("name", this.Name);
writer.WriteOptionalGpxElementValue("cmt", this.Comment);
writer.WriteOptionalGpxElementValue("desc", this.Description);
writer.WriteOptionalGpxElementValue("src", this.Source);
writer.WriteGpxElementValues("link", this.Links);
writer.WriteOptionalGpxElementValue("number", this.Number);
writer.WriteOptionalGpxElementValue("type", this.Classification);
writer.WriteExtensions(this.Extensions, settings.ExtensionWriter.ConvertRouteExtension);
foreach (var segment in this.Segments)
{
writer.WriteGpxStartElement("trkseg");
segment.SaveNoValidation(writer, settings);
writer.WriteEndElement();
}
}
}
}