1
Vote

KmzFile ignores changes to KmlFile

description

When changing the elements of a KML file, the KmzFile ignores them. This is due to the fact that the KmlFile is saved to the zip entry when the KmzFile is created. The KmzFile should cache the KmlFile and then save it when the user calls Save.

Use case:
Need to add files to the KmzFile and also add the files as placemarks to the KmlFile

Example:
var folder = new Folder();
// 
// Add placemarks to folder
// 

var doc = new Document { Description = new Description { Text = "doc" } };
doc.AddFeature(folder);
var kml = new Kml { Feature = doc };

var kmlFile = KmlFile.Create(kml, false);
var kmzFile = KmzFile.Create(kmlFile);

// Loop through the attachments to add to the kmz and add as placemarks
foreach (var attachment in GetAttachments())
{
    string attachmentName = Path.GetFileName(attachment.Path);
    string path = Path.Combine("files", attachmentName);
    using (var attachmentStream = File.OpenRead(attachment.Path))
    {
        kmzFile.AddFile(path, attachmentStream);
    }

    // add the attachment as a placemark to the kml
    string description = string.Format("<![CDATA[<img style=\"max-width:500px;\" src=\"{0}\">]]>", path);
    folder.AddFeature(new Placemark
    {
        Name = attachmentName,
        Description = new Description { Text = description },
        Geometry = geometry
    });
}
string directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"\Export\");
using (var kmlStream = new FileStream(Path.Combine(directory, GetFileName() + ".kml"), FileMode.Create))
{
    // This DOES save the new placemarks
    kmlFile.Save(kmlStream);
}

// saves the file attachments but does NOT save the new kml placemarks
kmzFile.Save(stream);
Current work around is to loop through the attachments twice.

comments

skendrot wrote Jul 20 at 7:48 PM

Consider the use case of adding Photo Overlays as well.