Skip to content

Commit b4e6d43

Browse files
Copilotpetesramek
andauthored
fix: WithCreate receives denormalized doubles; replace sample wrapper classes with direct PolylineEncoder/Decoder usage
Agent-Logs-Url: https://github.com/petesramek/polyline-algorithm-csharp/sessions/76e69a37-a3c2-450f-8078-46133933fc61 Co-authored-by: petesramek <2333452+petesramek@users.noreply.github.com>
1 parent 3b971cc commit b4e6d43

17 files changed

Lines changed: 85 additions & 394 deletions

File tree

benchmarks/PolylineAlgorithm.Benchmarks/PolylineDecoderBenchmark.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class PolylineDecoderBenchmark {
5757
FormatterBuilder<(double Latitude, double Longitude), T>.Create()
5858
.AddValue("lat", static c => c.Latitude)
5959
.AddValue("lon", static c => c.Longitude)
60-
.WithCreate(static v => (v[0] / 1e5, v[1] / 1e5))
60+
.WithCreate(static v => (v[0], v[1]))
6161
.ForPolyline(write, read)
6262
.Build();
6363

samples/PolylineAlgorithm.NetTopologySuite.Sample/NetTopologyPolylineDecoder.cs

Lines changed: 0 additions & 42 deletions
This file was deleted.

samples/PolylineAlgorithm.NetTopologySuite.Sample/NetTopologyPolylineEncoder.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.

samples/PolylineAlgorithm.NetTopologySuite.Sample/Program.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,27 @@
44
//
55

66
using NetTopologySuite.Geometries;
7-
using PolylineAlgorithm.NetTopologySuite.Sample;
7+
using PolylineAlgorithm;
88

99
public class Program {
1010
public static void Main(string[] args) {
11+
// Build a formatter for NetTopologySuite's Point type.
12+
// NTS convention: Y = latitude, X = longitude.
13+
PolylineFormatter<Point, string> formatter =
14+
FormatterBuilder<Point, string>.Create()
15+
.AddValue("lat", static p => p.Y)
16+
.AddValue("lon", static p => p.X)
17+
// The formatter automatically denormalizes scaled values, so v[0] = latitude, v[1] = longitude.
18+
.WithCreate(static v => new Point(x: v[1], y: v[0]))
19+
.ForPolyline(
20+
static m => m.IsEmpty ? string.Empty : new string(m.Span),
21+
static s => s.AsMemory())
22+
.Build();
23+
24+
PolylineOptions<Point, string> options = new(formatter);
25+
PolylineEncoder<Point, string> encoder = new(options);
26+
PolylineDecoder<string, Point> decoder = new(options);
27+
1128
// Sample route: Seattle → Bellevue → Redmond
1229
var points = new Point[]
1330
{
@@ -16,9 +33,6 @@ public static void Main(string[] args) {
1633
new(x: -122.1215, y: 47.6740), // Redmond
1734
};
1835

19-
var encoder = new NetTopologyPolylineEncoder();
20-
var decoder = new NetTopologyPolylineDecoder();
21-
2236
// Encode
2337
string encoded = encoder.Encode(points);
2438

samples/PolylineAlgorithm.SensorData.Sample/Program.cs

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

6+
using PolylineAlgorithm;
67
using PolylineAlgorithm.SensorData.Sample;
8+
using System;
79

810
public static class Program {
11+
// 2020-01-01 00:00:00 UTC in Unix seconds. Used as the delta-encoding baseline for timestamps
12+
// so that the first absolute delta stays within the int32 safe range of the polyline algorithm.
13+
private const long TimestampBaseEpoch = 1_577_836_800L;
14+
915
public static void Main(string[] args) {
16+
// Build a formatter for SensorReading: timestamp (Unix seconds, precision 0) + temperature.
17+
// SetBaseline keeps the first timestamp delta small; the formatter adds it back when decoding.
18+
PolylineFormatter<SensorReading, string> formatter =
19+
FormatterBuilder<SensorReading, string>.Create()
20+
.AddValue("timestamp", static r => (double)r.Timestamp.ToUnixTimeSeconds(), precision: 0)
21+
.SetBaseline(TimestampBaseEpoch)
22+
.AddValue("temperature", static r => r.Temperature, precision: 5)
23+
// The formatter automatically denormalizes: v[0] = Unix seconds, v[1] = temperature.
24+
.WithCreate(static v => new SensorReading(
25+
DateTimeOffset.FromUnixTimeSeconds((long)v[0]),
26+
v[1]))
27+
.ForPolyline(static m => new string(m.Span), static s => s.AsMemory())
28+
.Build();
29+
30+
PolylineOptions<SensorReading, string> options = new(formatter);
31+
PolylineEncoder<SensorReading, string> encoder = new(options);
32+
PolylineDecoder<string, SensorReading> decoder = new(options);
33+
1034
// Sample temperature readings from a sensor over six seconds
1135
var readings = new SensorReading[]
1236
{
@@ -19,9 +43,6 @@ public static void Main(string[] args) {
1943
new(DateTimeOffset.UtcNow.AddSeconds(6), 22.3),
2044
};
2145

22-
var encoder = new SensorDataEncoder();
23-
var decoder = new SensorDataDecoder();
24-
2546
// Encode
2647
string encoded = encoder.Encode(readings);
2748

samples/PolylineAlgorithm.SensorData.Sample/SensorDataDecoder.cs

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)