| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.IO; |
| | 4 | | using Itinero.Logging; |
| | 5 | | using Newtonsoft.Json.Linq; |
| | 6 | |
|
| | 7 | | namespace Itinero.IO.Osm.Tiles.Parsers.Semantics; |
| | 8 | |
|
| | 9 | | public static class TagMapperConfigParser |
| | 10 | | { |
| | 11 | | public static Dictionary<string, TagMapperConfig> Parse(Stream stream) |
| 0 | 12 | | { |
| 0 | 13 | | var mappings = new Dictionary<string, TagMapperConfig>(); |
| | 14 | |
|
| 0 | 15 | | using (var textReader = new StreamReader(stream)) |
| 0 | 16 | | { |
| 0 | 17 | | var parsed = JArray.Parse(textReader.ReadToEnd()); |
| | 18 | |
|
| 0 | 19 | | foreach (var item in parsed) |
| 0 | 20 | | { |
| | 21 | | try |
| 0 | 22 | | { |
| 0 | 23 | | var osmKeyValue = item["osm_key"]; |
| 0 | 24 | | if (osmKeyValue == null) |
| 0 | 25 | | { |
| 0 | 26 | | throw new Exception("osm_key not found."); |
| | 27 | | } |
| | 28 | |
|
| 0 | 29 | | if (osmKeyValue.Type != JTokenType.String) |
| 0 | 30 | | { |
| 0 | 31 | | throw new Exception("osm_key not a string."); |
| | 32 | | } |
| | 33 | |
|
| 0 | 34 | | var osmKey = osmKeyValue.Value<string>(); |
| 0 | 35 | | var predicateValue = item["predicate"]; |
| 0 | 36 | | if (predicateValue == null) |
| 0 | 37 | | { |
| 0 | 38 | | throw new Exception("predicate not found."); |
| | 39 | | } |
| | 40 | |
|
| 0 | 41 | | if (predicateValue.Type != JTokenType.String) |
| 0 | 42 | | { |
| 0 | 43 | | throw new Exception("predicate not a string."); |
| | 44 | | } |
| | 45 | |
|
| 0 | 46 | | var predicate = predicateValue.Value<string>(); |
| | 47 | |
|
| 0 | 48 | | var map = item["mapping"]; |
| 0 | 49 | | Dictionary<string, string>? mapping = null; |
| 0 | 50 | | if (map != null) |
| 0 | 51 | | { |
| 0 | 52 | | mapping = new Dictionary<string, string>(); |
| 0 | 53 | | foreach (var child in map.Children()) |
| 0 | 54 | | { |
| 0 | 55 | | if (!(child is JProperty property)) |
| 0 | 56 | | { |
| 0 | 57 | | continue; |
| | 58 | | } |
| | 59 | |
|
| 0 | 60 | | if (property.Value is JValue val) |
| 0 | 61 | | { |
| 0 | 62 | | mapping[val.Value.ToInvariantString()] = property.Name; |
| 0 | 63 | | } |
| 0 | 64 | | } |
| 0 | 65 | | } |
| | 66 | |
|
| 0 | 67 | | mappings[predicate] = new TagMapperConfig |
| 0 | 68 | | { |
| 0 | 69 | | ReverseMapping = mapping, |
| 0 | 70 | | OsmKey = osmKey, |
| 0 | 71 | | Predicate = predicate |
| 0 | 72 | | }; |
| 0 | 73 | | } |
| 0 | 74 | | catch (Exception ex) |
| 0 | 75 | | { |
| 0 | 76 | | Logger.Log($"{nameof(TagMapperConfigParser)}.{nameof(Parse)}", TraceEventType.Error, |
| 0 | 77 | | "Could not fully parse mapping configuration {0}", ex); |
| 0 | 78 | | throw; |
| | 79 | | } |
| 0 | 80 | | } |
| 0 | 81 | | } |
| | 82 | |
|
| 0 | 83 | | return mappings; |
| 0 | 84 | | } |
| | 85 | | } |