Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -82,7 +82,7 @@ private static IEnumerable<MatcherSample> 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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sandwych.MapMatchingKit/Markov/ISample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace Sandwych.MapMatchingKit.Markov
{
public interface ISample
{
long Time { get; }
DateTimeOffset Time { get; }
}
}
2 changes: 1 addition & 1 deletion src/Sandwych.MapMatchingKit/Markov/IStateMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public interface IStateMemory<TCandidate, TTransition, TSample>
/// <summary>
/// Time of the last state update in milliseconds epoch time.
/// </summary>
long Time { get; }
DateTimeOffset Time { get; }


/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Sandwych.MapMatchingKit/Markov/KState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ public TSample Sample
}
}

public long Time
public DateTimeOffset Time
{
get
{
if (_sequence.Count == 0)
{
return -1;
return DateTimeOffset.MaxValue;
}
else
{
Expand Down Expand Up @@ -154,7 +154,7 @@ public void Update(ICollection<TCandidate> 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;
Expand Down
5 changes: 2 additions & 3 deletions src/Sandwych.MapMatchingKit/Matching/Matcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public override IDictionary<MatcherCandidate, IDictionary<MatcherCandidate, Tran

var transitions = new Dictionary<MatcherCandidate, IDictionary<MatcherCandidate, TransitionProbability>>();
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)
{
Expand All @@ -165,8 +165,7 @@ public override IDictionary<MatcherCandidate, IDictionary<MatcherCandidate, Tran
// route.length() - dt)) to avoid unnecessary routes in case of u-turns.

var beta = this.Lambda == 0D
? (2.0 * Math.Max(1d,
candidates.Item1.Time - predecessors.Item1.Time) / 1000D)
? (2.0 * Math.Max(1d, (candidates.Item1.Time - predecessors.Item1.Time).TotalMilliseconds) / 1000D)
: 1D / this.Lambda;

var transition = (1D / beta) * Math.Exp(
Expand Down
12 changes: 6 additions & 6 deletions src/Sandwych.MapMatchingKit/Matching/MatcherSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ namespace Sandwych.MapMatchingKit.Matching
public sealed class MatcherSample : ISample
{
public long Id { get; }
public long Time { get; }
public DateTimeOffset Time { get; }
public float Azimuth { get; }
public Coordinate2D Coordinate { get; }
public bool IsNaN => this.Id < 0;

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);
}
Expand Down
24 changes: 14 additions & 10 deletions src/Sandwych.MapMatchingKit/Roads/RoadMap.cs
Original file line number Diff line number Diff line change
@@ -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<Road>
/// 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}.
/// <para>
/// <b>Note:</b> Since {@link Road} objects are directed representations of {@link BaseRoad} objects,
/// identifiers have a special mapping, see {@link Road}.
/// </para>
public sealed class RoadMap : AbstractGraph<Road>
{
public ISpatialIndex<RoadInfo> Index { get; }
private readonly ISpatialOperation _spatial;
Expand All @@ -26,26 +31,25 @@ public RoadMap(IEnumerable<Road> roads) : this(roads, GeographySpatialOperation.
{
}

private IEnumerable<RoadPoint> Split(IEnumerable<(RoadInfo road, double distance)> points)
private IEnumerable<RoadPoint> 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<RoadPoint> Radius(Coordinate2D c, double r) =>
public IEnumerable<RoadPoint> Radius(in Coordinate2D c, double r) =>
this.Split(this.Index.Radius(c, r));


}
}
4 changes: 2 additions & 2 deletions test/Sandwych.MapMatchingKit.Tests/Markov/MockSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion test/Sandwych.MapMatchingKit.Tests/Matching/MatcherTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private void AssertTransition(in TransitionProbability<MatcherTransition> 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)
Expand Down