1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
using System.Text.RegularExpressions;
using Enmarcha.Backend.Types.Arrivals;
namespace Enmarcha.Backend.Services;
public class FeedService
{
private static readonly Regex RemoveQuotationMarks = new(@"[""”]", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static readonly Regex StreetNameRegex = new(@"^(.*?)(?:,|\s\s|\s-\s| \d| S\/N|\s\()", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static readonly Dictionary<string, string> NameReplacements = new(StringComparer.OrdinalIgnoreCase)
{
{ "Rúa da Salguera Entrada", "Rúa da Salgueira" },
{ "Rúa da Salgueira Entrada", "Rúa da Salgueira" },
{ "Estrada de Miraflores", "Estrada Miraflores" },
{ "Avda. de Europa", "Avda. Europa" },
{ "Avda. de Galicia", "Avda. Galicia" },
{ "Avda. de Vigo", "Avda. Vigo" },
{ "FORA DE SERVIZO.G.B.", "" },
{ "Praza de Fernando O Católico", "" },
{ "Rúa da Travesía de Vigo", "Travesía de Vigo" },
{ "Rúa de ", " " },
{ "Rúa do ", " " },
{ "Rúa da ", " " },
{ "Rúa das ", " " },
{ "Avda. de ", " " },
{ "Avda. do ", " " },
{ "Avda. da ", " " },
{ "Avda. das ", " " },
{ "Riós", "Ríos" },
{ "Avda. Beiramar Porto Pesqueiro Berbés", "Berbés" },
{ "Conde de Torrecedeira", "Torrecedeira" },
};
public (string Color, string TextColor) GetFallbackColourForFeed(string feed)
{
return feed switch
{
"vitrasa" => ("#81D002", "#000000"),
"tussa" => ("#508096", "#FFFFFF"),
"tranvias" => ("#E61C29", "#FFFFFF"),
"ourense" => ("#ffb319", "#000000"),
"xunta" => ("#007BC4", "#FFFFFF"),
"renfe" => ("#870164", "#FFFFFF"),
"feve" => ("#EE3D32", "#FFFFFF"),
_ => ("#000000", "#FFFFFF"),
};
}
public string NormalizeStopCode(string feedId, string code)
{
if (feedId == "vitrasa")
{
var digits = new string(code.Where(char.IsDigit).ToArray());
if (int.TryParse(digits, out int numericCode))
{
return numericCode.ToString();
}
}
return code;
}
public string NormalizeRouteShortName(string feedId, string shortName)
{
if (feedId == "xunta" && shortName.StartsWith("XG"))
{
if (shortName.Length >= 8)
{
// XG817014 -> 817.14
var contract = shortName.Substring(2, 3);
var lineStr = shortName.Substring(5);
if (int.TryParse(lineStr, out int line))
{
return $"{contract}.{line:D2}";
}
}
else if (shortName.Length > 2)
{
// XG883 -> 883
return shortName.Substring(2);
}
}
return shortName;
}
public string GetUniqueRouteShortName(string feedId, string shortName)
{
if (feedId == "xunta" && shortName.StartsWith("XG") && shortName.Length >= 8)
{
var contract = shortName.Substring(2, 3);
return $"XG{contract}";
}
return NormalizeRouteShortName(feedId, shortName);
}
/// <summary>
/// When 5 or more distinct routes share the same 3-character short-name prefix,
/// they are collapsed into a single entry showing "XG{prefix}" (xunta feed only).
/// </summary>
private const int RouteCollapseThreshold = 5;
/// <summary>
/// Deduplicates routes by <see cref="RouteInfo.ShortName"/> (always). For the xunta feed only,
/// also collapses groups of <see cref="RouteCollapseThreshold"/> or more routes that share the
/// same 3-character prefix into a single entry named "XG{prefix}" (e.g. "XG621").
/// Other feeds are returned deduplicated but otherwise unchanged.
/// </summary>
public List<RouteInfo> ConsolidateRoutes(string feedId, IEnumerable<RouteInfo> routes)
{
// Deduplicate by short name (case-insensitive)
var deduplicated = routes
.GroupBy(r => r.ShortName, StringComparer.OrdinalIgnoreCase)
.Select(g => g.First())
.ToList();
// Prefix collapsing only applies to xunta routes, which can have dozens of
// sub-routes per contract that would otherwise flood the badge list.
if (feedId != "xunta")
{
return deduplicated;
}
// Group by the first 3 characters; collapse groups meeting the threshold.
// When collapsing, the first entry's colour is used — routes in the same prefix
// group (e.g. all xunta "621.*" lines) share the same operator colour.
var result = new List<RouteInfo>();
foreach (var group in deduplicated.GroupBy(r => r.ShortName.Length >= 3 ? r.ShortName[..3] : r.ShortName))
{
var items = group.ToList();
if (items.Count >= RouteCollapseThreshold)
{
result.Add(new RouteInfo
{
GtfsId = items[0].GtfsId,
ShortName = $"XG{group.Key}",
Colour = items[0].Colour,
TextColour = items[0].TextColour
});
}
else
{
result.AddRange(items);
}
}
return result;
}
public string NormalizeStopName(string feedId, string name)
{
if (feedId == "vitrasa")
{
return name
.Trim()
.Replace("\"", "")
.Replace(" ", ", ")
.Trim();
}
return name;
}
public string NormalizeRouteNameForMatching(string name)
{
var normalized = name.Trim().ToLowerInvariant();
// Remove diacritics/accents
normalized = Regex.Replace(normalized.Normalize(System.Text.NormalizationForm.FormD), @"\p{Mn}", "");
// Keep only alphanumeric
return Regex.Replace(normalized, @"[^a-z0-9]", "");
}
public string GetStreetName(string originalName)
{
var name = RemoveQuotationMarks.Replace(originalName, "").Trim();
var match = StreetNameRegex.Match(name);
var streetName = match.Success ? match.Groups[1].Value : name;
foreach (var replacement in NameReplacements)
{
if (streetName.Contains(replacement.Key, StringComparison.OrdinalIgnoreCase))
{
streetName = streetName.Replace(replacement.Key, replacement.Value, StringComparison.OrdinalIgnoreCase);
return streetName.Trim();
}
}
return streetName.Trim();
}
public string? GenerateMarquee(string feedId, List<string> nextStops)
{
if (nextStops.Count == 0) return null;
if (feedId is "vitrasa" or "tranvias" or "tussa" or "ourense")
{
var streets = nextStops
.Select(GetStreetName)
.Where(s => !string.IsNullOrWhiteSpace(s))
.Distinct()
.ToList();
return string.Join(" - ", streets);
}
return feedId switch
{
"xunta" => string.Join(" > ", nextStops),
"renfe" => string.Join(" - ", nextStops),
_ => string.Join(", ", nextStops.Take(4))
};
}
public bool IsStopHidden(string stopId)
{
return HiddenStops.Contains(stopId);
}
public ShiftBadge? GetShiftBadge(string feedId, string tripId)
{
if (feedId != "vitrasa") return null;
// Example: C1 04LN 02_001004_4
var parts = tripId.Split('_');
if (parts.Length < 2) return null;
var shiftGroup = parts[parts.Length - 2]; // 001004
var tripNumber = parts[parts.Length - 1]; // 4
if (shiftGroup.Length != 6) return null;
if (!int.TryParse(shiftGroup.Substring(0, 3), out var routeNum)) return null;
if (!int.TryParse(shiftGroup.Substring(3, 3), out var shiftNum)) return null;
var routeName = routeNum switch
{
1 => "C1",
3 => "C3",
30 => "N1",
33 => "N4",
8 => "A",
101 => "H",
201 => "U1",
202 => "U2",
150 => "REF",
500 => "TUR",
_ => $"L{routeNum}"
};
return new ShiftBadge
{
ShiftName = $"{routeName}-{shiftNum}",
ShiftTrip = tripNumber
};
}
private static readonly string[] HiddenStops =
[
"vitrasa:20223", // Castrelos (Pavillón - U1)
"vitrasa:20146", // García Barbón, 7 (A, 18A)
"vitrasa:20220", // COIA-SAMIL (15)
"vitrasa:20001", // Samil por Beiramar (15B)
"vitrasa:20002", // Samil por Torrecedeira (15C)
"vitrasa:20144", // Samil por Coia (C3d, C3i)
"vitrasa:20145" // Samil por Bouzs (C3d, C3i)
];
}
|