[GIS] QGIS drops KML styling

kmlqgisstyle

Is there a plugin for supporting opening KML files with colors, as currently the KML format is supported, but the colors and styles that comes with it are dropped?

I've created a KML style file:
http://www.files.com/shared/599d872e33689/sierraLeone.kml.zip

Uploading it to KML viewer, it's displaying properly, but in QGIS it fails. The kml viewer site:
http://ivanrublev.me/kml/

Best Answer

I found another way... Hold on it's tricky ! I put here this solution with the main parts you need. It's a bit long to set up compared to your actual work but it can be used for wider sets of data.

  • As KML is an XML file, you can import it into Excel, for example, to read it in a tabular way. I tried that bluntly and got the following :

XML import

  • Let's check in Google Earth the color value. Picture below is in french but you can see the HTML color code (#something)

html color

  • As you can see, the hexadecimal code is in the table (in red), just in front of the name column. Yeah, we have the name field and the color value.

  • Get all the name and the styleUrl into another sheet.

  • Clean the data by removing duplicates.

  • You should now have only a flat table with a name and a color column.

Now you have a choice. Either you extract the RGB values in Excel or in QGIS. QGIS needs at the end values in R,G,B,A (Red Green Blue Alpha) style.


Prepair the color value in EXCEL

  • In Excel, you will first have to split up the hexa values in 3 with string operators like MID(), then convert them into decimal with HEX2DEC().

  • You will then need to reassemble in a string field to match QGIS formating needs in the data defined column for the color.

  • See here : Convert HEX to RGB in Excel

xl hexdec


Prepair the color value in QGIS

  • In QGIS, import your previous name + color table. Join this to your KML based on the name column :

join in QGIS

  • You will need to create a little function (see below) to convert hexa values to decimal, but it's almost the same logic.

  • Put the following code in the function editor, then click on load.

hex2dec

"""
Define new functions using @qgsfunction. feature and parent must always be the
last args. Use args=-1 to pass a list of values as arguments
"""

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def hex2dex(value1, feature, parent):
    """
        Converts a hex value to decimal
        hex2dec(hexa number)
    """
    return int( value1 ,16)

  • When this is done, you will be able to use it as a new function :

hex2dec expression editor


Final rendering

  • You need to get to a final color column formated this way :

Expected input: string [r,g,b,a] as int 0-255 Valid input types: string

color field

  • Let's have a try. You need to put the fill color as a data defined field pointing to your formatted color field :

data defined

  • And...Tadaaam !

KML and color and QGIS

  • Comparison with the source file in Google Earth :

source kml