6

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/

4

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

1

This is an interesting question. My research on the subject led to using the OGR_STYLE keyword combined with SQL but something must be wrong it doesn't work.

The trick is to try and get the XML style info and put it into a new field in the SHP file you're writing.

ogr2ogr -f "ESRI Shapefile" -SQL "select *, CAST (OGR_STYLE AS Character (255)) AS style from sierraLeone.kml" "sierraLeone.shp" "sierraLeone.kml"

This answer is not complete but I haven't found anything further for the moment and my attempts are not successful. Some people seem to achieve the same type of manipulation with Mapinfo, see an example below (source):

"2.9 Using OGR SQL to transfer the style between the data sources

We can use the OGR_STYLE special field to extract the feature level style, and ogr2ogr can be used to transfer the style string between the data sources according to the following example:

ogr2ogr -f "ESRI Shapefile" -sql "select *, OGR_STYLE from rivers"
rivers.shp rivers.tab

Without specifying the length of the style field the output driver may truncate the length to a default value. Therefore it may be necessary to specify the target length manually, like:

ogr2ogr -f "ESRI Shapefile" -sql "select *, CAST(OGR_STYLE AS
character(255)) from rivers" rivers.shp rivers.tab

OGR is aware of using the OGR_STYLE field if exists and OGRFeature::GetStyleString will return the value of this field if no style string have been specified programmatically."

See also a similar question to yours discussed here :

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.