Example #1
0
    def __init__(self, iface):
        # Save reference to the QGIS interface
        self.iface = iface
        # initialize plugin directory
        self.plugin_dir = QFileInfo(QgsApplication.qgisUserDbFilePath()).path(
        ) + "/python/plugins/meshtoolsplugin"
        # initialize locale
        localePath = ""
        locale = QSettings().value("locale/userLocale").toString()[0:2]

        if QFileInfo(self.plugin_dir).exists():
            localePath = self.plugin_dir + "/i18n/meshtoolsplugin_" + locale + ".qm"

        if QFileInfo(localePath).exists():
            self.translator = QTranslator()
            self.translator.load(localePath)

            if qVersion() > '4.3.3':
                QCoreApplication.installTranslator(self.translator)

        # Create the dialog (after translation) and keep reference
        self.dlgGenerate = MeshToolsPluginDialogGenerate(self.iface)
Example #2
0
    def __init__(self, iface):
        # Save reference to the QGIS interface
        self.iface = iface
        # initialize plugin directory
        self.plugin_dir = QFileInfo(QgsApplication.qgisUserDbFilePath()).path() + "/python/plugins/meshtoolsplugin"
        # initialize locale
        localePath = ""
        locale = QSettings().value("locale/userLocale").toString()[0:2]

        if QFileInfo(self.plugin_dir).exists():
            localePath = self.plugin_dir + "/i18n/meshtoolsplugin_" + locale + ".qm"

        if QFileInfo(localePath).exists():
            self.translator = QTranslator()
            self.translator.load(localePath)

            if qVersion() > '4.3.3':
                QCoreApplication.installTranslator(self.translator)

        # Create the dialog (after translation) and keep reference
        self.dlgGenerate = MeshToolsPluginDialogGenerate(self.iface)
Example #3
0
class MeshToolsPlugin:

    def __init__(self, iface):
        # Save reference to the QGIS interface
        self.iface = iface
        # initialize plugin directory
        self.plugin_dir = QFileInfo(QgsApplication.qgisUserDbFilePath()).path() + "/python/plugins/meshtoolsplugin"
        # initialize locale
        localePath = ""
        locale = QSettings().value("locale/userLocale").toString()[0:2]

        if QFileInfo(self.plugin_dir).exists():
            localePath = self.plugin_dir + "/i18n/meshtoolsplugin_" + locale + ".qm"

        if QFileInfo(localePath).exists():
            self.translator = QTranslator()
            self.translator.load(localePath)

            if qVersion() > '4.3.3':
                QCoreApplication.installTranslator(self.translator)

        # Create the dialog (after translation) and keep reference
        self.dlgGenerate = MeshToolsPluginDialogGenerate(self.iface)
        

    def initGui(self):
        # Create actions
        self.actionGenerate = QAction(
            QIcon(":/plugins/meshtoolsplugin/icon_newmesh.svg"),
            u"Generate Mesh", self.iface.mainWindow())
        self.actionAdd = QAction(
            QIcon(":/plugins/meshtoolsplugin/icon_addmesh.svg"),
            u"Add Mesh", self.iface.mainWindow())
        self.actionSave = QAction(
            QIcon(":/plugins/meshtoolsplugin/icon_savemesh.svg"),
            u"Save Mesh", self.iface.mainWindow())

        
        # Connect actions to functions
        self.actionGenerate.triggered.connect(self.runGenerate)
        self.actionAdd.triggered.connect(self.runAdd)
        self.actionSave.triggered.connect(self.runSave)

        # Add toolbar button and menu item
        self.iface.addToolBarIcon(self.actionGenerate)
        self.iface.addToolBarIcon(self.actionAdd)
        self.iface.addToolBarIcon(self.actionSave)
        
        self.iface.addPluginToMenu(u"&Mesh Tools", self.actionGenerate)
        self.iface.addPluginToMenu(u"&Mesh Tools", self.actionAdd)
        self.iface.addPluginToMenu(u"&Mesh Tools", self.actionSave)
        

    def unload(self):
        # Remove the plugin menu item and icon
        self.iface.removePluginMenu(u"&Mesh Tools", self.actionGenerate)
        self.iface.removePluginMenu(u"&Mesh Tools", self.actionAdd)
        self.iface.removePluginMenu(u"&Mesh Tools", self.actionSave)
        
        self.iface.removeToolBarIcon(self.actionGenerate)
        self.iface.removeToolBarIcon(self.actionAdd)
        self.iface.removeToolBarIcon(self.actionSave)



    # run method that performs all the real work
    def runGenerate(self):
        # show the dialog
        self.dlgGenerate.populateLayerComboBoxes()
        self.dlgGenerate.populateAttributeComboBoxes()
        self.dlgGenerate.show()
        # Run the dialog event loop
        result = self.dlgGenerate.exec_()
        # See if OK was pressed
        if result == 1:
            # do something useful (delete the line containing pass and
            # substitute with your code)
            pass
    
    def runAdd(self):
        fileName = str(QFileDialog.getOpenFileName(self.dlgGenerate, 'Open file', 
                '', "Mesh Tools object (*.pickle);;GridBuilder slice (*.xyc);;All files (*)"))
        if fileName:
            baseName, extension = os.path.splitext(fileName)
            if extension == ".pickle":
                type = "pickle"
            elif extension == ".xyc":
                type = "gb"
            mesh = mt.readMesh(fileName,type)
            self.createMemoryMeshLayer(mesh)
    
    def runSave(self, mesh):
        layer = self.iface.activeLayer()
        if hasattr(layer, 'mesh'):
            fileName = str(QFileDialog.getSaveFileName(self.dlgGenerate, 'Save mesh file', 
                                                       "","GMS 2DM Mesh (*.2dm)"))
            if fileName:
                mt.writeMeshGMS(layer.mesh, fileName)
        else:
            QMessageBox.warning(self.dlgGenerate, 'Mesh Tools',
                                "Selected layer is not a recognised mesh layer. Please select a different layer.")

    def createShapefile(self, fname, geometryType):
        if fname != "":
            try:
                os.remove(fname)
            except OSError:
                 pass
            crs = self.iface.mapCanvas().mapRenderer().destinationCrs()
            fields = { 0 : QgsField("Length", QVariant.Double),
                      1 : QgsField("Type", QVariant.Int) }
            writer = QgsVectorFileWriter(fname, "CP1250", fields, geometryType, crs, "ESRI Shapefile")
            if writer.hasError() != QgsVectorFileWriter.NoError:
                print "Error when creating shapefile: ", writer.hasError()
            del writer
Example #4
0
class MeshToolsPlugin:
    def __init__(self, iface):
        # Save reference to the QGIS interface
        self.iface = iface
        # initialize plugin directory
        self.plugin_dir = QFileInfo(QgsApplication.qgisUserDbFilePath()).path(
        ) + "/python/plugins/meshtoolsplugin"
        # initialize locale
        localePath = ""
        locale = QSettings().value("locale/userLocale").toString()[0:2]

        if QFileInfo(self.plugin_dir).exists():
            localePath = self.plugin_dir + "/i18n/meshtoolsplugin_" + locale + ".qm"

        if QFileInfo(localePath).exists():
            self.translator = QTranslator()
            self.translator.load(localePath)

            if qVersion() > '4.3.3':
                QCoreApplication.installTranslator(self.translator)

        # Create the dialog (after translation) and keep reference
        self.dlgGenerate = MeshToolsPluginDialogGenerate(self.iface)

    def initGui(self):
        # Create actions
        self.actionGenerate = QAction(
            QIcon(":/plugins/meshtoolsplugin/icon_newmesh.svg"),
            u"Generate Mesh", self.iface.mainWindow())
        self.actionAdd = QAction(
            QIcon(":/plugins/meshtoolsplugin/icon_addmesh.svg"), u"Add Mesh",
            self.iface.mainWindow())
        self.actionSave = QAction(
            QIcon(":/plugins/meshtoolsplugin/icon_savemesh.svg"), u"Save Mesh",
            self.iface.mainWindow())

        # Connect actions to functions
        self.actionGenerate.triggered.connect(self.runGenerate)
        self.actionAdd.triggered.connect(self.runAdd)
        self.actionSave.triggered.connect(self.runSave)

        # Add toolbar button and menu item
        self.iface.addToolBarIcon(self.actionGenerate)
        self.iface.addToolBarIcon(self.actionAdd)
        self.iface.addToolBarIcon(self.actionSave)

        self.iface.addPluginToMenu(u"&Mesh Tools", self.actionGenerate)
        self.iface.addPluginToMenu(u"&Mesh Tools", self.actionAdd)
        self.iface.addPluginToMenu(u"&Mesh Tools", self.actionSave)

    def unload(self):
        # Remove the plugin menu item and icon
        self.iface.removePluginMenu(u"&Mesh Tools", self.actionGenerate)
        self.iface.removePluginMenu(u"&Mesh Tools", self.actionAdd)
        self.iface.removePluginMenu(u"&Mesh Tools", self.actionSave)

        self.iface.removeToolBarIcon(self.actionGenerate)
        self.iface.removeToolBarIcon(self.actionAdd)
        self.iface.removeToolBarIcon(self.actionSave)

    # run method that performs all the real work
    def runGenerate(self):
        # show the dialog
        self.dlgGenerate.populateLayerComboBoxes()
        self.dlgGenerate.populateAttributeComboBoxes()
        self.dlgGenerate.show()
        # Run the dialog event loop
        result = self.dlgGenerate.exec_()
        # See if OK was pressed
        if result == 1:
            # do something useful (delete the line containing pass and
            # substitute with your code)
            pass

    def runAdd(self):
        fileName = str(
            QFileDialog.getOpenFileName(
                self.dlgGenerate, 'Open file', '',
                "Mesh Tools object (*.pickle);;GridBuilder slice (*.xyc);;All files (*)"
            ))
        if fileName:
            baseName, extension = os.path.splitext(fileName)
            if extension == ".pickle":
                type = "pickle"
            elif extension == ".xyc":
                type = "gb"
            mesh = mt.readMesh(fileName, type)
            self.createMemoryMeshLayer(mesh)

    def runSave(self, mesh):
        layer = self.iface.activeLayer()
        if hasattr(layer, 'mesh'):
            fileName = str(
                QFileDialog.getSaveFileName(self.dlgGenerate, 'Save mesh file',
                                            "", "GMS 2DM Mesh (*.2dm)"))
            if fileName:
                mt.writeMeshGMS(layer.mesh, fileName)
        else:
            QMessageBox.warning(
                self.dlgGenerate, 'Mesh Tools',
                "Selected layer is not a recognised mesh layer. Please select a different layer."
            )

    def createShapefile(self, fname, geometryType):
        if fname != "":
            try:
                os.remove(fname)
            except OSError:
                pass
            crs = self.iface.mapCanvas().mapRenderer().destinationCrs()
            fields = {
                0: QgsField("Length", QVariant.Double),
                1: QgsField("Type", QVariant.Int)
            }
            writer = QgsVectorFileWriter(fname, "CP1250", fields, geometryType,
                                         crs, "ESRI Shapefile")
            if writer.hasError() != QgsVectorFileWriter.NoError:
                print "Error when creating shapefile: ", writer.hasError()
            del writer