Skip to content

Commit 01656f2

Browse files
author
Petr Šrámek
committed
Implementation refactored into single class responsible for encoding and decoding polyline path
1 parent 9d5ed42 commit 01656f2

20 files changed

+439
-503
lines changed

src/Cloudikka.PolylineAlgorithm/Cloudikka.PolylineAlgorithm.sln

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "nuget", "nuget", "{7F9807A1
1414
..\..\nuget\Cloudikka.PolylineAlgorithm.nuspec = ..\..\nuget\Cloudikka.PolylineAlgorithm.nuspec
1515
EndProjectSection
1616
EndProject
17-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cloudikka.PolylineAlgorithm.Tests", "..\..\tests\Cloudikka.PolylineAlgorithm.Tests\Cloudikka.PolylineAlgorithm.Tests.csproj", "{1926DF66-E776-4369-AA0E-6A087F7137B5}"
17+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cloudikka.PolylineAlgorithm.Tests", "..\..\tests\Cloudikka.PolylineAlgorithm.Tests\Cloudikka.PolylineAlgorithm.Tests.csproj", "{10B555A6-E4C9-4828-B4A6-64E248CC1D14}"
1818
EndProject
1919
Global
2020
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -26,17 +26,17 @@ Global
2626
{882322A6-E758-4662-8D1C-7C555C8FC3F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
2727
{882322A6-E758-4662-8D1C-7C555C8FC3F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
2828
{882322A6-E758-4662-8D1C-7C555C8FC3F2}.Release|Any CPU.Build.0 = Release|Any CPU
29-
{1926DF66-E776-4369-AA0E-6A087F7137B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
30-
{1926DF66-E776-4369-AA0E-6A087F7137B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
31-
{1926DF66-E776-4369-AA0E-6A087F7137B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
32-
{1926DF66-E776-4369-AA0E-6A087F7137B5}.Release|Any CPU.Build.0 = Release|Any CPU
29+
{10B555A6-E4C9-4828-B4A6-64E248CC1D14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
30+
{10B555A6-E4C9-4828-B4A6-64E248CC1D14}.Debug|Any CPU.Build.0 = Debug|Any CPU
31+
{10B555A6-E4C9-4828-B4A6-64E248CC1D14}.Release|Any CPU.ActiveCfg = Release|Any CPU
32+
{10B555A6-E4C9-4828-B4A6-64E248CC1D14}.Release|Any CPU.Build.0 = Release|Any CPU
3333
EndGlobalSection
3434
GlobalSection(SolutionProperties) = preSolution
3535
HideSolutionNode = FALSE
3636
EndGlobalSection
3737
GlobalSection(NestedProjects) = preSolution
3838
{882322A6-E758-4662-8D1C-7C555C8FC3F2} = {51C886AF-D610-48A4-9D73-2DEB38742801}
39-
{1926DF66-E776-4369-AA0E-6A087F7137B5} = {576FEFFC-B624-40C3-A8AF-4E5233802EA0}
39+
{10B555A6-E4C9-4828-B4A6-64E248CC1D14} = {576FEFFC-B624-40C3-A8AF-4E5233802EA0}
4040
EndGlobalSection
4141
GlobalSection(ExtensibilityGlobals) = postSolution
4242
SolutionGuid = {93A268DC-0947-4FBB-B495-DDAD4B013D82}

src/Cloudikka.PolylineAlgorithm/Cloudikka.PolylineAlgorithm/Cloudikka.PolylineAlgorithm.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
</PropertyGroup>
66

7+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
8+
<DocumentationFile></DocumentationFile>
9+
</PropertyGroup>
10+
11+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
12+
<DocumentationFile></DocumentationFile>
13+
</PropertyGroup>
14+
715
<ItemGroup>
816
<Compile Update="ExceptionMessageResource.Designer.cs">
917
<DesignTime>True</DesignTime>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// Copyright (c) Petr Šrámek. All rights reserved.
3+
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace Cloudikka.PolylineAlgorithm.Encoding {
7+
using System.Collections.Generic;
8+
9+
#region Interfaces
10+
11+
/// <summary>
12+
/// Defines the <see cref="IPolylineEncoding{T}" />
13+
/// </summary>
14+
/// <typeparam name="T"></typeparam>
15+
public interface IPolylineEncoding<T> {
16+
#region Methods
17+
18+
/// <summary>
19+
/// The Decode
20+
/// </summary>
21+
/// <param name="source">The <see cref="string"/></param>
22+
/// <returns>The <see cref="IEnumerable{T}"/></returns>
23+
IEnumerable<T> Decode(string source);
24+
25+
/// <summary>
26+
/// The Encode
27+
/// </summary>
28+
/// <param name="source">The <see cref="IEnumerable{T}"/></param>
29+
/// <returns>The <see cref="string"/></returns>
30+
string Encode(IEnumerable<T> source);
31+
32+
#endregion
33+
}
34+
35+
#endregion
36+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// Copyright (c) Petr Šrámek. All rights reserved.
3+
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace Cloudikka.PolylineAlgorithm.Encoding {
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
11+
/// <summary>
12+
/// Defines the <see cref="PolylineEncoding{T}" />
13+
/// </summary>
14+
/// <typeparam name="T"></typeparam>
15+
public abstract class PolylineEncoding<T> : IPolylineEncoding<T> {
16+
#region Methods
17+
18+
/// <summary>
19+
/// The Decode
20+
/// </summary>
21+
/// <param name="source">The <see cref="string"/></param>
22+
/// <returns>The <see cref="IEnumerable{T}"/></returns>
23+
public IEnumerable<T> Decode(string source) {
24+
if (String.IsNullOrEmpty(source)) {
25+
throw new ArgumentException(ExceptionMessageResource.SourcePolylineStringCannotBeNullOrEmpty, nameof(source));
26+
}
27+
28+
char[] polyline = source.ToCharArray();
29+
30+
return PolylineAlgorithm.Decode(polyline)
31+
.Select(c => CreateResult(c.Latitude, c.Longitude));
32+
}
33+
34+
/// <summary>
35+
/// The Encode
36+
/// </summary>
37+
/// <param name="source">The <see cref="IEnumerable{T}"/></param>
38+
/// <returns>The <see cref="string"/></returns>
39+
public string Encode(IEnumerable<T> source) {
40+
if (source == null || !source.Any()) {
41+
throw new ArgumentException(ExceptionMessageResource.SourceCharArrayCannotBeNullOrEmpty, nameof(source));
42+
}
43+
44+
var coordinates = source.Select(s => GetCoordinate(s));
45+
46+
return PolylineAlgorithm.Encode(coordinates);
47+
}
48+
49+
/// <summary>
50+
/// The CreateResult
51+
/// </summary>
52+
/// <param name="latitude">The <see cref="double"/></param>
53+
/// <param name="longitude">The <see cref="double"/></param>
54+
/// <returns>The <see cref="T"/></returns>
55+
protected abstract T CreateResult(double latitude, double longitude);
56+
57+
/// <summary>
58+
/// The GetCoordinates
59+
/// </summary>
60+
/// <param name="source">The <see cref="IEnumerable{T}"/></param>
61+
/// <returns>The <see cref="IEnumerable{(double Latitude, double Longitude)}"/></returns>
62+
protected abstract (double Latitude, double Longitude) GetCoordinate(T source);
63+
64+
#endregion
65+
}
66+
}

src/Cloudikka.PolylineAlgorithm/Cloudikka.PolylineAlgorithm/TuplePolylineDecoder.cs renamed to src/Cloudikka.PolylineAlgorithm/Cloudikka.PolylineAlgorithm/Encoding/TuplePolylineEncoding.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
44
//
55

6-
namespace Cloudikka.PolylineAlgorithm {
6+
namespace Cloudikka.PolylineAlgorithm.Encoding {
77
/// <summary>
8-
/// Defines the <see cref="TuplePolylineDecoder" />
8+
/// Defines the <see cref="TuplePolylineEncoding" />
99
/// </summary>
10-
public class TuplePolylineDecoder : PolylineDecoderBase<(double Latitude, double Longitude)> {
10+
public sealed class TuplePolylineEncoding : PolylineEncoding<(double Latitude, double Longitude)> {
1111
#region Methods
1212

1313
/// <summary>
@@ -20,6 +20,15 @@ protected override (double Latitude, double Longitude) CreateResult(double latit
2020
return (latitude, longitude);
2121
}
2222

23+
/// <summary>
24+
/// The GetCoordinate
25+
/// </summary>
26+
/// <param name="source">The <see cref="(double Latitude, double Longitude)"/></param>
27+
/// <returns>The <see cref="(double Latitude, double Longitude)"/></returns>
28+
protected override (double Latitude, double Longitude) GetCoordinate((double Latitude, double Longitude) source) {
29+
return source;
30+
}
31+
2332
#endregion
2433
}
2534
}

src/Cloudikka.PolylineAlgorithm/Cloudikka.PolylineAlgorithm/ExceptionMessageResource.Designer.cs

Lines changed: 104 additions & 87 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)