Search code examples
javascriptgeojsonkml

Getting colors from KML style hashes tmcw/togeojson


I'm using the tmcw/togeojson method to parse a KML file and grab the data to save to a database. The kml function seems to return a properties object with a styleUrl and a styleMapHash, using the Google StyleMap id's found in the KML file. However, it does not return the actual values for the color or the icon for markers.

For example, this is what one of my Points returns from the kml function:

{
  name: 'Idaho Springs',
  styleUrl: '#__managed_style_0AB600160E1B41A3CC31',
  styleHash: undefined,
  styleMapHash: {
    normal: '#__managed_style_1B90B87D5A1B41A3CC31',
    highlight: '#__managed_style_20709421461B41A3CC31'
  }
}

Is there a simple way to take these values and retrieve the actual color and icon information?


Solution

  • The tmcw/togeojson library attaches only the styles found in the Style section. This works for KMLs downloaded through the Google Earth desktop version, but not the Google Earth web browser version. In the web browser version, the Styles are saved into a 'gx:CascadingStyle' section, and not the required Style section that tmcw/togeojson looks to load.

    To fix this issue, I created my own XML parser and grabbed all of the information I needed, doing away with tmcw/togeojson. This also allowed me to save them in the Files that you can create in Google Earth.