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?

2
  • 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 ? Commented Sep 1, 2021 at 9: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
    Commented Sep 1, 2021 at 9:31

2 Answers 2

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

6
  • Something is missing, still the same
    – Geographos
    Commented Sep 1, 2021 at 11:23
  • I tried tyour method twice, but after reopening QGIS says that he couldn't load MMQGIS plugin due to an error when calling its classFactory()
    – Geographos
    Commented Sep 1, 2021 at 13:14
  • Is the plugin updated. I downloaded it today, made changes in the latest version and it worked. Commented Sep 1, 2021 at 13:23
  • @MKR what is the error message? – Commented Sep 1, 2021 at 13:31
  • I try in Windows10 and Ubuntu, it works. Commented Sep 1, 2021 at 13:32
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()
5
  • 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
    Commented Sep 1, 2021 at 10:40
  • it would be more something like layer_name = layer.name(). And the error, is it the Layer not found error ? Commented Sep 1, 2021 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 "<input>", line 1, in <module> File "<string>", line 18 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
    – Geographos
    Commented Sep 1, 2021 at 11:20
  • I added a r in front of path to consider the path string as raw, I think the error will disappear. Commented Sep 1, 2021 at 12:26
  • 1
    Now is fine. I have combined it also with the code presented here: gis.stackexchange.com/questions/140749/… and works really smoothly :)
    – Geographos
    Commented Sep 1, 2021 at 13:08

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.