public KmlDocument CreateKmlDoc(DataAccessLayer DAL)
        {
            #region Create Document

            KmlDocument doc = new KmlDocument(Values.Settings.ProjectOptions.ProjectName, Values.Settings.ProjectOptions.Description);

            doc.Open = true;
            doc.Properties = new KmlProperties();
            doc.Properties.Snippit = "Generated in TwoTrails";
            doc.Visibility = true;

            #endregion

            #region Create Styles / StyleMaps

            const int AdjLineSize = 5;
            const int UnAdjLineSize = 7;

            KmlStyle sAdjBound = new KmlStyle("AdjBound");
            KmlStyle sUnAdjBound = new KmlStyle("UnAdjBound");
            KmlStyle sAdjNav = new KmlStyle("AdjNav");
            KmlStyle sUnAdjNav = new KmlStyle("UnAdjNav");
            KmlStyle sAdjMisc = new KmlStyle("AdjMisc");
            KmlStyle sUnAdjMisc = new KmlStyle("UnAdjMisc");
            KmlStyle sWay = new KmlStyle("Way");

            KmlColor AdjBoundColor = new KmlColor(27, 211, 224, 255);   //1BD3E0
            KmlColor UnAdjBoundColor = new KmlColor(27, 112, 224, 255); //1B70E0
            KmlColor AdjNavColor = new KmlColor(27, 224, 142, 255);     //1BE08E
            KmlColor UnAdjNavColor = new KmlColor(14, 168, 86, 255);    //0E8A56
            KmlColor AdjMiscColor = new KmlColor(234, 90, 250, 255);    //EA5AFA
            KmlColor UnAdjMiscColor = new KmlColor(164, 29, 179, 255);  //A41DB3
            KmlColor WayColor = new KmlColor(255, 0, 0, 255);           //FF0000
            KmlColor KmlWhite = new KmlColor(255, 255, 255, 255);       //FFFFFF

            sAdjBound.SetColorsILP(AdjBoundColor);
            sAdjBound.IconScale = 1;
            sAdjBound.LineWidth = AdjLineSize;
            sAdjBound.LineLabelVisibility = true;
            sAdjBound.PolygonFill = false;
            sAdjBound.PolygonOutline = true;
            sAdjBound.BalloonDisplayMode = DisplayMode.Default;

            sUnAdjBound.SetColorsILP(UnAdjBoundColor);
            sUnAdjBound.IconScale = 1;
            sUnAdjBound.LineWidth = UnAdjLineSize;
            sUnAdjBound.LineLabelVisibility = false;
            sUnAdjBound.PolygonFill = false;
            sUnAdjBound.PolygonOutline = true;
            sUnAdjBound.BalloonDisplayMode = DisplayMode.Default;

            sAdjNav.SetColorsILP(AdjNavColor);
            sAdjNav.IconScale = 1;
            sAdjNav.LineWidth = AdjLineSize;
            sAdjNav.LineLabelVisibility = false;
            sAdjNav.PolygonFill = false;
            sAdjNav.PolygonOutline = true;
            sAdjNav.BalloonDisplayMode = DisplayMode.Default;

            sUnAdjNav.SetColorsILP(UnAdjNavColor);
            sUnAdjNav.IconScale = 1;
            sUnAdjNav.LineWidth = UnAdjLineSize;
            sUnAdjNav.LineLabelVisibility = false;
            sUnAdjNav.PolygonFill = false;
            sUnAdjNav.PolygonOutline = true;
            sUnAdjNav.BalloonDisplayMode = DisplayMode.Default;

            sUnAdjMisc.IconColorMode = ColorMode.Normal;
            sUnAdjMisc.IconColor = UnAdjMiscColor;
            sUnAdjMisc.IconScale = 1;
            sUnAdjMisc.BalloonDisplayMode = DisplayMode.Default;

            sWay.IconColorMode = ColorMode.Normal;
            sWay.IconColor = WayColor;
            sWay.IconScale = 1;
            sWay.BalloonDisplayMode = DisplayMode.Default;

            KmlStyle sAdjBoundH = new KmlStyle(sAdjBound, "AdjBoundH");
            KmlStyle sUnAdjBoundH = new KmlStyle(sUnAdjBound, "UnAdjBoundH");
            KmlStyle sAdjNavH = new KmlStyle(sAdjNav, "AdjNavH");
            KmlStyle sUnAdjNavH = new KmlStyle(sUnAdjNav, "UnAdjNavH");
            KmlStyle sAdjMiscH = new KmlStyle(sAdjMisc, "AdjMiscH");
            KmlStyle sUnAdjMiscH = new KmlStyle(sUnAdjMisc, "UnAdjMiscH");
            KmlStyle sWayH = new KmlStyle(sWay, "WayH");

            sAdjBoundH.IconScale = 1.1;
            sAdjBoundH.IconColor = KmlWhite;
            sUnAdjBoundH.IconScale = 1.1;
            sUnAdjBoundH.IconColor = KmlWhite;
            sAdjNavH.IconScale = 1.1;
            sAdjNavH.IconColor = KmlWhite;
            sUnAdjNavH.IconScale = 1.1;
            sUnAdjNavH.IconColor = KmlWhite;
            sAdjMiscH.IconScale = 1.1;
            sAdjMiscH.IconColor = KmlWhite;
            sUnAdjMiscH.IconScale = 1.1;
            sUnAdjMiscH.IconColor = KmlWhite;
            sWayH.IconScale = 1.1;
            sWayH.IconColor = KmlWhite;

            doc.AddStyle(sAdjBound);
            doc.AddStyle(sAdjBoundH);
            doc.AddStyle(sUnAdjBound);
            doc.AddStyle(sUnAdjBoundH);
            doc.AddStyle(sAdjNav);
            doc.AddStyle(sAdjNavH);
            doc.AddStyle(sUnAdjNav);
            doc.AddStyle(sUnAdjNavH);
            doc.AddStyle(sAdjMisc);
            doc.AddStyle(sAdjMiscH);
            doc.AddStyle(sUnAdjMisc);
            doc.AddStyle(sUnAdjMiscH);
            doc.AddStyle(sWay);
            doc.AddStyle(sWayH);

            KmlStyleMap sAdjBoundMap = new KmlStyleMap("AdjBoundMap", sAdjBound.StyleUrl, sAdjBoundH.StyleUrl);
            KmlStyleMap sUnAdjBoundMap = new KmlStyleMap("UnAdjBoundMap", sUnAdjBound.StyleUrl, sUnAdjBoundH.StyleUrl);
            KmlStyleMap sAdjNavMap = new KmlStyleMap("AdjNavMap", sAdjNav.StyleUrl, sAdjNavH.StyleUrl);
            KmlStyleMap sUnAdjNavMap = new KmlStyleMap("UnAdjNavMap", sUnAdjNav.StyleUrl, sUnAdjNavH.StyleUrl);
            KmlStyleMap sAdjMiscMap = new KmlStyleMap("AdjMiscMap", sAdjMisc.StyleUrl, sAdjMiscH.StyleUrl);
            KmlStyleMap sUnAdjMiscMap = new KmlStyleMap("UnAdjMiscMap", sUnAdjMisc.StyleUrl, sUnAdjMiscH.StyleUrl);
            KmlStyleMap sWayMap = new KmlStyleMap("WayMap", sWay.StyleUrl, sWayH.StyleUrl);

            doc.AddStyleMap(sAdjBoundMap);
            doc.AddStyleMap(sUnAdjBoundMap);
            doc.AddStyleMap(sAdjNavMap);
            doc.AddStyleMap(sUnAdjNavMap);
            doc.AddStyleMap(sAdjMiscMap);
            doc.AddStyleMap(sUnAdjMiscMap);
            doc.AddStyleMap(sWayMap);

            #endregion

            #region Create Polygons

            List<TtPolygon> polys = DAL.GetPolygons();

            foreach (TtPolygon poly in polys)
            {
                List<TtPoint> points = DAL.GetPointsInPolygon(poly.CN);

                #region Create root Polygon Folder
                KmlFolder folder = new KmlFolder(poly.Name, poly.Description);

                folder.Open = false;
                folder.Visibility = true;

                folder.Properties = new KmlProperties();
                folder.Properties.Snippit = poly.Description;
                folder.Properties.ExtendedData = new KmlExtendedData();
                folder.Properties.ExtendedData.AddData(new KmlExtendedData.Data(" ", "In Meters"));
                folder.Properties.ExtendedData.AddData(new KmlExtendedData.Data("Accuracy", poly.PolyAccu.ToString()));
                folder.Properties.ExtendedData.AddData(new KmlExtendedData.Data("Perimeter", poly.Perimeter.ToString()));
                folder.Properties.ExtendedData.AddData(new KmlExtendedData.Data("Area", poly.Area.ToString()));
                #endregion

                #region Create SubFolders under Polygon root
                KmlFolder fAdjBound = new KmlFolder("AdjBound", "Adjusted Boundary Polygon");
                KmlFolder fUnAdjBound = new KmlFolder("UnAdjBound", "UnAdjusted Boundary Polygon");
                KmlFolder fAdjNav = new KmlFolder("AdjNav", "Adjusted Navigation Polygon");
                KmlFolder fUnAdjNav = new KmlFolder("UnAdjNav", "UnAdjusted Navigation Polygon");
                KmlFolder fMiscPoints = new KmlFolder("Misc", "Misc Points");
                KmlFolder fWayPoints = new KmlFolder("Waypoints", "Waypoints");

                fAdjBound.StyleUrl = sAdjBoundMap.StyleUrl;
                fUnAdjBound.StyleUrl = sUnAdjBoundMap.StyleUrl;
                fAdjNav.StyleUrl = sAdjNavMap.StyleUrl;
                fUnAdjNav.StyleUrl = sUnAdjNavMap.StyleUrl;
                fMiscPoints.StyleUrl = sAdjMiscMap.StyleUrl;
                fWayPoints.StyleUrl = sWayMap.StyleUrl;

                fAdjBound.Visibility = true;
                fUnAdjBound.Visibility = false;
                fAdjNav.Visibility = false;
                fUnAdjNav.Visibility = false;
                fMiscPoints.Visibility = false;
                fWayPoints.Visibility = false;

                fAdjBound.Open = false;
                fUnAdjBound.Open = false;
                fAdjNav.Open = false;
                fUnAdjNav.Open = false;
                fMiscPoints.Open = false;
                fWayPoints.Open = false;

                #endregion

                #region Create SubFolders for Bound, Nav and Misc

                KmlFolder fAdjBoundPoints = new KmlFolder("Points", "Adjusted Boundary Points");
                KmlFolder fUnAdjBoundPoints = new KmlFolder("Points", "UnAdjusted Boundary Points");
                KmlFolder fAdjNavPoints = new KmlFolder("Points", "Adjusted Navigation Points");
                KmlFolder fUnAdjNavPoints = new KmlFolder("Points", "UnAdjusted Navigation Points");
                KmlFolder fAdjMiscPoints = new KmlFolder("Adj Points", "Adjusted Misc Points");
                KmlFolder fUnAdjMiscPoints = new KmlFolder("UnAdj Points", "UnAdjusted Misc Points");

                fAdjBoundPoints.Visibility = true;
                fUnAdjBoundPoints.Visibility = false;
                fAdjNavPoints.Visibility = false;
                fUnAdjNavPoints.Visibility = false;
                fAdjMiscPoints.Visibility = false;
                fUnAdjMiscPoints.Visibility = false;

                fAdjBoundPoints.Open = false;
                fUnAdjBoundPoints.Open = false;
                fAdjNavPoints.Open = false;
                fUnAdjNavPoints.Open = false;
                fAdjMiscPoints.Open = false;
                fUnAdjMiscPoints.Open = false;
                #endregion

                #region Create Geometry

                KmlPolygon AdjBoundPoly = new KmlPolygon("AdjBoundPoly");
                KmlPolygon UnAdjBoundPoly = new KmlPolygon("UnAdjBoundPoly");
                List<Coordinates> AdjBoundPointList = new List<Coordinates>();
                List<Coordinates> UnAdjBoundPointList = new List<Coordinates>();

                KmlPolygon AdjNavPoly = new KmlPolygon("AdjNavPoly");
                KmlPolygon UnAdjNavPoly = new KmlPolygon("UnAdjNavPoly");
                List<Coordinates> AdjNavPointList = new List<Coordinates>();
                List<Coordinates> UnAdjNavPointList = new List<Coordinates>();

                AdjBoundPoly.AltMode = AltitudeMode.clampToGround;
                UnAdjBoundPoly.AltMode = AltitudeMode.clampToGround;
                AdjNavPoly.AltMode = AltitudeMode.clampToGround;
                UnAdjNavPoly.AltMode = AltitudeMode.clampToGround;

                AdjBoundPoly.IsPath = false;
                UnAdjBoundPoly.IsPath = false;
                AdjNavPoly.IsPath = true;
                UnAdjNavPoly.IsPath = true;

                #endregion

                #region Add Placemarks

                TtMetaData md = null;

                if (points.Count > 0)
                {
                    md = DAL.GetMetaDataByCN(Values.EmptyGuid);

                    if (md == null)
                        md = DAL.GetMetaData()[0];

                    if (md == null)
                        throw new Exception("Meta Data is null. Cant obtain UTM Zone");
                }

                foreach (TtPoint point in points)
                {
                    double lat, lon;
                    TtUtils.UTMtoLatLon(point.AdjX, point.AdjY, md.Zone, out lat, out lon);
                    Coordinates adjCoord = new Coordinates(lat, lon, point.AdjZ);

                    TtUtils.UTMtoLatLon(point.UnAdjX, point.UnAdjY, md.Zone, out lat, out lon);
                    Coordinates unAdjCoord = new Coordinates(lat, lon, point.UnAdjZ);

                    string snippit = "Point Operation: " + point.op.ToString();

                    #region Create Placemarks for Bound/Nav

                    KmlPlacemark adjPm = new KmlPlacemark(point.PID.ToString(),
                        String.Format("Point Operation: {0}<br><div>\t     Adjusted<br>UtmX: {1}<br>UtmY: {2}</div><br>{3}",
                            point.op.ToString(), point.AdjX, point.AdjY, point.Comment));
                    adjPm.View = new KmlView();
                    adjPm.View.TimeStamp = point.Time;
                    adjPm.View.Coordinates = adjCoord;
                    adjPm.View.Tilt = 15;
                    adjPm.View.AltMode = AltitudeMode.clampToGround;
                    adjPm.View.Range = 150;

                    adjPm.Properties = new KmlProperties();
                    adjPm.Properties.Snippit = snippit;

                    adjPm.StyleUrl = sAdjBoundMap.StyleUrl;
                    adjPm.Open = false;
                    adjPm.Visibility = false;
                    adjPm.AddPoint(new KmlPoint(adjCoord, AltitudeMode.clampToGround));

                    KmlPlacemark unAdjPm = new KmlPlacemark(point.PID.ToString(),
                        String.Format("Point Operation: {0}<br><div>     Unadjusted<br>UtmX: {1}<br>UtmY: {2}</div><br>{3}",
                            point.op.ToString(), point.UnAdjX, point.UnAdjY, point.Comment));
                    unAdjPm.View = new KmlView();
                    unAdjPm.View.TimeStamp = point.Time;
                    unAdjPm.View.Coordinates = adjCoord;
                    unAdjPm.View.Tilt = 15;
                    unAdjPm.View.AltMode = AltitudeMode.clampToGround;
                    unAdjPm.View.Range = 150;

                    unAdjPm.Properties = new KmlProperties();
                    unAdjPm.Properties.Snippit = snippit;

                    unAdjPm.StyleUrl = sUnAdjBoundMap.StyleUrl;
                    unAdjPm.Open = false;
                    unAdjPm.Visibility = false;
                    unAdjPm.AddPoint(new KmlPoint(adjCoord, AltitudeMode.clampToGround));

                    #endregion

                    #region Add points and placemarks to lists
                    if (point.IsBndPoint())
                    {
                        AdjBoundPointList.Add(adjCoord);
                        UnAdjBoundPointList.Add(unAdjCoord);
                        fAdjBoundPoints.AddPlacemark(new KmlPlacemark(adjPm));
                        fUnAdjBoundPoints.AddPlacemark(new KmlPlacemark(unAdjPm));
                    }

                    unAdjPm.StyleUrl = String.Copy(sUnAdjNavMap.StyleUrl);
                    adjPm.StyleUrl = String.Copy(sAdjNavMap.StyleUrl);
                    adjPm.Visibility = false;

                    if (point.IsNavPoint())
                    {
                        AdjNavPointList.Add(adjCoord);
                        UnAdjNavPointList.Add(unAdjCoord);
                        fAdjNavPoints.AddPlacemark(new KmlPlacemark(adjPm));
                        fUnAdjNavPoints.AddPlacemark(new KmlPlacemark(unAdjPm));
                    }
                    else if (point.op == OpType.Quondam)
                    {
                        QuondamPoint p = point as QuondamPoint;

                        if (p.IsNavPoint())
                        {
                            AdjNavPointList.Add(adjCoord);
                            UnAdjNavPointList.Add(unAdjCoord);
                            fAdjNavPoints.AddPlacemark(new KmlPlacemark(adjPm));
                            fUnAdjNavPoints.AddPlacemark(new KmlPlacemark(unAdjPm));
                        }
                    }

                    #region Create Way / Misc point placemarks

                    unAdjPm.StyleUrl = String.Copy(sWayMap.StyleUrl);

                    if (point.op == OpType.WayPoint)
                    {
                        fWayPoints.AddPlacemark(new KmlPlacemark(unAdjPm));
                    }

                    #endregion
                    #endregion
                }

                #region Create Poly Placemarks

                //assign points to polys

                if(AdjBoundPointList.Count > 0)
                    AdjBoundPointList.Add(AdjBoundPointList[0]);
                if(UnAdjBoundPointList.Count > 0)
                    UnAdjBoundPointList.Add(UnAdjBoundPointList[0]);

                AdjBoundPoly.OuterBoundary = AdjBoundPointList;
                UnAdjBoundPoly.OuterBoundary = UnAdjBoundPointList;
                AdjNavPoly.OuterBoundary = AdjNavPointList;
                UnAdjNavPoly.OuterBoundary = UnAdjNavPointList;

                //get default data for the placemarks
                KmlView.KmlTimeSpan ts = null;
                if(points.Count > 0 )
                    new KmlView.KmlTimeSpan(points[0].Time, points[points.Count - 1].Time);
                KmlPolygon.Dimensions adjDim = AdjBoundPoly.GetOuterDimensions();
                KmlPolygon.Dimensions unAdjDim = UnAdjBoundPoly.GetOuterDimensions();

                double? adjRange = null;
                double? unAdjRange = null;
                double? width;

                if (adjDim != null)
                {
                    adjRange = TtUtils.DistanceLatLon(0, adjDim.North, 0, adjDim.South);
                    width = TtUtils.DistanceLatLon(adjDim.East, 0, adjDim.West, 0);

                    if (width != null && width > adjRange)
                        adjRange = width;
                }

                if (unAdjDim != null)
                {
                    unAdjRange = TtUtils.DistanceLatLon(0, unAdjDim.North, 0, unAdjDim.South);
                    width = TtUtils.DistanceLatLon(unAdjDim.East, 0, unAdjDim.West, 0);

                    if (width != null && width > unAdjRange)
                        unAdjRange = width;
                }

                if (adjRange == null)
                    adjRange = 1000;
                else
                    adjRange *= 1.1;
                if (unAdjRange == null)
                    unAdjRange = 1000;
                else
                    unAdjRange *= 1.1;

                //AdjBoundPlacemark
                KmlPlacemark AdjBoundPlacemark = new KmlPlacemark("AdjBoundPoly", "Adjusted Boundary Polygon", new KmlView());
                AdjBoundPlacemark.View.AltMode = AltitudeMode.clampToGround;
                AdjBoundPlacemark.View.Coordinates = AdjBoundPoly.AveragedCoords;
                if (points.Count > 1)
                    AdjBoundPlacemark.View.TimeSpan = ts;
                AdjBoundPlacemark.View.Range = (double)adjRange;
                AdjBoundPlacemark.View.Tilt = 5;

                AdjBoundPlacemark.Properties = new KmlProperties();
                AdjBoundPlacemark.Properties.Snippit = poly.Description;

                AdjBoundPlacemark.Open = false;
                AdjBoundPlacemark.Visibility = true;
                AdjBoundPlacemark.AddPolygon(AdjBoundPoly);
                AdjBoundPlacemark.StyleUrl = sAdjBoundMap.StyleUrl;

                //UnAdjBoundPlacemark
                KmlPlacemark UnAdjBoundPlacemark = new KmlPlacemark("UnAdjBoundPoly", "UnAdjusted Boundary Polygon", new KmlView());
                UnAdjBoundPlacemark.View.AltMode = AltitudeMode.clampToGround;
                UnAdjBoundPlacemark.View.Coordinates = UnAdjBoundPoly.AveragedCoords;
                if (points.Count > 1)
                    UnAdjBoundPlacemark.View.TimeSpan = ts;
                UnAdjBoundPlacemark.View.Range = (double)unAdjRange;
                UnAdjBoundPlacemark.View.Tilt = 5;

                UnAdjBoundPlacemark.Properties = new KmlProperties();
                UnAdjBoundPlacemark.Properties.Snippit = poly.Description;

                UnAdjBoundPlacemark.Open = false;
                UnAdjBoundPlacemark.Visibility = false;
                UnAdjBoundPlacemark.AddPolygon(UnAdjBoundPoly);
                UnAdjBoundPlacemark.StyleUrl = sUnAdjBoundMap.StyleUrl;

                //AdjNavPlacemark
                KmlPlacemark AdjNavPlacemark = new KmlPlacemark("AdjNavPoly", "Adjusted Navigation Path", new KmlView());
                AdjNavPlacemark.View.AltMode = AltitudeMode.clampToGround;
                AdjNavPlacemark.View.Coordinates = AdjNavPoly.AveragedCoords;
                if (points.Count > 1)
                    AdjNavPlacemark.View.TimeSpan = ts;
                AdjNavPlacemark.View.Range = (double)adjRange;
                AdjNavPlacemark.View.Tilt = 5;

                AdjNavPlacemark.Properties = new KmlProperties();
                AdjNavPlacemark.Properties.Snippit = poly.Description;

                AdjNavPlacemark.Open = false;
                AdjNavPlacemark.Visibility = false;
                AdjNavPlacemark.AddPolygon(AdjNavPoly);
                AdjNavPlacemark.StyleUrl = sAdjNavMap.StyleUrl;

                //UnAdjNavPlacemark
                KmlPlacemark UnAdjNavPlacemark = new KmlPlacemark("UnAdjNavPoly", "UnAdjusted Navigation Path", new KmlView());
                UnAdjNavPlacemark.View.AltMode = AltitudeMode.clampToGround;
                UnAdjNavPlacemark.View.Coordinates = UnAdjNavPoly.AveragedCoords;
                if (points.Count > 1)
                    UnAdjNavPlacemark.View.TimeSpan = ts;
                UnAdjNavPlacemark.View.Range = (double)unAdjRange;
                UnAdjNavPlacemark.View.Tilt = 5;

                UnAdjNavPlacemark.Properties = new KmlProperties();
                UnAdjNavPlacemark.Properties.Snippit = poly.Description;

                UnAdjNavPlacemark.Open = false;
                UnAdjNavPlacemark.Visibility = false;
                UnAdjNavPlacemark.AddPolygon(UnAdjNavPoly);
                UnAdjNavPlacemark.StyleUrl = sUnAdjNavMap.StyleUrl;

                //add placemarks
                fAdjBound.AddPlacemark(AdjBoundPlacemark);
                fUnAdjBound.AddPlacemark(UnAdjBoundPlacemark);
                fAdjNav.AddPlacemark(AdjNavPlacemark);
                fUnAdjNav.AddPlacemark(UnAdjNavPlacemark);

                #endregion

                #endregion

                #region Add Folders To eachother
                //added point folders to bound/nav/misc folders
                fAdjBound.AddFolder(fAdjBoundPoints);
                fUnAdjBound.AddFolder(fUnAdjBoundPoints);
                fAdjNav.AddFolder(fAdjNavPoints);
                fUnAdjNav.AddFolder(fUnAdjNavPoints);
                fMiscPoints.AddFolder(fAdjMiscPoints);
                fMiscPoints.AddFolder(fUnAdjMiscPoints);

                //add bound/nav/misc/way folders to root polygon folder
                folder.AddFolder(fAdjBound);
                folder.AddFolder(fUnAdjBound);
                folder.AddFolder(fAdjNav);
                folder.AddFolder(fUnAdjNav);
                folder.AddFolder(fMiscPoints);
                folder.AddFolder(fWayPoints);

                //add polygon root to KmlDoc
                doc.AddFolder(folder);
                #endregion
            }

            #endregion

            return doc;
        }
 public void AddStyle(KmlStyle s)
 {
     if (s != null)
         _Styles.Add(s);
 }
        public void WriteKmlStyle(KmlStyle style)
        {
            try
            {
                //start style
                WriteStartElement("Style");
                WriteAttributeString("id", style.ID);

                #region Icon Style
                if (style.IconUrl != null || style.IconColor != null || style.IconColorMode != null || style.IconScale != null ||
                    style.IconHeading != null || style.IconHotSpot != null)
                {
                    WriteStartElement("IconStyle");
                    if (style.IconID != null)
                        WriteAttributeString("id", style.IconID);

                    if (style.IconColor != null)
                        WriteElementString("color", style.IconColor.ToString());
                    if (style.IconUrl != null && style.IconUrl != "")
                    {
                        WriteStartElement("Icon");
                        WriteElementString("href", style.IconUrl);
                        WriteEndElement();
                    }
                    if (style.IconScale != null)
                        WriteElementString("scale", style.IconScale.ToString());
                    if (style.IconColorMode != null)
                        WriteElementString("colorMode", ConvertColorMode(style.IconColorMode));
                    if (style.IconHeading != null)
                        WriteElementString("heading", style.IconHeading.ToString());
                    if (style.IconHotSpot != null)
                    {
                        WriteStartElement("hotSpot");
                        WriteAttributeString("x", style.IconHotSpot.Value.X.ToString());
                        WriteAttributeString("y", style.IconHotSpot.Value.Y.ToString());
                        WriteAttributeString("xunits", ConvertXYUnits(style.IconHotSpot.Value.xUnits));
                        WriteAttributeString("yunits", ConvertXYUnits(style.IconHotSpot.Value.yUnits));
                        WriteEndElement();
                    }

                    WriteEndElement();
                }
                #endregion

                #region Label Style

                if (style.LabelColor != null || style.LabelColorMode != null || style.LabelScale != null)
                {
                    WriteStartElement("LabelStyle");
                    if (style.LabelID != null)
                        WriteAttributeString("id", style.LabelID);

                    if (style.LabelColor != null)
                        WriteElementString("color", style.LabelColor.ToString());
                    if (style.LabelColorMode != null)
                        WriteElementString("colorMode", ConvertColorMode(style.LabelColorMode));
                    if (style.LabelScale != null)
                        WriteElementString("scale", style.LabelScale.ToString());

                    WriteEndElement();
                }
                #endregion

                #region Line Style

                if (style.LineColor != null || style.LineColorMode != null || style.LineLabelVisibility != null || style.LineOuterColor != null ||
                    style.LineOuterWidth != null || style.LinePhysicalWidth != null || style.LineWidth != null)
                {
                    WriteStartElement("LineStyle");
                    if (style.LineID != null)
                        WriteAttributeString("id", style.LineID);

                    if (style.LineColor != null)
                        WriteElementString("color", style.LineColor.ToString());
                    if (style.LineColorMode != null)
                        WriteElementString("colorMode", ConvertColorMode(style.LineColorMode));
                    if (style.LineWidth != null)
                        WriteElementString("width", style.LineWidth.ToString());

                    if (style.LineOuterColor != null)
                        WriteElementString("gx:outerColor", style.LineOuterColor.ToString());
                    if (style.LineOuterWidth != null)
                        WriteElementString("gx:outerWidth", style.LineOuterWidth.ToString());
                    if (style.LinePhysicalWidth != null)
                        WriteElementString("gx:physicalWidth", style.LinePhysicalWidth.ToString());
                    if (style.LineLabelVisibility != null)
                        WriteElementString("gx:labelVisibility", ConvertBool((bool)style.LineLabelVisibility).ToString());

                    WriteEndElement();
                }

                #endregion

                #region Poly Style

                if (style.PolygonColor != null || style.PolygonColorMode != null || style.PolygonFill != null || style.PolygonOutline != null)
                {
                    WriteStartElement("PolyStyle");
                    if (style.PolygonID != null)
                        WriteAttributeString("id", style.PolygonID);

                    if (style.PolygonColor != null)
                        WriteElementString("color", style.PolygonColor.ToString());
                    if (style.PolygonColorMode != null)
                        WriteElementString("colorMode", ConvertColorMode(style.PolygonColorMode));
                    if (style.PolygonFill != null)
                        WriteElementString("fill", ConvertBool((bool)style.PolygonFill).ToString());
                    if (style.PolygonOutline != null)
                        WriteElementString("outline", ConvertBool((bool)style.PolygonOutline).ToString());

                    WriteEndElement();
                }
                #endregion

                #region Balloon Style

                if (style.BalloonBgColor != null || style.BalloonTextColor != null || style.BalloonText != null || style.BalloonDisplayMode != null)
                {
                    WriteStartElement("BalloonStyle");
                    if (style.BalloonID != null)
                        WriteAttributeString("id", style.BalloonID);

                    if (style.BalloonBgColor != null)
                        WriteElementString("bgColor", style.BalloonBgColor.ToString());
                    if (style.BalloonTextColor != null)
                        WriteElementString("textColor", style.BalloonTextColor.ToString());
                    if (style.BalloonText != null && style.BalloonText != "")
                        WriteElementString("text", style.BalloonText);
                    if (style.BalloonDisplayMode != null)
                        WriteElementString("displayMode", ConvertDisplayMode(style.BalloonDisplayMode));

                    WriteEndElement();
                }
                #endregion

                #region List Style

                if (style.ListBgColor != null || style.ListItemIconUrl != null || style.ListItemState != null || style.ListListItemType != null)
                {
                    WriteStartElement("listItemType");
                    if (style.ListID != null)
                        WriteAttributeString("id", style.ListID);

                    if (style.ListListItemType != null)
                        WriteElementString("listItemType", ConvertListItemType(style.ListListItemType));
                    if (style.ListBgColor != null)
                        WriteElementString("bgColor", style.ListBgColor.ToString());

                    if (style.ListItemState != null || style.ListItemIconUrl != null)
                    {
                        WriteStartElement("ItemIcon");

                        if (style.ListItemState != null)
                            WriteElementString("state", ConvertState(style.ListItemState));
                        if (style.ListItemIconUrl != null && style.ListItemIconUrl != "")
                            WriteElementString("href", style.ListItemIconUrl);

                        WriteEndElement();
                    }
                    WriteEndElement();
                }
                #endregion

                //end style
                WriteEndElement();
            }
            catch (Exception ex)
            {
                throw new Exception("WriteKmlStyle Error: " + ex.Message);
            }
        }
        private void initK(KmlStyle s, string id)
        {
            if (id != null)
                _id = id;
            else
                _id = Guid.NewGuid().ToString();

            PolygonID = s.PolygonID;
            PolygonColorMode = s.PolygonColorMode;
            PolygonColor = s.PolygonColor;
            PolygonFill = s.PolygonFill;
            PolygonOutline = s.PolygonOutline;

            IconID = s.IconID;
            IconUrl = s.IconUrl;
            IconColor = s.IconColor;
            IconScale = s.IconScale;
            IconHeading = s.IconHeading;
            IconHotSpot = s.IconHotSpot;

            LabelID = s.LabelID;
            LabelColor = s.LabelColor;
            LabelScale = s.LabelScale;
            LabelColorMode = s.LabelColorMode;

            LineID = s.LineID;
            LineColor = s.LineColor;
            LineWidth = s.LineWidth;
            LineColorMode = s.LineColorMode;
            LineOuterColor = s.LineOuterColor;
            LineOuterWidth = s.LineOuterWidth;
            LinePhysicalWidth = s.LinePhysicalWidth;
            LineLabelVisibility = s.LineLabelVisibility;

            BalloonID = s.BalloonID;
            BalloonBgColor = s.BalloonBgColor;
            BalloonTextColor = s.BalloonTextColor;
            BalloonText = s.BalloonText;
            BalloonDisplayMode = s.BalloonDisplayMode;

            ListID = s.ListID;
            ListListItemType = s.ListListItemType;
            ListBgColor = s.ListBgColor;
            ListItemState = s.ListItemState;
            ListItemIconUrl = s.ListItemIconUrl;
        }
 public KmlStyle(KmlStyle s)
 {
     initK(s, null);
 }
 public KmlStyle(KmlStyle s, string id)
 {
     initK(s, id);
 }