Skip to content

Commit 635ee54

Browse files
committed
added sample of net topology usage
1 parent 1537806 commit 635ee54

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed

PolylineAlgorithm.slnx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
<Project Path="benchmarks/PolylineAlgorithm.Benchmarks/PolylineAlgorithm.Benchmarks.csproj" />
44
<Project Path="benchmarks/PolylineAlgorithm.Comparison.Benchmarks/PolylineAlgorithm.Comparison.Benchmarks.csproj" />
55
</Folder>
6+
<Folder Name="/samples/">
7+
<Project Path="samples/PolylineAlgorithm.NetTopologySuite.Sample/PolylineAlgorithm.NetTopologySuite.Sample.csproj" Id="27a8fc47-0a3c-49c7-af5e-530514827785" />
8+
</Folder>
69
<Folder Name="/src/">
710
<Project Path="src/PolylineAlgorithm.Abstraction/PolylineAlgorithm.Abstraction.csproj" Id="a39b8ed4-13da-4ef7-b144-68f0c281474f" />
811
<Project Path="src/PolylineAlgorithm/PolylineAlgorithm.csproj" />
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace PolylineAlgorithm.NetTopologySuite.Sample;
2+
3+
using global::NetTopologySuite.Geometries;
4+
using PolylineAlgorithm.Abstraction;
5+
using System;
6+
using System.Buffers;
7+
8+
internal class NetTopologyPolylineDecoder : PolylineDecoder<Point, string> {
9+
protected override Point CreateCoordinate(double latitude, double longitude) {
10+
return new Point(latitude, longitude);
11+
}
12+
13+
protected override ReadOnlySequence<char> GetReadOnlySequence(string? polyline) {
14+
if(string.IsNullOrWhiteSpace(polyline)) {
15+
throw new ArgumentException("Value cannot be null, empty or whitespace.", nameof(polyline));
16+
}
17+
18+
return new(polyline.AsMemory());
19+
}
20+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace PolylineAlgorithm.NetTopologySuite.Sample;
2+
3+
using global::NetTopologySuite.Geometries;
4+
using PolylineAlgorithm.Abstraction;
5+
using System;
6+
using System.Buffers;
7+
using System.Text;
8+
9+
internal class NetTopologyPolylineEncoder : PolylineEncoder<Point, string> {
10+
protected override string CreatePolyline(ReadOnlySequence<char> sequence) {
11+
if(sequence.IsEmpty) {
12+
return string.Empty;
13+
}
14+
15+
if(sequence.IsSingleSegment) {
16+
return sequence.FirstSpan.ToString();
17+
}
18+
19+
var enumerator = sequence.GetEnumerator();
20+
var sb = new StringBuilder();
21+
22+
while (enumerator.MoveNext()) {
23+
sb.Append(enumerator.Current);
24+
}
25+
26+
return sb.ToString();
27+
}
28+
29+
protected override double GetLatitude(Point? current) {
30+
return current?.X ?? 0d;
31+
}
32+
33+
protected override double GetLongitude(Point? current) {
34+
return current?.Y ?? 0d;
35+
}
36+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net9.0</TargetFrameworks>
6+
<LangVersion>13.0</LangVersion>
7+
<Nullable>enable</Nullable>
8+
<ImplicitUsings>enable</ImplicitUsings>
9+
<InvariantGlobalization>true</InvariantGlobalization>
10+
<NeutralLanguage>en</NeutralLanguage>
11+
</PropertyGroup>
12+
13+
<PropertyGroup>
14+
<IsPackable>false</IsPackable>
15+
</PropertyGroup>
16+
17+
<ItemGroup>
18+
<PackageReference Include="NetTopologySuite" Version="2.6.0" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<ProjectReference Include="..\..\src\PolylineAlgorithm.Abstraction\PolylineAlgorithm.Abstraction.csproj" />
23+
</ItemGroup>
24+
25+
</Project>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
namespace PolylineAlgorithm.NetTopologySuite.Sample;
2+
3+
using global::NetTopologySuite.Geometries;
4+
5+
internal class Program {
6+
private static readonly NetTopologyPolylineDecoder _decoder = new();
7+
private static readonly NetTopologyPolylineEncoder _encoder = new();
8+
9+
static void Main(string[] _) {
10+
Start:
11+
Console.WriteLine($"Please, select an operation:{Environment.NewLine}");
12+
Console.WriteLine($"[0] Decode{Environment.NewLine}");
13+
Console.WriteLine($"[1] Encode{Environment.NewLine}");
14+
15+
var key = Console.ReadKey(false);
16+
17+
switch (key.Key) {
18+
case ConsoleKey.D0:
19+
goto Decode;
20+
case ConsoleKey.D1:
21+
goto Encode;
22+
default:
23+
Console.WriteLine($"Wrong number!{Environment.NewLine}");
24+
goto Start;
25+
}
26+
27+
Decode:
28+
29+
Console.WriteLine($"{Environment.NewLine}Enter polyline string. You can make one on https://developers.google.com/maps/documentation/utilities/polylineutility.{Environment.NewLine}");
30+
31+
var polyline = Console.ReadLine();
32+
33+
if (string.IsNullOrWhiteSpace(polyline)) {
34+
Console.WriteLine($"{Environment.NewLine}Polyline doesn't seems to be valid.{Environment.NewLine}");
35+
goto Decode;
36+
}
37+
38+
try {
39+
var result = _decoder.Decode(polyline).ToList();
40+
41+
Console.WriteLine($"{Environment.NewLine}Type: {result}{Environment.NewLine}");
42+
43+
foreach (var item in result) {
44+
Console.WriteLine($"AsText: {item.AsText()}{Environment.NewLine}");
45+
}
46+
} catch (Exception ex) {
47+
Console.WriteLine($"Error: {ex.Message}{Environment.NewLine}");
48+
goto Start;
49+
}
50+
51+
Encode:
52+
Console.WriteLine($"{Environment.NewLine}Enter collection of NetTopology points in format latitude1,longitude1;latitude2,longitude2{Environment.NewLine}");
53+
54+
var coordinates = Console.ReadLine();
55+
56+
if (string.IsNullOrWhiteSpace(coordinates) || !TryParse(coordinates!, out var points)) {
57+
Console.WriteLine($"{Environment.NewLine}Polyline doesn't seems to be valid.{Environment.NewLine}");
58+
goto Encode;
59+
}
60+
61+
try {
62+
var result = _encoder.Encode(points);
63+
64+
Console.WriteLine($"{Environment.NewLine}Polyline: {result}{Environment.NewLine}");
65+
} catch (Exception ex) {
66+
Console.WriteLine($"Error: {ex.Message}{Environment.NewLine}");
67+
goto Start;
68+
}
69+
}
70+
71+
private static bool TryParse(string coordinates, out IEnumerable<Point> result) {
72+
var temp = new List<Point>();
73+
74+
var pairs = coordinates
75+
.Split(';');
76+
77+
foreach (var pair in pairs) {
78+
var coords = pair.Split(',');
79+
80+
if (coords.Length != 2 || !double.TryParse(coords[0], out double x) || !double.TryParse(coords[1], out double y)) {
81+
result = temp;
82+
return false;
83+
}
84+
85+
temp.Add(new Point(x, y));
86+
}
87+
88+
result = temp;
89+
return true;
90+
}
91+
}

0 commit comments

Comments
 (0)