From f9da091072e468420c713bdb3ad079c65046f8e4 Mon Sep 17 00:00:00 2001 From: "Wei \"oldrev\" Li" Date: Sun, 18 Feb 2018 12:04:48 +0800 Subject: [PATCH] time related from long to DateTimeOffset --- .../Program.cs | 4 ++-- src/Sandwych.MapMatchingKit/Markov/ISample.cs | 2 +- .../Markov/IStateMemory.cs | 2 +- src/Sandwych.MapMatchingKit/Markov/KState.cs | 6 ++--- .../Matching/Matcher.cs | 5 ++-- .../Matching/MatcherSample.cs | 12 +++++----- src/Sandwych.MapMatchingKit/Roads/RoadMap.cs | 24 +++++++++++-------- .../Markov/MockSample.cs | 4 ++-- .../Matching/MatcherTest.cs | 2 +- 9 files changed, 32 insertions(+), 29 deletions(-) diff --git a/examples/Sandwych.MapMatchingKit.Examples.HelloWorldApp/Program.cs b/examples/Sandwych.MapMatchingKit.Examples.HelloWorldApp/Program.cs index cc99e22..3d7ef92 100644 --- a/examples/Sandwych.MapMatchingKit.Examples.HelloWorldApp/Program.cs +++ b/examples/Sandwych.MapMatchingKit.Examples.HelloWorldApp/Program.cs @@ -60,7 +60,7 @@ static void Main(string[] args) if (cand.HasTransition) { var geom = cand.Transition.Route.ToGeometry(); // path geometry from last matching candidate - Console.WriteLine("RoadID={0}\t\tFraction={1}", roadId, cand.Point.Fraction); + Console.WriteLine("Lng={0}\tLat={1}\tRoadID={2}", coord.X, coord.Y, roadId, cand.Point.Fraction); } } @@ -82,7 +82,7 @@ private static IEnumerable ReadSamples() var timeStr = i.Properties["field_8"].ToString().Substring(0, timeFormat.Length); var time = DateTimeOffset.ParseExact(timeStr, timeFormat, CultureInfo.InvariantCulture); var longTime = time.ToUnixTimeMilliseconds(); - yield return new MatcherSample(longTime, longTime, coord2D); + yield return new MatcherSample(longTime, time, coord2D); } } diff --git a/src/Sandwych.MapMatchingKit/Markov/ISample.cs b/src/Sandwych.MapMatchingKit/Markov/ISample.cs index 505a6b6..6e6330b 100644 --- a/src/Sandwych.MapMatchingKit/Markov/ISample.cs +++ b/src/Sandwych.MapMatchingKit/Markov/ISample.cs @@ -6,6 +6,6 @@ namespace Sandwych.MapMatchingKit.Markov { public interface ISample { - long Time { get; } + DateTimeOffset Time { get; } } } diff --git a/src/Sandwych.MapMatchingKit/Markov/IStateMemory.cs b/src/Sandwych.MapMatchingKit/Markov/IStateMemory.cs index 447ae07..3c9732c 100644 --- a/src/Sandwych.MapMatchingKit/Markov/IStateMemory.cs +++ b/src/Sandwych.MapMatchingKit/Markov/IStateMemory.cs @@ -41,7 +41,7 @@ public interface IStateMemory /// /// Time of the last state update in milliseconds epoch time. /// - long Time { get; } + DateTimeOffset Time { get; } /// diff --git a/src/Sandwych.MapMatchingKit/Markov/KState.cs b/src/Sandwych.MapMatchingKit/Markov/KState.cs index 3c2acd0..1fbfba2 100644 --- a/src/Sandwych.MapMatchingKit/Markov/KState.cs +++ b/src/Sandwych.MapMatchingKit/Markov/KState.cs @@ -71,13 +71,13 @@ public TSample Sample } } - public long Time + public DateTimeOffset Time { get { if (_sequence.Count == 0) { - return -1; + return DateTimeOffset.MaxValue; } else { @@ -154,7 +154,7 @@ public void Update(ICollection vector, in TSample sample) _sequence.AddToBack((vector, sample, kestimate)); - while ((_t > 0 && sample.Time - _sequence.PeekFirst().Item2.Time > _t) + while ((_t > 0 && (sample.Time - _sequence.PeekFirst().Item2.Time).TotalMilliseconds > _t) || (_k >= 0 && _sequence.Count > _k + 1)) { var deletes = _sequence.PeekFirst().Item1; diff --git a/src/Sandwych.MapMatchingKit/Matching/Matcher.cs b/src/Sandwych.MapMatchingKit/Matching/Matcher.cs index 4e4a0d8..e569812 100644 --- a/src/Sandwych.MapMatchingKit/Matching/Matcher.cs +++ b/src/Sandwych.MapMatchingKit/Matching/Matcher.cs @@ -145,7 +145,7 @@ public override IDictionary>(); var base_ = 1.0 * _spatial.Distance(predecessors.Item1.Coordinate, candidates.Item1.Coordinate) / 60.0; - var bound = Math.Max(1000.0, Math.Min(this.MaxDistance, ((candidates.Item1.Time - predecessors.Item1.Time) / 1000.0) * 100.0)); + var bound = Math.Max(1000.0, Math.Min(this.MaxDistance, (candidates.Item1.Time - predecessors.Item1.Time).TotalSeconds * 100.0)); foreach (var predecessor in predecessors.Item2) { @@ -165,8 +165,7 @@ public override IDictionary this.Id < 0; @@ -23,23 +23,23 @@ public sealed class MatcherSample : ISample public MatcherSample(long id, long time, double lng, double lat, float azimuth = float.NaN) { this.Id = id; - this.Time = time; + this.Time = DateTimeOffset.MinValue.AddMilliseconds(time); this.Coordinate = new Coordinate2D(lng, lat); this.Azimuth = NormAzimuth(azimuth); } - public MatcherSample(long id, long time, Coordinate2D point, float azimuth = float.NaN) + public MatcherSample(long id, long time, in Coordinate2D point, float azimuth = float.NaN) { this.Id = id; - this.Time = time; + this.Time = DateTimeOffset.MinValue.AddMilliseconds(time); this.Coordinate = point; this.Azimuth = NormAzimuth(azimuth); } - public MatcherSample(long id, DateTime time, Coordinate2D point, float azimuth = float.NaN) + public MatcherSample(long id, DateTimeOffset time, in Coordinate2D point, float azimuth = float.NaN) { this.Id = id; - this.Time = time.ToFileTime(); + this.Time = time; this.Coordinate = point; this.Azimuth = NormAzimuth(azimuth); } diff --git a/src/Sandwych.MapMatchingKit/Roads/RoadMap.cs b/src/Sandwych.MapMatchingKit/Roads/RoadMap.cs index 20e0eb4..c14936c 100644 --- a/src/Sandwych.MapMatchingKit/Roads/RoadMap.cs +++ b/src/Sandwych.MapMatchingKit/Roads/RoadMap.cs @@ -1,17 +1,22 @@ using System; using System.Linq; -using Sandwych.MapMatchingKit.Topology; -using System.Collections.Generic; using System.Text; +using System.Collections.Generic; +using Sandwych.MapMatchingKit.Topology; using Sandwych.MapMatchingKit.Spatial; -using NetTopologySuite.Geometries; -using GeoAPI.Geometries; using Sandwych.MapMatchingKit.Spatial.Geometries; using Sandwych.MapMatchingKit.Spatial.Index; namespace Sandwych.MapMatchingKit.Roads { - public class RoadMap : AbstractGraph + /// Implementation of a road map with (directed) roads, i.e. {@link Road} objects. It provides a road + /// network for routing that is derived from {@link Graph} and spatial search of roads with a + /// {@link SpatialIndex}. + /// + /// Note: Since {@link Road} objects are directed representations of {@link BaseRoad} objects, + /// identifiers have a special mapping, see {@link Road}. + /// + public sealed class RoadMap : AbstractGraph { public ISpatialIndex Index { get; } private readonly ISpatialOperation _spatial; @@ -26,26 +31,25 @@ public RoadMap(IEnumerable roads) : this(roads, GeographySpatialOperation. { } - private IEnumerable Split(IEnumerable<(RoadInfo road, double distance)> points) + private IEnumerable Split(IEnumerable<(RoadInfo road, double fraction)> points) { /* * This uses the road */ foreach (var point in points) { - yield return new RoadPoint(this.Edges[point.road.Id * 2], point.Item2, _spatial); + yield return new RoadPoint(this.Edges[point.road.Id * 2], point.fraction, _spatial); var backwardRoadId = point.road.Id * 2 + 1; if (this.Edges.TryGetValue(backwardRoadId, out var road)) { - yield return new RoadPoint(road, 1.0 - point.Item2, _spatial); + yield return new RoadPoint(road, 1.0 - point.fraction, _spatial); } } } - public IEnumerable Radius(Coordinate2D c, double r) => + public IEnumerable Radius(in Coordinate2D c, double r) => this.Split(this.Index.Radius(c, r)); - } } diff --git a/test/Sandwych.MapMatchingKit.Tests/Markov/MockSample.cs b/test/Sandwych.MapMatchingKit.Tests/Markov/MockSample.cs index b6e41c7..32a4e3b 100644 --- a/test/Sandwych.MapMatchingKit.Tests/Markov/MockSample.cs +++ b/test/Sandwych.MapMatchingKit.Tests/Markov/MockSample.cs @@ -7,11 +7,11 @@ namespace Sandwych.MapMatchingKit.Tests.Markov { public sealed class MockSample : ISample { - public long Time { get; } + public DateTimeOffset Time { get; } public MockSample(long time) { - this.Time = time; + this.Time = DateTimeOffset.MinValue.AddMilliseconds(time); } } } diff --git a/test/Sandwych.MapMatchingKit.Tests/Matching/MatcherTest.cs b/test/Sandwych.MapMatchingKit.Tests/Matching/MatcherTest.cs index 02179d5..f73361f 100644 --- a/test/Sandwych.MapMatchingKit.Tests/Matching/MatcherTest.cs +++ b/test/Sandwych.MapMatchingKit.Tests/Matching/MatcherTest.cs @@ -83,7 +83,7 @@ private void AssertTransition(in TransitionProbability transi Assert.Equal(route.StartPoint.Edge.Id, transition.Transition.Route.StartPoint.Edge.Id); Assert.Equal(route.EndPoint.Edge.Id, transition.Transition.Route.EndPoint.Edge.Id); - double beta = lambda == 0 ? (2.0 * (target.Item2.Time - source.Item2.Time) / 1000) + double beta = lambda == 0 ? (2.0 * (target.Item2.Time - source.Item2.Time).TotalSeconds) : 1 / lambda; double @base = 1.0 * _spatial.Distance(source.Item2.Coordinate, target.Item2.Coordinate) / 60; double p = (1 / beta)