| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Collections.Immutable; |
| | | 4 | | using System.Linq; |
| | | 5 | | using Itinero.Indexes; |
| | | 6 | | |
| | | 7 | | namespace Itinero.IO.Osm; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// An edge type map that keeps only OSM tags relevant for routing. |
| | | 11 | | /// Both key AND value are validated — only known routing-relevant key-value pairs are kept. |
| | | 12 | | /// This ensures edges with different routing characteristics get different edgeTypeIds, |
| | | 13 | | /// while irrelevant tags are stripped to maximize deduplication. |
| | | 14 | | /// </summary> |
| | | 15 | | public class OsmEdgeTypeMap : AttributeSetMap |
| | | 16 | | { |
| | | 17 | | private static readonly ImmutableDictionary<string, ImmutableHashSet<string>> RoutingTags; |
| | | 18 | | |
| | | 19 | | static OsmEdgeTypeMap() |
| | 0 | 20 | | { |
| | 0 | 21 | | var tags = new Dictionary<string, ImmutableHashSet<string>>(); |
| | | 22 | | |
| | | 23 | | // access |
| | 0 | 24 | | tags["access"] = Set("customers", "delivery", "designated", "destination", "dismount", |
| | 0 | 25 | | "no", "permissive", "permit", "private", "service"); |
| | 0 | 26 | | tags["bicycle"] = Set("designated", "dismount", "no", "official", "permissive", |
| | 0 | 27 | | "permit", "private", "use_sidepath", "yes"); |
| | 0 | 28 | | tags["foot"] = Set("designated", "no", "official", "permissive", "permit", |
| | 0 | 29 | | "private", "use_sidepath", "yes"); |
| | 0 | 30 | | tags["hgv"] = Set("no", "permissive", "permit", "private", "yes"); |
| | 0 | 31 | | tags["motor_vehicle"] = Set("customers", "destination", "no", "permissive", |
| | 0 | 32 | | "permit", "private", "yes"); |
| | 0 | 33 | | tags["motorcar"] = Set("customers", "destination", "no", "permissive", |
| | 0 | 34 | | "permit", "private", "yes"); |
| | 0 | 35 | | tags["emergency"] = Set("yes"); |
| | 0 | 36 | | tags["motorroad"] = Set("yes"); |
| | 0 | 37 | | tags["area"] = Set("yes"); |
| | | 38 | | |
| | | 39 | | // road classification — wildcard, accept any value |
| | 0 | 40 | | tags["highway"] = ImmutableHashSet<string>.Empty; |
| | 0 | 41 | | tags["route"] = ImmutableHashSet<string>.Empty; |
| | | 42 | | |
| | 0 | 43 | | tags["service"] = Set("alley", "bus", "driveway", "parking_aisle"); |
| | 0 | 44 | | tags["tracktype"] = Set("grade1", "grade2", "grade3", "grade4", "grade5"); |
| | 0 | 45 | | tags["railway"] = Set("abandoned"); |
| | | 46 | | |
| | | 47 | | // direction |
| | 0 | 48 | | tags["oneway"] = Set("-1", "1", "no", "yes"); |
| | 0 | 49 | | tags["oneway:bicycle"] = Set("-1", "1", "no", "yes"); |
| | 0 | 50 | | tags["oneway:foot"] = Set("yes"); |
| | 0 | 51 | | tags["junction"] = Set("roundabout"); |
| | | 52 | | |
| | | 53 | | // speed — wildcard, validated as integer |
| | 0 | 54 | | tags["maxspeed"] = ImmutableHashSet<string>.Empty; |
| | | 55 | | |
| | | 56 | | // surface |
| | 0 | 57 | | tags["surface"] = Set("asphalt", "cobblestone", "compacted", "concrete", |
| | 0 | 58 | | "concrete:lanes", "concrete:plates", "dirt", "earth", "fine_gravel", |
| | 0 | 59 | | "grass", "grass_paver", "gravel", "ground", "metal", "mud", "paved", |
| | 0 | 60 | | "paving_stones", "pebblestone", "sand", "sett", "snow", |
| | 0 | 61 | | "unhewn_cobblestone", "unpaved", "wood", "woodchips"); |
| | 0 | 62 | | tags["smoothness"] = Set("bad", "excellent", "good", "intermediate", "very_good"); |
| | 0 | 63 | | tags["sidewalk:surface"] = Set("asphalt", "concrete", "paving_stones", "sett"); |
| | | 64 | | |
| | | 65 | | // cycling |
| | 0 | 66 | | tags["cycleway"] = Set("lane", "opposite", "opposite_lane", "opposite_share_busway", |
| | 0 | 67 | | "opposite_track", "right", "share_busway", "shared", "shared_lane", |
| | 0 | 68 | | "track", "yes"); |
| | 0 | 69 | | tags["cycleway:left"] = Set("lane", "opposite", "opposite_lane", "opposite_track", |
| | 0 | 70 | | "share_busway", "shared", "shared_lane", "track", "yes"); |
| | 0 | 71 | | tags["cycleway:right"] = Set("lane", "share_busway", "shared", "shared_lane", |
| | 0 | 72 | | "track", "yes"); |
| | 0 | 73 | | tags["cycleway:left:oneway"] = Set("no"); |
| | 0 | 74 | | tags["cycleway:right:oneway"] = Set("no"); |
| | 0 | 75 | | tags["cyclestreet"] = Set("yes"); |
| | 0 | 76 | | tags["cycle_highway"] = ImmutableHashSet<string>.Empty; |
| | 0 | 77 | | tags["bicycle:class"] = Set("-1", "-2", "-3", "0", "1", "2", "3"); |
| | 0 | 78 | | tags["ramp:bicycle"] = Set("yes"); |
| | 0 | 79 | | tags["towpath"] = Set("yes"); |
| | 0 | 80 | | tags["designation"] = Set("towpath"); |
| | | 81 | | |
| | | 82 | | // barriers |
| | 0 | 83 | | tags["barrier"] = Set("bollard", "sump_buster"); |
| | | 84 | | |
| | | 85 | | // incline |
| | 0 | 86 | | tags["incline"] = Set("-10%", "-20%", "-30%", "0", "0%", "10%", "20%", "30%", |
| | 0 | 87 | | "down", "up"); |
| | | 88 | | |
| | | 89 | | // other routing-relevant — wildcards |
| | 0 | 90 | | tags["type"] = ImmutableHashSet<string>.Empty; |
| | 0 | 91 | | tags["network:type"] = ImmutableHashSet<string>.Empty; |
| | 0 | 92 | | tags["operator"] = ImmutableHashSet<string>.Empty; |
| | 0 | 93 | | tags["state"] = ImmutableHashSet<string>.Empty; |
| | | 94 | | |
| | 0 | 95 | | RoutingTags = tags.ToImmutableDictionary(); |
| | 0 | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Creates a new OSM edge type map with the default set of routing-relevant tags. |
| | | 100 | | /// </summary> |
| | | 101 | | public OsmEdgeTypeMap() |
| | 0 | 102 | | : base(new Guid("45d22274-cb26-490c-9685-aab0ef7d7e9c")) |
| | 0 | 103 | | { |
| | 0 | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <inheritdoc/> |
| | | 107 | | public override IEnumerable<(string key, string value)> Map( |
| | | 108 | | IEnumerable<(string key, string value)> attributes) |
| | 0 | 109 | | { |
| | 0 | 110 | | return attributes.Where(a => IsRelevant(a.key, a.value)); |
| | 0 | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Returns true if the key-value pair is relevant for routing. |
| | | 115 | | /// </summary> |
| | | 116 | | private static bool IsRelevant(string key, string value) |
| | 0 | 117 | | { |
| | 0 | 118 | | if (!RoutingTags.TryGetValue(key, out var allowedValues)) return false; |
| | | 119 | | |
| | | 120 | | // if allowed values are specified, check membership. |
| | 0 | 121 | | if (allowedValues.Count > 0) return allowedValues.Contains(value); |
| | | 122 | | |
| | | 123 | | // wildcard keys — custom validation per key. |
| | 0 | 124 | | return key switch |
| | 0 | 125 | | { |
| | 0 | 126 | | "maxspeed" => int.TryParse(value, out _), |
| | 0 | 127 | | _ => true |
| | 0 | 128 | | }; |
| | 0 | 129 | | } |
| | | 130 | | |
| | | 131 | | private static ImmutableHashSet<string> Set(params string[] values) |
| | 0 | 132 | | { |
| | 0 | 133 | | return values.ToImmutableHashSet(); |
| | 0 | 134 | | } |
| | | 135 | | } |