R – Changing KMZ Embedded Symbols

kmlrsymbology

EDIT: This question was originally posed for QGIS. However, since I found a way to solve it in R, the title and tags have been changed accordingly.

I have got a Google Earth KMZ file with points. The points are categorised using symbols, and when I import the file into QGIS, I get different colours for the different categories. My problem is that these categories are probably the most important data attribute, and I would like to work with them.

Here is a small example: A town is marked with a red square, villages with yellow triangles and beaches with a water symbol.
https://www.dropbox.com/s/whv0ydg5ilspbss/example%20points.kmz?dl=0

When I import this file into QGIS as a layer, I get a single symbol with different colours:
screenshot of layer in QGIS

In the layer properties menu, the symbology is given as "embedded symbols"
embedded symbols in layer symbology menu

The attribute table does not contain any information to this end, the "icon" column is empty.
enter image description here

Is there any way of accessing (and ultimately changing) the embedded symbols in QGIS? They must be encoded in the KMZ somewhere, but where?

I have tried re-exporting the .kml as a shapefile from QGIS, which results in the embedded symbols being lost altogether, and importing the KML into an sf object in R, with the same result. I also tried reading the KMZ into R as an XML (using xml2::read_xml) to see whether I could find out where the relevant code was, but I don't know enough about XML files to be able to do this.

Best Answer

If you unzip the example KMZ and extract the KML, you can then open it up to view in a text editor as XML. In there you'll see that each <Placemark> only contains the <name>, <LookAt> (view info), <StyleUrl> and the <Point> geometry. There is no <ExtendedData> in your placemarks, which would include attribute data (name/value pairs) that QGIS could extract for each point.

The only way the symbols/icons are encoded in there is with the pointer in each StyleUrl, which reference the shared Styles (icons, etc.) at the top of the file. Unfortunately QGIS does not extract the contents of the StyleUrl for each feature. You would have to parse each placemark to extract the contents of they StyleUrls, and use those to categorize your features.

Looks like QGIS can recognize the Style data and converts it to the "Embedded Styles" for each feature. Theoretically, someone might be able to build a QGIS script or plugin that converts the embedded style info into a new attribute column. Might already exist somewhere, but I'm not aware of any tool like that.

From the comments, sounds like you figured out how to do it using other tools. Good luck.

Related Question