2

We are now using QGIS 3.12. Our basic python script from earlier QGIS version to export to Mapinfo and mid/mif seems to no longer work. I understand QGIS 3+ has upgraded this functionality. and i have no idea where to start to fix this.

Can someone please advise how to run the below script, or advise how to upgrade it also? Can someone please review?

from qgis.gui import*
from qgis.core import *
import os

def run_script(iface):
    PATH = QgsProject.instance().readPath("./")
    MifPath = PATH + '/Submit/MID_MIF/'
    ShapePath = PATH + '/Submit/ESRI_SHAPE/'        

    if not os.path.exists(MifPath):
        os.mkdir(MifPath)
    if not os.path.exists(ShapePath):
        os.mkdir(ShapePath)

    for vLayer in iface.mapCanvas().layers():
        QgsVectorFileWriter.writeAsVectorFormat( vLayer, 
        MifPath + vLayer.name() + ".mif", "utf-8", 
        vLayer.crs(), 'Mapinfo File', datasourceOptions='FORMAT=MIF')

    for vLayer in iface.mapCanvas().layers():
        QgsVectorFileWriter.writeAsVectorFormat( vLayer, 
        ShapePath + vLayer.name() + ".shp", "utf-8", 
        vLayer.crs(), "ESRI Shapefile" )

    from PyQt4.QtGui import QMessageBox
    QMessageBox.information(None, "MapInfo Mif/Mid Exported to", "" + unicode(MifPath))
    QMessageBox.information(None, "ESRI Shapefile to", "" + unicode(ShapePath))
2

A Python3 and QGIS 3 version would be :

from PyQt5.QtWidgets import QMessageBox
from qgis.gui import*
from qgis.core import *
import os

def run_script():
    path = QgsProject.instance().readPath(".{0}".format(os.sep))
    path = os.getcwd()
    dir = "Submit"
    mif_path = os.sep.join([path, dir, "MID_MIF"]) + os.sep
    shape_path = os.sep.join([path, dir, "ESRI_SHAPE"]) + os.sep

    if not os.path.exists(dir):
        os.mkdir(dir)
    if not os.path.exists(mif_path):
        os.mkdir(mif_path)
    if not os.path.exists(shape_path):
        os.mkdir(shape_path)

    for vlayer in iface.mapCanvas().layers():
        file_path = "{0}{1}.mif".format(mif_path, vlayer.name())
        options = QgsVectorFileWriter.SaveVectorOptions()
        options.driverName = "Mapinfo File"
        QgsVectorFileWriter.writeAsVectorFormatV2(
            vlayer, file_path, QgsCoordinateTransformContext(), options
        )

    for vlayer in iface.mapCanvas().layers():
        file_path = "{0}{1}.shp".format(shape_path, vlayer.name())
        options = QgsVectorFileWriter.SaveVectorOptions()
        options.driverName = "ESRI Shapefile"
        QgsVectorFileWriter.writeAsVectorFormatV2(
            vlayer, file_path, QgsCoordinateTransformContext(), options
        )

    QMessageBox.information(None, "MapInfo Mif/Mid Exported to", "" + mif_path)
    QMessageBox.information(None, "ESRI Shapefile to", "" + shape_path)

The main changes are the switch between PyQt4 into PyQt5, and update to QgsVectorFileWriter.writeAsVectorFormatV2.

| improve this answer | |
  • Be careful, fstring is nice but it is still early and is not supported on all Python 3 versions. Some users might come later with this bug. – etrimaille Apr 6 at 8:31
  • Thanks for the prompt reply!!Can you please clarify your last comments RE 'Transform...'. Is this another step required to export to the formats. – TeeM Apr 6 at 8:35
  • Please excuse my naivety with this... i get the following error. 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 1, in <module> ImportError: cannot import name 'QMessageBox' from 'PyQt5.QtGui' (C:\PROGRA~1\QGIS3~1.12\apps\Python37\lib\site-packages\PyQt5\QtGui.pyd) – TeeM Apr 6 at 8:49
  • @etrimaille: switching f-string to classic format. – J. Monticolo Apr 6 at 9:06
  • @TeeM: No other steps, I clarify the last comment. I corrected also this error, QMessageBox belongs to PyQt5.QtWidgets. – J. Monticolo Apr 6 at 9:07

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.