3

In my code below, I am trying to export certain layers as KML files. It works, but it does not maintain layer properties such as color and thickness, nor does it export the attribute table associated with the layer. Can someone help explain the other parameters that QgsVectorFileWriter.writeAsVectorFormat takes so I can make this happen. I tried to read the documentation for it, but I couldn't find anything helpful.

from qgis.core import QgsVectorFileWriter, QgsVectorLayer
from PyQt4.QtCore import QSettings

layers = iface.legendInterface().layers()


for layer in layers:
    name=layer.name()
    if "Road" in name:
    output_layer = r"C:/Users/efrank/Desktop/Dump/" +  name + ".kml"
    QgsVectorFileWriter.writeAsVectorFormat(layer, output_layer, "utf-8", layer.crs(), "KML")

print "Layer to KML Conversion Complete"

2
  • 1
    When exporting to KML from the GUI, the default is to export with "no symbology."
    – csk
    Commented Feb 25, 2019 at 22:35
  • When I used your code it worked for me. I have QGIS 3.4.4 with PyQt5 and Python 3.7. I did try to somehow add the FeatureSymbology but could not get it to work. I tried to get FeatureSymbology to work but no luck. .qgis.org/api/…
    – Cary H
    Commented Feb 25, 2019 at 23:34

2 Answers 2

3

Original poster here... Thanks for all the suggestions. Fortunately, I was able to get it to work. Below is an example of my working code:

from qgis.core import QgsVectorFileWriter, QgsVectorLayer
from PyQt4.QtCore import QSettings
from qgis.core import *
from qgis.utils import qgsfunction

layers = iface.legendInterface().layers()


for layer in layers:
    name=layer.name()
    if "Road" in name:
        output_layer = r"C:/Users/efrank/Desktop/Dump/" +  name + ".kml"
QgsVectorFileWriter.writeAsVectorFormat(layer, output_layer, "utf-8", layer.crs(), "KML",symbologyExport=QgsVectorFileWriter.FeatureSymbology)
1

I'm sure there are ways to do this directly, but I've had the best experience importing and exporting vectors layers indirectly, QGIS <> GPKG <> KML. You have more flexible control of the conversion options to/from KML that way than QGIS <> KML using the standard input drivers. (This is certainly true with GUI import/export, not sure about what you can control direct with PyQGIS).

Basically, create a test GPKG from your layers and then use ogr2ogr, playing around with the options (in particular look at -oo, -doo, -lcso, -dsco), using https://www.gdal.org/drv_libkml.html as reference. Make sure to use the LIBKML driver not the KML driver. Once you have found what you need using the ogr2ogr command line utility, implement it in Python using gdal.vectorTranslate since you can specify the same options in its arguments.

For an example going the other way (KML to GPKG to displayed map layers), see my question QGIS/QDAL: Accessing OGR_STYLE across all layers in a KML file.

What you can hope to pass fully automatically as styling is limited, and you'll likely do best to pick out and pass what you really care about via an explicit OGR_STYLE attribute in your layer, either specified directly using the specification at https://www.gdal.org/ogr_feature_style.html (I've never done this), or using an @namedstyle that you then (re)define manually as a styleURL (with a # not a @) in the KML file using KML's more restricted styling options.

Updating to add: You should be able to actually write KML directly (without the GPKG intermediary) using VectorTranslate, passing your layer's data source as a parameter I think. It's still klugey, but less so, and retains the flexibility to control the LIBKML driver fully.

You could also just try creating a OGR_STYLE attribute in the layer (or a throwaway memory copy) and then just saving the layer as KML normally (without the overhead of VectorTranslate). I'm not sure if the appropriate ogr options are turned on by default to convert that specially named attribute into layer- and/or feature-specific styling in the KML file, however, especially given what you've written about trouble passing the whole attribute table (I'm not familiar with that, sorry).

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.