Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to replace a elements in a KML file? #20

Closed
vividos opened this issue Aug 1, 2019 · 2 comments
Closed

How to replace a elements in a KML file? #20

vividos opened this issue Aug 1, 2019 · 2 comments

Comments

@vividos
Copy link

vividos commented Aug 1, 2019

I'd like to use SharpKml to scan a kml file for NetworkLink elements and replace them with their (current) downloadable content. For this I check all elements (using kml.Root.Flatten() if they are NetworkLink elements, get their URL and download the kml content. I parse that text and get another KmlFile object. Then I generate a new Folder object, clone and copy all features from the loaded KmlFile's Document in that new folder and try to replace the NetworkLink with that. I couldn't figure out that last part, though. My code looks mostly like this:

Kml insertKml = LoadKmlFromText(kmlToInsert);
var doc = insertKml.Feature as Document;

var folder = new Folder
{
    Name = doc.Name
};

foreach (var featureToInsert in doc.Features)
    folder.AddFeature(featureToInsert.Clone());

networkLink.Parent.AddChild(folder);
networkLink.Parent.RemoveChild(networkLink);

How is that supposed to work? Is it possible at all? Thanks!

@samcragg
Copy link
Owner

samcragg commented Aug 2, 2019

It looks like you're on the right path. Since the parent of the NetworkLink is a Container, you might be best using the methods on that type:

var container = (Container)networkLink.Parent;
container.AddFeature(folder);
container.RemoveFeature(networkLink.Id)

Hope that helps

@vividos
Copy link
Author

vividos commented Aug 7, 2019

Thanks, that worked! I had to check if it's really a Container or the Kml element (when the NetworkLink is the only and topmost element), but that works. I also had to do a workaround when networkLink.Id is null; I generated a temporary Guid that's then used to remove the feature again.

@vividos vividos closed this as completed Aug 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants