I am using the Drivers.Kml.OpenLayer to process features in a .KML file.
A sample placemark has attributes like this:
<Placemark>
<name>HH-01625409</name>
<styleUrl>#s47690667</styleUrl>
<Point>
<coordinates>-99.9999,99.9999,0</coordinates>
</Point>
</Placemark>
I am able to read attributes such as “name”, but I am not able to pull the “styleUrl” attribute, which I need to then relate the object to it’s styling higher in the KML in this area:
<Style id="s47690667">
<IconStyle>
<scale>0.5</scale>
<Icon>
<href>http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png</href>
</Icon>
<hotSpot x="0" y="0" xunits="pixels" yunits="pixels"/>
</IconStyle>
</Style>
I need this reference because I need to be able to extract the scale, icon URL, and other styling attributes such as line color and width (for other shapes).
How can I extract this data from the KML using your KML parsers?
This is what ChatGpt is suggesting, but I’m wondering if you have a better built in parser?
using (var layer = Drivers.Kml.OpenLayer("your.kml"))
{
foreach (var feature in layer)
{
var name = feature.GetValueOrDefault<string>("name");
var geometry = feature.Geometry;
// **Get the styleUrl manually from raw XML**
// Match using "name" or spatial location (less ideal but possible)
var placemark = xdoc.Descendants(ns + "Placemark")
.FirstOrDefault(p => (string)p.Element(ns + "name") == name);
if (placemark != null)
{
var styleUrl = placemark.Element(ns + "styleUrl")?.Value?.TrimStart('#');
if (!string.IsNullOrEmpty(styleUrl) && styles.TryGetValue(styleUrl, out XElement styleElement))
{
// Found the corresponding style!
var iconStyle = styleElement.Element(ns + "IconStyle");
if (iconStyle != null)
{
var scale = iconStyle.Element(ns + "scale")?.Value;
var href = iconStyle.Element(ns + "Icon")?.Element(ns + "href")?.Value;
Console.WriteLine($"Feature {name}:");
Console.WriteLine($" Icon scale: {scale}");
Console.WriteLine($" Icon URL: {href}");
}
}
}
}
}