| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.IO; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Security.Cryptography; |
| | 6 | | using Itinero.Indexes; |
| | 7 | | using Itinero.Profiles; |
| | 8 | | using Itinero.Profiles.EdgeTypesMap; |
| | 9 | | // ReSharper disable PossibleMultipleEnumeration |
| | 10 | |
|
| | 11 | | namespace Itinero.Profiles.EdgeTypesMap; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// An edge types map based using profiles. |
| | 15 | | /// </summary> |
| | 16 | | internal class ProfilesEdgeTypeMap : AttributeSetMap |
| | 17 | | { |
| | 18 | | private readonly ProfileEdgeTypeSetMinimizer _profileEdgeTypeSetMinimizer; |
| | 19 | |
|
| | 20 | | public ProfilesEdgeTypeMap(IEnumerable<Profile> profiles) |
| 0 | 21 | | : base(GenerateGuidFromHash(profiles)) |
| 0 | 22 | | { |
| 0 | 23 | | var sorted = profiles.ToArray(); |
| 0 | 24 | | Array.Sort(sorted, (x, y) => |
| 0 | 25 | | string.Compare(x.Name, y.Name, StringComparison.Ordinal)); |
| | 26 | |
|
| 0 | 27 | | _profileEdgeTypeSetMinimizer = new ProfileEdgeTypeSetMinimizer(sorted, "highway", "oneway", "access", "surface") |
| 0 | 28 | | } |
| | 29 | |
|
| | 30 | | public override IEnumerable<(string key, string value)> Map(IEnumerable<(string key, string value)> attributes) |
| 0 | 31 | | { |
| 0 | 32 | | return _profileEdgeTypeSetMinimizer.MinimizeAttributes(attributes); |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | private static Guid GenerateGuidFromHash(IEnumerable<Profile> profiles) |
| 0 | 36 | | { |
| | 37 | | // build a byte array with all hashes of each profile. |
| 0 | 38 | | var memoryStream = new MemoryStream(); |
| 0 | 39 | | foreach (var t in profiles) |
| 0 | 40 | | { |
| 0 | 41 | | memoryStream.Write( |
| 0 | 42 | | BitConverter.GetBytes(t.GetHashCode()), 0, 4); |
| 0 | 43 | | } |
| | 44 | |
|
| | 45 | | // calculate md5 from byte array. |
| 0 | 46 | | using var md5 = MD5.Create(); |
| 0 | 47 | | var hash = md5.ComputeHash(memoryStream.ToArray()); |
| 0 | 48 | | return new Guid(hash); |
| 0 | 49 | | } |
| | 50 | | } |