本文整理匯總了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()