|
| 1 | +using System; |
| 2 | +using System.Collections.Immutable; |
| 3 | +using System.Linq; |
| 4 | +using System.Xml.Linq; |
| 5 | + |
| 6 | +namespace NetTopologySuite.IO |
| 7 | +{ |
| 8 | + internal static class DataObjectBuilders |
| 9 | + { |
| 10 | + public static GpxMetadata RandomGpxMetadata(Random random) |
| 11 | + { |
| 12 | + return new GpxMetadata( |
| 13 | + creator: "Creator_" + random.Next(), |
| 14 | + name: "Name_" + random.Next(), |
| 15 | + description: "Description_" + random.Next(), |
| 16 | + author: new GpxPerson( |
| 17 | + name: "Name_" + random.Next(), |
| 18 | + email: new GpxEmail("nts", "example.com"), |
| 19 | + link: RandomWebLink(random)), |
| 20 | + copyright: new GpxCopyright( |
| 21 | + author: "Author_" + random.Next(), |
| 22 | + year: random.Next(1990, 2100), |
| 23 | + licenseUri: RandomUri(random)), |
| 24 | + links: ImmutableArray.CreateRange( |
| 25 | + Enumerable.Repeat(0, random.Next(1, 5)).Select(_ => RandomWebLink(random))), |
| 26 | + creationTimeUtc: RandomDateTimeUtc(random), |
| 27 | + keywords: "Keywords_" + random.Next(), |
| 28 | + bounds: RandomBoundingBox(random), |
| 29 | + extensions: RandomExtensions(random)); |
| 30 | + } |
| 31 | + |
| 32 | + public static GpxWaypoint RandomWaypoint(Random random) |
| 33 | + { |
| 34 | + return new GpxWaypoint( |
| 35 | + longitude: RandomGpxLongitude(random), |
| 36 | + latitude: RandomGpxLatitude(random), |
| 37 | + elevationInMeters: 5000 * random.NextDouble(), |
| 38 | + timestampUtc: RandomDateTimeUtc(random), |
| 39 | + magneticVariation: new GpxDegrees(360 * random.NextDouble()), |
| 40 | + geoidHeight: 5000 * random.NextDouble(), |
| 41 | + name: "Name_" + random.Next(), |
| 42 | + comment: "Comment_" + random.Next(), |
| 43 | + description: "Description_" + random.Next(), |
| 44 | + source: "Source_" + random.Next(), |
| 45 | + links: ImmutableArray.CreateRange( |
| 46 | + Enumerable.Repeat(0, random.Next(1, 5)).Select(_ => RandomWebLink(random))), |
| 47 | + symbolText: "SymbolText_" + random.Next(), |
| 48 | + classification: "Classification_" + random.Next(), |
| 49 | + fixKind: (GpxFixKind)random.Next(5), |
| 50 | + numberOfSatellites: (uint)random.Next(100), |
| 51 | + horizontalDilutionOfPrecision: 20 * random.NextDouble() - 10, |
| 52 | + verticalDilutionOfPrecision: 20 * random.NextDouble() - 10, |
| 53 | + positionDilutionOfPrecision: 20 * random.NextDouble() - 10, |
| 54 | + secondsSinceLastDgpsUpdate: 300 * random.NextDouble(), |
| 55 | + dgpsStationId: new GpxDgpsStationId((ushort)(random.Next(GpxDgpsStationId.MaxValue.Value + 1))), |
| 56 | + extensions: RandomExtensions(random)); |
| 57 | + } |
| 58 | + |
| 59 | + public static GpxRoute RandomRoute(Random random) |
| 60 | + { |
| 61 | + return new GpxRoute( |
| 62 | + name: "Name_" + random.Next(), |
| 63 | + comment: "Comment_" + random.Next(), |
| 64 | + description: "Description_" + random.Next(), |
| 65 | + source: "Source_" + random.Next(), |
| 66 | + links: ImmutableArray.CreateRange( |
| 67 | + Enumerable.Repeat(0, random.Next(1, 5)).Select(_ => RandomWebLink(random))), |
| 68 | + number: (uint)random.Next(100000), |
| 69 | + classification: "Classification_" + random.Next(), |
| 70 | + extensions: RandomExtensions(random), |
| 71 | + waypoints: new ImmutableGpxWaypointTable( |
| 72 | + Enumerable.Repeat(0, random.Next(10)).Select(_ => RandomWaypoint(random)))); |
| 73 | + } |
| 74 | + |
| 75 | + public static GpxTrack RandomTrack(Random random) |
| 76 | + { |
| 77 | + return new GpxTrack( |
| 78 | + name: "Name_" + random.Next(), |
| 79 | + comment: "Comment_" + random.Next(), |
| 80 | + description: "Description_" + random.Next(), |
| 81 | + source: "Source_" + random.Next(), |
| 82 | + links: ImmutableArray.CreateRange( |
| 83 | + Enumerable.Repeat(0, random.Next(1, 5)).Select(_ => RandomWebLink(random))), |
| 84 | + number: (uint)random.Next(100000), |
| 85 | + classification: "Classification_" + random.Next(), |
| 86 | + extensions: RandomExtensions(random), |
| 87 | + segments: ImmutableArray.CreateRange( |
| 88 | + Enumerable.Repeat(0, random.Next(10)).Select(_ => RandomTrackSegment(random)))); |
| 89 | + } |
| 90 | + |
| 91 | + public static GpxTrackSegment RandomTrackSegment(Random random) |
| 92 | + { |
| 93 | + return new GpxTrackSegment( |
| 94 | + waypoints: new ImmutableGpxWaypointTable( |
| 95 | + Enumerable.Repeat(0, random.Next(10)).Select(_ => RandomWaypoint(random))), |
| 96 | + extensions: RandomExtensions(random)); |
| 97 | + } |
| 98 | + |
| 99 | + public static GpxBoundingBox RandomBoundingBox(Random random) |
| 100 | + { |
| 101 | + var longitude1 = RandomGpxLongitude(random); |
| 102 | + var longitude2 = RandomGpxLongitude(random); |
| 103 | + var latitude1 = RandomGpxLatitude(random); |
| 104 | + var latitude2 = RandomGpxLatitude(random); |
| 105 | + |
| 106 | + return new GpxBoundingBox( |
| 107 | + longitude1 < longitude2 ? longitude1 : longitude2, |
| 108 | + latitude1 < latitude2 ? latitude1 : latitude2, |
| 109 | + longitude1 < longitude2 ? longitude2 : longitude1, |
| 110 | + latitude1 < latitude2 ? latitude2 : latitude1); |
| 111 | + } |
| 112 | + |
| 113 | + public static GpxLongitude RandomGpxLongitude(Random random) |
| 114 | + { |
| 115 | + return new GpxLongitude(360 * random.NextDouble() - 180); |
| 116 | + } |
| 117 | + |
| 118 | + public static GpxLatitude RandomGpxLatitude(Random random) |
| 119 | + { |
| 120 | + return new GpxLatitude(180 * random.NextDouble() - 90); |
| 121 | + } |
| 122 | + |
| 123 | + public static GpxWebLink RandomWebLink(Random random) |
| 124 | + { |
| 125 | + return new GpxWebLink( |
| 126 | + href: RandomUri(random), |
| 127 | + text: "Text_" + random.Next(), |
| 128 | + contentType: "Type_" + random.Next()); |
| 129 | + } |
| 130 | + |
| 131 | + public static ImmutableXElementContainer RandomExtensions(Random random) |
| 132 | + { |
| 133 | + var mainResult = new XElement(XName.Get("parent" + random.Next(), "http://www.example.com/schema")); |
| 134 | + for (int i = 0, cnt = random.Next(10); i < cnt; i++) |
| 135 | + { |
| 136 | + mainResult.Add(new XElement($"something{i}", new XAttribute("data", random.Next(100)))); |
| 137 | + } |
| 138 | + |
| 139 | + return new ImmutableXElementContainer(new[] { mainResult }); |
| 140 | + } |
| 141 | + |
| 142 | + private static Uri RandomUri(Random random) |
| 143 | + { |
| 144 | + return new Uri("http://example.com/" + random.Next()); |
| 145 | + } |
| 146 | + |
| 147 | + private static DateTime RandomDateTimeUtc(Random random) |
| 148 | + { |
| 149 | + return new DateTime(random.Next(1990, 2100), random.Next(1, 12), random.Next(1, 28), random.Next(23), random.Next(59), random.Next(59), DateTimeKind.Utc); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments