public string WriteKmzFile(KmlDocument doc)
        {
            if (doc != null)
            {
                string file = String.Format("{0}\\{1}.kml", SelectedPath, Values.Settings.ProjectOptions.ProjectName);

                if (File.Exists(file))
                    File.Delete(file);

                try
                {
                    using (KmlWriter kw = new KmlWriter(file, doc))
                    {
                        kw.WriteStartDocument();
                        kw.WriteStartKml();
                        kw.WriteKmlDocument();
                        kw.WriteEndKml();
                        kw.WriteEndDocument();
                    }

                    string zfile = file.Remove(file.Length - 3, 3) + "kmz";

                    using (ZipFile zip = new ZipFile())
                    {
                        if (File.Exists(file))
                            zip.AddFile(file);

                        //save zip
                        zip.Save(zfile);

                        File.Delete(file);
                    }

                    return zfile;
                }
                catch (Exception ex)
                {
                    TtUtils.WriteError(ex.Message, "DataOutput:WriteKmzFile", ex.StackTrace);
                }
            }

            return null;
        }
        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 WriteKmlDocument(KmlDocument doc)
        {
            if (_open && doc != null)
            {
                //start document
                WriteStartElement("Document");

                //kml name
                WriteStartElement("name");
                if (doc.Name != null)
                    WriteValue(doc.Name);
                else
                    WriteValue("TwoTrails");
                WriteEndElement();

                //description
                WriteKmlDescription(doc.Desctription);

                if (doc.Visibility != null)
                    WriteElementString("visibility", ConvertBool((bool)doc.Visibility).ToString());
                if (doc.Open != null)
                    WriteElementString("open", ConvertBool((bool)doc.Open).ToString());

                if (doc.Properties != null)
                    WriteKmlProperties(doc.Properties);

                foreach (KmlStyle style in doc.Styles)
                {
                    WriteKmlStyle(style);
                }
                foreach (KmlStyleMap map in doc.StyleMaps)
                {
                    WriteKmlStyleMap(map);
                }
                foreach (KmlFolder subFolder in doc.SubFolders)
                {
                    WriteKmlFolder(subFolder);
                }
                foreach (KmlPlacemark pm in doc.PlaceMarks)
                {
                    WriteKmlPlacemark(pm);
                }

                //end document
                WriteEndElement();
            }
            else if (!_open)
            {
                throw new Exception("KmlStart not written.");
            }
            else
            {
                throw new Exception("Document is null.");
            }
        }
 private void Init(string filename, KmlDocument doc)
 {
     Document = doc;
     _FileName = filename;
     Formatting = Formatting.Indented;
     Indentation = 4;
 }
 public KmlWriter(string filename, KmlDocument doc)
     : base(filename, null)
 {
     Init(filename, doc);
 }