private static void GetStyle(XElement style, KMLStyle kmlStyle)
{
XNamespace kmlNS = style.Name.Namespace;
XAttribute styleId = style.Attribute("id");
if (styleId != null)
{
kmlStyle.StyleId = styleId.Value;
}
// If style contains an BalloonStyle, then extract that information
XElement balloonStyle = style.Element(kmlNS + "BalloonStyle");
if (balloonStyle != null)
{
XElement text = balloonStyle.Element(kmlNS + "text");
if (text != null)
{
kmlStyle.BalloonText = text.Value;
}
}
// If style contains an IconStyle, then extract that information
XElement iconStyle = style.Element(kmlNS + "IconStyle");
if (iconStyle != null)
{
XElement icon = iconStyle.Element(kmlNS + "Icon");
if (icon != null)
{
XElement href = icon.Element(kmlNS + "href");
if (href != null)
{
string iconUrl = href.Value;
const string googlePal = "root://icons/palette-";
if(iconUrl.StartsWith(googlePal, StringComparison.OrdinalIgnoreCase))
{
// Replace Google earth built-in palette URL by the real URL
int x = 0;
int y = 0;
int numPalette = 0;
XElement xElement = icon.Element(kmlNS + "x");
if (xElement != null)
int.TryParse(xElement.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out x);
XElement yElement = icon.Element(kmlNS + "y");
if (yElement != null)
int.TryParse(yElement.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out y);
string pal = iconUrl.Substring(googlePal.Length, 1);
int.TryParse(pal, NumberStyles.Integer, CultureInfo.InvariantCulture, out numPalette);
if (numPalette > 0)
{
int numIcon = 8 * (7 - y/32) + x/32;
iconUrl = string.Format("http://maps.google.com/mapfiles/kml/pal{0}/icon{1}.png", numPalette, numIcon);
}
}
kmlStyle.IconHref = iconUrl;
}
}
// Extract IconColor
XElement iconColor = iconStyle.Element(kmlNS + "color");
if (iconColor != null)
{
kmlStyle.IconColor = GetColorFromHexString(iconColor.Value);
}
// If the hotspot element is present, make use of it
XElement hotspot = iconStyle.Element(kmlNS + "hotSpot");
if (hotspot != null)
{
XAttribute units;
XAttribute val;
units = hotspot.Attribute("xunits");
if (units != null)
{
try
{
kmlStyle.IconHotspotUnitsX = (HotSpotUnitType)Enum.Parse(typeof(HotSpotUnitType), units.Value, true);
val = hotspot.Attribute("x");
if (val != null)
{
double x;
if (double.TryParse(val.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out x))
kmlStyle.IconHotspotX = x;
}
}
catch { }
}
units = hotspot.Attribute("yunits");
if (units != null)
{
try
{
kmlStyle.IconHotspotUnitsY = (HotSpotUnitType)Enum.Parse(typeof(HotSpotUnitType), units.Value, true);
val = hotspot.Attribute("y");
if (val != null)
{
double y;
if (double.TryParse(val.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out y))
kmlStyle.IconHotspotY = y;
}
}
catch { }
}
}
// If the heading element is present, make use of it
XElement heading = iconStyle.Element(kmlNS + "heading");
if (heading != null)
{
double degrees;
if (double.TryParse(heading.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out degrees))
kmlStyle.IconHeading = degrees;
}
// If the scale element is present, make use of it
XElement scale = iconStyle.Element(kmlNS + "scale");
if (scale != null)
{
double scaleAmount;
if (double.TryParse(scale.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out scaleAmount))
kmlStyle.IconScale = scaleAmount;
}
}
// If style contains a LineStyle, then extract that information
XElement lineStyle = style.Element(kmlNS + "LineStyle");
if (lineStyle != null)
{
XElement color = lineStyle.Element(kmlNS + "color");
if (color != null)
{
kmlStyle.LineColor = GetColorFromHexString(color.Value);
}
XElement width = lineStyle.Element(kmlNS + "width");
if (width != null)
{
double widthVal;
if (double.TryParse(width.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out widthVal))
kmlStyle.LineWidth = widthVal;
}
}
// If style contains a PolyStyle, then extract that information
XElement polyStyle = style.Element(kmlNS + "PolyStyle");
if (polyStyle != null)
{
XElement color = polyStyle.Element(kmlNS + "color");
if (color != null)
{
kmlStyle.PolyFillColor = GetColorFromHexString(color.Value);
}
XElement fill = polyStyle.Element(kmlNS + "fill");
if (fill != null)
{
kmlStyle.PolyFill = StringToBool(fill.Value);
}
XElement outline = polyStyle.Element(kmlNS + "outline");
if (outline != null)
{
kmlStyle.PolyOutline = StringToBool(outline.Value);
}
}
}