Let's assume that you want to export multiple layers in QGIS as a single
kmz file. The first step is to read the current project layers:
# Reading layers layers = QgsProject.instance().mapLayers().values()
At this point, you can iterate through the layers and export those that are selected (check-marked):
# Printing visible layer's name for layer in layers: name = layer.name() if QgsProject.instance().layerTreeRoot().findLayer(layer).isVisible(): print(output_layer)
Now, you can transform each layer into a
from qgis.core import QgsVectorFileWriter, QgsVectorLayer from PyQt5.QtCore import QSettings from qgis.core import * from qgis.utils import qgsfunction # Reading layers layers = QgsProject.instance().mapLayers().values() # Transforming selected layers into KML's for layer in layers: name = layer.name() if QgsProject.instance().layerTreeRoot().findLayer(layer).isVisible(): output_layer = r"D:/Desktop/" + name + ".kml" QgsVectorFileWriter.writeAsVectorFormat(layer, output_layer, "utf-8", layer.crs(), "KML", symbologyExport=QgsVectorFileWriter.FeatureSymbology)
If you run the script, you'd generate a bunch of
.kml files. But it would be nice to combine the files into a single
.kmz. You can do it manually by opening the whole group of files with Google Earth and exporting it as a single
.kmz. However, it would be better to do it using the same script. I don't know how to do it yet. 😅
If you have any ideas on how to compress the files into a single
.kmz using Python, it would be really helpful.
Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.