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 AddPlacemark(KmlPlacemark p)
 {
     if (p != null && p.CN != "")
         _Placemarks.Add(p);
 }
        public KmlPlacemark(KmlPlacemark pm)
        {
            Init(pm.Name, pm.Desctription, new KmlView(pm.View));

            _CN = pm._CN;
            StyleUrl = pm.StyleUrl; ;
            Properties = new KmlProperties(pm.Properties);
            Visibility = pm.Visibility;
            Open = pm.Open;

            _Polygons = pm.Polygons.ToList();
            _Points = pm.Points.ToList();
        }
        public void WriteKmlPlacemark(KmlPlacemark pm)
        {
            if (pm != null)
            {
                WriteStartElement("Placemark");
                WriteComment("Placemark Guid: " + pm.CN);

                WriteElementString("name", pm.Name);

                WriteKmlDescription(pm.Desctription);

                if (pm.View != null && pm.View.Coordinates != null)
                    WriteKmlView(pm.View);
                else
                {
                    KmlView v = new KmlView();

                    if (pm.Polygons != null && pm.Polygons.Count > 0)
                    {
                        v.Coordinates = pm.Polygons[0].AveragedCoords;
                        v.AltMode = pm.Polygons[0].AltMode;
                    }
                    else if (pm.Points != null && pm.Points.Count > 0)
                    {
                        v.Coordinates = pm.Points[0].Coordinates;
                        v.AltMode = pm.Points[0]._AltMode;
                    }

                    WriteKmlView(v);
                }

                if(pm.StyleUrl != null && pm.StyleUrl != "")
                    WriteElementString("styleUrl", pm.StyleUrl);

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

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

                if ((pm.Points.Count + pm.Polygons.Count) > 1)
                {
                    WriteStartElement("MultiGeometry");

                    foreach (KmlPolygon poly in pm.Polygons)
                    {
                        WriteKmlPolygon(poly);
                    }
                    foreach (KmlPoint point in pm.Points)
                    {
                        WriteKmlPoint(point);
                    }

                    //end MultiGeo
                    WriteEndElement();
                }
                else
                {
                    if (pm.Polygons.Count > 0)
                    {
                        WriteKmlPolygon(pm.Polygons[0]);
                    }
                    else if (pm.Points.Count > 0)
                    {
                        WriteKmlPoint(pm.Points[0]);
                    }
                    else
                    {
                        //just point and polys for now
                    }
                }

                //end placemark
                WriteEndElement();
            }
        }