Hello, I'm new with the ArcGIS SDK and try to find a way how to get the Geometry from a KmlNode, respectively from a kml file.
The general idea is to show to whole KmlNode structure from the kml file in a XAML TreeView and when you click on an item it should extract the Geometry.
Uri kmlUri = new Uri(path);
KmlDataset dataset = new KmlDataset(kmlUri);
KmlLayer layer = new KmlLayer(dataset);
MyMapView.Map.OperationalLayers.Add(layer);
foreach (KmlNode kmlRootNode in dataset.RootNodes)
{
KmlPlacemark kmlPlacemark = kmlRootNode as KmlPlacemark;
Geometry geometry = kmlPlacemark.Geometry;
}
I cast the KmlNode as a KmlPlacemark correctly, but the Geometry is ALWAYS null, I don't know why. Everything else like the GrahicType, the BalloonContent or the Extent of the KmlPlacemark are correct.
Does somebody have a hint for me what I am doing wrong or how I can solve this issue?
Thanks in advance.
Hi,
Is the RootNode definitely of type KmlPlacemark? I recommend conditionally checking for the type of each node, noting that some may be containers (e.g. KmlFolder) containing collections of nodes, or other containers, etc.
Also, although you add the layer to the view, it may still be loading when the next line tries to iterate the rootnode. You should await the KmlDataset LoadAsync e.g. `await dataset.LoadAsync();`. Alternatively you can add an event handler for the LoadStatusChanged event.
Hi,
thanks for your reply. For the sake of simplicity I only copy & pasted the important parts of the code.
The more detailed code would be:
I first load the kml file with an open file dialog. And when the loading succeeds, I create the dataset and the layer:
string path = openFileDialog.FileName;
Uri kmlUri = new Uri(path);
KmlDataset dataset = new KmlDataset(kmlUri);
KmlLayer layer = new KmlLayer(dataset);
MyMapView.Map.OperationalLayers.Add(layer);
await LoadKmlData(dataset);
The method LoadKmlData would add all the KmlNodes to the WPF/XAML TreeView:
await dataset.LoadAsync();
foreach (KmlNode kmlRootNode in dataset.RootNodes)
{
TreeViewItem treeViewItem = new TreeViewItem(kmlRootNode, null);
treeViewItems.Add(treeViewItem);
TreeViewItem.AddChildrenItemsToRootItem(treeViewItem);
}
TreeView.ItemsSource = treeViewItems;
The TreeView shows all the items from my kml file correctly. When I click on a TreeView item, I would like to get the Geometry of that item by the Event handler TreeView_SelectedItemChanged:
TreeViewItem selectedItem = e.NewValue as TreeViewItem;
if(selectedItem != null)
{
KmlNode node = selectedItem.Node;
if(node.GetType() == typeof(KmlPlacemark))
{
Geometry geometry = ((KmlPlacemark)node).Geometry;
}
}
But unfortunately the geometry is null.
Try the .Geometries property instead. They are slightly different. See the doc:
Cool, that helped a lot. Thank you very much.