Can I import a KML file with styles to QGIS 3? (As of now version 3.4.1)

I have a .kml file with cycling routes that have a different color for the time between sunset and sunrise.

When I import the .kml to QGIS all styles are overridden.

Is there a way to display or even edit the preset KML features in QGIS?

A trivial sample file: https://gist.github.com/mkpaa/a6a6f433366019eabdc2811ecafba2af

  • KML data may added by drag & drop or via the add layer dialogue.
    – Erik
    Commented Nov 26, 2018 at 8:14
  • 1
    I can add kml layer. Problem is that the layer loses styles. The line with two colors only has only the QGIS layer's color/style.
    – mkpaa
    Commented Nov 26, 2018 at 16:14
  • So far I've found out that Gdal's drv_kml (default driver) has limited style support. However drv_libkml (alternative driver) should have support for styles. I still don't know if QGIS can actually display these styles.
    – mkpaa
    Commented Dec 3, 2018 at 22:48
  • It looks like the answer I am looking for is simply: No QGIS doesn't support KML styles. The workarounds help me in this specific case, but in practise I would need to do the work for each different file and some would have hundreds of colors to set.
    – mkpaa
    Commented Dec 4, 2018 at 21:41

3 Answers 3


Sorry but you can't do it using your raw data directly. You should add first some attribute (for instance "Name") readable by qgis. Then you can aplly a categorized style and set, if you wish, the exact same color (ee222222 and eecccccc in your data).

In case your data is not too large you can edit that attribute in google earth/properties in each segment or adding the kml to google maps and then export again to kml.

UPDATE: There is a way to add the colour attribute in order to be readed by QGIS. Just edit the .kml document with notepad and use this code:

Replace en Notepad --> 
By this:

Replace in Notepad --> 
By this:

Now you can add it to QGIS and sorry, you will not have those exact colors but if you always name those in the same way, you just need to set the style once and then save that style layer in .qml

enter image description here enter image description here enter image description here


If you are comfortable with using the command line, and your KML file has the styles referenced in individual placemarks using styleURLs, you can try an approach as follows:

  1. Instead of just importing the KML into QGIS directly, convert it to a gpkg using the command line utility (installed with GDAL/QGIS) ogr2ogr. An example you can build from is at my question here (see Approach 2 there - which doesn't work for me due to multiple folders but should work for you if you have only one folder or a small number of folders).

  2. Then add layer(s) in QGIS from the resultant gpkg instead. The approach in step 1 will now have added a field called OGR_STYLE to your layer that contains the styleURL in the KML file.

  3. Now style your layer in QGIS, using an expression depending on OGR_STYLE to change the colour (or anything else you want, e.g. dashed or dotted lines).

As some of the other options, this is not an easy point'n'click solution, but it should get you what you want.


Here are some answers for you, but their difficulty may scare you off.

Nethertheless, QGis have a dedicated style file format, named .qml

in order to make one, double-click on the layer in the legend, a box will open, go on the Style tab. Then you should be able to define the style you want by adding rules and save it for a later use.

Now, you have to apply it. In order to do so, open the python console (Ctrl+Alt+P) and find the vector layer you need, then use this function vl.loadNamedStyle(pathToQml) to apply the stylefile to it.

PS : In order to get the vector layer object you are looking for, you could use this function that makes a list of the layers you selected in the legend.

selectedLayers = iface.legendInterface().selectedLayers()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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