Skip to content

Latest commit

 

History

History

README.md

PolylineAlgorithm for .NET

A modern, fully compliant Google Encoded Polyline Algorithm library for .NET Standard 2.1+, supporting strong input validation, extensibility for custom coordinate types, and robust performance.

Features

  • Google-compliant polyline encoding/decoding for geographic coordinates
  • Extensible APIs for custom coordinate and polyline types (AbstractPolylineEncoder<TCoordinate, TPolyline>, AbstractPolylineDecoder<TPolyline, TCoordinate>)
  • Extension methods for encoding from List<T> and arrays (PolylineEncoderExtensions)
  • Robust input validation and descriptive exceptions
  • Configurable with PolylineEncodingOptions (precision, buffer size, logging)
  • Thread-safe, stateless APIs
  • Low-level utilities via static PolylineEncoding class (Normalize, Denormalize, TryReadValue, TryWriteValue, ValidateFormat, etc.)
  • Benchmarks and unit tests for correctness and performance
  • Auto-generated API docs (API Reference)
  • Supports .NET Core, .NET 5+, Xamarin, Unity, Blazor via netstandard2.1

Installation

dotnet add package PolylineAlgorithm

or via NuGet PMC:

Install-Package PolylineAlgorithm

Quick Start

The library provides abstract base classes to build your own encoder and decoder for any coordinate and polyline type.

Implement a custom encoder

using PolylineAlgorithm;
using PolylineAlgorithm.Abstraction;

public sealed class MyPolylineEncoder : AbstractPolylineEncoder<(double Latitude, double Longitude), string> {
    protected override double GetLatitude((double Latitude, double Longitude) coordinate) => coordinate.Latitude;
    protected override double GetLongitude((double Latitude, double Longitude) coordinate) => coordinate.Longitude;
    protected override string CreatePolyline(ReadOnlyMemory<char> polyline) => polyline.ToString();
}

Encode coordinates

using PolylineAlgorithm.Extensions;

var coordinates = new List<(double Latitude, double Longitude)>
{
    (48.858370, 2.294481),
    (51.500729, -0.124625)
};

var encoder = new MyPolylineEncoder();
string encoded = encoder.Encode(coordinates); // extension method for List<T>

Console.WriteLine(encoded);

Implement a custom decoder

using PolylineAlgorithm;
using PolylineAlgorithm.Abstraction;

public sealed class MyPolylineDecoder : AbstractPolylineDecoder<string, (double Latitude, double Longitude)> {
    protected override (double Latitude, double Longitude) CreateCoordinate(double latitude, double longitude) => (latitude, longitude);
    protected override ReadOnlyMemory<char> GetReadOnlyMemory(in string polyline) => polyline.AsMemory();
}

Decode polyline

string encoded = "yseiHoc_MwacOjnwM";

var decoder = new MyPolylineDecoder();
IEnumerable<(double Latitude, double Longitude)> decoded = decoder.Decode(encoded);

Advanced Usage

  • Pass a PolylineEncodingOptions (built via PolylineEncodingOptionsBuilder) to the encoder/decoder constructor for custom precision, stack-alloc limit, and logging.
  • Use static methods on PolylineEncoding for low-level normalization, validation, and bit-level read/write operations.

See API Reference for full documentation.

FAQ

  • What coordinate ranges are valid?
    Latitude: -90..90, Longitude: -180..180 (throws ArgumentOutOfRangeException for invalid input)
  • What .NET versions are supported?
    Any environment supporting netstandard2.1
  • How do I customize encoder options?
    Use PolylineEncodingOptionsBuilder and pass the built options to the encoder or decoder constructor.
  • Where can I get help?
    GitHub issues

License

MIT License © Pete Sramek