Select to view content in your preferred language

Load, edit and save kmz file

384
0
11-15-2022 03:21 AM
MarkvanWinkelen
New Contributor

Hello all,

I've been working on an WinUI app in which I can load and display a kmz file as an operational layer on a mapview. But, I want also to edit and save the kmz file with the added information.

To load the information, I use the following:

kmlLayer = new KmlLayer(new Uri(@"C:\Users\markv\Downloads\Trektochtlocaties.kmz"));
MapView.Map.OperationalLayers.Add(kmlLayer);

I am also able to add a placemark using:

try
                {
                    SketchCreationMode creationMode;
                    creationMode = SketchCreationMode.Point;
                    Geometry geometry = await MapView.SketchEditor.StartAsync(creationMode, true);
                    Geometry projectedGeometry = GeometryEngine.Project(geometry, SpatialReferences.Wgs84);
                    KmlGeometry kmlGeometry = new KmlGeometry(projectedGeometry, KmlAltitudeMode.ClampToGround);
                    currentPlacemark = new KmlPlacemark(kmlGeometry);
                    currentPlacemark.Style = new KmlStyle();
                    currentPlacemark.Style.IconStyle = new KmlIconStyle(new KmlIcon(new Uri("https://static.arcgis.com/images/Symbols/Shapes/BluePin1LargeB.png")), 1.0);
                    kmlDocument.ChildNodes.Add(currentPlacemark);
                }
                catch { }

and then save a new file using the following code:

try
                {
                    MapView.SketchEditor.CompleteCommand.Execute(null);
                    //Remove old file
                    try { System.IO.File.Delete(@"C:\Users\markv\Downloads\Trektochtlocaties_old.kmz"); } catch { }

                    //Rename and delete current file
                    System.IO.File.Copy(@"C:\Users\markv\Downloads\Trektochtlocaties.kmz", @"C:\Users\markv\Downloads\Trektochtlocaties_old.kmz");
                    System.IO.File.Delete(@"C:\Users\markv\Downloads\Trektochtlocaties.kmz");

                    //Save new file
                    await kmlDocument.SaveAsAsync(@"C:\Users\markv\Downloads\Trektochtlocaties.kmz");
                }
                catch { }

 So, I can load the existing file, and seperate from that add placemarks to a new kmlDocument and save that to a file. But, this causes the existing file to be overwritten with the new file, so the existing information disappears.

 

How can I combine these codes in order to load a file, edit that file and save all the information? Thanks!

0 Kudos
0 Replies