Пример #1
0
        private static KmlPoint ParsePoint(XmlNode xnode)
        {
            KmlPoint point = new KmlPoint();

            if (xnode.Attributes.Count > 0)
            {
                point.Name = xnode.Attributes[ATTR_ID]?.FirstChild.Value;
            }

            foreach (XmlNode node in xnode.ChildNodes)
            {
                switch (node.Name.ToLower())
                {
                case "extrude":
                    if (Int32.TryParse(node.FirstChild.Value, out int ext))
                    {
                        point.Extrude = ext;
                    }
                    break;

                case "altitudeMode":
                    if (Enum.TryParse(node.FirstChild.Value, out AltitudeMode mode))
                    {
                        point.AltMode = mode;
                    }
                    break;

                case "coordinates":
                {
                    string[] c = node.FirstChild.Value.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
                    if (c.Length > 0)
                    {
                        string[] v = c[0].Split(',').Select(y => y.Trim()).ToArray();
                        if (v.Length > 1)
                        {
                            if (Double.TryParse(v[0], out double lon))
                            {
                                if (Double.TryParse(v[1], out double lat))
                                {
                                    if (v.Length > 2 && double.TryParse(v[2], out double alt))
                                    {
                                        point.Coordinates = new Coordinates(lat, lon, alt);
                                    }
                                    else
                                    {
                                        point.Coordinates = new Coordinates(lat, lon);
                                    }
                                }
                            }
                        }
                    }
                    break;
                }
                }
            }

            return(point);
        }
Пример #2
0
        public void WritePoint(KmlPoint point)
        {
            try
            {
                if (point != null)
                {
                    WriteStartElement("Point");

                    if (point.Name != null)
                    {
                        WriteAttributeString("id", point.Name);
                    }
                    WriteComment("Point Guid: " + point.CN);

                    if (point.Extrude != null)
                    {
                        WriteElementString("extrude", point.Extrude.ToString());
                    }

                    if (point.AltMode != null)
                    {
                        if (point.AltMode == AltitudeMode.ClampToGround || point.AltMode == AltitudeMode.RelativeToGround || point.AltMode == AltitudeMode.Absolute)
                        {
                            WriteElementString("altitudeMode", point.AltMode.ToString());
                        }
                        else
                        {
                            WriteElementString("gx:altitudeMode", point.AltMode.ToString());
                        }
                    }

                    WriteStartElement("coordinates");
                    StringBuilder sb = new StringBuilder();
                    sb.Append($"{point.Coordinates.Longitude},{point.Coordinates.Latitude}");
                    sb.Append(point.Coordinates.Altitude != null ? $",{point.Coordinates.Altitude} " : " ");
                    WriteValue(sb.ToString());
                    WriteEndElement();

                    //end poly
                    WriteEndElement();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("WritePoint Error: " + ex.Message);
            }
        }
Пример #3
0
        private static Placemark ParsePlacemark(XmlNode xnode)
        {
            Placemark placemark = new Placemark(xnode.Attributes[ATTR_ID]?.FirstChild.Value);

            foreach (XmlNode node in xnode.ChildNodes)
            {
                string name = node.Name.ToLower();
                switch (name)
                {
                case "lookat": placemark.View = ParseView(node); break;

                case "point":
                {
                    KmlPoint point = ParsePoint(node);
                    if (point != null)
                    {
                        placemark.Points.Add(point);
                    }
                    break;
                }

                case "polygon":
                case "linestring":
                {
                    Polygon poly = ParsePolygon(node);
                    if (poly != null)
                    {
                        placemark.Polygons.Add(poly);
                    }
                    break;
                }

                case "multigeometry":
                {
                    foreach (XmlNode inode in node.ChildNodes)
                    {
                        switch (inode.Name.ToLower())
                        {
                        case "point":
                        {
                            KmlPoint point = ParsePoint(node);
                            if (point != null)
                            {
                                placemark.Points.Add(point);
                            }
                            break;
                        }

                        case "polygon":
                        case "linestring":
                        {
                            Polygon poly = ParsePolygon(node);
                            if (poly != null)
                            {
                                placemark.Polygons.Add(poly);
                            }
                            break;
                        }
                        }
                    }
                    break;
                }

                default: ParseProperties(placemark, name, node); break;
                }
            }

            return(placemark);
        }