本文整理汇总了Python中algorithm.Algorithm.setProject方法的典型用法代码示例。如果您正苦于以下问题:Python Algorithm.setProject方法的具体用法?Python Algorithm.setProject怎么用?Python Algorithm.setProject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类algorithm.Algorithm
的用法示例。
在下文中一共展示了Algorithm.setProject方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: calculate
# 需要导入模块: from algorithm import Algorithm [as 别名]
# 或者: from algorithm.Algorithm import setProject [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 setProject [as 别名]
class OutputTabManager(QtCore.QObject):
''' Class to hide managing of relative tab
'''
projectModified = QtCore.pyqtSignal()
def __init__(self, parent=None):
'''constructor'''
super(OutputTabManager, self).__init__(parent)
# parent is the dock widget with all graphical elements
self.gui = parent
self.plugin = parent.parent
# init some globals
self.applicationPath = os.path.dirname(os.path.realpath(__file__))
self.project = None
self.projectPath = None
self.outLayer = None
self.outLayerId = None
self.outLayerRemoved = False
# retrieve the current tab index
self.initTabTabIndex()
# disable tab at the beginning
self.gui.tabWidget.setTabEnabled(self.tabIndex, False)
# add some gui events
self.gui.createNewLayer_RButton.toggled.connect(self.setInputLayer)
self.gui.selectOutfile_TButton.clicked.connect(self.selectNewOutFile)
self.gui.calculate_PButton.clicked.connect(self.calculate)
def initTabTabIndex(self):
''' Retrieve what tab index refer the current tab manager
'''
for tabIndex in range(self.gui.tabWidget.count()):
if self.gui.tabWidget.tabText(tabIndex) == "Output":
self.tabIndex = tabIndex
def setProject(self, project=None):
''' setting the new project on which the tab is based
'''
self.project = project
if self.project:
# write everything on disk
self.project.sync()
# set some globals
confFileName = self.project.fileName()
self.projectPath = os.path.dirname(confFileName)
# emit configurationLoaded with the status of loading
self.setTabGUIBasingOnProject()
# enable current tab because project has been loaded
self.gui.tabWidget.setTabEnabled(self.tabIndex, True)
else:
# disable current tab because no project has been loaded yet
self.gui.tabWidget.setTabEnabled(self.tabIndex, False)
def selectNewOutFile(self):
''' Select the the file
'''
oldOutputLayer = self.gui.outFile_LEdit.text()
# get last conf to start from its path
startPath = self.projectPath
newOutputLayer = self.project.value('Processing.OutputFileDefinition/newOutputLayer', '')
if newOutputLayer:
startPath = os.path.dirname( newOutputLayer )
# ask for the new out file
newOutputLayer = QtGui.QFileDialog.getSaveFileName(self.gui, "Select an autput file", startPath,
self.plugin.tr("All (*)"))
if not newOutputLayer:
return
if oldOutputLayer == newOutputLayer:
return
# add shp extension
components = os.path.splitext(newOutputLayer)
if len(components) == 1:
newOutputLayer = newOutputLayer + '.shp'
else:
if components[1] != '.shp':
newOutputLayer = components[0] + '.shp'
# update gui
self.gui.outFile_LEdit.setText(newOutputLayer)
self.saveTabOnProject()
def setInputLayer(self):
''' Set GUI basing on toggled ComboBox to set if the layer is the input one
or have to select a new one
'''
self.gui.outFile_LEdit.setEnabled( self.gui.createNewLayer_RButton.isChecked() )
self.gui.selectOutfile_TButton.setEnabled( self.gui.createNewLayer_RButton.isChecked() )
#.........这里部分代码省略.........