5

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 Nov 26 '18 at 8:14
  • 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 Nov 26 '18 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 Dec 3 '18 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 Dec 4 '18 at 21:41
3
+25

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 --> 
<Style><LineStyle><color>eecccccc</color>
By this:
<name>GREY</name><Style><LineStyle><color>eecccccc</color>

Replace in Notepad --> 
This:
<Style><LineStyle><color>ee222222</color>
By this:
<name>BLACK</name><Style><LineStyle><color>ee222222</color>

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

2

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.

0

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()
-3

There are two possible workarounds

  1. Open your data in Google Earth and then save it as KMZ (instead of KML) then add this layer.
  2. A nice explanation is given here on importing KML to QGIS. Try to follow it.
  • You probably need to collaborate your answer, since the KML styling import into QGIS was not explained – Taras Nov 26 '18 at 7:03
  • Adding as a .kmz didn't add any styles to Qgis. Still the the Qgis's line styling. – mkpaa Nov 26 '18 at 16:02
  • @mkpaa can you please share an example kml file which can be used to reproduce your problem? – datakeen Nov 26 '18 at 19:01
  • Added a example file to the question. – mkpaa Nov 26 '18 at 21:45

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.