From e306b2366341071354a50c71ae0e08c9f40e3714 Mon Sep 17 00:00:00 2001 From: Filip Strajnar Date: Mon, 24 Jun 2024 11:01:19 +0200 Subject: [PATCH] Added some useful parsing functions. --- OsmToDatabase.Common/TagParsing.cs | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 OsmToDatabase.Common/TagParsing.cs diff --git a/OsmToDatabase.Common/TagParsing.cs b/OsmToDatabase.Common/TagParsing.cs new file mode 100644 index 0000000..ba79e74 --- /dev/null +++ b/OsmToDatabase.Common/TagParsing.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using OsmSharp; +using OsmSharp.Tags; + +namespace OsmToDatabase.Common +{ + public static class TagParsing + { + /// + /// Gets the value of a tag with the given key. + /// + /// Node that is being searched for tags. + /// Key of the tag that is being searched for. + /// Value of the tag, or null if it is not found. + public static string? TagValueByKey(this Node node, string tagKey) + { + return node + .Tags.Select(t => t) + .FirstOrDefault(tag => tag is not null && tag.Value.Key == tagKey) + ?.Value; + } + + public static double? TagDoubleValueByKey(this Node node, string tagKey) + { + string? tagValue = node.TagValueByKey(tagKey); + bool success = double.TryParse(tagValue, out double result); + return success ? result : null; + } + } +}