aboutsummaryrefslogtreecommitdiff
path: root/src/Enmarcha.Backend/Services/Processors/AbstractProcessor.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Enmarcha.Backend/Services/Processors/AbstractProcessor.cs')
-rw-r--r--src/Enmarcha.Backend/Services/Processors/AbstractProcessor.cs56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/Enmarcha.Backend/Services/Processors/AbstractProcessor.cs b/src/Enmarcha.Backend/Services/Processors/AbstractProcessor.cs
new file mode 100644
index 0000000..d6b420f
--- /dev/null
+++ b/src/Enmarcha.Backend/Services/Processors/AbstractProcessor.cs
@@ -0,0 +1,56 @@
+using Enmarcha.Backend.Services;
+
+public abstract class AbstractRealTimeProcessor : IArrivalsProcessor
+{
+ public abstract Task ProcessAsync(ArrivalsContext context);
+
+ protected static List<(double Lat, double Lon)> Decode(string encodedPoints)
+ {
+ if (string.IsNullOrEmpty(encodedPoints))
+ return new List<(double, double)>();
+
+ var poly = new List<(double, double)>();
+ char[] polylineChars = encodedPoints.ToCharArray();
+ int index = 0;
+
+ int currentLat = 0;
+ int currentLng = 0;
+ int next5bits;
+ int sum;
+ int shifter;
+
+ while (index < polylineChars.Length)
+ {
+ // calculate next latitude
+ sum = 0;
+ shifter = 0;
+ do
+ {
+ next5bits = (int)polylineChars[index++] - 63;
+ sum |= (next5bits & 31) << shifter;
+ shifter += 5;
+ } while (next5bits >= 32 && index < polylineChars.Length);
+
+ if (index >= polylineChars.Length)
+ break;
+
+ currentLat += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
+
+ // calculate next longitude
+ sum = 0;
+ shifter = 0;
+ do
+ {
+ next5bits = (int)polylineChars[index++] - 63;
+ sum |= (next5bits & 31) << shifter;
+ shifter += 5;
+ } while (next5bits >= 32 && index < polylineChars.Length);
+
+ currentLng += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
+
+ poly.Add((Convert.ToDouble(currentLat) / 100000.0, Convert.ToDouble(currentLng) / 100000.0));
+ }
+
+ return poly;
+ }
+}