Ejemplo n.º 1
0
        /// <summary>
        /// Opens a document stored in the old QuickRoute XML file format. This version can't save documents in this file format.
        /// </summary>
        /// <param name="fileName">The file name of the QuickRoute 1.0 xml document.</param>
        /// <param name="settings">The document settings to apply.</param>
        /// <returns></returns>
        public static Document OpenFromXml(string fileName, DocumentSettings settings)
        {
            XmlTextReader reader = null;

            RouteSegment     rs      = new RouteSegment();
            HandleCollection handles = new HandleCollection();
            Map map;

            try
            {
                reader = new XmlTextReader(fileName);
                reader.WhitespaceHandling = WhitespaceHandling.None;

                reader.ReadStartElement("QuickRoute");

                reader.ReadStartElement("Route");

                while (reader.Read() && reader.NodeType != XmlNodeType.EndElement)
                {
                    while (reader.NodeType != XmlNodeType.Element)
                    {
                        reader.Read();
                    }
                    Waypoint t = new Waypoint();
                    t.Time              = DateTime.Parse(reader.GetAttribute("time"));
                    t.LongLat           = new LongLat();
                    t.LongLat.Longitude = double.Parse(reader.GetAttribute("longitude"));
                    t.LongLat.Latitude  = double.Parse(reader.GetAttribute("latitude"));
                    t.Altitude          = double.Parse(reader.GetAttribute("altitude"));
                    t.HeartRate         = int.Parse(reader.GetAttribute("heartRate"));
                    rs.Waypoints.Add(t);
                }
                reader.ReadEndElement();

                reader.ReadStartElement("Markers");
                while (reader.Name == "Handle")
                {
                    reader.Read();
                    Handle h = new Handle();
                    h.ParameterizedLocation = new ParameterizedLocation(0, double.Parse(reader.GetAttribute("value")));
                    reader.Read();
                    double x = double.Parse(reader.GetAttribute("x"));
                    double y = double.Parse(reader.GetAttribute("y"));
                    h.Location = new PointD(x, y);
                    reader.Read();
                    h.TransformationMatrix = new GeneralMatrix(3, 3);
                    h.MarkerDrawer         = (new ApplicationSettings()).DefaultDocumentSettings.DefaultSessionSettings.MarkerDrawers[MarkerType.Handle];
                    for (int row = 0; row < 3; row++)
                    {
                        for (int col = 0; col < 3; col++)
                        {
                            reader.Read();
                            h.TransformationMatrix.SetElement(row, col, double.Parse(reader.GetAttribute("value")));
                        }
                    }
                    reader.Read();
                    reader.ReadEndElement();
                    reader.ReadEndElement();
                    handles.Add(h);
                }
                reader.ReadEndElement();

                map = new Map(Base64StringToBitmap(reader.ReadElementContentAsString()));
            }
            catch (Exception ex)
            {
                if (reader != null)
                {
                    reader.Close();
                }
                throw new Exception(ex.Message);
            }
            reader.Close();

            List <RouteSegment> routeSegments = new List <RouteSegment>();

            routeSegments.Add(rs);
            Document doc = new Document(map, new Route(routeSegments), new LapCollection(), null, settings);

            foreach (var h in handles)
            {
                doc.Sessions[0].AddHandle(h);
            }
            doc.FileFormat = QuickRouteFileFormat.Xml;
            doc.Initialize();
            UpdateDocumentToCurrentVersion(doc);
            return(doc);
        }
Ejemplo n.º 2
0
        private static Session ReadSession(BinaryReader reader, int length)
        {
            List <DateTime>  mapReadingList   = null;
            Route            route            = null;
            HandleCollection handles          = null;
            LongLat          projectionOrigin = null;
            LapCollection    laps             = null;
            var         startPos    = reader.BaseStream.Position;
            SessionInfo sessionInfo = null;
            DateTime    lastTime;

            while (reader.BaseStream.Position < startPos + length)
            {
                var tag       = (Tags)reader.ReadByte();
                var tagLength = Convert.ToInt32(reader.ReadUInt32());
                switch (tag)
                {
                case Tags.Route:
                    var attributes = reader.ReadUInt16();
                    var extraWaypointAttributesLength = reader.ReadUInt16();
                    var routeSegments = new List <RouteSegment>();
                    var segmentCount  = reader.ReadUInt32();
                    lastTime = DateTime.MinValue;
                    for (var i = 0; i < segmentCount; i++)
                    {
                        var rs            = new RouteSegment();
                        var waypointCount = reader.ReadUInt32();
                        for (var j = 0; j < waypointCount; j++)
                        {
                            var w = new Waypoint();
                            w.LongLat = ReadLongLat(reader);
                            w.Time    = ReadTime(lastTime, reader);
                            lastTime  = w.Time;
                            if ((attributes & (UInt16)WaypointAttribute.HeartRate) == (UInt16)WaypointAttribute.HeartRate)
                            {
                                w.HeartRate = reader.ReadByte();
                            }
                            if ((attributes & (UInt16)WaypointAttribute.Altitude) == (UInt16)WaypointAttribute.Altitude)
                            {
                                w.Altitude = reader.ReadInt16();
                            }
                            reader.BaseStream.Position += extraWaypointAttributesLength; // for forward compatibility
                            rs.Waypoints.Add(w);
                        }
                        routeSegments.Add(rs);
                    }
                    route = new Route(routeSegments);
                    break;

                case Tags.Handles:
                    handles = new HandleCollection();
                    var handleCount        = reader.ReadUInt32();
                    var handleMarkerDrawer = SessionSettings.CreateDefaultMarkerDrawers()[MarkerType.Handle];
                    for (var i = 0; i < handleCount; i++)
                    {
                        var handle = new Handle();
                        // transformation matrix
                        handle.TransformationMatrix = new GeneralMatrix(3, 3);
                        for (var j = 0; j < 9; j++)
                        {
                            handle.TransformationMatrix.SetElement(j / 3, j % 3, reader.ReadDouble());
                        }
                        // parameterized location
                        var segmentIndex = Convert.ToInt32(reader.ReadUInt32());
                        var value        = reader.ReadDouble();
                        handle.ParameterizedLocation = new ParameterizedLocation(segmentIndex, value);

                        // pixel location
                        handle.Location = new PointD(reader.ReadDouble(), reader.ReadDouble());
                        // type
                        handle.Type = (Handle.HandleType)reader.ReadInt16();
                        // use default marker drawer
                        handle.MarkerDrawer = handleMarkerDrawer;

                        handles.Add(handle);
                    }
                    break;

                case Tags.ProjectionOrigin:
                    projectionOrigin = ReadLongLat(reader);
                    break;

                case Tags.Laps:
                    laps = new LapCollection();
                    var lapCount = reader.ReadUInt32();
                    for (var i = 0; i < lapCount; i++)
                    {
                        var lap = new Lap();
                        lap.Time    = DateTime.FromBinary(reader.ReadInt64());
                        lap.LapType = (LapType)reader.ReadByte();
                        laps.Add(lap);
                    }
                    break;

                case Tags.SessionInfo:
                    sessionInfo             = new SessionInfo();
                    sessionInfo.Person      = new SessionPerson();
                    sessionInfo.Person.Name = ReadString(reader);
                    sessionInfo.Person.Club = ReadString(reader);
                    sessionInfo.Person.Id   = reader.ReadUInt32();
                    sessionInfo.Description = ReadString(reader);
                    // when more fields are added, check so that tagLength is not passed
                    break;

                case Tags.MapReadingInfo:
                    mapReadingList = new List <DateTime>();
                    lastTime       = DateTime.MinValue;
                    var startPosition = reader.BaseStream.Position;
                    while (reader.BaseStream.Position - startPosition < tagLength)
                    {
                        var time = ReadTime(lastTime, reader);
                        mapReadingList.Add(time);
                        lastTime = time;
                    }
                    break;

                default:
                    reader.BaseStream.Position += tagLength;
                    break;
                }
            }

            if (mapReadingList != null && route != null)
            {
                route = new Route(Route.AddMapReadingWaypoints(route.Segments, mapReadingList));
            }
            var session = new Session(
                route,
                laps,
                new Size(0, 0),
                handles != null && handles.Count > 0 ? handles[0].TransformationMatrix : null,
                projectionOrigin,
                new SessionSettings());

            if (handles != null)
            {
                foreach (var h in handles)
                {
                    session.AddHandle(h);
                }
            }
            if (sessionInfo != null)
            {
                session.SessionInfo = sessionInfo;
            }

            return(session);
        }