Using SharpKml to create a KML file

4.9k Views Asked by At

I'm sure it's something stupid I'm doing but I can't figure it out. I'm trying to write a KML from scratch. It will be inside a KMZ with a bunch of georeferenced pictures that I won't to show up as photo overlays. I can't get it to write a KML though, I find the documentation too basic to incorporate a Folder. When I pause the program and debug, it shows Kml kml as having no children, but the feature 'folder' has 42. And then the namespaces aren't showing up, and it's not writing the Kml file. Please, any help you can give me!

public void WriteKML_sharp()
    {

        Kml kml = new Kml();

        kml.AddNamespacePrefix(KmlNamespaces.GX22Prefix, KmlNamespaces.GX22Namespace);

        Folder folder = new Folder();
        kml.Feature = folder;
        foreach (Picture picture in _process.pictures)
        {
            PhotoOverlay photooverlay = new PhotoOverlay();

            photooverlay.Name = picture.name + picture.extension;
            photooverlay.Open = true;
            photooverlay.Shape = Shape.Rectangle;

            Icon icon = new Icon();
            Uri uri = new Uri(Path.Combine("files", picture.name + picture.extension), UriKind.Relative);
            icon.Href = uri;
            photooverlay.Icon = icon;

            Point point = new Point();
            point.Coordinate = new Vector(picture.gpslat, picture.gpslong);
            //point.Coordinate = new Vector(picture.gpslat, picture.gpslong, picture.gpsalt);
            point.AltitudeMode = AltitudeMode.RelativeToGround;
            photooverlay.Location = point;

            ViewVolume viewvolume = new ViewVolume();
            viewvolume.Top = 25;
            viewvolume.Bottom = -25;
            viewvolume.Left = 20;
            viewvolume.Right = -20;
            photooverlay.View = viewvolume;

            folder.AddFeature(photooverlay);
        }

        KmlFile kmlfile = KmlFile.Create(kml, false);
        using (var stream = System.IO.File.OpenWrite(_filename))
        {
            kmlfile.Save(stream);
        }

    }
1

There are 1 best solutions below

0
On

I am not sure if this will be the reason but u can try adding a document in the KML file and assign that document as kml.Feature and Add your feature to this document using .AddFeature(folder); It works for me that way