Optional Logger
This commit is contained in:
parent
d29ab7e01e
commit
1384abfaf2
@ -1,3 +1,3 @@
|
|||||||
Logging.Logger logger = new Logging.Logger(Logging.LogType.Console, Logging.loglevel.DEBUG);
|
Logging.Logger logger = new Logging.Logger(Logging.LogType.Console, Logging.loglevel.DEBUG);
|
||||||
Dictionary<UInt64, Graph.Node> nodes = OpenStreetMap_Importer.Importer.Import(ref logger);
|
Dictionary<UInt64, Graph.Node> nodes = OpenStreetMap_Importer.Importer.Import(logger);
|
||||||
new astar.Astar(nodes, ref logger);
|
new astar.Astar(nodes, logger);
|
@ -7,7 +7,7 @@ namespace OpenStreetMap_Importer
|
|||||||
public class Importer
|
public class Importer
|
||||||
{
|
{
|
||||||
|
|
||||||
public static Dictionary<ulong, Node> Import(ref Logger logger)
|
public static Dictionary<ulong, Node> Import(Logger ?logger = null)
|
||||||
{
|
{
|
||||||
List<Way> ways = new List<Way>();
|
List<Way> ways = new List<Way>();
|
||||||
Dictionary<ulong, Node> nodes = new Dictionary<ulong, Node>();
|
Dictionary<ulong, Node> nodes = new Dictionary<ulong, Node>();
|
||||||
@ -30,7 +30,7 @@ namespace OpenStreetMap_Importer
|
|||||||
{
|
{
|
||||||
if (reader.Name == "way" && reader.IsStartElement())
|
if (reader.Name == "way" && reader.IsStartElement())
|
||||||
{
|
{
|
||||||
logger.Log(loglevel.VERBOSE, "WAY {0} nodes {1}", currentWay.highway.ToString(), currentWay.nodeIds.Count);
|
logger?.Log(loglevel.VERBOSE, "WAY {0} nodes {1}", currentWay.highway.ToString(), currentWay.nodeIds.Count);
|
||||||
if (currentWay.highway != Way.highwayType.NONE)
|
if (currentWay.highway != Way.highwayType.NONE)
|
||||||
{
|
{
|
||||||
ways.Add(currentWay);
|
ways.Add(currentWay);
|
||||||
@ -46,9 +46,8 @@ namespace OpenStreetMap_Importer
|
|||||||
#pragma warning disable CS8604
|
#pragma warning disable CS8604
|
||||||
string value = reader.GetAttribute("v");
|
string value = reader.GetAttribute("v");
|
||||||
string key = reader.GetAttribute("k");
|
string key = reader.GetAttribute("k");
|
||||||
logger.Log(loglevel.VERBOSE, "TAG {0} {1}", key, value);
|
logger?.Log(loglevel.VERBOSE, "TAG {0} {1}", key, value);
|
||||||
#pragma warning restore CS8600
|
#pragma warning restore CS8600
|
||||||
#pragma warning restore CS8604
|
|
||||||
switch (key)
|
switch (key)
|
||||||
{
|
{
|
||||||
case "highway":
|
case "highway":
|
||||||
@ -73,6 +72,7 @@ namespace OpenStreetMap_Importer
|
|||||||
|
|
||||||
break;*/
|
break;*/
|
||||||
}
|
}
|
||||||
|
#pragma warning restore CS8604
|
||||||
}
|
}
|
||||||
else if(reader.Name == "nd" && wayTag)
|
else if(reader.Name == "nd" && wayTag)
|
||||||
{
|
{
|
||||||
@ -85,7 +85,7 @@ namespace OpenStreetMap_Importer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Log(loglevel.DEBUG, "Ways: {0} Nodes: {1}", ways.Count, nodes.Count);
|
logger?.Log(loglevel.DEBUG, "Ways: {0} Nodes: {1}", ways.Count, nodes.Count);
|
||||||
|
|
||||||
reader.Close();
|
reader.Close();
|
||||||
reader = XmlReader.Create(new MemoryStream(OSM_Data.map), readerSettings);
|
reader = XmlReader.Create(new MemoryStream(OSM_Data.map), readerSettings);
|
||||||
@ -107,12 +107,12 @@ namespace OpenStreetMap_Importer
|
|||||||
float lon = Convert.ToSingle(reader.GetAttribute("lon").Replace('.', ','));
|
float lon = Convert.ToSingle(reader.GetAttribute("lon").Replace('.', ','));
|
||||||
#pragma warning restore CS8602
|
#pragma warning restore CS8602
|
||||||
nodes[id] = new Node(lat, lon);
|
nodes[id] = new Node(lat, lon);
|
||||||
logger.Log(loglevel.VERBOSE, "NODE {0} {1} {2}", id, lat, lon);
|
logger?.Log(loglevel.VERBOSE, "NODE {0} {1} {2}", id, lat, lon);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Log(loglevel.INFO, "Import finished. Calculating distances.");
|
logger?.Log(loglevel.INFO, "Import finished. Calculating distances.");
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Add connections between nodes based on ways and calculate distance
|
* Add connections between nodes based on ways and calculate distance
|
||||||
@ -128,7 +128,7 @@ namespace OpenStreetMap_Importer
|
|||||||
Node neighborNode = nodes[way.nodeIds[index + 1]];
|
Node neighborNode = nodes[way.nodeIds[index + 1]];
|
||||||
double weight = Utils.DistanceBetweenNodes(currentNode, neighborNode);
|
double weight = Utils.DistanceBetweenNodes(currentNode, neighborNode);
|
||||||
currentNode.edges.Add(new Edge(neighborNode, weight));
|
currentNode.edges.Add(new Edge(neighborNode, weight));
|
||||||
logger.Log(loglevel.VERBOSE, "EDGE {0} -- {1} --> {2}", way.nodeIds[index], weight, way.nodeIds[index + 1]);
|
logger?.Log(loglevel.VERBOSE, "EDGE {0} -- {1} --> {2}", way.nodeIds[index], weight, way.nodeIds[index + 1]);
|
||||||
edges++;
|
edges++;
|
||||||
if (!way.oneway)
|
if (!way.oneway)
|
||||||
{
|
{
|
||||||
@ -145,7 +145,7 @@ namespace OpenStreetMap_Importer
|
|||||||
Node neighborNode = nodes[way.nodeIds[index - 1]];
|
Node neighborNode = nodes[way.nodeIds[index - 1]];
|
||||||
double weight = Utils.DistanceBetweenNodes(currentNode, neighborNode);
|
double weight = Utils.DistanceBetweenNodes(currentNode, neighborNode);
|
||||||
currentNode.edges.Add(new Edge(neighborNode, weight));
|
currentNode.edges.Add(new Edge(neighborNode, weight));
|
||||||
logger.Log(loglevel.VERBOSE, "EDGE {0} -- {1} --> {2}", way.nodeIds[index], weight, way.nodeIds[index - 1]);
|
logger?.Log(loglevel.VERBOSE, "EDGE {0} -- {1} --> {2}", way.nodeIds[index], weight, way.nodeIds[index - 1]);
|
||||||
edges++;
|
edges++;
|
||||||
if (!way.oneway)
|
if (!way.oneway)
|
||||||
{
|
{
|
||||||
@ -157,7 +157,7 @@ namespace OpenStreetMap_Importer
|
|||||||
}
|
}
|
||||||
reader.Close();
|
reader.Close();
|
||||||
|
|
||||||
logger.Log(loglevel.DEBUG, "Edges: {0}", edges);
|
logger?.Log(loglevel.DEBUG, "Edges: {0}", edges);
|
||||||
return nodes;
|
return nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,27 +5,25 @@ namespace astar
|
|||||||
{
|
{
|
||||||
public class Astar
|
public class Astar
|
||||||
{
|
{
|
||||||
private Logger logger;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Loads the graph, chooses two nodes at random and calls a*
|
* Loads the graph, chooses two nodes at random and calls a*
|
||||||
*/
|
*/
|
||||||
public Astar(Dictionary<UInt64, Node> nodes, ref Logger logger)
|
public Astar(Dictionary<UInt64, Node> nodes, Logger ?logger = null)
|
||||||
{
|
{
|
||||||
this.logger = logger;
|
|
||||||
Random r = new Random();
|
Random r = new Random();
|
||||||
List<Node> path = new List<Node>();
|
List<Node> path = new List<Node>();
|
||||||
while(path.Count < 1)
|
while(path.Count < 1)
|
||||||
{
|
{
|
||||||
Node n1 = nodes[nodes.Keys.ElementAt(r.Next(0, nodes.Count - 1))];
|
Node n1 = nodes[nodes.Keys.ElementAt(r.Next(0, nodes.Count - 1))];
|
||||||
Node n2 = nodes[nodes.Keys.ElementAt(r.Next(0, nodes.Count - 1))];
|
Node n2 = nodes[nodes.Keys.ElementAt(r.Next(0, nodes.Count - 1))];
|
||||||
logger.Log(loglevel.INFO, "From {0} - {1} to {2} - {3}", n1.lat, n1.lon, n2.lat, n2.lon);
|
logger?.Log(loglevel.INFO, "From {0} - {1} to {2} - {3}", n1.lat, n1.lon, n2.lat, n2.lon);
|
||||||
path = FindPath(ref nodes, n1, n2, ref this.logger);
|
path = FindPath(ref nodes, n1, n2, ref logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Log(loglevel.INFO, "Path found");
|
logger?.Log(loglevel.INFO, "Path found");
|
||||||
foreach (Node n in path)
|
foreach (Node n in path)
|
||||||
logger.Log(loglevel.INFO, "lat {0:000.00000} lon {1:000.00000} traveled {2:0000.00} / {3:0000.00} beeline {4:0000.00}", n.lat, n.lon, n.pathLength, path.ElementAt(path.Count-1).pathLength, n.goalDistance);
|
logger?.Log(loglevel.INFO, "lat {0:000.00000} lon {1:000.00000} traveled {2:0000.00} / {3:0000.00} beeline {4:0000.00}", n.lat, n.lon, n.pathLength, path.ElementAt(path.Count-1).pathLength, n.goalDistance);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -43,7 +41,7 @@ namespace astar
|
|||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
private static List<Node> FindPath(ref Dictionary<ulong, Node> nodes, Node start, Node goal, ref Logger logger)
|
private static List<Node> FindPath(ref Dictionary<ulong, Node> nodes, Node start, Node goal, ref Logger ?logger)
|
||||||
{
|
{
|
||||||
Reset(ref nodes);
|
Reset(ref nodes);
|
||||||
List<Node> toVisit = new List<Node>();
|
List<Node> toVisit = new List<Node>();
|
||||||
@ -54,7 +52,7 @@ namespace astar
|
|||||||
while(currentNode != goal && toVisit.Count > 0)
|
while(currentNode != goal && toVisit.Count > 0)
|
||||||
{
|
{
|
||||||
currentNode = toVisit.First();
|
currentNode = toVisit.First();
|
||||||
logger.Log(loglevel.VERBOSE, "toVisit-length: {0} path: {1} goal: {2}", toVisit.Count, currentNode.pathLength, currentNode.goalDistance);
|
logger?.Log(loglevel.VERBOSE, "toVisit-length: {0} path: {1} goal: {2}", toVisit.Count, currentNode.pathLength, currentNode.goalDistance);
|
||||||
//Check all neighbors of current node
|
//Check all neighbors of current node
|
||||||
foreach (Edge e in currentNode.edges)
|
foreach (Edge e in currentNode.edges)
|
||||||
{
|
{
|
||||||
@ -75,7 +73,7 @@ namespace astar
|
|||||||
|
|
||||||
if (currentNode != goal)
|
if (currentNode != goal)
|
||||||
{
|
{
|
||||||
logger.Log(loglevel.INFO, "No path between {0} - {1} and {2} - {3}", start.lat, start.lon, goal.lat, goal.lon);
|
logger?.Log(loglevel.INFO, "No path between {0} - {1} and {2} - {3}", start.lat, start.lon, goal.lat, goal.lon);
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user