I am writing my own c# custom map navigation program. I am using OpenStreetMaps for the map data. It's a large XML file that contains Nodes and ways. I wrote a convertor that strips the XML file from useless junk (like timestamp, user etc) so the file will become smaller.
Now while I attempt to loop through the XML and convert the data to C# object Ways and Nodes I came across a problem. I need to do so much looping that the loading time is way too long.
XML Example (w = way, n = node, rf = reference to a node ):
<n id="2638006578" l="5.9295547" b="52.5619519" />
<n id="2638006579" l="5.9301973" b="52.5619526" />
<n id="2638006581" l="5.9303625" b="52.5619565" />
<n id="2638006583" l="5.9389539" b="52.5619577" />
<n id="2638006589" l="5.9386643" b="52.5619733" />
<n id="2638006590" l="5.9296231" b="52.5619760" />
<n id="2638006595" l="5.9358987" b="52.5619864" />
<n id="2638006596" l="5.9335913" b="52.5619865" />
<w id="453071384">
<nd rf="2638006581" />
<nd rf="2638006590" />
<nd rf="2638006596" />
<nd rf="2638006583" />
<nd rf="2638006578" />
</w>
<w id="453071385">
<nd rf="2638006596" />
<nd rf="2638006578" />
<nd rf="2638006581" />
<nd rf="2638006583" />
</w>
The Way nodes contain references to nodes, so a node can be existent in multiple ways (the nodes connect the ways).
nodes contain a longitude and latitude (l = lon, b = lat).
These XML files contain a lot of nodes and ways, so it's not just a small file. The XML file in the code example below has 500K lines.
My Code
class Program {
static List<Way> ways = new List<Way>();
static List<Node> nodes = new List<Node>();
static void Main(String[] args) {
read();
}
public static void read() {
String xmlLoc = @"C:\Users\MyName\Desktop\result.xml";
long time = DateTime.Now.Ticks;
Parse(xmlLoc);
long time2 = DateTime.Now.Ticks;
Console.WriteLine("Done: {0} ms", (time2 - time) / 10000);
Console.WriteLine(" - Nodes Amount:" + nodes.Count());
Console.WriteLine(" - Ways Amount: " + ways.Count());
}
private static Node GetByRef(long reference) {
return nodes.First(x => x.ID == reference);
}
private static void Parse(string path) {
using (XmlReader reader = XmlReader.Create(path)) {
reader.MoveToContent();
while (reader.Read()) {
if (reader.NodeType == XmlNodeType.Element) {
XElement element = XElement.ReadFrom(reader) as XElement;
if ( element.Name.ToString().Equals("w")) {
Way w = new Way();
var name = element.Attribute("nm");
if (name != null) w.Name = name.Value;
var children = element.Descendants("nd");
foreach (XElement child in children) w.References.Add(long.Parse(child.Attribute("rf").Value));
ways.Add(w);
}else if (element.Name.ToString().Equals("n")) {
Node n = new Node();
n.ID = long.Parse(element.Attribute("id").Value);
n.Lon = double.Parse(element.Attribute("l").Value);
n.Lat = double.Parse(element.Attribute("b").Value);
nodes.Add(n);
}
}
}
}
}
}
class Way {
public List<long> References { get; private set; }
public long ID { get; set; }
public String Name { get; set; }
public bool OneWay { get; set; }
public Way() {
this.References = new List<long>();
}
}
class Node {
public long ID { get; set; }
public double Lat { get; set; }
public double Lon { get; set; }
}
Currently there's no actual relation between the class Way and the class Node. There's only a list with long
values. I somehow need to add a list with Nodes to the class Way, but that'll require me to work with another (two?) for/while loops. That would mean O(N4) I believe, which is slow.
Technically I am looking for both a solution and ways to build this better, if you have advise I'd like to hear it!
Thanks in advance!
PS: If I should update/edit my question, please tell me instead of downvoting immediately.