本文整理汇总了Python中algorithm.Algorithm.addResultToLayer方法的典型用法代码示例。如果您正苦于以下问题:Python Algorithm.addResultToLayer方法的具体用法?Python Algorithm.addResultToLayer怎么用?Python Algorithm.addResultToLayer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类algorithm.Algorithm
的用法示例。
在下文中一共展示了Algorithm.addResultToLayer方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: calculate
# 需要导入模块: from algorithm import Algorithm [as 别名]
# 或者: from algorithm.Algorithm import addResultToLayer [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()
示例2: OutputTabManager
# 需要导入模块: from algorithm import Algorithm [as 别名]
# 或者: from algorithm.Algorithm import addResultToLayer [as 别名]
#.........这里部分代码省略.........
if msgType == QgsMessageLog.CRITICAL:
msgType = QgsMessageBar.CRITICAL
if msgType == QgsMessageLog.WARNING:
duration = 3
msgType = QgsMessageBar.WARNING
iface.messageBar().pushMessage(message, msgType, duration)
def manageError(self, ex, exceptionMessage):
''' Do actions in case of alg thread error. Now only notify exception
'''
QgsMessageLog.logMessage(exceptionMessage, 'QTraffic', QgsMessageLog.CRITICAL)
iface.messageBar().pushMessage(exceptionMessage, QgsMessageBar.CRITICAL)
def manageFinished(self, success, reason):
''' Do action after notify that alg is finished. These are the postprocessing steps
1) merge result to the output layer
2) add the layer to canvas in case it is new
3) notify edn of processing
4) terminate the thread
'''
# finish the thread
self.thread.quit()
# check result
if not success:
QgsMessageLog.logMessage('Failed execution: {}'.format(reason), 'QTraffic', QgsMessageLog.CRITICAL)
iface.messageBar().pushCritical('QTraffic', self.tr("Error executing the algorithm: {}".format(reason)))
return
# prepare result
try:
self.alg.addResultToLayer(self.outLayer)
except Exception as ex:
traceback.print_exc()
QgsMessageLog.logMessage('Cannot add result to layer: {}'.format(str(ex)), 'QTraffic', QgsMessageLog.CRITICAL)
iface.messageBar().pushCritical('QTraffic', self.tr("Cannot add result to layer"))
return
# add or refresh rsult vector layer
addToInputLayer = self.gui.addToOriginaLayer_RButton.isChecked()
if not addToInputLayer:
QgsMapLayerRegistry.instance().addMapLayer(self.outLayer)
iface.mapCanvas().refresh()
# notify the user the end of process
iface.messageBar().pushSuccess('QTraffic', self.tr('Alg terminated successfully'))
def validate(self):
''' pre calcluation validation related only to this tab
Mandatory:
At least an output parameters have to be set
if "create new layer" a new layer name have to be set
'''
addToInputLayer = self.gui.addToOriginaLayer_RButton.isChecked()
newOutputLayer = self.gui.outFile_LEdit.text()
if (not addToInputLayer and
not newOutputLayer):
message = self.tr('No output vector specified')
iface.messageBar().pushMessage(message, QgsMessageBar.CRITICAL)
return False
Gasoline_Consumption = self.gui.fuelEnergyConsumptionGasoline_CBox.isChecked()
Diesel_Consumption = self.gui.fuelEnergyConsumptionDiesel_CBox.isChecked()
LPG_Consumption = self.gui.fuelEnergyConsumptionLPG_CBox.isChecked()