/// <summary> /// Adds the travel time to the DB /// </summary> /// <param name="toAdd"></param> public void AddTravelTime(TravelTime toAdd) { if (_storage.ContainsKey(toAdd.Segment) == false) { _storage.Add(toAdd.Segment, new List <TravelTime>()); } _storage[toAdd.Segment].Add(toAdd); }
/// <summary> /// Removes the travel time from the DB /// </summary> /// <param name="toRemove"></param> /// <returns></returns> public bool RemoveTravelTime(TravelTime toRemove) { if (_storage.ContainsKey(toRemove.Segment)) { return(_storage[toRemove.Segment].Remove(toRemove)); } return(false); }
void WriteTravelTime(TravelTime tt) { _xmlWriter.WriteStartElement("travel-time"); _xmlWriter.WriteAttributeString("start", tt.TimeStart.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'f")); _xmlWriter.WriteAttributeString("end", tt.TimeEnd.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'f")); foreach (var stop in tt.Stops) { WriteStop(stop); } _xmlWriter.WriteEndElement(); }
static void AddTrackToDB(ITravelTimesDB db, string path) { OSMDB track = new OSMDB(); Console.Write("Loading {0} ...", Path.GetFileName(path)); track.Load(path); try { var travelTimes = TravelTime.FromMatchedTrack(track); foreach (var travelTime in travelTimes) { db.AddTravelTime(travelTime); } Console.WriteLine("."); } catch (Exception e) { Console.WriteLine("Error: " + e.Message); } }
/// <summary> /// Creates a list of travel times from the matched track /// </summary> /// <param name="track"></param> /// <returns></returns> public static IEnumerable<TravelTime> FromMatchedTrack(OSMDB track) { List<TravelTime> result = new List<TravelTime>(); var orderedWays = track.Ways.OrderBy(way => int.Parse(way.Tags["order"].Value)).ToList(); //Find start of the first segment int index = 0; while (index < orderedWays.Count && track.Nodes[orderedWays[index].Nodes[0]].Tags.ContainsTag("crossroad") == false) index++; while (index < orderedWays.Count) { int startNodeId = int.Parse(track.Nodes[orderedWays[index].Nodes[0]].Tags["node-id"].Value); DateTime segmentStartTime = DateTime.MinValue; if (track.Nodes[orderedWays[index].Nodes[0]].Tags.ContainsTag("time")) segmentStartTime = DateTime.Parse(track.Nodes[orderedWays[index].Nodes[0]].Tags["time"].Value); else segmentStartTime = InterpolateStartTime(track, orderedWays, index); List<GPXPoint> points = new List<GPXPoint>(); points.Add(new GPXPoint(track.Nodes[orderedWays[index].Nodes[0]].Latitude, track.Nodes[orderedWays[index].Nodes[0]].Longitude, segmentStartTime)); while (index < orderedWays.Count && track.Nodes[orderedWays[index].Nodes.Last()].Tags.ContainsTag("crossroad") == false) { if (track.Nodes[orderedWays[index].Nodes.Last()].Tags.ContainsTag("time")) { points.Add(new GPXPoint(track.Nodes[orderedWays[index].Nodes.Last()].Latitude, track.Nodes[orderedWays[index].Nodes.Last()].Longitude, DateTime.Parse(track.Nodes[orderedWays[index].Nodes.Last()].Tags["time"].Value))); } index++; } if (index < orderedWays.Count) { int endNodeId = int.Parse(track.Nodes[orderedWays[index].Nodes.Last()].Tags["node-id"].Value); DateTime segmentEndTime = DateTime.MinValue; if (track.Nodes[orderedWays[index].Nodes.Last()].Tags.ContainsTag("time")) segmentEndTime = DateTime.Parse(track.Nodes[orderedWays[index].Nodes.Last()].Tags["time"].Value); else segmentEndTime = InterpolateEndTime(track, orderedWays, index); points.Add(new GPXPoint(track.Nodes[orderedWays[index].Nodes.Last()].Latitude, track.Nodes[orderedWays[index].Nodes.Last()].Longitude, segmentEndTime)); int wayId = int.Parse(orderedWays[index].Tags["way-id"].Value); SegmentInfo segment = new SegmentInfo() { NodeFromID = startNodeId, NodeToID = endNodeId, WayID = wayId }; List<double> avgSpeeds = new List<double>(); for (int i = 0; i < points.Count -1; i++) { avgSpeeds.Add(Calculations.GetDistance2D(points[i], points[i + 1]) / (points[i + 1].Time - points[i].Time).TotalSeconds); } TravelTime tt = new TravelTime(segment, segmentStartTime, segmentEndTime); int ii = 0; while (ii < avgSpeeds.Count) { if (avgSpeeds[ii] < 1.0) { Stop stop = new Stop() { From = points[ii].Time }; while (ii < avgSpeeds.Count && avgSpeeds[ii] < 1.0) ii++; stop.To = points[ii].Time; tt.Stops.Add(stop); } ii++; } result.Add(tt); index++; } } return result; }
/// <summary> /// Adds the travel time to the DB /// </summary> /// <param name="toAdd"></param> public void AddTravelTime(TravelTime toAdd) { if (_storage.ContainsKey(toAdd.Segment) == false) { _storage.Add(toAdd.Segment, new List<TravelTime>()); } _storage[toAdd.Segment].Add(toAdd); }
/// <summary> /// Removes the travel time from the DB /// </summary> /// <param name="toRemove"></param> /// <returns></returns> public bool RemoveTravelTime(TravelTime toRemove) { if (_storage.ContainsKey(toRemove.Segment)) { return _storage[toRemove.Segment].Remove(toRemove); } return false; }
/// <summary> /// Creates a list of travel times from the matched track /// </summary> /// <param name="track"></param> /// <returns></returns> public static IEnumerable <TravelTime> FromMatchedTrack(OSMDB track) { List <TravelTime> result = new List <TravelTime>(); var orderedWays = track.Ways.OrderBy(way => int.Parse(way.Tags["order"].Value)).ToList(); //Find start of the first segment int index = 0; while (index < orderedWays.Count && track.Nodes[orderedWays[index].Nodes[0]].Tags.ContainsTag("crossroad") == false) { index++; } while (index < orderedWays.Count) { int startNodeId = int.Parse(track.Nodes[orderedWays[index].Nodes[0]].Tags["node-id"].Value); DateTime segmentStartTime = DateTime.MinValue; if (track.Nodes[orderedWays[index].Nodes[0]].Tags.ContainsTag("time")) { segmentStartTime = DateTime.Parse(track.Nodes[orderedWays[index].Nodes[0]].Tags["time"].Value); } else { segmentStartTime = InterpolateStartTime(track, orderedWays, index); } List <GPXPoint> points = new List <GPXPoint>(); points.Add(new GPXPoint(track.Nodes[orderedWays[index].Nodes[0]].Latitude, track.Nodes[orderedWays[index].Nodes[0]].Longitude, segmentStartTime)); while (index < orderedWays.Count && track.Nodes[orderedWays[index].Nodes.Last()].Tags.ContainsTag("crossroad") == false) { if (track.Nodes[orderedWays[index].Nodes.Last()].Tags.ContainsTag("time")) { points.Add(new GPXPoint(track.Nodes[orderedWays[index].Nodes.Last()].Latitude, track.Nodes[orderedWays[index].Nodes.Last()].Longitude, DateTime.Parse(track.Nodes[orderedWays[index].Nodes.Last()].Tags["time"].Value))); } index++; } if (index < orderedWays.Count) { int endNodeId = int.Parse(track.Nodes[orderedWays[index].Nodes.Last()].Tags["node-id"].Value); DateTime segmentEndTime = DateTime.MinValue; if (track.Nodes[orderedWays[index].Nodes.Last()].Tags.ContainsTag("time")) { segmentEndTime = DateTime.Parse(track.Nodes[orderedWays[index].Nodes.Last()].Tags["time"].Value); } else { segmentEndTime = InterpolateEndTime(track, orderedWays, index); } points.Add(new GPXPoint(track.Nodes[orderedWays[index].Nodes.Last()].Latitude, track.Nodes[orderedWays[index].Nodes.Last()].Longitude, segmentEndTime)); int wayId = int.Parse(orderedWays[index].Tags["way-id"].Value); SegmentInfo segment = new SegmentInfo() { NodeFromID = startNodeId, NodeToID = endNodeId, WayID = wayId }; List <double> avgSpeeds = new List <double>(); for (int i = 0; i < points.Count - 1; i++) { avgSpeeds.Add(Calculations.GetDistance2D(points[i], points[i + 1]) / (points[i + 1].Time - points[i].Time).TotalSeconds); } TravelTime tt = new TravelTime(segment, segmentStartTime, segmentEndTime); int ii = 0; while (ii < avgSpeeds.Count) { if (avgSpeeds[ii] < 1.0) { Stop stop = new Stop() { From = points[ii].Time }; while (ii < avgSpeeds.Count && avgSpeeds[ii] < 1.0) { ii++; } stop.To = points[ii].Time; tt.Stops.Add(stop); } ii++; } result.Add(tt); index++; } } return(result); }