Rewrote Edge-adding

This commit is contained in:
C9Glax 2022-05-11 21:30:54 +02:00
parent e0f17c1953
commit e212aeef0d

View File

@ -9,21 +9,14 @@ namespace OpenStreetMap_Importer
public static Dictionary<ulong, Node> Import(string filePath = "", Logger ?logger = null)
{
Stream mapData;
if (!File.Exists(filePath))
{
mapData = new MemoryStream(OSM_Data.map);
logger?.Log(LogLevel.INFO, "Filepath '{0}' does not exist.", filePath);
}
else
{
mapData = new FileStream(filePath, FileMode.Open, FileAccess.Read);
logger?.Log(LogLevel.INFO, "File '{0}' loaded.", filePath);
}
List<Way> ways = new();
Dictionary<ulong, Node> nodes = new();
Stream mapData;
XmlReaderSettings readerSettings = new()
{
IgnoreWhitespace = true,
IgnoreComments = true
};
bool wayTag = false;
Way currentWay = new();
@ -34,11 +27,17 @@ namespace OpenStreetMap_Importer
* Import "ways" with a tag "highway"
* Count occurances of "nodes" to find junctions
*/
XmlReaderSettings readerSettings = new()
if (!File.Exists(filePath))
{
IgnoreWhitespace = true,
IgnoreComments = true
};
mapData = new MemoryStream(OSM_Data.map);
logger?.Log(LogLevel.INFO, "Filepath '{0}' does not exist. Using standard file.", filePath);
}
else
{
mapData = new FileStream(filePath, FileMode.Open, FileAccess.Read);
logger?.Log(LogLevel.INFO, "Using file '{0}'", filePath);
}
XmlReader reader = XmlReader.Create(mapData, readerSettings);
reader.MoveToContent();
while (reader.Read())
@ -107,6 +106,11 @@ namespace OpenStreetMap_Importer
logger?.Log(LogLevel.DEBUG, "Ways: {0} Nodes: {1}", ways.Count, nodes.Count);
reader.Close();
if (File.Exists(filePath))
{
mapData.Close();
mapData = new FileStream(filePath, FileMode.Open, FileAccess.Read);
}
reader = XmlReader.Create(mapData, readerSettings);
reader.MoveToContent();
@ -143,85 +147,68 @@ namespace OpenStreetMap_Importer
Node junction1 = nodes[way.nodeIds[0]];
Node junction2;
double weight = 0;
//Iterate Node-ids in current way forwards or backwards (depending on way.direction)
if (way.direction == Way.wayDirection.forward)
{
for (int index = 1; index < way.nodeIds.Count - 1; index++)
for (int index = 0; index < way.nodeIds.Count - 1; index++)
{
Node currentNode = nodes[way.nodeIds[index]];
if (count[way.nodeIds[index]] > 1)
{
junction2 = nodes[way.nodeIds[index]];
junction1.edges.Add(new Edge(junction2, weight));
logger?.Log(LogLevel.VERBOSE, "EDGE {0} {1} -- {2} --> {3} {4}", junction1.lat, junction1.lon, weight, junction2.lat, junction2.lon);
if (!way.oneway)
{
junction2.edges.Add(new Edge(junction1, weight));
logger?.Log(LogLevel.VERBOSE, "EDGE {0} {1} -- {2} --> {3} {4}", junction2.lat, junction2.lon, weight, junction1.lat, junction1.lon);
edges++;
}
junction1 = junction2;
weight = 0;
}
else
{
Node nextNode = nodes[way.nodeIds[index + 1]];
weight += Utils.DistanceBetweenNodes(currentNode, nextNode);
}
edges++;
}
junction2 = nodes[way.nodeIds[way.nodeIds.Count - 1]];
if (count[way.nodeIds[index + 1]] > 1 || index == way.nodeIds.Count - 2)
{
/*
* If Node is referenced more than once => Junction
* If Node is last node of way => Junction
* Add an edge between two junctions
*/
junction2 = nodes[way.nodeIds[index + 1]];
junction1.edges.Add(new Edge(junction2, weight));
logger?.Log(LogLevel.VERBOSE, "EDGE {0} {1} -- {2} --> {3} {4}", junction1.lat, junction1.lon, weight, junction2.lat, junction2.lon);
logger?.Log(LogLevel.VERBOSE, "EDGE {0} -- {1} --> {2}", way.nodeIds[index], weight, way.nodeIds[index + 1]);
edges++;
if (!way.oneway)
{
junction2.edges.Add(new Edge(junction1, weight));
logger?.Log(LogLevel.VERBOSE, "EDGE {0} {1} -- {2} --> {3} {4}", junction2.lat, junction2.lon, weight, junction1.lat, junction1.lon);
edges++;
}
}
else
{
for (int index = way.nodeIds.Count - 2; index > 1; index--)
{
Node currentNode = nodes[way.nodeIds[index]];
if (count[way.nodeIds[index]] > 1)
{
junction2 = nodes[way.nodeIds[index]];
junction1.edges.Add(new Edge(junction2, weight));
logger?.Log(LogLevel.VERBOSE, "EDGE {0} {1} -- {2} --> {3} {4}", junction1.lat, junction1.lon, weight, junction2.lat, junction2.lon);
if (!way.oneway)
{
junction2.edges.Add(new Edge(junction1, weight));
logger?.Log(LogLevel.VERBOSE, "EDGE {0} {1} -- {2} --> {3} {4}", junction2.lat, junction2.lon, weight, junction1.lat, junction1.lon);
logger?.Log(LogLevel.VERBOSE, "EDGE {0} -- {1} --> {2}", way.nodeIds[index + 1], weight, way.nodeIds[index]);
edges++;
}
junction1 = junction2;
weight = 0;
}
}
}
else
{
for (int index = way.nodeIds.Count - 2; index > 0; index--)
{
Node currentNode = nodes[way.nodeIds[index]];
Node nextNode = nodes[way.nodeIds[index - 1]];
weight += Utils.DistanceBetweenNodes(currentNode, nextNode);
}
edges++;
}
junction2 = nodes[way.nodeIds[way.nodeIds.Count - 1]];
if (count[way.nodeIds[index - 1]] > 1 || index == 1)
{
/*
* If Node is referenced more than once => Junction
* If Node is last node of way => Junction
* Add an edge between two junctions
*/
junction2 = nodes[way.nodeIds[index - 1]];
junction1.edges.Add(new Edge(junction2, weight));
logger?.Log(LogLevel.VERBOSE, "EDGE {0} {1} -- {2} --> {3} {4}", junction1.lat, junction1.lon, weight, junction2.lat, junction2.lon);
logger?.Log(LogLevel.VERBOSE, "EDGE {0} -- {1} --> {2}", way.nodeIds[index], weight, way.nodeIds[index - 1]);
edges++;
if (!way.oneway)
{
junction2.edges.Add(new Edge(junction1, weight));
logger?.Log(LogLevel.VERBOSE, "EDGE {0} {1} -- {2} --> {3} {4}", junction2.lat, junction2.lon, weight, junction1.lat, junction1.lon);
logger?.Log(LogLevel.VERBOSE, "EDGE {0} -- {1} --> {2}", way.nodeIds[index - 1], weight, way.nodeIds[index]);
edges++;
}
junction1 = junction2;
weight = 0;
}
}
}
}
reader.Close();