3

I have a bulk of data, which I would like to export as the KML file by using the MMQGIS plugin.

enter image description here

Everything is fine with the plugin, but I am wondering about auto-change the output file name, which could be based on the input layer name. Is it possible at all?

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
Geographos
  • 4,087
  • 2
  • 27
  • 88
  • I bet it's to export Eclipse data ! Just by curiosity, why export KML with MMQGIS and not with the "Save as ..." QGIS native function ? Is it for the HTML description ? – J. Monticolo Sep 01 '21 at 09:22
  • I have to use the MMQGIS because of the fill color. The standard KML export in QGIS results in the empty polygon only without fill. Yes, you were right, it's for eclipse data. However, I am looking forward to the solution also towards another, eclipse-free situation. – Geographos Sep 01 '21 at 09:31

2 Answers2

6

You can achieve this by changing the source code of the plugin. Otherwise, the plugin uses the name temp for each output file.

Open mmqgis_dialogs.py file in MMQGIS Plugin folder (Settings > User Profiles > Open Active Profile Folder, navigate to python/plugins/mmqgis)

Change mmqgis_temp_file_name method (line ~260) in mmqgis_dialog class as follows:

def mmqgis_temp_file_name(self, suffix, name=None):
    project = QgsProject.instance()
home_path = project.homePath()
if not home_path:
    home_path = os.getcwd()

for x in range(1, 10):
    if name:
        name = home_path + "/" + name + str(x) + suffix
    else:
        name = home_path + "/temp" + str(x) + suffix
    if not os.path.isfile(name):
        return name

return home_path + "/temp" + suffix

Find and change the following line (between approx. line 2170-2175):

self.output_file_name.setFilePath(self.mmqgis_temp_file_name(".kml"))

to

self.output_file_name.setFilePath(self.mmqgis_temp_file_name(".kml", self.input_layer_name.currentText()))

And add the following line (same as above) to changelayers method (approx. line 2175-2180):

self.output_file_name.setFilePath(self.mmqgis_temp_file_name(".kml", self.input_layer_name.currentText()))

enter image description here

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
5

Under QGIS, once you installed MMQGIS plugin, you can open a Python console editor and adapt the code below :

from mmqgis.mmqgis_dialogs import mmqgis_kml_export_dialog

you can adapt the code by iterate over all the project layer

for e.g. to retrieve the active layer name

layer_name = iface.activeLayer().name()

layer_name = "16h37m20"

create a mmqgis kml export dialog class instance

kml_export = mmqgis_kml_export_dialog(iface)

search layer name in the dialog combobox

layer_index = kml_export.input_layer_name.findText(layer_name)

if layer name is not found in the dialog combobox

if layer_index == -1: raise ValueError("Layer not found") else: kml_export.input_layer_name.setCurrentIndex(layer_index) # set the output kml file path kml_export.output_file_name.setFilePath(fr"C:/folder1/folder2/{layer_name}.kml") kml_export.run()

J. Monticolo
  • 15,695
  • 1
  • 29
  • 64
  • It throws error. Anyway, I am thinking also about combination it with rename an active layer. I have code like this: for layer in iface.layerTreeView().selectedLayers(): layer.setName('16h45m00s') then we could do sth like this: layer_name = layer.setName – Geographos Sep 01 '21 at 10:40
  • it would be more something like layer_name = layer.name(). And the error, is it the Layer not found error ? – J. Monticolo Sep 01 '21 at 11:10
  • Traceback (most recent call last): File "C:\PROGRA~1\QGIS3~1.12\apps\Python37\lib\code.py", line 90, in runcode exec(code, self.locals) File "", line 1, in File "", line 18 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape – Geographos Sep 01 '21 at 11:20
  • I added a r in front of path to consider the path string as raw, I think the error will disappear. – J. Monticolo Sep 01 '21 at 12:26
  • 1
    Now is fine. I have combined it also with the code presented here: https://gis.stackexchange.com/questions/140749/rename-layers-with-pyqgis-script and works really smoothly :) – Geographos Sep 01 '21 at 13:08