From 87944997dbb274f3e2b80bbaa719b8efbe651d2a Mon Sep 17 00:00:00 2001 From: C9Glax <13404778+C9Glax@users.noreply.github.com> Date: Sun, 5 Feb 2023 20:44:23 +0100 Subject: [PATCH] Fixed "highway" tag to key instead of value Memory usage decrease --- OSMSplitter/XMLImporter.cs | 59 +++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/OSMSplitter/XMLImporter.cs b/OSMSplitter/XMLImporter.cs index 9d6eed0..b5f6e92 100644 --- a/OSMSplitter/XMLImporter.cs +++ b/OSMSplitter/XMLImporter.cs @@ -50,26 +50,27 @@ namespace OSMImporter Dictionary retSet = new Dictionary(); while (xmlReader.ReadToFollowing("way")) { - byte[] byteArray = Encoding.ASCII.GetBytes(xmlReader.ReadOuterXml()); - MemoryStream wayStream = new MemoryStream(byteArray); - XmlReader wayReader = XmlReader.Create(wayStream); + XmlReader wayReader = xmlReader.ReadSubtree(); bool addNodes = false; - while (wayReader.ReadToFollowing("tag") && !addNodes) + HashSet nodeIds = new(); + while (wayReader.Read()) { - if (wayReader.GetAttribute("v")!.Equals("highway")) + if (xmlReader.IsStartElement() && xmlReader.Name.Equals("nd")) + { + nodeIds.Add(wayReader.GetAttribute("ref")!); + } + else if (xmlReader.IsStartElement() && !addNodes && xmlReader.Name.Equals("tag") && xmlReader.GetAttribute("k")!.Equals("highway")) { addNodes = true; - break; } } + wayReader.Close(); if (addNodes) { - wayStream.Position = 0; - wayReader = XmlReader.Create(wayStream); - while (wayReader.ReadToFollowing("nd")) + foreach (string nodeId in nodeIds) { - retSet.TryAdd(Convert.ToUInt64(wayReader.GetAttribute("ref")!), null); + retSet.TryAdd(Convert.ToUInt64(nodeId), null); } } } @@ -99,31 +100,29 @@ namespace OSMImporter { while (xmlReader.ReadToFollowing("way")) { - byte[] byteArray = Encoding.ASCII.GetBytes(xmlReader.ReadOuterXml()); - MemoryStream wayStream = new MemoryStream(byteArray); - XmlReader wayReader = XmlReader.Create(wayStream); - Dictionary tags = new Dictionary(); - + XmlReader wayReader = xmlReader.ReadSubtree(); + HashSet nodesInWay = new (); + Dictionary tags = new (); bool addNodes = false; - while (wayReader.ReadToFollowing("tag")) + while (wayReader.Read()) { - if (wayReader.GetAttribute("v")!.Equals("highway")) - { - addNodes = true; - } - tags.Add(wayReader.GetAttribute("k")!, value: wayReader.GetAttribute("v")!); - } - - - if (addNodes) - { - HashSet nodesInWay = new HashSet(); - wayStream.Position = 0; - wayReader = XmlReader.Create(wayStream); - while (wayReader.ReadToFollowing("nd")) + if (wayReader.IsStartElement() && wayReader.Name.Equals("nd")) { nodesInWay.Add(Convert.ToUInt64(wayReader.GetAttribute("ref"))); } + else if (wayReader.IsStartElement() && wayReader.Name.Equals("tag")) + { + if (wayReader.GetAttribute("k")!.Equals("highway")) + { + addNodes = true; + } + tags.Add(wayReader.GetAttribute("k")!, value: wayReader.GetAttribute("v")!); + } + } + wayReader.Close(); + + if (addNodes) + { ConnectNodesOfWay(nodes, nodesInWay.ToArray(), tags); } }