Exemplo n.º 1
0
        Gpx.GpxWayPoint SetGpxWayPoint(Models.GpxPoint pt)
        {
            var pto = new Gpx.GpxWayPoint();

            pto.Elevation = (Double?)pt.Ele;
            pto.Latitude  = (Double)(pt.Lat ?? 0);
            pto.Longitude = (Double)(pt.Lon ?? 0);
            return(pto);
        }
        private GpxWayPoint ReadGpxWayPoint()
        {
            string elementName    = Reader_.Name;
            bool   isEmptyElement = Reader_.IsEmptyElement;

            GpxWayPoint wayPoint = new GpxWayPoint();

            GetPointLocation(wayPoint);
            if (isEmptyElement)
            {
                return(wayPoint);
            }

            while (Reader_.Read())
            {
                switch (Reader_.NodeType)
                {
                case XmlNodeType.Element:

                    switch (Reader_.Name)
                    {
                    case "extensions":
                        ReadWayPointExtensions(wayPoint);
                        break;

                    default:
                        if (!ProcessPointField(wayPoint))
                        {
                            SkipElement();
                        }
                        break;
                    }

                    break;

                case XmlNodeType.EndElement:
                    if (Reader_.Name != elementName)
                    {
                        throw new FormatException(Reader_.Name);
                    }
                    return(wayPoint);
                }
            }

            throw new FormatException(elementName);
        }
        private void ReadWayPointAliases(GpxWayPoint wayPoint)
        {
            if (Reader_.IsEmptyElement)
            {
                return;
            }

            string elementName = Reader_.Name;

            while (Reader_.Read())
            {
                switch (Reader_.NodeType)
                {
                case XmlNodeType.Element:
                    switch (Reader_.LocalName)
                    {
                    case "alias":
                        wayPoint.Aliases.Add(ReadContentAsString());
                        break;

                    default:
                        SkipElement();
                        break;
                    }

                    break;

                case XmlNodeType.EndElement:
                    if (Reader_.Name != elementName)
                    {
                        throw new FormatException(Reader_.Name);
                    }
                    return;
                }
            }

            throw new FormatException(elementName);
        }
        public void WriteWayPoint(GpxWayPoint wayPoint)
        {
            Writer_.WriteStartElement("wpt");
            WritePoint(wayPoint);

            if (wayPoint.HasExtensions)
            {
                Writer_.WriteStartElement("extensions");

                if (wayPoint.HasGarminExtensions || wayPoint.HasGarminWaypointExtensions)
                {
                    Writer_.WriteStartElement("WaypointExtension", GpxNamespaces.GARMIN_WAYPOINT_EXTENSIONS_NAMESPACE);

                    if (wayPoint.Proximity != null)
                    {
                        Writer_.WriteElementString("Proximity", GpxNamespaces.GARMIN_WAYPOINT_EXTENSIONS_NAMESPACE, wayPoint.Proximity.Value.ToString());
                    }
                    if (wayPoint.Temperature != null)
                    {
                        Writer_.WriteElementString("Temperature", GpxNamespaces.GARMIN_WAYPOINT_EXTENSIONS_NAMESPACE, wayPoint.Temperature.Value.ToString());
                    }
                    if (wayPoint.Depth != null)
                    {
                        Writer_.WriteElementString("Depth", GpxNamespaces.GARMIN_WAYPOINT_EXTENSIONS_NAMESPACE, wayPoint.Depth.Value.ToString());
                    }
                    if (wayPoint.DisplayMode != null)
                    {
                        Writer_.WriteElementString("DisplayMode", GpxNamespaces.GARMIN_WAYPOINT_EXTENSIONS_NAMESPACE, wayPoint.DisplayMode);
                    }

                    if (wayPoint.Categories.Count != 0)
                    {
                        Writer_.WriteStartElement("Categories", GpxNamespaces.GARMIN_WAYPOINT_EXTENSIONS_NAMESPACE);

                        foreach (string category in wayPoint.Categories)
                        {
                            Writer_.WriteElementString("Category", GpxNamespaces.GARMIN_WAYPOINT_EXTENSIONS_NAMESPACE, category);
                        }

                        Writer_.WriteEndElement();
                    }

                    if (wayPoint.Address != null)
                    {
                        WriteAddress("Address", wayPoint.Address);
                    }

                    foreach (GpxPhone phone in wayPoint.Phones)
                    {
                        WritePhone("PhoneNumber", phone);
                    }

                    if (wayPoint.Samples != null)
                    {
                        Writer_.WriteElementString("Samples", GpxNamespaces.GARMIN_WAYPOINT_EXTENSIONS_NAMESPACE, wayPoint.Samples.Value.ToString());
                    }
                    if (wayPoint.Expiration != null)
                    {
                        Writer_.WriteElementString("Expiration", GpxNamespaces.GARMIN_WAYPOINT_EXTENSIONS_NAMESPACE, ToGpxDateString(wayPoint.Expiration.Value));
                    }

                    Writer_.WriteEndElement();
                }

                if (wayPoint.Aliases.Count != 0)
                {
                    Writer_.WriteStartElement("aliases", GpxNamespaces.DLG_EXTENSIONS_NAMESPACE);

                    foreach (string alias in wayPoint.Aliases)
                    {
                        Writer_.WriteElementString("alias", GpxNamespaces.DLG_EXTENSIONS_NAMESPACE, alias);
                    }

                    Writer_.WriteEndElement();
                }

                if (wayPoint.Level != null)
                {
                    Writer_.WriteElementString("level", GpxNamespaces.DLG_EXTENSIONS_NAMESPACE, wayPoint.Level.Value.ToString());
                }

                Writer_.WriteEndElement();
            }

            Writer_.WriteEndElement();
        }
        private void ReadWayPointExtensions(GpxWayPoint wayPoint)
        {
            if (Reader_.IsEmptyElement)
            {
                return;
            }

            string elementName = Reader_.Name;

            while (Reader_.Read())
            {
                switch (Reader_.NodeType)
                {
                case XmlNodeType.Element:

                    if (Reader_.NamespaceURI == GpxNamespaces.GARMIN_EXTENSIONS_NAMESPACE || Reader_.NamespaceURI == GpxNamespaces.GARMIN_WAYPOINT_EXTENSIONS_NAMESPACE)
                    {
                        switch (Reader_.LocalName)
                        {
                        case "WaypointExtension":
                            ReadGarminWayPointExtensions(wayPoint);
                            break;

                        default:
                            SkipElement();
                            break;
                        }

                        break;
                    }

                    if (Reader_.NamespaceURI == GpxNamespaces.DLG_EXTENSIONS_NAMESPACE)
                    {
                        switch (Reader_.LocalName)
                        {
                        case "level":
                            wayPoint.Level = ReadContentAsInt();
                            break;

                        case "aliases":
                            ReadWayPointAliases(wayPoint);
                            break;

                        default:
                            SkipElement();
                            break;
                        }

                        break;
                    }

                    SkipElement();
                    break;

                case XmlNodeType.EndElement:
                    if (Reader_.Name != elementName)
                    {
                        throw new FormatException(Reader_.Name);
                    }
                    return;
                }
            }

            throw new FormatException(elementName);
        }
        private void ReadGarminWayPointExtensions(GpxWayPoint wayPoint)
        {
            if (Reader_.IsEmptyElement)
            {
                return;
            }

            string elementName = Reader_.Name;

            while (Reader_.Read())
            {
                switch (Reader_.NodeType)
                {
                case XmlNodeType.Element:
                    switch (Reader_.LocalName)
                    {
                    case "Proximity":
                        wayPoint.Proximity = ReadContentAsDouble();
                        break;

                    case "Temperature":
                        wayPoint.Temperature = ReadContentAsDouble();
                        break;

                    case "Depth":
                        wayPoint.Depth = ReadContentAsDouble();
                        break;

                    case "DisplayMode":
                        wayPoint.DisplayMode = ReadContentAsString();
                        break;

                    case "Categories":
                        ReadGarminCategories(wayPoint);
                        break;

                    case "Address":
                        wayPoint.Address = ReadGarminGpxAddress();
                        break;

                    case "PhoneNumber":
                        wayPoint.Phones.Add(ReadGarminGpxPhone());
                        break;

                    case "Samples":
                        wayPoint.Samples = ReadContentAsInt();
                        break;

                    case "Expiration":
                        wayPoint.Expiration = ReadContentAsDateTime();
                        break;

                    default:
                        SkipElement();
                        break;
                    }

                    break;

                case XmlNodeType.EndElement:
                    if (Reader_.Name != elementName)
                    {
                        throw new FormatException(Reader_.Name);
                    }
                    return;
                }
            }

            throw new FormatException(elementName);
        }