void WriteStop(Stop stop) { _xmlWriter.WriteStartElement("stop"); _xmlWriter.WriteAttributeString("start", stop.From.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'f")); _xmlWriter.WriteAttributeString("end", stop.To.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'f")); _xmlWriter.WriteEndElement(); }
/// <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> /// 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); }