当前位置: 首页>>代码示例>>Python>>正文


Python Algorithm.prepareRun方法代码示例

本文整理汇总了Python中algorithm.Algorithm.prepareRun方法的典型用法代码示例。如果您正苦于以下问题:Python Algorithm.prepareRun方法的具体用法?Python Algorithm.prepareRun怎么用?Python Algorithm.prepareRun使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在algorithm.Algorithm的用法示例。


在下文中一共展示了Algorithm.prepareRun方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: calculate

# 需要导入模块: from algorithm import Algorithm [as 别名]
# 或者: from algorithm.Algorithm import prepareRun [as 别名]
    def calculate(self):
        ''' Prepare environment to run the alg and run it. After run, merge produced 
        data basing on plugin configuration
        '''
        alg = Algorithm()
        roadLayer = self.gui.getRoadLayer()
        
        # prepare layer where to add result
        addToInputLayer = self.gui.addToOriginaLayer_RButton.isChecked()
        newOutputLayer = self.gui.outFile_LEdit.text()

        self.outLayer = roadLayer
        if not addToInputLayer:
            if not newOutputLayer:
                message = self.tr('No output vector specified')
                QgsMessageLog.logMessage(message, 'QTraffic', QgsMessageLog.CRITICAL)
                iface.messageBar().pushCritical('QTraffic', message)
                return
            
            # copy input layer to the new one
            writeError = QgsVectorFileWriter.writeAsVectorFormat(roadLayer, newOutputLayer, 'utf-8',  roadLayer.crs())
            if writeError != QgsVectorFileWriter.NoError:
                message = self.tr('Error writing vector file {}'.format(newOutputLayer))
                QgsMessageLog.logMessage(message, 'QTraffic', QgsMessageLog.CRITICAL)
                iface.messageBar().pushCritical('QTraffic', message)
                return
            
            # load the layer
            newLayerName =os.path.splitext(os.path.basename(  newOutputLayer ))[0]
            self.outLayer = QgsVectorLayer(newOutputLayer, newLayerName, 'ogr')
            if not self.outLayer.isValid():
                message = self.tr('Error loading vector file {}'.format(newOutputLayer))
                QgsMessageLog.logMessage(message, 'QTraffic', QgsMessageLog.CRITICAL)
                iface.messageBar().pushCritical('QTraffic', message)
                return
        
        # prepare environment
        try:
            alg.setProject(self.project)
            alg.setLayer( roadLayer )
            alg.init()
            alg.prepareRun()
        except:
            traceback.print_exc()
            message = self.tr('Error preparing context for the algoritm')
            QgsMessageLog.logMessage(message, 'QTraffic', QgsMessageLog.CRITICAL)
            iface.messageBar().pushCritical('QTraffic', message)
            return
        
        # run the alg
        success = alg.run()
        if not success:
            return
        
        # prepare result
        try:
            alg.addResultToLayer(self.outLayer)
        except:
            return
        
        # add or refresh rsult vector layer
        if not addToInputLayer:
            QgsMapLayerRegistry.instance().addMapLayer(self.outLayer)
        iface.mapCanvas().refresh()
开发者ID:enricofer,项目名称:QTraffic,代码行数:66,代码来源:output_tab_manager.py

示例2: OutputTabManager

# 需要导入模块: from algorithm import Algorithm [as 别名]
# 或者: from algorithm.Algorithm import prepareRun [as 别名]

#.........这里部分代码省略.........
                    self.outLayer = None
                    self.outLayerId = None

                    if os.path.exists(newOutputLayer):
                        if not QgsVectorFileWriter.deleteShapeFile(newOutputLayer):
                            message = self.tr("Error removing shape: {}".format(newOutputLayer))
                            iface.messageBar().pushMessage(message, QgsMessageBar.CRITICAL)
                            return
            
            # copy input layer to the new one
            writeError = QgsVectorFileWriter.writeAsVectorFormat(roadLayer, newOutputLayer, 'utf-8',  roadLayer.crs())
            if writeError != QgsVectorFileWriter.NoError:
                message = self.tr('Error writing vector file {}'.format(newOutputLayer))
                QgsMessageLog.logMessage(message, 'QTraffic', QgsMessageLog.CRITICAL)
                iface.messageBar().pushCritical('QTraffic', message)
                return
            
            # load the layer
            newLayerName =os.path.splitext(os.path.basename(  newOutputLayer ))[0]
            self.outLayer = QgsVectorLayer(newOutputLayer, newLayerName, 'ogr')
            if not self.outLayer.isValid():
                message = self.tr('Error loading vector file {}'.format(newOutputLayer))
                QgsMessageLog.logMessage(message, 'QTraffic', QgsMessageLog.CRITICAL)
                iface.messageBar().pushCritical('QTraffic', message)
                return
    
            self.outLayerId = self.outLayer.id()
            
        # prepare environment
        try:
            self.alg.setProject(self.project)
            self.alg.setLayer( roadLayer )
            self.alg.initConfig()
            self.alg.prepareRun()
        except Exception as ex:
            traceback.print_exc()
            message = self.tr('Error preparing running contex for the algoritm: %s' % str(ex))
            QgsMessageLog.logMessage(message, 'QTraffic', QgsMessageLog.CRITICAL)
            iface.messageBar().pushCritical('QTraffic', message)
            return
        
        # run the self.alg
        self.thread = QtCore.QThread(self)
        self.thread.started.connect(self.alg.run)
        self.thread.finished.connect(self.threadCleanup)
        self.thread.terminated.connect(self.threadCleanup)
        
        self.alg.moveToThread(self.thread)
        self.alg.started.connect(self.manageStarted)
        self.alg.progress.connect(self.manageProgress)
        self.alg.message.connect(self.manageMessage)
        self.alg.error.connect(self.manageError)
        self.alg.finished.connect(self.manageFinished)

        # set wait cursor and start
        QgsApplication.instance().setOverrideCursor(QtCore.Qt.WaitCursor)
        self.thread.start()
    
    def checkOutLayerRemoved(self, layerId):
        ''' check when outLayer has been removed settign semaphore to True
        '''
        if self.outLayerId == layerId:
            QgsMessageLog.logMessage('Completly removed outLayer', 'QTraffic', QgsMessageLog.INFO)
            self.outLayerRemoved = True
        
    def threadCleanup(self):
开发者ID:QTrafficmodel,项目名称:QTraffic,代码行数:70,代码来源:output_tab_manager.py


注:本文中的algorithm.Algorithm.prepareRun方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。