def __init__(self, iface): GenericTool.__init__(self, iface) self.labelName = "Calculate Area in Region" self.dlg = CalculateRegionDialog() self.dlg.lineEditShapeClip.clear() self.dlg.lineEditFolder.clear() self.dlg.lineEditShapeOut.clear() self.dlg.lineEditCoordRef.clear() self.dlg.pushButtonShape.clicked.connect(self.InputShape) self.dlg.pushButtonFolder.clicked.connect(self.InputFolder) self.dlg.pushButtonShapeOut.clicked.connect(self.OutputShape) self.dlg.pushButtonCoordRef.clicked.connect(self.SelectCoordSystem) self.ListUnits = ['Square meters','Square kilometers','Hectares'] self.dlg.Units.addItems(self.ListUnits)
class CalculateRegion(GenericTool): """QGIS Plugin Implementation.""" def __init__(self, iface): GenericTool.__init__(self, iface) self.labelName = "Calculate Area in Region" self.dlg = CalculateRegionDialog() self.dlg.lineEditShapeClip.clear() self.dlg.lineEditFolder.clear() self.dlg.lineEditShapeOut.clear() self.dlg.lineEditCoordRef.clear() self.dlg.pushButtonShape.clicked.connect(self.InputShape) self.dlg.pushButtonFolder.clicked.connect(self.InputFolder) self.dlg.pushButtonShapeOut.clicked.connect(self.OutputShape) self.dlg.pushButtonCoordRef.clicked.connect(self.SelectCoordSystem) self.ListUnits = ['Square meters','Square kilometers','Hectares'] self.dlg.Units.addItems(self.ListUnits) def run(self): """Run method that performs all the real work""" # show the dialog self.dlg.show() # Run the dialog event loop result = self.dlg.exec_() # See if OK was pressed Lista = ['Square meters','Square kilometers','Hectares'] TypeUnits = Lista[self.dlg.Units.currentIndex()] if result: ExtrairShape = self.dlg.lineEditFolder.text() CalcShape = self.dlg.lineEditShapeClip.text() OutShape = self.dlg.lineEditShapeOut.text() Ref = self.dlg.lineEditCoordRef.text() self.startWorker(ExtrairShape, CalcShape, OutShape, Ref, TypeUnits) #Function input clip shape file def InputShape(self): filters = "ShapeFiles (*.shp)" iShape = QFileDialog.getOpenFileName(self.dlg,'Insert Shapefile','',filters) self.dlg.lineEditShapeClip.setText(iShape) #Function input folder shapes def InputFolder(self): iFolder = QFileDialog.getExistingDirectory(self.dlg,'Insert Folder','',QFileDialog.ShowDirsOnly) self.dlg.lineEditFolder.setText(iFolder) #Function output shape file def OutputShape(self): filters = "ShapeFiles (*.shp)" oShape = QFileDialog.getSaveFileName(self.dlg,'Output Shapefile','',filters) self.dlg.lineEditShapeOut.setText(oShape) def SelectCoordSystem(self): projection = None projection = qgis.gui.QgsGenericProjectionSelector() teste = projection.exec_() EPSG = projection.selectedAuthId() self.dlg.lineEditCoordRef.setText(EPSG) #Function Start thread processing def startWorker(self, ExtrairShape, CalcShape, OutShape, Ref, TypeUnits): # create a new worker instance worker = Worker(ExtrairShape, CalcShape, OutShape, Ref, TypeUnits) # configure the QgsMessageBar qgis.utils.iface.messageBar().clearWidgets() progressMessageBar = qgis.utils.iface.messageBar().createMessage('Calculating area...') progressBar = QProgressBar() progressBar.setMaximum(100) progressMessageBar.layout().addWidget(progressBar) qgis.utils.iface.messageBar().pushWidget(progressMessageBar) self.progressMessageBar = progressMessageBar # start the worker in a new thread thread = QThread() worker.moveToThread(thread) worker.finished.connect(self.workerFinished) worker.error.connect(self.workerError) worker.progress.connect(progressBar.setValue) thread.started.connect(worker.runCalc) thread.start() self.thread = thread self.worker = worker self.output = OutShape #Function to finished thread processing def workerFinished(self,value): # clean up the worker and thread self.worker.deleteLater() self.thread.quit() self.thread.wait() self.thread.deleteLater() LayerName = os.path.basename(self.output)[0:len(os.path.basename(self.output))-4] qgis.utils.iface.addVectorLayer(self.output,LayerName,"ogr") qgis.utils.iface.messageBar().clearWidgets() if value > 0: QMessageBox.warning(qgis.utils.iface.mainWindow(),"Calculate Area in Region",'There '+str(value)+ ' geometries not valid. For more information see the log file at:'+os.path.join(os.path.dirname(self.output),"LogErros.txt")) else: QMessageBox.information(qgis.utils.iface.mainWindow(),"Calculate Area in Region","Process finished with successfully"+ '\n'+'Layer:'+str(os.path.basename(self.output)[0:len(os.path.basename(self.output))-4])+" map added") #Function report error in work def workerError(self, e, exception_string): QgsMessageLog.logMessage('Worker thread raised an exception:\n'.format(exception_string), level=QgsMessageLog.CRITICAL)