Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8316


Ignore:
Timestamp:
07/21/12 23:49:56 (12 years ago)
Author:
spimming
Message:

#1894:

  • check if way has missing node references
  • check if attribute exists before using it
  • test program restructured
Location:
branches/RoutePlanning
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning.Test/Program.cs

    r8315 r8316  
    11
    22using System;
     3using System.Diagnostics;
     4using System.IO;
    35using System.Xml;
    46using HeuristicLab.Algorithms.GraphRouting;
     
    911  class Program {
    1012    static void Main(string[] args) {
    11       Test1();
    12     }
     13      string file1 = @"..\..\OsmTestFiles\test.osm";
     14      string file2 = @"..\..\OsmTestFiles\testNode1.osm";
     15      string file3 = @"..\..\OsmTestFiles\testWay1.osm";
     16      string file4 = @"..\..\OsmTestFiles\testRelation1.osm";
     17      string file5 = @"..\..\OsmTestFiles\test_mid.osm";
     18      string file6 = @"C:\dev\osmfiles\oberosterreich.highway.osm";
    1319
    14     private static void Test1() {
    15       string filepath = @"..\..\OsmTestFiles\test_mid.osm";
    16 
    17       Console.Write("Loading data ... ");
    18       XmlDataSource xmlDs = new XmlDataSource(filepath);
    19       Console.WriteLine("done.");
    20 
    21       Graph.Graph graph = new Graph.Graph(xmlDs);
    22       IRouter router = new DijkstraAlgorithm(graph);
    23 
    24       Console.Write("Calculate route ... ");
    25       long[] result = router.Calculate(529102170, 1001363194);
    26       System.Console.WriteLine("done.");
    27 
    28       Console.Write("Result: ");
    29       foreach (long i in result) System.Console.Write(i + "; ");
    30       Console.WriteLine();
    31 
    32       WriteGPXFile(graph, result);
    33 
     20      Test(file5, 529102170, 1001363194, true, false);
     21      //Test(file6, 529102170, 1001363194, true, false);
     22      //Test(file6, 529102170, 31372732, false, true); // inz - hgb
    3423      System.Console.Read();
    3524    }
    3625
    37     private static void Test2() {
    38       //string filepath = @"..\..\OsmTestFiles\test.osm";
    39       //string filepath = @"..\..\OsmTestFiles\testNode1.osm";
    40       //string filepath = @"..\..\OsmTestFiles\testWay1.osm";
    41       //string filepath = @"..\..\OsmTestFiles\testRelation1.osm";
    42       string filepath = @"..\..\OsmTestFiles\test_mid.osm";
    43 
     26    private static void Test(string filepath, long sourceNodeId, long targetNodeId, bool showResult, bool writeResultFile) {
     27      FileInfo file = new FileInfo(filepath);
     28      Console.WriteLine("Test BEGIN ------------------------------");
     29      Console.WriteLine("File name: {0}", file.Name);
     30      Console.WriteLine();
    4431      Console.Write("Loading data ... ");
     32      var sw = Stopwatch.StartNew();
    4533      XmlDataSource xmlDs = new XmlDataSource(filepath);
     34      sw.Stop();
    4635      Console.WriteLine("done.");
    47 
     36      Console.WriteLine("Loading Time: {0}", sw.Elapsed);
     37      Console.WriteLine();
    4838      Graph.Graph graph = new Graph.Graph(xmlDs);
    4939      IRouter router = new DijkstraAlgorithm(graph);
    50 
    5140      Console.Write("Calculate route ... ");
    52       long[] result = router.Calculate(529102170, 1001363194);
    53       System.Console.WriteLine("done.");
    54 
    55       Console.Write("Result: ");
    56       foreach (long i in result) System.Console.Write(i + "; ");
     41      sw = Stopwatch.StartNew();
     42      long[] result = router.Calculate(sourceNodeId, targetNodeId);
     43      sw.Stop();
     44      Console.WriteLine("done.");
     45      Console.WriteLine("Execution Time: {0}", sw.Elapsed);
    5746      Console.WriteLine();
    58       System.Console.Read();
     47      if (showResult) {
     48        Console.WriteLine("Result: ");
     49        foreach (long i in result) System.Console.Write(i + "; ");
     50        Console.WriteLine();
     51        Console.WriteLine();
     52      }
     53      if (writeResultFile) {
     54        string dt = DateTime.Now.ToString("yyyyMMddhhmmss_");
     55        string fn = Path.GetFileNameWithoutExtension(file.Name);
     56        string resultFile = dt + fn + "_" + sourceNodeId + "-" + targetNodeId + ".gpx";
     57        Console.WriteLine("Result file: {0}", resultFile);
     58        WriteGPXFile(graph, result, resultFile);
     59      }
     60      Console.WriteLine("=========================================");
     61      Console.WriteLine();
    5962    }
    6063
    61     private static void WriteGPXFile(Graph.Graph graph, long[] route) {
     64    private static void WriteGPXFile(Graph.Graph graph, long[] route, string file) {
    6265      XmlWriterSettings settings = new XmlWriterSettings();
    6366      settings.Indent = true;
    6467      settings.IndentChars = ("   ");
    6568
    66       using (XmlWriter writer = XmlWriter.Create("RoutePath.gpx", settings)) {
     69      using (XmlWriter writer = XmlWriter.Create(file, settings)) {
    6770        writer.WriteStartElement("gpx", "http://www.topografix.com/GPX/1/0");
    6871        writer.WriteAttributeString("version", "1.1");
  • branches/RoutePlanning/HeuristicLab.Problems.RoutePlanning/3.3/Osm.Data/XmlDataSource.cs

    r8300 r8316  
    9191      using (XmlReader reader = XmlReader.Create(file.FullName, settings)) {
    9292        reader.ReadStartElement("osm");
    93         reader.ReadStartElement("bounds");
     93        //reader.ReadStartElement("bounds");
     94        reader.Read();
    9495        while (reader.LocalName.Equals("node")) {
    9596          Node node = new Node();
     
    119120          GetBaseAttributes(reader, way);
    120121          reader.Read();
     122          bool missingNodes = false;
    121123          while (reader.LocalName.Equals("nd") || reader.LocalName.Equals("tag")) {
    122124            if (reader.LocalName.Equals("nd")) {
    123125              long refNodeId = XmlConvert.ToInt64(reader.GetAttribute("ref"));
    124               way.Nodes.Add(GetNode(refNodeId));
     126              Node refNode = GetNode(refNodeId);
     127              if (refNode != null) {
     128                way.Nodes.Add(refNode);
     129              } else {
     130                missingNodes = true;
     131              }
    125132
    126133            } else if (reader.LocalName.Equals("tag")) {
     
    131138            reader.Read();
    132139          }
    133 
     140          if (missingNodes) {
     141            // TODO: Error/Trace output
     142            // if Way xxx has incomplete nodes. At least one node was missing in the data.
     143          }
    134144          ways.Add(way.Id, way);
    135145          InsertNodeWayRelation(way);
     
    181191        obj.UserId = XmlConvert.ToInt32(reader.GetAttribute("uid"));
    182192      }
    183       obj.Visible = XmlConvert.ToBoolean(reader.GetAttribute("visible"));
    184       obj.Version = XmlConvert.ToInt32(reader.GetAttribute("version"));
    185       obj.Changeset = XmlConvert.ToInt64(reader.GetAttribute("changeset"));
    186       obj.Timestamp = XmlConvert.ToDateTime(reader.GetAttribute("timestamp"), XmlDateTimeSerializationMode.Utc);
     193      if (reader.GetAttribute("visible") != null) {
     194        obj.Visible = XmlConvert.ToBoolean(reader.GetAttribute("visible"));
     195      }
     196      if (reader.GetAttribute("version") != null) {
     197        obj.Version = XmlConvert.ToInt32(reader.GetAttribute("version"));
     198      }
     199      if (reader.GetAttribute("changeset") != null) {
     200        obj.Changeset = XmlConvert.ToInt64(reader.GetAttribute("changeset"));
     201      }
     202      if (reader.GetAttribute("timestamp") != null) {
     203        obj.Timestamp = XmlConvert.ToDateTime(reader.GetAttribute("timestamp"), XmlDateTimeSerializationMode.Utc);
     204      }
    187205
    188206    }
Note: See TracChangeset for help on using the changeset viewer.