|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Linq; |
4 | | -using GeoCoordinatePortable; |
5 | 4 | using Tracked.Models; |
6 | 5 |
|
7 | 6 | namespace Tracked.Utilities { |
8 | 7 | public static class PolyUtils { |
9 | | - public static bool HasPointOnLine(this IList<LatLng> path, LatLng point, int toleranceInMetres = 25) { |
10 | | - return path.Any(i => i.CalculateDistance(point) * 1000 <= toleranceInMetres); |
11 | | - } |
12 | | - |
13 | | - public static double CalculateDistanceKm(this IList<LatLng> path) { |
14 | | - double totalKm = 0; |
15 | | - |
16 | | - for (int i = 0; i < path.Count - 1; i++) { |
17 | | - var pin1 = GetGeoModel(path[i]); |
18 | | - var pin2 = GetGeoModel(path[i + 1]); |
19 | | - |
20 | | - var km = pin2.GetDistanceTo(pin1) / 1000; |
21 | | - totalKm += km; |
22 | | - } |
23 | | - |
24 | | - return totalKm; |
25 | | - } |
26 | | - |
27 | | - public static double CalculateDistanceKm(this IList<Location> path) { |
28 | | - return path.Select(i => i.Point) |
29 | | - .ToList() |
30 | | - .CalculateDistanceKm(); |
31 | | - } |
32 | | - |
33 | | - public static double CalculateDistanceMi(this IList<LatLng> path) { |
34 | | - return path.CalculateDistanceKm() * 0.621371192; |
35 | | - } |
36 | | - |
37 | | - public static double CalculateDistanceMi(this IList<Location> path) { |
38 | | - return path.CalculateDistanceKm() * 0.621371192; |
39 | | - } |
40 | | - |
41 | | - private static GeoCoordinate GetGeoModel(LatLng model) { |
42 | | - return new GeoCoordinate(model.Latitude, model.Longitude); |
43 | | - } |
44 | | - |
45 | | - public static double CalculateDistance(this LatLng latLong1, LatLng latLong2) { |
46 | | - return new List<LatLng>() { |
47 | | - latLong1, |
48 | | - latLong2, |
49 | | - }.CalculateDistanceKm(); |
50 | | - } |
51 | | - |
52 | | - public static LocationMatchResult LocationsMatch(Segment segment, IList<LatLng> rideLocations) { |
53 | | - bool matchesStart = rideLocations |
54 | | - .HasPointOnLine(segment.Start.Point); |
55 | | - |
56 | | - bool matchesEnd = rideLocations |
57 | | - .HasPointOnLine(segment.End.Point); |
58 | | - |
59 | | - if (!matchesStart || !matchesEnd) { |
60 | | - return new LocationMatchResult { |
61 | | - MatchesSegment = false, |
62 | | - }; |
63 | | - }; |
64 | | - |
65 | | - var closestPointToSegmentStart = segment.GetClosestStartPoint(rideLocations); |
66 | | - var closestPointToSegmentEnd = segment.GetClosestEndPoint(rideLocations); |
67 | | - |
68 | | - if (closestPointToSegmentStart == null || closestPointToSegmentEnd == null) { |
69 | | - return new LocationMatchResult { |
70 | | - MatchesSegment = false, |
71 | | - }; |
72 | | - } |
73 | | - |
74 | | - int startIdx = rideLocations.IndexOf(closestPointToSegmentStart); |
75 | | - int endIdx = rideLocations.IndexOf(closestPointToSegmentEnd); |
76 | | - |
77 | | - var filteredRideLocations = rideLocations.ToList().GetRange(startIdx, (endIdx - startIdx) + 1); |
78 | | - |
79 | | - int matchedPointCount = 0; |
80 | | - int missedPointCount = 0; |
81 | | - |
82 | | - foreach (var segmentLocation in segment.Points) { |
83 | | - if (filteredRideLocations.HasPointOnLine(segmentLocation.Point)) { |
84 | | - matchedPointCount++; |
85 | | - } else { |
86 | | - missedPointCount++; |
87 | | - } |
88 | | - } |
89 | | - |
90 | | - // return true if 90% of the segment points match the ride |
91 | | - return new LocationMatchResult { |
92 | | - MatchesSegment = matchedPointCount >= segment.Points.Count * 0.9, |
93 | | - StartIdx = startIdx, |
94 | | - EndIdx = endIdx, |
95 | | - }; |
96 | | - } |
97 | | - |
98 | 8 | public static IList<MapLocation> GetMapLocations(IRide ride) { |
99 | 9 | var locations = ride.Locations |
100 | 10 | .OrderBy(i => i.Timestamp) |
|
0 commit comments