This project has moved and is read-only. For the latest updates, please go here.

Combine series of KML files into one

Feb 9, 2014 at 10:36 AM
Edited Feb 9, 2014 at 10:36 AM
Hi all,

i have been working with KML files for a few hours now and i'm struggling to write a peice of code to combine a large number of KML files

i have extracted the data from the KML Files into a list<placemark> but i'm not entirely sure which way to go next

i'm working with polygons if that makes a difference.
    var direct = new DirectoryInfo(@"C:\Users\Jake\Downloads\KML");
            var files = direct.GetFiles(@"*.kml", SearchOption.AllDirectories);
            List<Placemark> pms = new List<Placemark>();
            foreach (var fileInfo in files)
                {
                    var t = KmlHelper.OpenFile(fileInfo.FullName);
                    var kml = t.Root as Kml;
                    if (kml != null)
                        {
                            var items = kml.Flatten()
                                           .OfType<Placemark>();
                            foreach (var placemark in items)
                                {
                                    placemark.ClearStyles();

                                    pms.Add(placemark);
                                }
                        }
                }
       //TODO: write List to single KML
i tried creating a folder but it threw an Exception of

'{"Cannot add child element to this instance because it belongs to another instance."}'


any pointers would be greatly appreciated
Feb 9, 2014 at 10:55 AM
Never mind i seem to have solved it Code here if any one needs to reference
   Document d = new Document();

            foreach (var p in pms) {
                
                Placemark pl  = new Placemark();
                pl.Name = p.Name;
                pl.Geometry = p.Geometry;
              d.AddFeature(pl);
            }

            Kml root = new Kml();
            root.Feature = d;
            KmlFile k = KmlFile.Create(root, false);
            k.Save(new FileStream(@"C:\Users\Jake\Downloads\Jake\temp.kml", FileMode.OpenOrCreate));