def createDataItem(self, path, parentItem): file_info = QFileInfo(path) if file_info.suffix().lower() == 'model3': alg = QgsProcessingModelAlgorithm() if alg.fromFile(path): return ProcessingModelItem(parentItem, alg.name(), path) return None
def runAlg(file): alg = QgsProcessingModelAlgorithm() if not alg.fromFile(file): return False alg.setProvider(QgsApplication.processingRegistry().providerById('model')) dlg = AlgorithmDialog(alg) dlg.show() return True
def runAlg(file): alg = QgsProcessingModelAlgorithm() if not alg.fromFile(file): return False alg.setProvider( QgsApplication.processingRegistry().providerById('model')) dlg = AlgorithmDialog(alg) dlg.show() return True
def runAlg(file): alg = QgsProcessingModelAlgorithm() if not alg.fromFile(file): return False alg.setProvider(QgsApplication.processingRegistry().providerById('model')) dlg = AlgorithmDialog(alg, parent=iface.mainWindow()) dlg.show() # do NOT remove!!!! if you do then sip forgets the python subclass of AlgorithmDialog and you get a broken # dialog dlg.exec_() return True
def handleFileDrop(self, file): if not file.lower().endswith('.model3'): return False alg = QgsProcessingModelAlgorithm() if not alg.fromFile(file): return False alg.setProvider( QgsApplication.processingRegistry().providerById('model')) dlg = AlgorithmDialog(alg) dlg.setAttribute(Qt.WA_DeleteOnClose) dlg.show() return True
def loadFromFolder(self, folder): if not os.path.exists(folder): return for path, subdirs, files in os.walk(folder): for descriptionFile in files: if descriptionFile.endswith('model3'): fullpath = os.path.join(path, descriptionFile) alg = QgsProcessingModelAlgorithm() if alg.fromFile(fullpath): if alg.name(): alg.setSourceFilePath(fullpath) self.algs.append(alg) else: QgsMessageLog.logMessage(self.tr('Could not load model {0}', 'ModelerAlgorithmProvider').format(descriptionFile), self.tr('Processing'), Qgis.Critical)
def execute(self): settings = QgsSettings() lastDir = settings.value('Processing/lastModelsDir', QDir.homePath()) filename, selected_filter = QFileDialog.getOpenFileName( self.toolbox, self.tr('Open Model', 'AddModelFromFileAction'), lastDir, self.tr('Processing models (*.model3 *.MODEL3)', 'AddModelFromFileAction')) if filename: settings.setValue('Processing/lastModelsDir', QFileInfo(filename).absoluteDir().absolutePath()) alg = QgsProcessingModelAlgorithm() if not alg.fromFile(filename): QMessageBox.warning( self.toolbox, self.tr('Open Model', 'AddModelFromFileAction'), self.tr('The selected file does not contain a valid model', 'AddModelFromFileAction')) return if QgsApplication.instance().processingRegistry().algorithmById( 'model:{}'.format(alg.id())): QMessageBox.warning( self.toolbox, self.tr('Open Model', 'AddModelFromFileAction'), self.tr('Model with the same name already exists', 'AddModelFromFileAction')) return destFilename = os.path.join(ModelerUtils.modelsFolders()[0], os.path.basename(filename)) if os.path.exists(destFilename): reply = QMessageBox.question( self.toolbox, self.tr('Open Model', 'AddModelFromFileAction'), self.tr( 'There is already a model file with the same name. Overwrite?', 'AddModelFromFileAction'), QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.No: return shutil.copyfile(filename, destFilename) QgsApplication.processingRegistry().providerById( 'model').refreshAlgorithms()
def add_processing_models(self, provider_id): if not (provider_id == 'model' or provider_id is None): return if provider_id is not None: # If method acted as slot QgsApplication.processingRegistry().providerAdded.disconnect( self.add_processing_models) # Add etl-model basepath = os.path.dirname(os.path.abspath(__file__)) plugin_models_dir = os.path.join(basepath, "processing", "models") filenames = list() # Go for subfolders. # We store models that depend on QGIS versions in folders like "314" (for QGIS 3.14.x) # This was initially needed for the FieldMapper input, which was migrated to C++ in QGIS 3.14 qgis_major_version = str(Qgis.QGIS_VERSION_INT)[:3] qgis_major_version_path = os.path.join(plugin_models_dir, qgis_major_version) if not os.path.isdir(qgis_major_version_path): # No folder for this version (e.g., unit tests on QGIS-dev), so let's find the most recent version subfolders = [ sf.name for sf in os.scandir(plugin_models_dir) if sf.is_dir() ] if subfolders: qgis_major_version_path = os.path.join(plugin_models_dir, max(subfolders)) for filename in glob.glob( os.path.join(qgis_major_version_path, '*.model3')): filenames.append(filename) for filename in filenames: alg = QgsProcessingModelAlgorithm() if not alg.fromFile(filename): print("ERROR: Couldn't load model from {}".format(filename)) return destFilename = os.path.join(ModelerUtils.modelsFolders()[0], os.path.basename(filename)) shutil.copyfile(filename, destFilename) QgsApplication.processingRegistry().providerById( 'model').refreshAlgorithms()
def loadModel(self, filename): alg = QgsProcessingModelAlgorithm() if alg.fromFile(filename): self.model = alg self.model.setProvider(QgsApplication.processingRegistry().providerById('model')) self.textGroup.setText(alg.group()) self.textName.setText(alg.name()) self.repaintModel() self.view.centerOn(0, 0) self.hasChanged = False else: QgsMessageLog.logMessage(self.tr('Could not load model {0}').format(filename), self.tr('Processing'), Qgis.Critical) QMessageBox.critical(self, self.tr('Could not open model'), self.tr('The selected model could not be loaded.\n' 'See the log for more information.'))
def loadModel(self, filename): alg = QgsProcessingModelAlgorithm() if alg.fromFile(filename): self.model = alg self.model.setProvider(QgsApplication.processingRegistry().providerById('model')) self.textGroup.setText(alg.group()) self.textName.setText(alg.name()) self.repaintModel() self.view.centerOn(0, 0) self.hasChanged = False else: QgsMessageLog.logMessage(self.tr('Could not load model {0}').format(filename), self.tr('Processing'), Qgis.Critical) QMessageBox.critical(self, self.tr('Open Model'), self.tr('The selected model could not be loaded.\n' 'See the log for more information.'))
def execute(self): settings = QgsSettings() lastDir = settings.value('Processing/lastModelsDir', '') filename, selected_filter = QFileDialog.getOpenFileName(self.toolbox, self.tr('Open Model', 'AddModelFromFileAction'), lastDir, self.tr('Processing models (*.model3 *.MODEL3)', 'AddModelFromFileAction')) if filename: settings.setValue('Processing/lastModelsDir', QFileInfo(filename).absoluteDir().absolutePath()) alg = QgsProcessingModelAlgorithm() if not alg.fromFile(filename): QMessageBox.warning( self.toolbox, self.tr('Open Model', 'AddModelFromFileAction'), self.tr('The selected file does not contain a valid model', 'AddModelFromFileAction')) return destFilename = os.path.join(ModelerUtils.modelsFolders()[0], os.path.basename(filename)) shutil.copyfile(filename, destFilename) QgsApplication.processingRegistry().providerById('model').refreshAlgorithms()
def execute(self): settings = QgsSettings() lastDir = settings.value('Processing/lastModelsDir', '') filename, selected_filter = QFileDialog.getOpenFileName(self.toolbox, self.tr('Open Model', 'AddModelFromFileAction'), lastDir, self.tr('Processing models (*.model3 *.MODEL3)', 'AddModelFromFileAction')) if filename: settings.setValue('Processing/lastModelsDir', QFileInfo(filename).absoluteDir().absolutePath()) alg = QgsProcessingModelAlgorithm() if not alg.fromFile(filename): QMessageBox.warning( self.toolbox, self.tr('Open Model', 'AddModelFromFileAction'), self.tr('The selected file does not contain a valid model', 'AddModelFromFileAction')) return destFilename = os.path.join(ModelerUtils.modelsFolders()[0], os.path.basename(filename)) shutil.copyfile(filename, destFilename) QgsApplication.processingRegistry().providerById('model').refreshAlgorithms()
def loadFromFolder(self, folder): if not os.path.exists(folder): return for path, subdirs, files in os.walk(folder): for descriptionFile in files: if descriptionFile.endswith('model3'): try: fullpath = os.path.join(path, descriptionFile) alg = QgsProcessingModelAlgorithm() if alg.fromFile(fullpath): if alg.name(): alg.setSourceFilePath(fullpath) self.algs.append(alg) else: QgsMessageLog.logMessage(self.tr('Could not load model {0}', 'ModelerAlgorithmProvider').format(descriptionFile), self.tr('Processing'), QgsMessageLog.CRITICAL) except WrongModelException as e: QgsMessageLog.logMessage(self.tr('Could not load model {0}\n{1}', 'ModelerAlgorithmProvider').format(descriptionFile, e.msg), self.tr('Processing'), QgsMessageLog.CRITICAL)
def add_processing_models(self, provider_id): if not (provider_id == 'model' or provider_id is None): return if provider_id is not None: # If method acted as slot QgsApplication.processingRegistry().providerAdded.disconnect(self.add_processing_models) # Add ladm_col models basepath = os.path.dirname(os.path.abspath(__file__)) plugin_models_dir = os.path.join(basepath, "processing", "models") for filename in glob.glob(os.path.join(plugin_models_dir, '*.model3')): alg = QgsProcessingModelAlgorithm() if not alg.fromFile(filename): self.log.logMessage("Couldn't load model from {}".format(filename), PLUGIN_NAME, Qgis.Critical) return destFilename = os.path.join(ModelerUtils.modelsFolders()[0], os.path.basename(filename)) shutil.copyfile(filename, destFilename) QgsApplication.processingRegistry().providerById('model').refreshAlgorithms()
def openModel(self): filename, selected_filter = QFileDialog.getOpenFileName(self, self.tr('Open Model'), ModelerUtils.modelsFolders()[0], self.tr('Processing models (*.model3 *.MODEL3)')) if filename: alg = QgsProcessingModelAlgorithm() if alg.fromFile(filename): self.model = alg self.model.setProvider(QgsApplication.processingRegistry().providerById('model')) self.textGroup.setText(alg.group()) self.textName.setText(alg.name()) self.repaintModel() self.view.centerOn(0, 0) self.hasChanged = False else: QgsMessageLog.logMessage(self.tr('Could not load model {0}').format(filename), self.tr('Processing'), QgsMessageLog.CRITICAL) QMessageBox.critical(self, self.tr('Could not open model'), self.tr('The selected model could not be loaded.\n' 'See the log for more information.'))
def openModel(self): filename, selected_filter = QFileDialog.getOpenFileName(self, self.tr('Open Model'), ModelerUtils.modelsFolders()[0], self.tr('Processing models (*.model3 *.MODEL3)')) if filename: alg = QgsProcessingModelAlgorithm() if alg.fromFile(filename): self.model = alg self.model.setProvider(QgsApplication.processingRegistry().providerById('model')) self.textGroup.setText(alg.group()) self.textName.setText(alg.name()) self.repaintModel() self.view.centerOn(0, 0) self.hasChanged = False else: QgsMessageLog.logMessage(self.tr('Could not load model {0}').format(filename), self.tr('Processing'), QgsMessageLog.CRITICAL) QMessageBox.critical(self, self.tr('Could not open model'), self.tr('The selected model could not be loaded.\n' 'See the log for more information.'))
def runModel(self, modelName=None): """ Executes chosen model, if possible. :param modelPath: (str) path to the model to be registered. """ if self.modelComboBox.currentIndex() < 1: return if not modelName or isinstance(modelName, bool): modelName = self.model() modelPath = self.modelPath() else: modelPath = os.path.join(self.defaultModelPath(), modelName) alg = QgsProcessingModelAlgorithm() if not self.modelExists(modelName): # if model was manually removed and combo box was not refreshed self.iface.messageBar().pushMessage( self.tr('Failed'), self.tr("model {model} seems to have been deleted.").format(model=modelName), level=Qgis.Critical, duration=5 ) return alg.fromFile(modelPath) alg.initAlgorithm() # as this tool assumes that every parameter is pre-set, only output shall # be passed on - ALL outputs from this tool is set to memory layers. param = {vl.name() : "memory:" for vl in alg.parameterDefinitions()} msg = self.tr("Would you like to run {model}").format(model=modelName) if self.options()["checkBeforeRunModel"] and not self.confirmAction(msg): return try: out = processing.run(alg, param) self.iface.messageBar().pushMessage( self.tr('Sucess'), self.tr("model {model} finished.").format(model=modelName), level=Qgis.Info, duration=5 ) QgsMessageLog.logMessage( self.tr( "Model {model} finished running with no errors. You may" " check model output on Processing log tab." ).format(model=modelName), 'DSGTools Plugin', Qgis.Info ) if not self.options()["loadModelOutput"]: return for var, value in out.items(): if isinstance(value, QgsMapLayer): value.setName( "{model} {layername}".format(model=modelName, layername=var) ) self.addLayerToGroup( value, "DSGTools Validation Toolbar Output", modelName ) except Exception as e: msg = self.tr( "Unable to run (check Processing tab for details on model " "execution log) {model}:\n{error}" ).format(model=modelName, error=str(e)) self.iface.messageBar().pushMessage( self.tr("Model {model} failed").format(model=modelName), self.tr("check log for more information."), level=Qgis.Critical, duration=5 ) QgsMessageLog.logMessage(msg, 'DSGTools Plugin',Qgis.Info)