|
1 | | -namespace Resgrid.Config |
| 1 | +using System; |
| 2 | + |
| 3 | +namespace Resgrid.Config |
2 | 4 | { |
3 | 5 | public static class MappingConfig |
4 | 6 | { |
| 7 | + public const string LeafletMapProvider = "leaflet"; |
| 8 | + public const string MapboxMapProvider = "mapbox"; |
| 9 | + |
5 | 10 | public static int PersonnelLocationStaleSeconds = 30; |
6 | 11 | public static int UnitLocationStaleSeconds = 30; |
7 | 12 | public static int PersonnelLocationMinMeters = 20; |
@@ -53,64 +58,292 @@ public static class MappingConfig |
53 | 58 | public static string BigBoardOSMKey = ""; |
54 | 59 |
|
55 | 60 | public static string DispatchAppMapboxKey = ""; |
| 61 | + public static string WebsiteMapboxKey = ""; |
| 62 | + public static string WebsiteMapboxAccessToken = ""; |
| 63 | + public static string WebsiteMapMode = LeafletMapProvider; |
56 | 64 |
|
57 | 65 | public static string LeafletTileUrl = "https://api.maptiler.com/maps/streets/{{z}}/{{x}}/{{y}}.png?key={0}"; |
58 | 66 | public static string MapBoxTileUrl = ""; |
| 67 | + public static string MapBoxStyleUrl = ""; |
59 | 68 |
|
60 | 69 | public static string LeafletAttribution = "© OpenStreetMap contributors CC-BY-SA"; |
| 70 | + public static string MapBoxAttribution = "© Mapbox © OpenStreetMap contributors"; |
61 | 71 |
|
62 | 72 | /*********************************** |
63 | 73 | * Geocoding and Routing Service URLs |
64 | 74 | ***********************************/ |
65 | 75 | public static string NominatimUrl = "https://nominatim.openstreetmap.org"; |
66 | 76 | public static string OsrmUrl = "https://router.project-osrm.org"; |
67 | 77 |
|
| 78 | + public static ResolvedMapConfig GetMapConfig(string key) |
| 79 | + { |
| 80 | + var surfaceKey = string.IsNullOrWhiteSpace(key) ? InfoConfig.WebsiteKey : key; |
| 81 | + var mapProvider = GetPreferredMapProvider(surfaceKey); |
| 82 | + |
| 83 | + if (mapProvider == MapboxMapProvider) |
| 84 | + { |
| 85 | + var mapboxAccessToken = NormalizePublicMapboxAccessToken(GetSystemMapboxAccessToken(surfaceKey)); |
| 86 | + |
| 87 | + if (TryCreateMapboxConfig(MapBoxStyleUrl, mapboxAccessToken, false, out var mapboxConfig)) |
| 88 | + return mapboxConfig; |
| 89 | + |
| 90 | + if (!string.IsNullOrWhiteSpace(mapboxAccessToken) && !string.IsNullOrWhiteSpace(MapBoxTileUrl)) |
| 91 | + { |
| 92 | + return new ResolvedMapConfig |
| 93 | + { |
| 94 | + MapProvider = MapboxMapProvider, |
| 95 | + TileUrl = ReplaceTileKey(MapBoxTileUrl, mapboxAccessToken), |
| 96 | + StyleUrl = MapBoxStyleUrl, |
| 97 | + AccessToken = mapboxAccessToken, |
| 98 | + Attribution = MapBoxAttribution, |
| 99 | + IsDepartmentOverride = false |
| 100 | + }; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return new ResolvedMapConfig |
| 105 | + { |
| 106 | + MapProvider = LeafletMapProvider, |
| 107 | + TileUrl = GetLegacyLeafletUrl(surfaceKey), |
| 108 | + StyleUrl = string.Empty, |
| 109 | + AccessToken = string.Empty, |
| 110 | + Attribution = LeafletAttribution, |
| 111 | + IsDepartmentOverride = false |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + public static bool TryCreateMapboxConfig(string styleUrl, string accessToken, bool isDepartmentOverride, out ResolvedMapConfig mapConfig) |
| 116 | + { |
| 117 | + mapConfig = null; |
| 118 | + var publicAccessToken = NormalizePublicMapboxAccessToken(accessToken); |
| 119 | + |
| 120 | + if (string.IsNullOrWhiteSpace(styleUrl) || string.IsNullOrWhiteSpace(publicAccessToken)) |
| 121 | + return false; |
| 122 | + |
| 123 | + var styleId = GetMapboxStyleId(styleUrl); |
| 124 | + |
| 125 | + if (string.IsNullOrWhiteSpace(styleId)) |
| 126 | + return false; |
| 127 | + |
| 128 | + mapConfig = new ResolvedMapConfig |
| 129 | + { |
| 130 | + MapProvider = MapboxMapProvider, |
| 131 | + TileUrl = $"https://api.mapbox.com/styles/v1/{styleId}/tiles/256/{{z}}/{{x}}/{{y}}@2x?access_token={publicAccessToken}", |
| 132 | + StyleUrl = NormalizeMapboxStyleUrl(styleUrl, styleId), |
| 133 | + AccessToken = publicAccessToken, |
| 134 | + Attribution = MapBoxAttribution, |
| 135 | + IsDepartmentOverride = isDepartmentOverride |
| 136 | + }; |
| 137 | + |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | + public static bool IsSupportedMapboxStyleUrl(string styleUrl) |
| 142 | + { |
| 143 | + return !string.IsNullOrWhiteSpace(GetMapboxStyleId(styleUrl)); |
| 144 | + } |
| 145 | + |
68 | 146 | public static string GetWebsiteOSMUrl() |
69 | 147 | { |
70 | | - if (!string.IsNullOrWhiteSpace(WebsiteOSMKey)) |
71 | | - return string.Format(MapBoxTileUrl, WebsiteOSMKey); |
72 | | - else |
73 | | - return LeafletTileUrl; |
| 148 | + return GetMapConfig(InfoConfig.WebsiteKey).TileUrl; |
74 | 149 | } |
75 | 150 |
|
76 | 151 | public static string GetApiOSMUrl() |
77 | 152 | { |
78 | | - if (!string.IsNullOrWhiteSpace(ApiOSMKey)) |
79 | | - return string.Format(LeafletTileUrl, ApiOSMKey); |
80 | | - else |
81 | | - return LeafletTileUrl; |
| 153 | + return GetMapConfig(InfoConfig.ApiKey).TileUrl; |
82 | 154 | } |
83 | 155 |
|
84 | 156 | public static string GetResponderAppOSMUrl() |
85 | 157 | { |
86 | | - if (!string.IsNullOrWhiteSpace(ResponderAppOSMKey)) |
87 | | - return string.Format(LeafletTileUrl, ResponderAppOSMKey); |
88 | | - else |
89 | | - return LeafletTileUrl; |
| 158 | + return GetMapConfig(InfoConfig.ResponderAppKey).TileUrl; |
90 | 159 | } |
91 | 160 |
|
92 | 161 | public static string GetUnitAppOSMUrl() |
93 | 162 | { |
94 | | - if (!string.IsNullOrWhiteSpace(UnitAppOSMKey)) |
95 | | - return string.Format(LeafletTileUrl, UnitAppOSMKey); |
96 | | - else |
97 | | - return LeafletTileUrl; |
| 163 | + return GetMapConfig(InfoConfig.UnitAppKey).TileUrl; |
98 | 164 | } |
99 | 165 |
|
100 | 166 | public static string GetBigBoardAppOSMUrl() |
101 | 167 | { |
102 | | - if (!string.IsNullOrWhiteSpace(BigBoardOSMKey)) |
103 | | - return string.Format(LeafletTileUrl, BigBoardOSMKey); |
104 | | - else |
105 | | - return LeafletTileUrl; |
| 168 | + return GetMapConfig(InfoConfig.BigBoardKey).TileUrl; |
106 | 169 | } |
107 | 170 |
|
108 | 171 | public static string GetDispatchAppOSMUrl() |
109 | 172 | { |
110 | | - if (!string.IsNullOrWhiteSpace(DispatchAppMapboxKey)) |
111 | | - return string.Format(MapBoxTileUrl, DispatchAppMapboxKey); |
112 | | - else |
| 173 | + return GetMapConfig(InfoConfig.DispatchAppKey).TileUrl; |
| 174 | + } |
| 175 | + |
| 176 | + private static string GetLegacyLeafletUrl(string key) |
| 177 | + { |
| 178 | + if (key == InfoConfig.WebsiteKey) |
| 179 | + { |
| 180 | + if (!string.IsNullOrWhiteSpace(WebsiteOSMKey)) |
| 181 | + return ReplaceTileKey(LeafletTileUrl, WebsiteOSMKey); |
| 182 | + |
| 183 | + return LeafletTileUrl; |
| 184 | + } |
| 185 | + |
| 186 | + if (key == InfoConfig.ApiKey) |
| 187 | + { |
| 188 | + if (!string.IsNullOrWhiteSpace(ApiOSMKey)) |
| 189 | + return ReplaceTileKey(LeafletTileUrl, ApiOSMKey); |
| 190 | + |
| 191 | + return LeafletTileUrl; |
| 192 | + } |
| 193 | + |
| 194 | + if (key == InfoConfig.ResponderAppKey) |
| 195 | + { |
| 196 | + if (!string.IsNullOrWhiteSpace(ResponderAppOSMKey)) |
| 197 | + return ReplaceTileKey(LeafletTileUrl, ResponderAppOSMKey); |
| 198 | + |
113 | 199 | return LeafletTileUrl; |
| 200 | + } |
| 201 | + |
| 202 | + if (key == InfoConfig.UnitAppKey) |
| 203 | + { |
| 204 | + if (!string.IsNullOrWhiteSpace(UnitAppOSMKey)) |
| 205 | + return ReplaceTileKey(LeafletTileUrl, UnitAppOSMKey); |
| 206 | + |
| 207 | + return LeafletTileUrl; |
| 208 | + } |
| 209 | + |
| 210 | + if (key == InfoConfig.BigBoardKey) |
| 211 | + { |
| 212 | + if (!string.IsNullOrWhiteSpace(BigBoardOSMKey)) |
| 213 | + return ReplaceTileKey(LeafletTileUrl, BigBoardOSMKey); |
| 214 | + |
| 215 | + return LeafletTileUrl; |
| 216 | + } |
| 217 | + |
| 218 | + if (key == InfoConfig.DispatchAppKey) |
| 219 | + { |
| 220 | + if (!string.IsNullOrWhiteSpace(DispatchAppOSMKey)) |
| 221 | + return ReplaceTileKey(LeafletTileUrl, DispatchAppOSMKey); |
| 222 | + |
| 223 | + return LeafletTileUrl; |
| 224 | + } |
| 225 | + |
| 226 | + return LeafletTileUrl; |
| 227 | + } |
| 228 | + |
| 229 | + private static string GetPreferredMapProvider(string key) |
| 230 | + { |
| 231 | + if (key == InfoConfig.WebsiteKey) |
| 232 | + return NormalizeMapProvider(WebsiteMapMode); |
| 233 | + |
| 234 | + if (key == InfoConfig.DispatchAppKey && !string.IsNullOrWhiteSpace(DispatchAppMapboxKey)) |
| 235 | + return MapboxMapProvider; |
| 236 | + |
| 237 | + if (key == InfoConfig.UnitAppKey && !string.IsNullOrWhiteSpace(UnitAppMapBoxKey)) |
| 238 | + return MapboxMapProvider; |
| 239 | + |
| 240 | + return LeafletMapProvider; |
| 241 | + } |
| 242 | + |
| 243 | + private static string GetSystemMapboxAccessToken(string key) |
| 244 | + { |
| 245 | + if (key == InfoConfig.WebsiteKey) |
| 246 | + { |
| 247 | + if (!string.IsNullOrWhiteSpace(WebsiteMapboxAccessToken)) |
| 248 | + return WebsiteMapboxAccessToken; |
| 249 | + |
| 250 | + if (!string.IsNullOrWhiteSpace(WebsiteMapboxKey)) |
| 251 | + return WebsiteMapboxKey; |
| 252 | + |
| 253 | + return WebsiteOSMKey; |
| 254 | + } |
| 255 | + |
| 256 | + if (key == InfoConfig.DispatchAppKey) |
| 257 | + return DispatchAppMapboxKey; |
| 258 | + |
| 259 | + if (key == InfoConfig.UnitAppKey) |
| 260 | + return UnitAppMapBoxKey; |
| 261 | + |
| 262 | + return string.Empty; |
| 263 | + } |
| 264 | + |
| 265 | + private static string NormalizeMapProvider(string provider) |
| 266 | + { |
| 267 | + if (string.IsNullOrWhiteSpace(provider)) |
| 268 | + return LeafletMapProvider; |
| 269 | + |
| 270 | + return provider.Trim().Equals(MapboxMapProvider, StringComparison.InvariantCultureIgnoreCase) |
| 271 | + ? MapboxMapProvider |
| 272 | + : LeafletMapProvider; |
| 273 | + } |
| 274 | + |
| 275 | + private static string ReplaceTileKey(string tileUrl, string key) |
| 276 | + { |
| 277 | + if (string.IsNullOrWhiteSpace(tileUrl)) |
| 278 | + return tileUrl; |
| 279 | + |
| 280 | + var normalizedTileUrl = tileUrl |
| 281 | + .Replace("{{", "{", StringComparison.InvariantCulture) |
| 282 | + .Replace("}}", "}", StringComparison.InvariantCulture); |
| 283 | + |
| 284 | + if (string.IsNullOrWhiteSpace(key)) |
| 285 | + return normalizedTileUrl; |
| 286 | + |
| 287 | + return normalizedTileUrl.Replace("{0}", key, StringComparison.InvariantCulture); |
| 288 | + } |
| 289 | + |
| 290 | + private static string NormalizeMapboxStyleUrl(string styleUrl, string styleId) |
| 291 | + { |
| 292 | + if (styleUrl.StartsWith("mapbox://styles/", StringComparison.InvariantCultureIgnoreCase)) |
| 293 | + return styleUrl; |
| 294 | + |
| 295 | + return $"mapbox://styles/{styleId}"; |
| 296 | + } |
| 297 | + |
| 298 | + private static string NormalizePublicMapboxAccessToken(string accessToken) |
| 299 | + { |
| 300 | + if (string.IsNullOrWhiteSpace(accessToken)) |
| 301 | + return string.Empty; |
| 302 | + |
| 303 | + var trimmedAccessToken = accessToken.Trim(); |
| 304 | + |
| 305 | + return trimmedAccessToken.StartsWith("pk.", StringComparison.Ordinal) |
| 306 | + ? trimmedAccessToken |
| 307 | + : string.Empty; |
| 308 | + } |
| 309 | + |
| 310 | + private static string GetMapboxStyleId(string styleUrl) |
| 311 | + { |
| 312 | + if (string.IsNullOrWhiteSpace(styleUrl)) |
| 313 | + return null; |
| 314 | + |
| 315 | + var trimmedStyleUrl = styleUrl.Trim(); |
| 316 | + |
| 317 | + if (trimmedStyleUrl.StartsWith("mapbox://styles/", StringComparison.InvariantCultureIgnoreCase)) |
| 318 | + return ExtractMapboxStyleId(trimmedStyleUrl.Substring("mapbox://styles/".Length)); |
| 319 | + |
| 320 | + if (Uri.TryCreate(trimmedStyleUrl, UriKind.Absolute, out var mapboxStyleUri)) |
| 321 | + { |
| 322 | + var path = mapboxStyleUri.AbsolutePath.Trim('/'); |
| 323 | + var stylesIndex = path.IndexOf("styles/v1/", StringComparison.InvariantCultureIgnoreCase); |
| 324 | + |
| 325 | + if (stylesIndex >= 0) |
| 326 | + { |
| 327 | + var stylePath = path.Substring(stylesIndex + "styles/v1/".Length); |
| 328 | + return ExtractMapboxStyleId(stylePath); |
| 329 | + } |
| 330 | + } |
| 331 | + |
| 332 | + return null; |
| 333 | + } |
| 334 | + |
| 335 | + private static string ExtractMapboxStyleId(string stylePath) |
| 336 | + { |
| 337 | + if (string.IsNullOrWhiteSpace(stylePath)) |
| 338 | + return null; |
| 339 | + |
| 340 | + var normalizedPath = stylePath.Trim('/'); |
| 341 | + var pathSegments = normalizedPath.Split('/', StringSplitOptions.RemoveEmptyEntries); |
| 342 | + |
| 343 | + if (pathSegments.Length < 2) |
| 344 | + return null; |
| 345 | + |
| 346 | + return $"{pathSegments[0]}/{pathSegments[1]}"; |
114 | 347 | } |
115 | 348 | } |
116 | 349 | } |
0 commit comments