def onReturnPressed(self): try: txt = self.editSearch.text().strip() self.plugin.lastSearch = self.editSearch.text() self.plugin.limitSearchToExtent = (self.cbExtent.isChecked()) options = self.plugin.gnOptions options2 = {} if self.plugin.limitSearchToExtent: sourceCrs = self.plugin.canvas.mapSettings().destinationCrs() targetCrs = QgsCoordinateReferenceSystem() targetCrs.createFromSrid(4326) xform = QgsCoordinateTransform(sourceCrs, targetCrs, QgsProject.instance()) geom = xform.transform(self.plugin.canvas.extent()) options2 = {'viewbox': str(geom.xMinimum()) + ',' + str(geom.yMaximum()) + ',' + str(geom.xMaximum()) + ',' + str(geom.yMinimum())} params = {'q': txt, 'addressdetails': '0'} self.searchJson(params, self.plugin.gnUsername, options, options2) except Exception as e: for m in e.args: QgsMessageLog.logMessage(m, 'Extensions') pass
def executeSaga(feedback): if isWindows(): command = ['cmd.exe', '/C ', sagaBatchJobFilename()] else: os.chmod(sagaBatchJobFilename(), stat.S_IEXEC | stat.S_IREAD | stat.S_IWRITE) command = ["'" + sagaBatchJobFilename() + "'"] loglines = [] loglines.append(QCoreApplication.translate('SagaUtils', 'SAGA execution console output')) with subprocess.Popen( command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.DEVNULL, stderr=subprocess.STDOUT, universal_newlines=True, ) as proc: try: for line in iter(proc.stdout.readline, ''): if '%' in line: s = ''.join([x for x in line if x.isdigit()]) try: feedback.setProgress(int(s)) except: pass else: line = line.strip() if line != '/' and line != '-' and line != '\\' and line != '|': loglines.append(line) feedback.pushConsoleInfo(line) except: pass if ProcessingConfig.getSetting(SAGA_LOG_CONSOLE): QgsMessageLog.logMessage('\n'.join(loglines), 'Processing', Qgis.Info)
def postInputs(self): """ After layer imports, we need to update some internal parameters """ # If projection has not already be set, use the project self.setSessionProjectionFromProject() # Build GRASS region if self.region.isEmpty(): self.region = QgsProcessingUtils.combineLayerExtents(self.inputLayers) command = 'g.region n={} s={} e={} w={}'.format( self.region.yMaximum(), self.region.yMinimum(), self.region.xMaximum(), self.region.xMinimum() ) # Handle cell size if self.parameterDefinition(self.GRASS_REGION_CELLSIZE_PARAMETER): if self.cellSize: cellSize = self.cellSize else: cellSize = self.getDefaultCellSize() command += ' res={}'.format(cellSize) # Handle align to resolution if self.alignToResolution: command += ' -a' # Add the default parameters commands self.commands.append(command) QgsMessageLog.logMessage(self.tr('processInputs end. Commands: {}').format(self.commands), 'Grass7', Qgis.Info)
def reject(self): #QMessageBox.warning(self.iface.mainWindow(), "VoGIS-Profiltool", "REJECTED") QgsMessageLog.logMessage("reject: {0}".format(self.exaggeration_edited), "VoGis", Qgis.Info) if self.exaggeration_edited is True: self.exaggeration_edited = False return QDialog.reject(self)
def __init__(self, alg): super(HelpEditionDialog, self).__init__(None) self.setupUi(self) self.alg = alg self.descriptions = {} if isinstance(self.alg, QgsProcessingModelAlgorithm): self.descriptions = self.alg.helpContent() else: if self.alg.descriptionFile is not None: helpfile = alg.descriptionFile + '.help' if os.path.exists(helpfile): try: with open(helpfile) as f: self.descriptions = json.load(f) except Exception: QgsMessageLog.logMessage(self.tr('Cannot open help file: {0}').format(helpfile), self.tr('Processing'), Qgis.Warning) self.currentName = self.ALG_DESC if self.ALG_DESC in self.descriptions: self.text.setText(self.descriptions[self.ALG_DESC]) self.tree.itemClicked.connect(self.changeItem) self.fillTree() self.updateHtmlView()
def killWorker(self): """Kill the worker thread.""" if self.worker is not None: QgsMessageLog.logMessage(self.tr('Killing worker'), self.HISTOGRAM, QgsMessageLog.INFO) self.worker.kill()
def copy(self, destinationFolder): """ Copy inventoried files considering the dataset destinationFolder: copy destination folder """ for fileName in self.files: # adjusting the separators according to the OS fileName = fileName.replace('/', os.sep) file = fileName.split(os.sep)[-1] newFileName = os.path.join(destinationFolder, file) newFileName = newFileName.replace('/', os.sep) try: gdalSrc = gdal.Open(fileName) ogrSrc = ogr.Open(fileName) if ogrSrc: self.copyOGRDataSource(ogrSrc, newFileName) elif gdalSrc: self.copyGDALDataSource(gdalSrc, newFileName) except Exception as e: QgsMessageLog.logMessage(self.messenger.getCopyErrorMessage()+'\n'+':'.join(e.args), "DSG Tools Plugin", QgsMessageLog.INFO) return (0, self.messenger.getCopyErrorMessage()+'\n'+':'.join(e.args)) QgsMessageLog.logMessage(self.messenger.getSuccessInventoryAndCopyMessage(), "DSG Tools Plugin", QgsMessageLog.INFO) return (1, self.messenger.getSuccessInventoryAndCopyMessage())
def addToLog(msgtype, msg): try: # It seems that this fails sometimes depending on the msg # added. To avoid it stopping the normal functioning of the # algorithm, we catch all errors, assuming that is better # to miss some log info than breaking the algorithm. if msgtype == ProcessingLog.LOG_ALGORITHM: line = msgtype + '|' + datetime.datetime.now().strftime( ProcessingLog.DATE_FORMAT) + '|' \ + msg + '\n' with codecs.open(ProcessingLog.logFilename(), 'a', encoding='utf-8') as logfile: logfile.write(line) algname = msg[len('Processing.runalg("'):] algname = algname[:algname.index('"')] if algname not in ProcessingLog.recentAlgs: ProcessingLog.recentAlgs.append(algname) recentAlgsString = ';'.join(ProcessingLog.recentAlgs[-6:]) ProcessingConfig.setSettingValue( ProcessingConfig.RECENT_ALGORITHMS, recentAlgsString) else: if isinstance(msg, list): msg = '\n'.join([m for m in msg]) msgtypes = {ProcessingLog.LOG_ERROR: QgsMessageLog.CRITICAL, ProcessingLog.LOG_INFO: QgsMessageLog.INFO, ProcessingLog.LOG_WARNING: QgsMessageLog.WARNING, } QgsMessageLog.logMessage(msg, ProcessingLog.tr("Processing"), msgtypes[msgtype]) except: pass
def killWorker(self): """Kill the worker thread.""" if self.worker is not None: QgsMessageLog.logMessage(self.tr('Killing worker'), self.SDELLIPSE, Qgis.Info) self.worker.kill()
def startConnection(self): if self.storage_combo_box.currentIndex() == 0: bucket_name = 'oam-qgis-plugin-test' else: bucket_name = str(self.specify_edit.text()) if not bucket_name: self.bar2.clearWidgets() self.bar2.pushMessage( 'WARNING', 'The bucket for upload must be provided', level=QgsMessageBar.WARNING) bucket_key = str(self.key_id_edit.text()) bucket_secret = str(self.secret_key_edit.text()) self.bucket = None for trial in xrange(3): if self.bucket: break try: connection = S3Connection(bucket_key,bucket_secret) self.bucket = connection.get_bucket(bucket_name) QgsMessageLog.logMessage( 'Connection established' % trial, 'OAM', level=QgsMessageLog.INFO) except: if trial == 2: QgsMessageLog.logMessage( 'Failed to connect after 3 attempts', 'OAM', level=QgsMessageLog.CRITICAL) return self.bucket
def testBlocker(self): app_log = QgsApplication.messageLog() spy = QSignalSpy(app_log.messageReceived) spy_received = QSignalSpy(app_log.messageReceived[bool]) QgsMessageLog.logMessage('test', 'tag', Qgis.Warning, notifyUser=True) self.assertEqual(len(spy), 1) self.assertEqual(spy[-1], ['test', 'tag', Qgis.Warning]) self.assertEqual(len(spy_received), 1) # block notifications b = QgsMessageLogNotifyBlocker() QgsMessageLog.logMessage('test', 'tag', Qgis.Warning, notifyUser=True) self.assertEqual(len(spy), 2) # should not be blocked self.assertEqual(len(spy_received), 1) # should be blocked # another blocker b2 = QgsMessageLogNotifyBlocker() QgsMessageLog.logMessage('test', 'tag', Qgis.Warning, notifyUser=True) self.assertEqual(len(spy), 3) # should not be blocked self.assertEqual(len(spy_received), 1) # should be blocked del b # still blocked because of b2 QgsMessageLog.logMessage('test', 'tag', Qgis.Warning, notifyUser=True) self.assertEqual(len(spy), 4) # should not be blocked self.assertEqual(len(spy_received), 1) # should be blocked del b2 # not blocked QgsMessageLog.logMessage('test', 'tag', Qgis.Warning, notifyUser=True) self.assertEqual(len(spy), 5) # should not be blocked self.assertEqual(len(spy_received), 2) # should not be blocked
def run(self): if not os.path.isdir(self.dataDirectory): os.mkdir(self.dataDirectory) self.prepare() self.consolidateProject() layers = QgsProject.instance().mapLayers() total = 100.0 / len(layers) for count, layer in enumerate(layers.values()): QgsMessageLog.logMessage('Consolidating {layer}.'.format(layer=layer.name()), 'QConsolidate', Qgis.Info) if self.isCanceled(): break layerType = layer.type() if layerType == QgsMapLayer.VectorLayer: self.consolidateVectorLayer(layer) elif layerType == QgsMapLayer.RasterLayer: self.consolidateRasterLayer(layer) elif layerType == QgsMapLayer.PluginLayer: self.consolidatePluginLayer(layer) elif layerType == QgsMapLayer.MeshLayer: self.consolidateMeshLayer(layer) self.setProgress(int(count * total)) self.project.write(self.projectFile) return self.cleanup()
def validateFile(self,filename): # check that file exists if not os.path.exists(filename): self.bar0.clearWidgets() self.bar0.pushMessage( "CRITICAL", "The file %s does not exist" % filename, level=QgsMessageBar.CRITICAL) return False # check that file is an image if imghdr.what(filename) is None: self.bar0.clearWidgets() self.bar0.pushMessage( "CRITICAL", "The file %s is not a supported data source" % filename, level=QgsMessageBar.CRITICAL) return False # check if gdal can read file try: raster = gdal.Open(filename,gdal.GA_ReadOnly) except: self.bar0.clearWidgets() self.bar0.pushMessage( "CRITICAL", "GDAL could not read file %s" % filename, level=QgsMessageBar.CRITICAL) return False # check if there is an object raster if not raster: self.bar0.clearWidgets() self.bar0.pushMessage( "CRITICAL", "GDAL could not read file %s" % filename, level=QgsMessageBar.CRITICAL) return False # check that image has at least 3 bands if raster.RasterCount < 3: self.bar0.clearWidgets() self.bar0.pushMessage( "CRITICAL", "The file %s has less than 3 raster bands" % filename, level=QgsMessageBar.CRITICAL) return False # check if projection is set if raster.GetProjection() is '': self.bar0.clearWidgets() self.bar0.pushMessage( "CRITICAL", "Could not extract projection from file %s" % filename, level=QgsMessageBar.CRITICAL) return False # finally, check if bbox has valid data xy_points = [(0.0,0.0),(0.0,raster.RasterYSize),(raster.RasterXSize,0.0),(raster.RasterXSize,raster.RasterYSize)] for point in xy_points: if point != self.GDALInfoReportCorner(raster,point[0],point[1]): QgsMessageLog.logMessage( 'File %s is a valid data source' % filename, 'OAM', level=QgsMessageLog.INFO) return True
def _processGdalDatasource(self, layer, destDirectory): uri = layer.source() filePath = layer.source() newDirectory = destDirectory.replace(self.baseDirectory, '.') newSource = '{dirName}/{fileName}'.format(dirName=newDirectory, fileName=os.path.split(filePath)[1]) if uri.startswith(('/vsizip/', '/vsigzip/', '/vsitar/')): m = GDAL_VSI.search(uri) if m is not None: prefix = m.group(1) filePath = m.group(2) layerPath = m.group(4) newSource = '{vsi}{dirName}/{fileName}/{layer}'.format(vsi=prefix, dirName=newDirectory, fileName=os.path.split(filePath)[1], layer=layerPath) else: QgsMessageLog.logMessage(self.tr('Failed to parse URI: "{uri}"'.format(uri=uri)), 'QConsolidate', Qgis.Warning) return else: # ignore other virtual filesystems if uri.startswith('/vsi'): prefix = uri.slit('/')[0] QgsMessageLog.logMessage(self.tr('Not supported GDAL virtual filesystem layer "{layerType}"'.format(layerType=prefix)), 'QConsolidate', Qgis.Info) return # handle multiple layers in the single dataset if '|' in uri: filePath = uri.split('|')[0] layerPath = uri.split('|')[1] newSource = '{dirName}/{fileName}|{layer}'.format(dirName=newDirectory, fileName=os.path.split(filePath)[1], layer=layerPath) self._copyLayerFiles(filePath, destDirectory) self.updateLayerSource(layer.id(), newSource)
def processAlgorithm(self, parameters, context, feedback): layerA = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(Difference.INPUT), context) layerB = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(Difference.OVERLAY), context) geomType = QgsWkbTypes.multiType(layerA.wkbType()) writer = self.getOutputFromName( Difference.OUTPUT).getVectorWriter(layerA.fields(), geomType, layerA.crs(), context) outFeat = QgsFeature() index = QgsProcessingUtils.createSpatialIndex(layerB, context) selectionA = QgsProcessingUtils.getFeatures(layerA, context) total = 100.0 / layerA.featureCount() if layerA.featureCount() else 0 for current, inFeatA in enumerate(selectionA): geom = inFeatA.geometry() diff_geom = QgsGeometry(geom) attrs = inFeatA.attributes() intersections = index.intersects(geom.boundingBox()) request = QgsFeatureRequest().setFilterFids(intersections).setSubsetOfAttributes([]) for inFeatB in layerB.getFeatures(request): tmpGeom = inFeatB.geometry() if diff_geom.intersects(tmpGeom): diff_geom = QgsGeometry(diff_geom.difference(tmpGeom)) try: outFeat.setGeometry(diff_geom) outFeat.setAttributes(attrs) writer.addFeature(outFeat, QgsFeatureSink.FastInsert) except: QgsMessageLog.logMessage(self.tr('Feature geometry error: One or more output features ignored due to invalid geometry.'), self.tr('Processing'), QgsMessageLog.WARNING) continue feedback.setProgress(int(current * total)) del writer
def getAuxiliaryDataDirectories(): gdal_data_dir = None gtiff_csv_dir = None otb_folder = OtbUtils.otbFolder() if os.name == 'nt': gdal_data_dir = os.path.join(otb_folder, 'share', 'data') gtiff_csv_dir = os.path.join(otb_folder, 'share', 'epsg_csv') else: env_profile = os.path.join(otb_folder, 'otbenv.profile') try: if os.path.exists(env_profile): with open(env_profile) as f: lines = f.readlines() lines = [x.strip() for x in lines] for line in lines: if not line or line.startswith('#'): continue if 'GDAL_DATA=' in line: gdal_data_dir = line.split("GDAL_DATA=")[1] if 'GEOTIFF_CSV='in line: gtiff_csv_dir = line.split("GEOTIFF_CSV=")[1] except BaseException as exc: errmsg = "Cannot find gdal and geotiff data directory." + str(exc) QgsMessageLog.logMessage(errmsg, OtbUtils.tr('Processing'), Qgis.Info) pass return gdal_data_dir, gtiff_csv_dir
def search(url, callback): QgsMessageLog.logMessage("URL:" + url, "Gazetteer") def requestFinished(reply): # Disconnect from the signal networkAccessManager = QgsNetworkAccessManager.instance() networkAccessManager.finished.disconnect(requestFinished) # Handle the reply if reply.error() != QNetworkReply.NoError: QgsMessageLog.logMessage("Network error #{0}: {1}".format(reply.error(), reply.errorString()), "Gazetteer") callback(u'') else: charset = 'UTF-8' try: _, params = cgi.parse_header(reply.header(QNetworkRequest.ContentTypeHeader)) charset = params['charset'] except: pass QgsMessageLog.logMessage("charset: " + charset, "Gazetteer") data = unicode(reply.readAll(), charset) reply.deleteLater() callback(data) networkAccessManager = QgsNetworkAccessManager.instance() networkAccessManager.finished.connect(requestFinished) networkAccessManager.get(QNetworkRequest(QUrl(QUrl.fromPercentEncoding(url))))
def readXml(self, node): # early read of custom properties self.readCustomProperties(node) # get layer type ol_layer_type = None ol_layer_type_name = self.customProperty( OpenlayersLayer.LAYER_PROPERTY, "") if ol_layer_type_name != "": ol_layer_type = self.olLayerTypeRegistry.getByName( ol_layer_type_name) else: # handle ol_layer_type idx stored in layer node # (OL plugin <= 1.1.2) ol_layer_type_idx = int(node.toElement().attribute( "ol_layer_type", "-1")) if ol_layer_type_idx != -1: ol_layer_type = self.olLayerTypeRegistry.getById( ol_layer_type_idx) if ol_layer_type is not None: self.setLayerType(ol_layer_type) else: # Set default layer type self.setLayerType( self.olLayerTypeRegistry.getByName("OpenStreetMap")) msg = "Obsolete or unknown layer type '%s', using OpenStreetMap\ instead" % ol_layer_type_name self.iface.messageBar().pushMessage("OpenLayers Plugin", msg, level=Qgis.MessageLevel(1)) QgsMessageLog.logMessage(msg, "OpenLayers Plugin", QgsMessageLog.WARNING) return True
def _fill_data_sources_list(self): self.data_sources = {} for ds_path in self.ds_paths: for root, dirs, files in os.walk(ds_path): for ini_file in [f for f in files if f.endswith('.ini')]: try: ini_full_path = os.path.join(root, ini_file) ds = DataSourceSerializer.read_from_ini(ini_full_path) # set contrib&user if ds_path in ROOT_MAPPING.keys(): ds.category = ROOT_MAPPING[ds_path] else: ds.category = DataSourceCategory.USER # action ds.action = QAction(QIcon(ds.icon_path), self.tr(ds.alias), None) ds.action.setData(ds) # append to array self.data_sources[ds.id] = ds except Exception, e: error_message = 'INI file can\'t be parsed: ' + e.message QgsMessageLog.logMessage(error_message, level=QgsMessageLog.CRITICAL)
def accept(self): """Creates and loads the WMS layer.""" self.close() currentIndex = self.comboBoxLayer.currentIndex() currentLayerId, unused_dataType = self.comboBoxLayer.itemData(currentIndex) currentLayerName = unicode(self.comboBoxLayer.currentText()) mapId = self.labelMapId.text() # Create the WMS layer token = oauth2_utils.getToken() url = 'https://mapsengine.google.com/%s-4/wms/%s/' wmsUrl = url % (mapId, token.access_token) currentFormatIndex = self.comboBoxFormat.currentIndex() imageFormat = unicode(self.comboBoxFormat.itemData(currentFormatIndex)) crs = self.comboBoxCrs.currentText() uri = QgsDataSourceURI() uri.setParam('url', wmsUrl) uri.setParam('layers', currentLayerId) uri.setParam('format', imageFormat) uri.setParam('crs', crs) uri.setParam('styles', '') rlayer = QgsRasterLayer(str(uri.encodedUri()), currentLayerName, 'wms') if rlayer.isValid(): QgsMapLayerRegistry.instance().addMapLayer(rlayer) else: logText = 'Failed to add WMS layer %s with URI %s' % ( currentLayerName, str(uri.encodedUri())) warnText = 'Failed to add WMS layer %s' % currentLayerName QgsMessageLog.logMessage(logText, 'GMEConnector', QgsMessageLog.CRITICAL) self.iface.messageBar().pushMessage( 'Google Maps Engine Connector', warnText, level=QgsMessageBar.CRITICAL, duration=3)
def return_batch_route(self, features): """Save and/or display the routes retrieved""" osrm_batch_route_layer = QgsVectorLayer( "Linestring?crs=epsg:4326&field=id:integer" "&field=total_time:integer(20)&field=distance:integer(20)", "routes_osrm{}".format(self.nb_done), "memory") provider = osrm_batch_route_layer.dataProvider() provider.addFeatures(features) QgsMapLayerRegistry.instance().addMapLayer(osrm_batch_route_layer) if self.filename: error = QgsVectorFileWriter.writeAsVectorFormat( osrm_batch_route_layer, self.filename, self.encoding, None, "ESRI Shapefile") if error != QgsVectorFileWriter.NoError: self.iface.messageBar().pushMessage( "Error", "Can't save the result into {} - Output have been " "added to the canvas (see QGis log for error trace" "back)".format(self.filename), duration=10) QgsMessageLog.logMessage( 'OSRM-plugin error report :\n {}'.format(error), level=QgsMessageLog.WARNING) self.iface.setActiveLayer(osrm_batch_route_layer) return -1 else: QtGui.QMessageBox.information( self.iface.mainWindow(), 'Info', "Result saved in {}".format(self.filename)) if self.check_add_layer.isChecked(): self.iface.setActiveLayer(osrm_batch_route_layer) else: QgsMapLayerRegistry.instance().removeMapLayer( osrm_batch_route_layer.id()) self.iface.messageBar().clearWidgets()
def unserialize(self, st): style = None orig_geom = None geom = None try: t = pickle.loads(st) except Exception as e: try: t = pickle.loads(st, encoding='utf-8') # strange, Exception says : No module named 'PyQt4' except Exception as e: for m in e.args: QgsMessageLog.logMessage(str(m), 'Extensions') raise Exception("Mask - Error when loading mask") if len(t) == 12: # older version (self.do_buffer, self.buffer_units, self.buffer_segments, self.do_simplify, self.simplify_tolerance, self.do_save_as, self.file_path, self.file_format, self.limited_layers_obsolete, style, self.polygon_mask_method, self.line_mask_method) = t else: (self.do_buffer, self.buffer_units, self.buffer_segments, self.do_simplify, self.simplify_tolerance, self.do_save_as, self.file_path, self.file_format, self.limited_layers_obsolete, style, self.polygon_mask_method, self.line_mask_method, orig_geom, geom ) = t self.style = None self.geometry = None if style is not None: self.style = style if geom is not None: self.geometry = QgsGeometry() self.geometry.fromWkb(geom) if orig_geom is not None: gl = [] for g in orig_geom: geo = QgsGeometry() geo.fromWkb(g) gl.append(geo) self.orig_geometry = gl
def showInfo(self, text): """Show info.""" self.iface.messageBar().pushMessage(self.tr('Info'), text, level=Qgis.Info, duration=2) QgsMessageLog.logMessage('Info: ' + text, self.THINGREYSCALE, Qgis.Info)
def showError(self, text): """Show an error.""" self.iface.messageBar().pushMessage(self.tr('Error'), text, level=QgsMessageBar.CRITICAL, duration=3) QgsMessageLog.logMessage('Error: ' + text, self.THINGREYSCALE, QgsMessageLog.CRITICAL)
def showWarning(self, text): """Show a warning.""" self.iface.messageBar().pushMessage(self.tr('Warning'), text, level=QgsMessageBar.WARNING, duration=2) QgsMessageLog.logMessage('Warning: ' + text, self.THINGREYSCALE, QgsMessageLog.WARNING)
def executeOtb(command, feedback, addToLog=True): loglines = [] with subprocess.Popen( [command], shell=True, stdout=subprocess.PIPE, stdin=subprocess.DEVNULL, stderr=subprocess.STDOUT, universal_newlines=True ) as proc: try: for line in iter(proc.stdout.readline, ''): line = line.strip() #'* ]' and ' ]' says its some progress update #print('line[-3:]',line[-3:]) if line[-3:] == "* ]" or line[-3:] == " ]": part = line.split(':')[1] percent = part.split('%')[0] try: if int(percent) >= 100: loglines.append(line) feedback.setProgress(int(percent)) except: pass else: loglines.append(line) except BaseException as e: loglines.append(str(e)) pass for logline in loglines: if feedback is None: QgsMessageLog.logMessage(logline, 'Processing', Qgis.Info) else: feedback.pushConsoleInfo(logline)
def __calculate_statistics(self, thema, subthema, layers): #features = processing.features(self.coverage_layer) features = self.coverage_layer.selectedFeatures() for gstk in features: try: gnr = gstk[self.settings.fld_gnr()] flaeche = gstk.geometry().area() gstk_stats = VRPStatistik(gnr, thema.name, flaeche, self.gem_name) #pyqtRemoveInputHook() #pdb.set_trace() #go thru all data sources of subthema for quelle in subthema.quellen: if VRP_DEBUG is True: QgsMessageLog.logMessage(u'quelle:{0}'.format(quelle.name), DLG_CAPTION) #only use those with statistik == True if quelle.statistik is False: continue lyr_curr_quelle = None for lyr in layers: if quelle.name == lyr.name(): lyr_curr_quelle = lyr if lyr_curr_quelle is None: continue text_flaeche = self.__get_text_flaeche(quelle, gstk, lyr_curr_quelle, quelle.attribut) sub_stat = VRPStatistikSubThema(quelle.name, text_flaeche) gstk_stats.add_subthema(thema.name, sub_stat) if gnr in self.statistics: self.statistics[gnr].append(gstk_stats) else: self.statistics[gnr] = [gstk_stats] except: msg = '__calculate_statistics:\n\n{0}'.format(traceback.format_exc()) QgsMessageLog.logMessage(msg, DLG_CAPTION, QgsMessageLog.CRITICAL) msg = msg.replace('\n', '') self.iface.messageBar().pushMessage(msg, QgsMessageBar.CRITICAL) return
def loadLessonsFromPaths(paths): hasErrors = False for path in paths: for folder in os.listdir(path): if os.path.isdir(os.path.join(path, folder)): groupFiles = [os.path.join(path, folder, f) for f in ["group.html", "group.md"]] for groupFile in groupFiles: if os.path.exists(groupFile): groups[folder.replace("_", " ")] = groupFile break for subfolder in os.listdir(os.path.join(path, folder)): if os.path.isdir(os.path.join(path, folder, subfolder)): try: f = os.path.join(path, folder, subfolder, "__init__.py") if os.path.exists(f): m = imp.load_source("{}.{}".format(folder, subfolder), f) addLessonModule(m) except Exception as e: QgsMessageLog.logMessage("Can not load lesson from {}:\n{}".format(f, str(e)), "Lessons") hasErrors = True if isYamlLessonFolder(os.path.join(path, folder), subfolder): lesson = lessonFromYamlFile(os.path.join(path, folder, subfolder, "lesson.yaml")) if lesson: _addLesson(lesson) else: hasErrors = True return hasErrors
def loadConfiguration(self): ''' Function to load last conf get from settings ''' if not self.project: return # from th eproject, get JSON filename to load currentVehicleClassesJson = self.project.value('/FleetComposition/fleetComposition', self.gui.defaultVehicleClassesFileName) # check if json is relative path or absolute if not os.path.isabs(currentVehicleClassesJson): currentVehicleClassesJson = os.path.join(self.projectPath, currentVehicleClassesJson) # load json conf try: with open(currentVehicleClassesJson) as confFile: # loaded in and OrderedDict to allow maintaining the order of classes # contained in the json file. This will be reflected in the order # shown in the sunburst visualization/editor self.vehicleClassesDict = json.load(confFile, object_pairs_hook=collections.OrderedDict) self.configurationLoaded.emit(True) except Exception as ex: self.vehicleClassesDict = None QgsMessageLog.logMessage(traceback.format_exc(), 'QTraffic', QgsMessageLog.CRITICAL) iface.messageBar().pushMessage(self.tr("Error loading Conf JSON file. Please check the log"), QgsMessageBar.CRITICAL) return
def install(self): """Install the processing scripts in the collection. We copy the processing scripts exist in the processing dir to the user's processing scripts directory and refresh the provider. """ # Check if the dir exists, pass installing silently if it doesn't exist if not os.path.exists(self.resource_dir): return # Get all the script files under self.resource_dir processing_files = [] for item in os.listdir(self.resource_dir): file_path = os.path.join(self.resource_dir, item) if fnmatch.fnmatch(file_path, '*.py'): processing_files.append(file_path) valid = 0 for processing_file in processing_files: # Install silently the processing file try: shutil.copy(processing_file, self.scripts_folder()) valid += 1 except OSError as e: QgsMessageLog.logMessage("Could not copy script '{}'\n{}".format(processing_file, str(e)), "Processing", Qgis.Warning) if valid > 0: self.refresh_script_provider()
def processAlgorithm(self, parameters, context, feedback): if system.isWindows(): path = Grass7Utils.grassPath() if path == '': raise GeoAlgorithmExecutionException( self.tr( 'GRASS GIS 7 folder is not configured. Please ' 'configure it before running GRASS GIS 7 algorithms.')) # Create brand new commands lists self.commands = [] self.outputCommands = [] self.exportedLayers = {} # If GRASS session has been created outside of this algorithm then # get the list of layers loaded in GRASS otherwise start a new # session existingSession = Grass7Utils.sessionRunning if existingSession: self.exportedLayers = Grass7Utils.getSessionLayers() else: Grass7Utils.startGrass7Session() # Handle ext functions for inputs/command/outputs if self.module: if hasattr(self.module, 'processInputs'): func = getattr(self.module, 'processInputs') func(self) else: self.processInputs(parameters, context) if hasattr(self.module, 'processCommand'): func = getattr(self.module, 'processCommand') func(self) else: self.processCommand() if hasattr(self.module, 'processOutputs'): func = getattr(self.module, 'processOutputs') func(self) else: self.processOutputs() else: self.processInputs(parameters, context) self.processCommand() self.processOutputs() # Run GRASS loglines = [] loglines.append(self.tr('GRASS GIS 7 execution commands')) for line in self.commands: feedback.pushCommandInfo(line) loglines.append(line) if ProcessingConfig.getSetting(Grass7Utils.GRASS_LOG_COMMANDS): QgsMessageLog.logMessage("\n".join(loglines), self.tr('Processing'), QgsMessageLog.INFO) Grass7Utils.executeGrass7(self.commands, feedback, self.outputCommands) for out in self.outputs: if isinstance(out, OutputHTML): with open(self.getOutputFromName("rawoutput").value) as f: rawOutput = "".join(f.readlines()) with open(out.value, "w") as f: f.write("<pre>%s</pre>" % rawOutput) # If the session has been created outside of this algorithm, add # the new GRASS GIS 7 layers to it otherwise finish the session if existingSession: Grass7Utils.addSessionLayers(self.exportedLayers) else: Grass7Utils.endGrass7Session()
def processAlgorithm(self, context, feedback): source_layer = QgsProcessingUtils.mapLayerFromString( self.getParameterValue(Clip.INPUT), context) mask_layer = QgsProcessingUtils.mapLayerFromString( self.getParameterValue(Clip.OVERLAY), context) writer = self.getOutputFromName(self.OUTPUT).getVectorWriter( source_layer.fields(), QgsWkbTypes.multiType(source_layer.wkbType()), source_layer.crs(), context) # first build up a list of clip geometries clip_geoms = [] for maskFeat in QgsProcessingUtils.getFeatures( mask_layer, context, QgsFeatureRequest().setSubsetOfAttributes([])): clip_geoms.append(maskFeat.geometry()) # are we clipping against a single feature? if so, we can show finer progress reports if len(clip_geoms) > 1: combined_clip_geom = QgsGeometry.unaryUnion(clip_geoms) single_clip_feature = False else: combined_clip_geom = clip_geoms[0] single_clip_feature = True # use prepared geometries for faster intersection tests engine = QgsGeometry.createGeometryEngine( combined_clip_geom.geometry()) engine.prepareGeometry() tested_feature_ids = set() for i, clip_geom in enumerate(clip_geoms): input_features = [ f for f in QgsProcessingUtils.getFeatures( source_layer, context, QgsFeatureRequest().setFilterRect(clip_geom.boundingBox())) ] if not input_features: continue if single_clip_feature: total = 100.0 / len(input_features) else: total = 0 for current, in_feat in enumerate(input_features): if not in_feat.geometry(): continue if in_feat.id() in tested_feature_ids: # don't retest a feature we have already checked continue tested_feature_ids.add(in_feat.id()) if not engine.intersects(in_feat.geometry().geometry()): continue if not engine.contains(in_feat.geometry().geometry()): cur_geom = in_feat.geometry() new_geom = combined_clip_geom.intersection(cur_geom) if new_geom.wkbType( ) == QgsWkbTypes.Unknown or QgsWkbTypes.flatType( new_geom.geometry().wkbType( )) == QgsWkbTypes.GeometryCollection: int_com = in_feat.geometry().combine(new_geom) int_sym = in_feat.geometry().symDifference(new_geom) new_geom = int_com.difference(int_sym) else: # clip geometry totally contains feature geometry, so no need to perform intersection new_geom = in_feat.geometry() try: out_feat = QgsFeature() out_feat.setGeometry(new_geom) out_feat.setAttributes(in_feat.attributes()) writer.addFeature(out_feat) except: QgsMessageLog.logMessage( self.tr('Feature geometry error: One or more ' 'output features ignored due to ' 'invalid geometry.'), self.tr('Processing'), QgsMessageLog.CRITICAL) continue if single_clip_feature: feedback.setProgress(int(current * total)) if not single_clip_feature: # coarse progress report for multiple clip geometries feedback.setProgress(100.0 * i / len(clip_geoms)) del writer
def processAlgorithm(self, parameters, context, feedback): commands = list() self.exportedLayers = {} self.preProcessInputs() extent = None crs = None # 1: Export rasters to sgrd and vectors to shp # Tables must be in dbf format. We check that. for param in self.parameterDefinitions(): if isinstance(param, QgsProcessingParameterRasterLayer): if param.name() not in parameters or parameters[param.name()] is None: continue if isinstance(parameters[param.name()], str): if parameters[param.name()].lower().endswith('sdat'): self.exportedLayers[param.name()] = parameters[param.name()][:-4] + 'sgrd' elif parameters[param.name()].lower().endswith('sgrd'): self.exportedLayers[param.name()] = parameters[param.name()] else: layer = self.parameterAsRasterLayer(parameters, param.name(), context) exportCommand = self.exportRasterLayer(param.name(), layer) if exportCommand is not None: commands.append(exportCommand) else: if parameters[param.name()].source().lower().endswith('sdat'): self.exportedLayers[param.name()] = parameters[param.name()].source()[:-4] + 'sgrd' if parameters[param.name()].source().lower().endswith('sgrd'): self.exportedLayers[param.name()] = parameters[param.name()].source() else: exportCommand = self.exportRasterLayer(param.name(), parameters[param.name()]) if exportCommand is not None: commands.append(exportCommand) elif isinstance(param, QgsProcessingParameterFeatureSource): if param.name() not in parameters or parameters[param.name()] is None: continue if not crs: source = self.parameterAsSource(parameters, param.name(), context) if source is None: raise QgsProcessingException(self.invalidSourceError(parameters, param.name())) crs = source.sourceCrs() layer_path = self.parameterAsCompatibleSourceLayerPath(parameters, param.name(), context, ['shp'], 'shp', feedback=feedback) if layer_path: self.exportedLayers[param.name()] = layer_path else: raise QgsProcessingException( self.tr('Unsupported file format')) elif isinstance(param, QgsProcessingParameterMultipleLayers): if param.name() not in parameters or parameters[param.name()] is None: continue layers = self.parameterAsLayerList(parameters, param.name(), context) if layers is None or len(layers) == 0: continue if param.layerType() == QgsProcessing.TypeRaster: files = [] for i, layer in enumerate(layers): if layer.source().lower().endswith('sdat'): files.append(layer.source()[:-4] + 'sgrd') if layer.source().lower().endswith('sgrd'): files.append(layer.source()) else: exportCommand = self.exportRasterLayer(param.name(), layer) files.append(self.exportedLayers[param.name()]) if exportCommand is not None: commands.append(exportCommand) self.exportedLayers[param.name()] = files else: for layer in layers: temp_params = {} temp_params[param.name()] = layer if not crs: source = self.parameterAsSource(temp_params, param.name(), context) if source is None: raise QgsProcessingException(self.invalidSourceError(parameters, param.name())) crs = source.sourceCrs() layer_path = self.parameterAsCompatibleSourceLayerPath(temp_params, param.name(), context, ['shp'], 'shp', feedback=feedback) if layer_path: if param.name() in self.exportedLayers: self.exportedLayers[param.name()].append(layer_path) else: self.exportedLayers[param.name()] = [layer_path] else: raise QgsProcessingException( self.tr('Unsupported file format')) # 2: Set parameters and outputs command = self.undecorated_group + ' "' + self.cmdname + '"' command += ' ' + ' '.join(self.hardcoded_strings) for param in self.parameterDefinitions(): if not param.name() in parameters or parameters[param.name()] is None: continue if param.isDestination(): continue if isinstance(param, (QgsProcessingParameterRasterLayer, QgsProcessingParameterFeatureSource)): command += ' -{} "{}"'.format(param.name(), self.exportedLayers[param.name()]) elif isinstance(param, QgsProcessingParameterMultipleLayers): if parameters[param.name()]: # parameter may have been an empty list command += ' -{} "{}"'.format(param.name(), ';'.join(self.exportedLayers[param.name()])) elif isinstance(param, QgsProcessingParameterBoolean): if self.parameterAsBool(parameters, param.name(), context): command += ' -{} true'.format(param.name().strip()) else: command += ' -{} false'.format(param.name().strip()) elif isinstance(param, QgsProcessingParameterMatrix): tempTableFile = getTempFilename('txt') with open(tempTableFile, 'w') as f: f.write('\t'.join([col for col in param.headers()]) + '\n') values = self.parameterAsMatrix(parameters, param.name(), context) for i in range(0, len(values), 3): s = '{}\t{}\t{}\n'.format(values[i], values[i + 1], values[i + 2]) f.write(s) command += ' -{} "{}"'.format(param.name(), tempTableFile) elif isinstance(param, QgsProcessingParameterExtent): # 'We have to substract/add half cell size, since SAGA is # center based, not corner based halfcell = self.getOutputCellsize(parameters, context) / 2 offset = [halfcell, -halfcell, halfcell, -halfcell] rect = self.parameterAsExtent(parameters, param.name(), context) values = [] values.append(rect.xMinimum()) values.append(rect.xMaximum()) values.append(rect.yMinimum()) values.append(rect.yMaximum()) for i in range(4): command += ' -{} {}'.format(param.name().split(' ')[i], float(values[i]) + offset[i]) elif isinstance(param, QgsProcessingParameterNumber): if param.dataType() == QgsProcessingParameterNumber.Integer: command += ' -{} {}'.format(param.name(), self.parameterAsInt(parameters, param.name(), context)) else: command += ' -{} {}'.format(param.name(), self.parameterAsDouble(parameters, param.name(), context)) elif isinstance(param, QgsProcessingParameterEnum): command += ' -{} {}'.format(param.name(), self.parameterAsEnum(parameters, param.name(), context)) elif isinstance(param, (QgsProcessingParameterString, QgsProcessingParameterFile)): command += ' -{} "{}"'.format(param.name(), self.parameterAsFile(parameters, param.name(), context)) elif isinstance(param, (QgsProcessingParameterString, QgsProcessingParameterField)): command += ' -{} "{}"'.format(param.name(), self.parameterAsString(parameters, param.name(), context)) output_layers = [] output_files = {} #If the user has entered an output file that has non-ascii chars, we use a different path with only ascii chars output_files_nonascii = {} for out in self.destinationParameterDefinitions(): filePath = self.parameterAsOutputLayer(parameters, out.name(), context) if isinstance(out, (QgsProcessingParameterRasterDestination, QgsProcessingParameterVectorDestination)): output_layers.append(filePath) try: filePath.encode('ascii') except UnicodeEncodeError: nonAsciiFilePath = filePath filePath = QgsProcessingUtils.generateTempFilename(out.name() + os.path.splitext(filePath)[1]) output_files_nonascii[filePath] = nonAsciiFilePath output_files[out.name()] = filePath command += ' -{} "{}"'.format(out.name(), filePath) commands.append(command) # special treatment for RGB algorithm # TODO: improve this and put this code somewhere else for out in self.destinationParameterDefinitions(): if isinstance(out, QgsProcessingParameterRasterDestination): filename = self.parameterAsOutputLayer(parameters, out.name(), context) filename2 = os.path.splitext(filename)[0] + '.sgrd' if self.cmdname == 'RGB Composite': commands.append('io_grid_image 0 -COLOURING 4 -GRID:"{}" -FILE:"{}"'.format(filename2, filename)) # 3: Run SAGA commands = self.editCommands(commands) SagaUtils.createSagaBatchJobFileFromSagaCommands(commands) loglines = [] loglines.append(self.tr('SAGA execution commands')) for line in commands: feedback.pushCommandInfo(line) loglines.append(line) if ProcessingConfig.getSetting(SagaUtils.SAGA_LOG_COMMANDS): QgsMessageLog.logMessage('\n'.join(loglines), self.tr('Processing'), Qgis.Info) SagaUtils.executeSaga(feedback) if crs is not None: for out in output_layers: prjFile = os.path.splitext(out)[0] + '.prj' with open(prjFile, 'w') as f: f.write(crs.toWkt()) for old, new in output_files_nonascii.items(): oldFolder = os.path.dirname(old) newFolder = os.path.dirname(new) newName = os.path.splitext(os.path.basename(new))[0] files = [f for f in os.listdir(oldFolder)] for f in files: ext = os.path.splitext(f)[1] newPath = os.path.join(newFolder, newName + ext) oldPath = os.path.join(oldFolder, f) shutil.move(oldPath, newPath) result = {} for o in self.outputDefinitions(): if o.name() in output_files: result[o.name()] = output_files[o.name()] return result
def processCommand(self, parameters, context, delOutputs=False): """ Prepare the GRASS algorithm command :param parameters: :param context: :param delOutputs: do not add outputs to commands. """ noOutputs = [ o for o in self.parameterDefinitions() if o not in self.destinationParameterDefinitions() ] command = '{} '.format(self.grass7Name) command += '{}'.join(self.hardcodedStrings) # Add algorithm command for param in noOutputs: paramName = param.name() value = None # Exclude default GRASS parameters if paramName in [ self.GRASS_REGION_CELLSIZE_PARAMETER, self.GRASS_REGION_EXTENT_PARAMETER, self.GRASS_MIN_AREA_PARAMETER, self.GRASS_SNAP_TOLERANCE_PARAMETER, self.GRASS_OUTPUT_TYPE_PARAMETER, self.GRASS_REGION_ALIGN_TO_RESOLUTION, self.GRASS_RASTER_FORMAT_OPT, self.GRASS_RASTER_FORMAT_META ]: continue # Raster and vector layers if isinstance(param, (QgsProcessingParameterRasterLayer, QgsProcessingParameterVectorLayer)): if paramName in self.exportedLayers: value = self.exportedLayers[paramName] else: value = self.parameterAsCompatibleSourceLayerPath( parameters, paramName, context, QgsVectorFileWriter.supportedFormatExtensions()) # MultipleLayers elif isinstance(param, QgsProcessingParameterMultipleLayers): layers = self.parameterAsLayerList(parameters, paramName, context) values = [] for idx in range(len(layers)): layerName = '{}_{}'.format(paramName, idx) values.append(self.exportedLayers[layerName]) value = ','.join(values) # For booleans, we just add the parameter name elif isinstance(param, QgsProcessingParameterBoolean): if self.parameterAsBool(parameters, paramName, context): command += ' {}'.format(paramName) # For Extents, remove if the value is null elif isinstance(param, QgsProcessingParameterExtent): if self.parameterAsExtent(parameters, paramName, context): value = self.parameterAsString(parameters, paramName, context) # For enumeration, we need to grab the string value elif isinstance(param, QgsProcessingParameterEnum): # Handle multiple values if param.allowMultiple(): indexes = self.parameterAsEnums(parameters, paramName, context) else: indexes = [ self.parameterAsEnum(parameters, paramName, context) ] if indexes: value = '"{}"'.format(','.join( [param.options()[i] for i in indexes])) # For strings, we just translate as string elif isinstance(param, QgsProcessingParameterString): data = self.parameterAsString(parameters, paramName, context) # if string is empty, we don't add it if len(data) > 0: value = '"{}"'.format( self.parameterAsString(parameters, paramName, context)) # For fields, we just translate as string elif isinstance(param, QgsProcessingParameterField): value = ','.join( self.parameterAsFields(parameters, paramName, context)) elif isinstance(param, QgsProcessingParameterFile): if self.parameterAsString(parameters, paramName, context): value = '"{}"'.format( self.parameterAsString(parameters, paramName, context)) # For numbers and points, we translate as a string elif isinstance( param, (QgsProcessingParameterNumber, QgsProcessingParameterPoint)): value = self.parameterAsString(parameters, paramName, context) # For everything else, we assume that it is a string else: value = '"{}"'.format( self.parameterAsString(parameters, paramName, context)) if value: command += ' {}={}'.format(paramName, value) # Handle outputs if not delOutputs: for out in self.destinationParameterDefinitions(): # We exclude hidden parameters if out.flags() & QgsProcessingParameterDefinition.FlagHidden: continue outName = out.name() # For File destination if isinstance(out, QgsProcessingParameterFileDestination): # for HTML reports, we need to redirect stdout if out.defaultFileExtension().lower() == 'html': command += ' > "{}"'.format( self.parameterAsFileOutput(parameters, outName, context)) else: command += ' {}="{}"'.format( outName, self.parameterAsFileOutput(parameters, outName, context)) # For folders destination elif isinstance(out, QgsProcessingParameterFolderDestination): # We need to add a unique temporary basename uniqueBasename = outName + self.uniqueSuffix command += ' {}={}'.format(outName, uniqueBasename) else: # We add an output name to make sure it is unique if the session # uses this algorithm several times. #value = self.parameterAsOutputLayer(parameters, outName, context) uniqueOutputName = outName + self.uniqueSuffix command += ' {}={}'.format(outName, uniqueOutputName) # Add output file to exported layers, to indicate that # they are present in GRASS self.exportedLayers[outName] = uniqueOutputName command += ' --overwrite' self.commands.append(command) QgsMessageLog.logMessage( 'processCommands end. Commands: {}'.format(self.commands), 'Grass7', QgsMessageLog.INFO)
def processAlgorithm(self, parameters, context, feedback): if isWindows(): path = Grass7Utils.grassPath() if path == '': raise QgsProcessingException( self.tr( 'GRASS GIS 7 folder is not configured. Please ' 'configure it before running GRASS GIS 7 algorithms.')) # Create brand new commands lists self.commands = [] self.outputCommands = [] self.exportedLayers = {} # If GRASS session has been created outside of this algorithm then # get the list of layers loaded in GRASS otherwise start a new # session existingSession = Grass7Utils.sessionRunning if existingSession: self.exportedLayers = Grass7Utils.getSessionLayers() else: Grass7Utils.startGrassSession() # Handle default GRASS parameters self.grabDefaultGrassParameters(parameters, context) # Handle ext functions for inputs/command/outputs for fName in ['Inputs', 'Command', 'Outputs']: fullName = 'process{}'.format(fName) if self.module and hasattr(self.module, fullName): getattr(self.module, fullName)(self, parameters, context) else: getattr(self, fullName)(parameters, context) # Run GRASS loglines = [] loglines.append(self.tr('GRASS GIS 7 execution commands')) for line in self.commands: feedback.pushCommandInfo(line) loglines.append(line) if ProcessingConfig.getSetting(Grass7Utils.GRASS_LOG_COMMANDS): QgsMessageLog.logMessage("\n".join(loglines), self.tr('Processing'), QgsMessageLog.INFO) Grass7Utils.executeGrass(self.commands, feedback, self.outputCommands) # If the session has been created outside of this algorithm, add # the new GRASS GIS 7 layers to it otherwise finish the session if existingSession: Grass7Utils.addSessionLayers(self.exportedLayers) else: Grass7Utils.endGrassSession() # Return outputs map outputs = {} for out in self.outputDefinitions(): outName = out.name() if outName in parameters: outputs[outName] = parameters[outName] if isinstance(out, QgsProcessingOutputHtml): self.convertToHtml(parameters[outName]) return outputs
def defineCharacteristicsFromFile(self): """ Create algorithm parameters and outputs from a text file. """ with open(self.descriptionFile) as lines: # First line of the file is the Grass algorithm name line = lines.readline().strip('\n').strip() self.grass7Name = line # Second line if the algorithm name in Processing line = lines.readline().strip('\n').strip() self._name = line self._display_name = QCoreApplication.translate( "GrassAlgorithm", line) if " - " not in self._name: self._name = self.grass7Name + " - " + self._name self._display_name = self.grass7Name + " - " + self._display_name self._name = self._name[:self._name.find(' ')].lower() # Read the grass group line = lines.readline().strip('\n').strip() self._group = QCoreApplication.translate("GrassAlgorithm", line) self._groupId = self.groupIdRegex.search(line).group(0).lower() hasRasterOutput = False hasRasterInput = False hasVectorInput = False vectorOutputs = False # Then you have parameters/output definition line = lines.readline().strip('\n').strip() while line != '': try: line = line.strip('\n').strip() if line.startswith('Hardcoded'): self.hardcodedStrings.append(line[len('Hardcoded|'):]) parameter = getParameterFromString(line) if parameter is not None: self.params.append(parameter) if isinstance(parameter, QgsProcessingParameterVectorLayer): hasVectorInput = True elif isinstance(parameter, QgsProcessingParameterRasterLayer): hasRasterInput = True elif isinstance(parameter, QgsProcessingParameterMultipleLayers): if parameter.layerType( ) < 3 or parameter.layerType() == 5: hasVectorInput = True elif parameter.layerType() == 3: hasRasterInput = True elif isinstance( parameter, QgsProcessingParameterVectorDestination): vectorOutputs = True elif isinstance( parameter, QgsProcessingParameterRasterDestination): hasRasterOutput = True line = lines.readline().strip('\n').strip() except Exception as e: QgsMessageLog.logMessage( self.tr( 'Could not open GRASS GIS 7 algorithm: {0}\n{1}'). format(self.descriptionFile, line), self.tr('Processing'), QgsMessageLog.CRITICAL) raise e param = QgsProcessingParameterExtent( self.GRASS_REGION_EXTENT_PARAMETER, self.tr('GRASS GIS 7 region extent'), optional=True) param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.params.append(param) if hasRasterOutput or hasRasterInput: # Add a cellsize parameter param = QgsProcessingParameterNumber( self.GRASS_REGION_CELLSIZE_PARAMETER, self.tr('GRASS GIS 7 region cellsize (leave 0 for default)'), type=QgsProcessingParameterNumber.Double, minValue=0.0, maxValue=sys.float_info.max + 1, defaultValue=0.0) param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.params.append(param) if hasRasterOutput: # Add a createopt parameter for format export param = QgsProcessingParameterString( self.GRASS_RASTER_FORMAT_OPT, self.tr('Output Rasters format options (createopt)'), multiLine=True, optional=True) param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.params.append(param) # Add a metadata parameter for format export param = QgsProcessingParameterString( self.GRASS_RASTER_FORMAT_META, self.tr('Output Rasters format metadata options (metaopt)'), multiLine=True, optional=True) param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.params.append(param) if hasVectorInput: param = QgsProcessingParameterNumber( self.GRASS_SNAP_TOLERANCE_PARAMETER, self.tr('v.in.ogr snap tolerance (-1 = no snap)'), type=QgsProcessingParameterNumber.Double, minValue=-1.0, maxValue=sys.float_info.max + 1, defaultValue=-1.0) param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.params.append(param) param = QgsProcessingParameterNumber( self.GRASS_MIN_AREA_PARAMETER, self.tr('v.in.ogr min area'), type=QgsProcessingParameterNumber.Double, minValue=0.0, maxValue=sys.float_info.max + 1, defaultValue=0.0001) param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.params.append(param) if vectorOutputs: param = QgsProcessingParameterEnum( self.GRASS_OUTPUT_TYPE_PARAMETER, self.tr('v.out.ogr output type'), self.OUTPUT_TYPES) param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.params.append(param)
def runGdal(commands, feedback=None): if feedback is None: feedback = QgsProcessingFeedback() envval = os.getenv('PATH') # We need to give some extra hints to get things picked up on OS X isDarwin = False try: isDarwin = platform.system() == 'Darwin' except IOError: # https://travis-ci.org/m-kuhn/QGIS#L1493-L1526 pass if isDarwin and os.path.isfile( os.path.join(QgsApplication.prefixPath(), "bin", "gdalinfo")): # Looks like there's a bundled gdal. Let's use it. os.environ['PATH'] = "{}{}{}".format( os.path.join(QgsApplication.prefixPath(), "bin"), os.pathsep, envval) os.environ['DYLD_LIBRARY_PATH'] = os.path.join( QgsApplication.prefixPath(), "lib") else: # Other platforms should use default gdal finder codepath settings = QgsSettings() path = settings.value('/GdalTools/gdalPath', '') if not path.lower() in envval.lower().split(os.pathsep): envval += '{}{}'.format(os.pathsep, path) os.putenv('PATH', envval) fused_command = ' '.join([str(c) for c in commands]) QgsMessageLog.logMessage(fused_command, 'Processing', QgsMessageLog.INFO) feedback.pushInfo('GDAL command:') feedback.pushCommandInfo(fused_command) feedback.pushInfo('GDAL command output:') success = False retry_count = 0 while not success: loglines = [] loglines.append('GDAL execution console output') try: with subprocess.Popen( fused_command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.DEVNULL, stderr=subprocess.STDOUT, universal_newlines=True, ) as proc: for line in proc.stdout: feedback.pushConsoleInfo(line) loglines.append(line) success = True except IOError as e: if retry_count < 5: retry_count += 1 else: raise IOError( str(e) + u'\nTried 5 times without success. Last iteration stopped after reading {} line(s).\nLast line(s):\n{}' .format(len(loglines), u'\n'.join(loglines[-10:]))) QgsMessageLog.logMessage('\n'.join(loglines), 'Processing', QgsMessageLog.INFO) GdalUtils.consoleOutput = loglines
def logMessage(message): try: QgsMessageLog.logMessage(str(message), "Qgis2threejs") except: pass
def show_err_msgbar(self, err): self.iface.messageBar().pushMessage(TAG_PLUGIN, repr(err), Qgis.Warning, 5) msg = format_traceback(err) QgsMessageLog.logMessage(msg, TAG_PLUGIN, Qgis.Warning)
def defineCharacteristicsFromFile(self): with open(self.descriptionFile) as lines: line = lines.readline().strip('\n').strip() self.grass7Name = line line = lines.readline().strip('\n').strip() self._name = line self._display_name = QCoreApplication.translate( "GrassAlgorithm", line) if " - " not in self._name: self._name = self.grass7Name + " - " + self._name self._display_name = self.grass7Name + " - " + self._display_name self._name = self._name[:self._name.find(' ')].lower() line = lines.readline().strip('\n').strip() self._group = QCoreApplication.translate("GrassAlgorithm", line) hasRasterOutput = False hasVectorInput = False vectorOutputs = 0 line = lines.readline().strip('\n').strip() while line != '': try: line = line.strip('\n').strip() if line.startswith('Hardcoded'): self.hardcodedStrings.append(line[len('Hardcoded|'):]) parameter = getParameterFromString(line) if parameter is not None: self.addParameter(parameter) if isinstance(parameter, ParameterVector): hasVectorInput = True if isinstance(parameter, ParameterMultipleInput) \ and parameter.datatype < 3: hasVectorInput = True else: output = getOutputFromString(line) self.addOutput(output) if isinstance(output, OutputRaster): hasRasterOutput = True elif isinstance(output, OutputVector): vectorOutputs += 1 if isinstance(output, OutputHTML): self.addOutput( OutputFile( "rawoutput", self.tr("{0} (raw output)").format( output.description()), "txt")) line = lines.readline().strip('\n').strip() except Exception as e: QgsMessageLog.logMessage( self.tr( 'Could not open GRASS GIS 7 algorithm: {0}\n{1}'). format(self.descriptionFile, line), self.tr('Processing'), QgsMessageLog.CRITICAL) raise e self.addParameter( ParameterExtent(self.GRASS_REGION_EXTENT_PARAMETER, self.tr('GRASS GIS 7 region extent'))) if hasRasterOutput: self.addParameter( ParameterNumber( self.GRASS_REGION_CELLSIZE_PARAMETER, self.tr( 'GRASS GIS 7 region cellsize (leave 0 for default)'), 0, None, 0.0)) if hasVectorInput: param = ParameterNumber(self.GRASS_SNAP_TOLERANCE_PARAMETER, 'v.in.ogr snap tolerance (-1 = no snap)', -1, None, -1.0) param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(param) param = ParameterNumber(self.GRASS_MIN_AREA_PARAMETER, 'v.in.ogr min area', 0, None, 0.0001) param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(param) if vectorOutputs == 1: param = ParameterSelection(self.GRASS_OUTPUT_TYPE_PARAMETER, 'v.out.ogr output type', self.OUTPUT_TYPES) param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced) self.addParameter(param)
def sendResponse(self): QgsMessageLog.logMessage("SimpleHelloFilter.sendResponse")
def handleAlgorithmResults(alg, context, feedback=None, showResults=True): wrongLayers = [] if feedback is None: feedback = QgsProcessingFeedback() feedback.setProgressText( QCoreApplication.translate('Postprocessing', 'Loading resulting layers')) i = 0 for l, details in context.layersToLoadOnCompletion().items(): if feedback.isCanceled(): return False if len(context.layersToLoadOnCompletion()) > 2: # only show progress feedback if we're loading a bunch of layers feedback.setProgress( 100 * i / float(len(context.layersToLoadOnCompletion()))) try: layer = QgsProcessingUtils.mapLayerFromString( l, context, typeHint=details.layerTypeHint) if layer is not None: set_layer_name(layer, details) style = None if details.outputName: style = RenderingStyles.getStyle(alg.id(), details.outputName) if style is None: if layer.type() == QgsMapLayer.RasterLayer: style = ProcessingConfig.getSetting( ProcessingConfig.RASTER_STYLE) else: if layer.geometryType() == QgsWkbTypes.PointGeometry: style = ProcessingConfig.getSetting( ProcessingConfig.VECTOR_POINT_STYLE) elif layer.geometryType() == QgsWkbTypes.LineGeometry: style = ProcessingConfig.getSetting( ProcessingConfig.VECTOR_LINE_STYLE) else: style = ProcessingConfig.getSetting( ProcessingConfig.VECTOR_POLYGON_STYLE) if style: layer.loadNamedStyle(style) details.project.addMapLayer( context.temporaryLayerStore().takeMapLayer(layer)) if details.postProcessor(): details.postProcessor().postProcessLayer( layer, context, feedback) else: wrongLayers.append(str(l)) except Exception: QgsMessageLog.logMessage( QCoreApplication.translate( 'Postprocessing', "Error loading result layer:") + "\n" + traceback.format_exc(), 'Processing', Qgis.Critical) wrongLayers.append(str(l)) i += 1 feedback.setProgress(100) if wrongLayers: msg = QCoreApplication.translate( 'Postprocessing', "The following layers were not correctly generated.") msg += "<ul>" + "".join(["<li>%s</li>" % lay for lay in wrongLayers]) + "</ul>" msg += QCoreApplication.translate( 'Postprocessing', "You can check the 'Log Messages Panel' in QGIS main window to find more information about the execution of the algorithm." ) feedback.reportError(msg) return len(wrongLayers) == 0
def executeGrass(commands, feedback, outputCommands=None): loglines = [] loglines.append(Grass7Utils.tr('GRASS GIS 7 execution console output')) grassOutDone = False command, grassenv = Grass7Utils.prepareGrassExecution(commands) # QgsMessageLog.logMessage('exec: {}'.format(command), 'DEBUG', Qgis.Info) # For MS-Windows, we need to hide the console window. kw = {} if isWindows(): si = subprocess.STARTUPINFO() si.dwFlags |= subprocess.STARTF_USESHOWWINDOW si.wShowWindow = subprocess.SW_HIDE kw['startupinfo'] = si if sys.version_info >= (3, 6): kw['encoding'] = "cp{}".format( Grass7Utils.getWindowsCodePage()) with subprocess.Popen(command, shell=False, stdout=subprocess.PIPE, stdin=subprocess.DEVNULL, stderr=subprocess.STDOUT, universal_newlines=True, env=grassenv, **kw) as proc: for line in iter(proc.stdout.readline, ''): if 'GRASS_INFO_PERCENT' in line: try: feedback.setProgress( int(line[len('GRASS_INFO_PERCENT') + 2:])) except: pass else: if 'r.out' in line or 'v.out' in line: grassOutDone = True loglines.append(line) if any([l in line for l in ['WARNING', 'ERROR']]): feedback.reportError(line.strip()) elif 'Segmentation fault' in line: feedback.reportError(line.strip()) feedback.reportError('\n' + Grass7Utils.tr( 'GRASS command crashed :( Try a different set of input parameters and consult the GRASS algorithm manual for more information.' ) + '\n') if ProcessingConfig.getSetting( Grass7Utils.GRASS_USE_VEXTERNAL): feedback.reportError( Grass7Utils. tr('Suggest disabling the experimental "use v.external" option from the Processing GRASS Provider options.' ) + '\n') elif line.strip(): feedback.pushConsoleInfo(line.strip()) # Some GRASS scripts, like r.mapcalculator or r.fillnulls, call # other GRASS scripts during execution. This may override any # commands that are still to be executed by the subprocess, which # are usually the output ones. If that is the case runs the output # commands again. if not grassOutDone and outputCommands: command, grassenv = Grass7Utils.prepareGrassExecution( outputCommands) # For MS-Windows, we need to hide the console window. kw = {} if isWindows(): si = subprocess.STARTUPINFO() si.dwFlags |= subprocess.STARTF_USESHOWWINDOW si.wShowWindow = subprocess.SW_HIDE kw['startupinfo'] = si if sys.version_info >= (3, 6): kw['encoding'] = "cp{}".format( Grass7Utils.getWindowsCodePage()) with subprocess.Popen(command, shell=False, stdout=subprocess.PIPE, stdin=subprocess.DEVNULL, stderr=subprocess.STDOUT, universal_newlines=True, env=grassenv, **kw) as proc: for line in iter(proc.stdout.readline, ''): if 'GRASS_INFO_PERCENT' in line: try: feedback.setProgress( int(line[len('GRASS_INFO_PERCENT') + 2:])) except: pass if any([l in line for l in ['WARNING', 'ERROR']]): loglines.append(line.strip()) feedback.reportError(line.strip()) elif line.strip(): loglines.append(line.strip()) feedback.pushConsoleInfo(line.strip()) if ProcessingConfig.getSetting(Grass7Utils.GRASS_LOG_CONSOLE): QgsMessageLog.logMessage('\n'.join(loglines), 'Processing', Qgis.Info)
def execute_in_place_run(alg, parameters, context=None, feedback=None, raise_exceptions=False): """Executes an algorithm modifying features in-place in the input layer. :param alg: algorithm to run :type alg: QgsProcessingAlgorithm :param parameters: parameters of the algorithm :type parameters: dict :param context: context, defaults to None :type context: QgsProcessingContext, optional :param feedback: feedback, defaults to None :type feedback: QgsProcessingFeedback, optional :param raise_exceptions: useful for testing, if True exceptions are raised, normally exceptions will be forwarded to the feedback :type raise_exceptions: boo, default to False :raises QgsProcessingException: raised when there is no active layer, or it cannot be made editable :return: a tuple with true if success and results :rtype: tuple """ if feedback is None: feedback = QgsProcessingFeedback() if context is None: context = dataobjects.createContext(feedback) # Only feature based algs have sourceFlags try: if alg.sourceFlags() & QgsProcessingFeatureSource.FlagSkipGeometryValidityChecks: context.setInvalidGeometryCheck(QgsFeatureRequest.GeometryNoCheck) except AttributeError: pass active_layer = parameters['INPUT'] # Run some checks and prepare the layer for in-place execution by: # - getting the active layer and checking that it is a vector # - making the layer editable if it was not already # - selecting all features if none was selected # - checking in-place support for the active layer/alg/parameters # If one of the check fails and raise_exceptions is True an exception # is raised, else the execution is aborted and the error reported in # the feedback try: if active_layer is None: raise QgsProcessingException(tr("There is not active layer.")) if not isinstance(active_layer, QgsVectorLayer): raise QgsProcessingException(tr("Active layer is not a vector layer.")) if not active_layer.isEditable(): if not active_layer.startEditing(): raise QgsProcessingException(tr("Active layer is not editable (and editing could not be turned on).")) if not alg.supportInPlaceEdit(active_layer): raise QgsProcessingException(tr("Selected algorithm and parameter configuration are not compatible with in-place modifications.")) except QgsProcessingException as e: if raise_exceptions: raise e QgsMessageLog.logMessage(str(sys.exc_info()[0]), 'Processing', Qgis.Critical) if feedback is not None: feedback.reportError(getattr(e, 'msg', str(e)), fatalError=True) return False, {} if not active_layer.selectedFeatureIds(): active_layer.selectAll() # Make sure we are working on selected features only parameters['INPUT'] = QgsProcessingFeatureSourceDefinition(active_layer.id(), True) parameters['OUTPUT'] = 'memory:' req = QgsFeatureRequest(QgsExpression(r"$id < 0")) req.setFlags(QgsFeatureRequest.NoGeometry) req.setSubsetOfAttributes([]) # Start the execution # If anything goes wrong and raise_exceptions is True an exception # is raised, else the execution is aborted and the error reported in # the feedback try: new_feature_ids = [] active_layer.beginEditCommand(alg.displayName()) # Checks whether the algorithm has a processFeature method if hasattr(alg, 'processFeature'): # in-place feature editing # Make a clone or it will crash the second time the dialog # is opened and run alg = alg.create() if not alg.prepare(parameters, context, feedback): raise QgsProcessingException(tr("Could not prepare selected algorithm.")) # Check again for compatibility after prepare if not alg.supportInPlaceEdit(active_layer): raise QgsProcessingException(tr("Selected algorithm and parameter configuration are not compatible with in-place modifications.")) # some algorithms have logic in outputFields/outputCrs/outputWkbType which they require to execute before # they can start processing features _ = alg.outputFields(active_layer.fields()) _ = alg.outputWkbType(active_layer.wkbType()) _ = alg.outputCrs(active_layer.crs()) field_idxs = range(len(active_layer.fields())) iterator_req = QgsFeatureRequest(active_layer.selectedFeatureIds()) iterator_req.setInvalidGeometryCheck(context.invalidGeometryCheck()) feature_iterator = active_layer.getFeatures(iterator_req) step = 100 / len(active_layer.selectedFeatureIds()) if active_layer.selectedFeatureIds() else 1 for current, f in enumerate(feature_iterator): if feedback.isCanceled(): break # need a deep copy, because python processFeature implementations may return # a shallow copy from processFeature input_feature = QgsFeature(f) new_features = alg.processFeature(input_feature, context, feedback) new_features = QgsVectorLayerUtils.makeFeaturesCompatible(new_features, active_layer) if len(new_features) == 0: active_layer.deleteFeature(f.id()) elif len(new_features) == 1: new_f = new_features[0] if not f.geometry().equals(new_f.geometry()): active_layer.changeGeometry(f.id(), new_f.geometry()) if f.attributes() != new_f.attributes(): active_layer.changeAttributeValues(f.id(), dict(zip(field_idxs, new_f.attributes())), dict(zip(field_idxs, f.attributes()))) new_feature_ids.append(f.id()) else: active_layer.deleteFeature(f.id()) # Get the new ids old_ids = set([f.id() for f in active_layer.getFeatures(req)]) # If multiple new features were created, we need to pass # them to createFeatures to manage constraints correctly features_data = [] for f in new_features: features_data.append(QgsVectorLayerUtils.QgsFeatureData(f.geometry(), dict(enumerate(f.attributes())))) new_features = QgsVectorLayerUtils.createFeatures(active_layer, features_data, context.expressionContext()) if not active_layer.addFeatures(new_features): raise QgsProcessingException(tr("Error adding processed features back into the layer.")) new_ids = set([f.id() for f in active_layer.getFeatures(req)]) new_feature_ids += list(new_ids - old_ids) feedback.setProgress(int((current + 1) * step)) results, ok = {}, True else: # Traditional 'run' with delete and add features cycle # There is no way to know if some features have been skipped # due to invalid geometries if context.invalidGeometryCheck() == QgsFeatureRequest.GeometrySkipInvalid: selected_ids = active_layer.selectedFeatureIds() else: selected_ids = [] results, ok = alg.run(parameters, context, feedback) if ok: result_layer = QgsProcessingUtils.mapLayerFromString(results['OUTPUT'], context) # TODO: check if features have changed before delete/add cycle new_features = [] # Check if there are any skipped features if context.invalidGeometryCheck() == QgsFeatureRequest.GeometrySkipInvalid: missing_ids = list(set(selected_ids) - set(result_layer.allFeatureIds())) if missing_ids: for f in active_layer.getFeatures(QgsFeatureRequest(missing_ids)): if not f.geometry().isGeosValid(): new_features.append(f) active_layer.deleteFeatures(active_layer.selectedFeatureIds()) for f in result_layer.getFeatures(): new_features.extend(QgsVectorLayerUtils. makeFeaturesCompatible([f], active_layer)) # Get the new ids old_ids = set([f.id() for f in active_layer.getFeatures(req)]) if not active_layer.addFeatures(new_features): raise QgsProcessingException(tr("Error adding processed features back into the layer.")) new_ids = set([f.id() for f in active_layer.getFeatures(req)]) new_feature_ids += list(new_ids - old_ids) active_layer.endEditCommand() if ok and new_feature_ids: active_layer.selectByIds(new_feature_ids) elif not ok: active_layer.rollBack() return ok, results except QgsProcessingException as e: active_layer.endEditCommand() active_layer.rollBack() if raise_exceptions: raise e QgsMessageLog.logMessage(str(sys.exc_info()[0]), 'Processing', Qgis.Critical) if feedback is not None: feedback.reportError(getattr(e, 'msg', str(e)), fatalError=True) return False, {}
def WoE(self):######################calculate W+,W-,Wf print('WoE') ################################################ idx=[] idx1=[] idx2=[] idx3=[] idx=np.where(np.isnan(self.catalog)) #print(len(self.catalog[self.catalog>-9999]),'catalog0') self.catalog[idx]=-9999 ############################################### product=np.array([]) diff=np.array([]) #print(len(self.catalog[self.catalog>-9999]),'catalog') #print(len(self.Raster[self.Raster>-9999]),'raster') product=(self.catalog*self.Raster) #print(len(product),'lunghezza1') diff=(self.Raster-product) #print(len(diff),'lunghezza2') ######################################clean nan values idx2=np.where(self.catalog<=-9999) product[idx2]=-9999 diff[idx2]=-9999 diff[idx3]=-9999 product[self.Raster<=-9999]=-9999 #print(len(product[product>-9999]),'lunghezza1-1') diff[self.Raster<=-9999]=-9999 #print(len(diff[self.Raster>-9999]),'lunghezza2-1') ############################################ M=int(np.nanmax(self.Raster)) print(M) # # countProduct = {} countDiff = {} #somma=0 for n in range(0,M+1): P=np.array([]) D=np.array([]) PP=None DD=None #P=np.argwhere(product==float(n)) P=np.where(product==float(n)) PP=float(len(P[0])) #print(len(product[product>0]),'PP',n) #print(len(product[product==0]),'PP',n) #print(len(product[(product<1)&(product>-1)]),'PP',n) #print(len(product[product>-9999]),'PP',n) countProduct[n]=PP D=np.where(diff==float(n)) DD=float(len(D[0])) #print(len(diff[diff>0]),'DD',n) #print(len(diff[diff==0]),'DD',n) countDiff[n]=DD #somma+= PP[n] print('product x class',n,countProduct[n]) print('diff x class',n,countDiff[n]) print('should be equal to:',len(product[product>-9999])) self.weighted=np.array([]) self.weighted=np.zeros(np.shape(self.Matrix),dtype='float32') self.weighted[:]=self.Matrix[:] file = open(self.txtout,'w')#################save W+, W- and Wf file.write('class,Npx1,Npx2,Npx3,Npx4,W+,W-,Wf\n') print(M,'M') for i in range(1,M+1): Npx1=None Npx2=None Npx3=None Npx4=None Wplus=None Wminus=None Wf=None var=[] print('count', countProduct[i],countDiff[i]) if countProduct[i]==0 or countDiff[i]==0: Wf=0. Wplus=0. Wminus=0. Npx1='none' Npx2='none' Npx3='none' Npx4='none' var=[i,Npx1,Npx2,Npx3,Npx4,Wplus,Wminus,Wf] file.write(','.join(str(e) for e in var)+'\n') self.weighted[self.Raster == i] = 0. print('class:',i) print(Npx1,':Npx1 ',Npx2,':Npx2 ',Npx3,':Npx3 ',Npx4,':Npx4 ') print(len(product[product>-9999]),'= Number of not null cells') else: Npx1=float(countProduct[i]) for ii in range(1,M+1): try: Npx2 += float(countProduct[ii]) #Npx2 = Npx2+float(countProduct[ii]) except: Npx2 = float(countProduct[ii]) Npx2 -= float(countProduct[i]) #Npx2 = Npx2-float(countProduct[i]) Npx3=float(countDiff[i]) for iii in range(1,M+1): # try: Npx4 += float(countDiff[iii]) #Npx4 = Npx4+float(countDiff[iii]) except: Npx4 = float(countDiff[iii]) Npx4 -= float(countDiff[i]) #Npx4 = Npx4-float(countDiff[i]) print('class:',i) print(Npx1,':Npx1 ',Npx2,':Npx2 ',Npx3,':Npx3 ',Npx4,':Npx4 ') print(len(product[product>-9999]),'= Number of not null cells') # if (Npx1+Npx2+Npx3+Npx4)==len(product[product>-9999]): print(len(product[product>-9999]),'= Number of not null cells') else: QgsMessageLog.logMessage("Failure to claculate Npx1,Npx2,Npx3,Npx4", tag="WoE") raise ValueError # Failure to claculate Npx1,Npx2,Npx3,Npx4, see 'WoE' Log Messages Panel #print(Npx1+Npx2+Npx3+Npx4,'sum') #W+ W- #Npx1,Npx2,Npx3,Npx4 if Npx1==0 or Npx3==0: Wplus=0. else: Wplus=math.log((Npx1/(Npx1+Npx2))/(Npx3/(Npx3+Npx4))) if Npx2==0 or Npx4==0: Wminus=0. else: Wminus=math.log((Npx2/(Npx1+Npx2))/(Npx4/(Npx3+Npx4))) Wf=Wplus-Wminus var=[i,Npx1,Npx2,Npx3,Npx4,Wplus,Wminus,Wf] file.write(','.join(str(e) for e in var)+'\n')#################save W+, W- and Wf self.weighted[self.Raster == i] = Wf # # file.close() product=np.array([]) diff=np.array([])
def requestReady(self): QgsMessageLog.logMessage("SimpleHelloFilter.requestReady")
def execute(self): """ Reimplementation of the execute method from the parent class """ QgsMessageLog.logMessage( self.tr('Starting ') + self.getName() + self.tr(' Process.'), "DSG Tools Plugin", QgsMessageLog.CRITICAL) self.startTimeCount() try: self.setStatus(self.tr('Running'), 3) #now I'm running! # getting parameters after the execution of our pre process self.flagsDict = self.abstractDb.getFlagsDictByProcess( 'IdentifySmallLinesProcess') flagsClasses = self.flagsDict.keys() self.startTimeCount() if len(flagsClasses) == 0: self.setStatus(self.tr('There are no small lines.'), 1) #Finished return 1 numberOfProblems = 0 for cl in flagsClasses: # preparation localProgress = ProgressWidget( 0, 1, self.tr('Preparing execution for ') + cl, parent=self.iface.mapCanvas()) localProgress.step() lyr = self.loadLayerBeforeValidationProcess(cl) localProgress.step() #running the process on cl localProgress = ProgressWidget(0, 1, self.tr('Running process on ') + cl, parent=self.iface.mapCanvas()) localProgress.step() problems = len(self.flagsDict[cl]) smallIds = [int(flag['id']) for flag in self.flagsDict[cl]] lyr.startEditing() lyr.deleteFeatures(smallIds) localProgress.step() numberOfProblems += problems QgsMessageLog.logMessage( str(problems) + self.tr(' features from ') + cl + self.tr(' were changed.'), "DSG Tools Plugin", QgsMessageLog.CRITICAL) self.logLayerTime(cl) self.setStatus( str(numberOfProblems) + self.tr(' features were removed.'), 1) #Finished with flags return 1 except Exception as e: QgsMessageLog.logMessage(':'.join(e.args), "DSG Tools Plugin", QgsMessageLog.CRITICAL) self.finishedWithError() return 0
# Sample script for QGIS Init Script # This script will add a demo WMS endpoint from qgis.PyQt.QtCore import QSettings from qgis.core import QgsMessageLog QgsMessageLog.logMessage("Init script: %s" % __file__, tag="Init script", level=Qgis.Info) WMS_URL="http://demo.boundlessgeo.com/geoserver/wms" WMS_NAME="Boundlessgeo Geoserver Demo" settings = QSettings() if "Qgis/WMS/%s/authcfg" % WMS_NAME not in settings.allKeys(): settings.setValue("qgis/WMS/%s/authcfg" % WMS_NAME, "") settings.setValue("qgis/WMS/%s/username" % WMS_NAME, "") settings.setValue("qgis/WMS/%s/password" % WMS_NAME, "") settings.setValue("qgis/connections-wms/%s/dpiMode" % WMS_NAME, 7) settings.setValue("qgis/connections-wms/%s/ignoreAxisOrientation" % WMS_NAME, False) settings.setValue("qgis/connections-wms/%s/ignoreGetFeatureInfoURI" % WMS_NAME, False) settings.setValue("qgis/connections-wms/%s/ignoreGetMapURI" % WMS_NAME, False) settings.setValue("qgis/connections-wms/%s/invertAxisOrientation" % WMS_NAME, False) settings.setValue("qgis/connections-wms/%s/referer" % WMS_NAME, "") settings.setValue("qgis/connections-wms/%s/smoothPixmapTransform" % WMS_NAME, "") settings.setValue("qgis/connections-wms/%s/url" % WMS_NAME, WMS_URL) QgsMessageLog.logMessage("WMS %s has been successfully installed" % WMS_NAME, tag="Init script", level=Qgis.Info) else: QgsMessageLog.logMessage("WMS %s was already installed" % WMS_NAME, tag="Init script", level=Qgis.Info)
def error(self, text): QMessageBox.critical(self, "Error", text) QgsMessageLog.logMessage(text, self.tr('Processing'), Qgis.Critical)
def iter(self): listcause=[] listclasses=[] if self.Wcause1==None: pass else: listcause.append(self.Wcause1) listclasses.append(self.classes1) if self.Wcause2==None: pass else: listcause.append(self.Wcause2) listclasses.append(self.classes2) if self.Wcause3==None: pass else: listcause.append(self.Wcause3) listclasses.append(self.classes3) if self.Wcause4==None: pass else: listcause.append(self.Wcause4) listclasses.append(self.classes4) for y in range(self.ii): if self.Wcauselista[y]==None: pass else: listcause.append(self.Wcauselista[y]) listclasses.append(self.classeslista[y]) ####################################################################### countcause=len(listcause)######delete empty cause!!!!!!!!!! #print(listclasses) #print(listcause) if countcause==0:#verify empty row input QgsMessageLog.logMessage('Select at least one cause', tag="WoE") raise ValueError # Select at least one cause, see 'WoE' Log Messages Panel ####################################translate dem and inventory self.newXNumPxl=(np.ceil(abs(self.xmax-self.xmin)/(self.w))-1).astype(int) self.newYNumPxl=(np.ceil(abs(self.ymax-self.ymin)/(self.h))-1).astype(int) self.xsize=self.newXNumPxl self.ysize=self.newYNumPxl self.shape=np.array([self.newXNumPxl,self.newYNumPxl]) self.extent = "%s,%s,%s,%s" % (self.xmin, self.xmax, self.ymin, self.ymax) ############## self.origine=[self.xmin,self.ymax] #######################################inventory from shp to tif #try: #try: dem_datas=np.zeros((self.ysize,self.xsize),dtype='int64') # write the data to output file rf1='/tmp/01inv.tif' dem_datas1=np.zeros(np.shape(dem_datas),dtype='float32') dem_datas1[:]=dem_datas[:]#[::-1] w1=self.w h1=self.h*(-1) self.array2raster(rf1,w1,h1,dem_datas1,self.origine)##########rasterize inventory del dem_datas del dem_datas1 ################################## self.IN3a=rf1 self.IN2a='/tmp/02invq.tif' #IN2b='/tmp/03invq.tif' #self.IN3a='/tmp/04inventorynxn.tif' #self.cutextent(IN1a,IN2a) self.cut(self.IN3a,self.IN2a)##########traslate inventory if self.polynum==0: self.IN2a=self.IN3a self.ds15= None self.ds15 = gdal.Open(self.IN2a) if self.ds15 is None:#####################verify empty row input QgsMessageLog.logMessage("ERROR: can't open raster input", tag="WoE") raise ValueError # can't open raster input, see 'WoE' Log Messages Panel ap=self.ds15.GetRasterBand(1) NoData=ap.GetNoDataValue() invmatrix = np.array(ap.ReadAsArray()).astype(np.int64) bands = self.ds15.RasterCount if bands>1:#####################verify bands QgsMessageLog.logMessage("ERROR: input rasters shoud be 1-band raster", tag="WoE") raise ValueError # input rasters shoud be 1-band raster, see 'WoE' Log Messages Panel gtinv= self.ds15.GetGeoTransform() self.origineinv=[gtinv[0],gtinv[3]] self.winv=gtinv[1] self.hinv=gtinv[5] #################################dem # except: # QgsMessageLog.logMessage("Failure to save sized inventory", tag="WoE") # raise ValueError # Failure to save sized inventory, see 'WoE' Log Messages Panel ###########################################load inventory self.catalog0=np.zeros(np.shape(invmatrix),dtype='int64') print(np.shape(invmatrix),'shape catalog') self.catalog0[:]=invmatrix[:] del invmatrix #######################################inventory from shp to tif fuori=self.vector2arrayinv(self.IN2a,self.inventory,self.catalog0) self.catalog=np.zeros(np.shape(fuori),dtype='int64') print(np.shape(fuori),'shape catalog') self.catalog[:]=fuori[:] del fuori # dem_datas=self.vector2array(self.inventory,self.w,self.h,self.xmin,self.ymin,self.xmax,self.ymax,self.xsize,self.ysize) # # write the data to output file # rf1='/tmp/inv.tif' # dem_datas1=np.zeros(np.shape(dem_datas),dtype='float32') # dem_datas1[:]=dem_datas[:]#[::-1] # w1=self.w # h1=self.h*(-1) # self.array2raster(rf1,w1,h1,dem_datas1,self.origine)##########rasterize inventory # del dem_datas # del dem_datas1 # ################################## # IN1a=rf1 # IN2a='/tmp/invq.tif' # IN3a=self.fold + '/inventorynxn.tif' # self.cut(IN1a,IN2a,IN3a)########## inventory # self.ds15=None # self.ds15 = gdal.Open(IN3a) # if self.ds15 is None:#####################verify empty row input # QgsMessageLog.logMessage("ERROR: can't open raster input", tag="WoE") # raise ValueError # can't open raster input, see 'WoE' Log Messages Panel # ap=self.ds15.GetRasterBand(1) # NoData=ap.GetNoDataValue() # invmatrix = np.array(ap.ReadAsArray()).astype(np.int64) # bands = self.ds15.RasterCount # if bands>1:#####################verify bands # QgsMessageLog.logMessage("ERROR: input rasters shoud be 1-band raster", tag="WoE") # raise ValueError # input rasters shoud be 1-band raster, see 'WoE' Log Messages Panel # #################################dem # # except: # # QgsMessageLog.logMessage("Failure to save sized inventory", tag="WoE") # # raise ValueError # Failure to save sized inventory, see 'WoE' Log Messages Panel # ###########################################load inventory # self.catalog=np.zeros(np.shape(invmatrix),dtype='int64') # print(np.shape(invmatrix),'shape catalog') # self.catalog[:]=invmatrix[:] # del invmatrix ##del valuess ###########cause for v in range(countcause): ds8=gdal.Open(listcause[v],0) ds8x = ds8.RasterXSize ds8y = ds8.RasterYSize gt= ds8.GetGeoTransform() # causexl = round(gt[0],2) # causeyt = round(gt[3],2) # causexr = round(gt[0] + gt[1] * ds8x,2) # causeyb = round(gt[3] + gt[5] * ds8y,2) causexl = gt[0] causeyt = gt[3] causexr = gt[0] + gt[1] * ds8x causeyb = gt[3] + gt[5] * ds8y QgsMessageLog.logMessage(self.extent, tag="WoE") if (np.round(causexl,2))>(self.xmin) or (np.round(causexr,2))<(self.xmax) or (np.round(causeyb,2))>(self.ymin) or (np.round(causeyt,2))<(self.ymax): print(self.xmin,self.ymin,self.xmax,self.ymax,'selected extension') print(np.round(causexl,2),np.round(causeyb,2),np.round(causexr,2),np.round(causeyt,2),'cause extension') QgsMessageLog.logMessage('Cause %0.2f extension cannot satisfy selected extension' %v, tag="WoE") raise ValueError # Cause extension cannot satisfy selected extension, see 'WoE' Log Messages Panel if self.w < abs(gt[1]) or self.h < abs(gt[5]): QgsMessageLog.logMessage('Resolution requested is higher than Cause resolution', tag="WoE") raise ValueError # Resolution requested is higher than Cause resolution, see 'WoE' Log Messages Panel ds8=None ################### Causes={} id={} Mat={} dimensioni={} self.Wfs={} for i in range(countcause): #matrix=None self.Wcause=None self.classes=None self.Wcause=listcause[i] self.classes=listclasses[i] self.Wreclassed=None self.Wreclassed=self.fold+'/04reclassedcause'+str(i)+'.tif' pathszcause=None pathszcause=self.fold+'/01Wsizedcause'+str(i)+'.tif' # self.ds2=None # self.ds2 = gdal.Open(self.Wcause) # if self.ds2 is None:#####################verify empty row input # QgsMessageLog.logMessage("ERROR: can't open raster input", tag="WoE") # raise ValueError # can't open raster input, see 'WoE' Log Messages Panel # gt=self.ds2.GetGeoTransform() # ww=gt[1] # hh=gt[5] # xyo=[gt[0],gt[3]] # a=self.ds2.GetRasterBand(1) # NoData=a.GetNoDataValue() # #self.RasterInt = np.array(a.ReadAsArray()).astype(int) # self.matrix = np.array(a.ReadAsArray()).astype(np.float32) # bands = self.ds2.RasterCount # if bands>1:#####################verify bands # QgsMessageLog.logMessage("ERROR: input rasters shoud be 1-band raster", tag="WoE") # raise ValueError # input rasters shoud be 1-band raster, see 'WoE' Log Messages Panel # ################################################################ # self.classification()############# # #del self.RasterInt # self.matrix1=np.zeros(np.shape(self.matrix2),dtype='float32') # self.matrix1[:]=self.matrix2[:] # #print(self.matrix[self.matrix==1]) # # #print(max(self.matrix1,'max')) # #np.size(self.RasterInt1) # self.array2raster(pathszcause,ww,hh,self.matrix1,xyo) # del self.matrix2 # del self.matrix1 # #print(ciao) # ################### IN2='/tmp/02causeq'+str(i)+'.tif' IN1=self.Wcause IN3='/tmp/03causec'+str(i)+'.tif' IN4=pathszcause self.alignRaster(self.IN3a, IN1, IN2) self.cutextent(IN2,IN3,self.IN3a) self.cut(IN3,IN4)############################## if self.polynum==0: pathszcause=IN3 #self.cutextent(IN2,IN3,self.IN3a) #self.cut(IN3,IN2,IN4) ################################################################ self.ds2=None self.ds2 = gdal.Open(pathszcause) if self.ds2 is None:#####################verify empty row input QgsMessageLog.logMessage("ERROR: can't open raster input", tag="WoE") raise ValueError # can't open raster input, see 'WoE' Log Messages Panel gt=self.ds2.GetGeoTransform() ww=gt[1] hh=gt[5] xyo=[gt[0],gt[3]] a=self.ds2.GetRasterBand(1) NoData=a.GetNoDataValue() #self.RasterInt = np.array(a.ReadAsArray()).astype(int) self.matrix = np.array(a.ReadAsArray()).astype(np.float32) idnul=np.where(self.matrix<=-9999) self.matrix[idnul]=-9999 bands = self.ds2.RasterCount if bands>1:#####################verify bands QgsMessageLog.logMessage("ERROR: input rasters shoud be 1-band raster", tag="WoE") raise ValueError # input rasters shoud be 1-band raster, see 'WoE' Log Messages Panel self.classification()############# #del self.RasterInt self.matrix1=np.zeros(np.shape(self.matrix2),dtype='float32') self.matrix1[:]=self.matrix2[:] #print(self.matrix[self.matrix==1]) #print(max(self.matrix1,'max')) #np.size(self.RasterInt1) self.array2raster(self.Wreclassed,ww,hh,self.matrix1,xyo) del self.matrix2 del self.matrix1 #print(ciao) ################### #print(ciao) self.matrix=None self.RasterInt=None self.ds22=None self.ds22 = gdal.Open(self.Wreclassed) if self.ds22 is None:#####################verify empty row input QgsMessageLog.logMessage("ERROR: can't open raster input", tag="WoE") raise ValueError # can't open raster input, see 'WoE' Log Messages Panel gt=self.ds22.GetGeoTransform() ww=gt[1] hh=gt[5] aa=self.ds22.GetRasterBand(1) NoData=aa.GetNoDataValue() self.RasterInt = np.array(aa.ReadAsArray()).astype(np.int64) print(np.max(self.RasterInt),'-1') self.matrix = np.array(aa.ReadAsArray()).astype(np.float32) bands = self.ds22.RasterCount if bands>1:#####################verify bands QgsMessageLog.logMessage("ERROR: input rasters shoud be 1-band raster", tag="WoE") raise ValueError # input rasters shoud be 1-band raster, see 'WoE' Log Messages Panel #################### Causes[i]=self.RasterInt[:] print(np.max(self.RasterInt),'0') Mat[i]=self.matrix[:] id[i]=np.where(self.RasterInt<=-9999) idcat=np.where(self.catalog<=-9999) dimensioni[i]=np.shape(self.matrix) ##################################-9999 del self.matrix del self.RasterInt #del out_bandC #del dataC for causa in range(countcause): self.Raster=np.array([]) self.Matrix=np.array([]) self.txtout=None self.Weightedcause=None self.txtout=self.fold+'/Wftxt'+str(causa)+'.txt' self.Weightedcause=self.fold+'/04weightedcause'+str(causa)+'.tif' self.ds10=None self.Raster=np.zeros(np.shape(Causes[causa]),dtype='int64') self.Raster[:]=Causes[causa] print(np.max(self.Raster),'1') self.Matrix=np.zeros(np.shape(Mat[causa]),dtype='float32') self.Matrix[:]=Mat[causa] self.Raster[idcat]=-9999 self.Matrix[idcat]=-9999 for cc in range(countcause): self.Raster[id[cc]]=-9999 #print(np.max(self.Raster),'2') self.Matrix[id[cc]]=-9999 self.catalog[id[cc]]=-9999 if self.method==0: self.WoE()################# elif self.method==1: self.FR()############## self.Wfs[causa]=self.weighted[:] self.saveWf()################## self.weighted=np.array([]) #del self.dem del self.catalog del Causes del Mat del id del self.Matrix del self.Raster
def processAlgorithm(self, parameters, context, feedback): layer = QgsProcessingUtils.mapLayerFromString( self.getParameterValue(self.INPUT_VECTOR), context) startPoint = self.getParameterValue(self.START_POINT) endPoints = QgsProcessingUtils.mapLayerFromString( self.getParameterValue(self.END_POINTS), context) strategy = self.getParameterValue(self.STRATEGY) directionFieldName = self.getParameterValue(self.DIRECTION_FIELD) forwardValue = self.getParameterValue(self.VALUE_FORWARD) backwardValue = self.getParameterValue(self.VALUE_BACKWARD) bothValue = self.getParameterValue(self.VALUE_BOTH) defaultDirection = self.getParameterValue(self.DEFAULT_DIRECTION) bothValue = self.getParameterValue(self.VALUE_BOTH) defaultDirection = self.getParameterValue(self.DEFAULT_DIRECTION) speedFieldName = self.getParameterValue(self.SPEED_FIELD) defaultSpeed = self.getParameterValue(self.DEFAULT_SPEED) tolerance = self.getParameterValue(self.TOLERANCE) fields = QgsFields() fields.append(QgsField('start', QVariant.String, '', 254, 0)) fields.append(QgsField('end', QVariant.String, '', 254, 0)) fields.append(QgsField('cost', QVariant.Double, '', 20, 7)) feat = QgsFeature() feat.setFields(fields) writer = self.getOutputFromName(self.OUTPUT_LAYER).getVectorWriter( fields, QgsWkbTypes.LineString, layer.crs(), context) tmp = startPoint.split(',') startPoint = QgsPointXY(float(tmp[0]), float(tmp[1])) directionField = -1 if directionFieldName is not None: directionField = layer.fields().lookupField(directionFieldName) speedField = -1 if speedFieldName is not None: speedField = layer.fields().lookupField(speedFieldName) director = QgsVectorLayerDirector(layer, directionField, forwardValue, backwardValue, bothValue, defaultDirection) distUnit = iface.mapCanvas().mapSettings().destinationCrs().mapUnits() multiplier = QgsUnitTypes.fromUnitToUnitFactor( distUnit, QgsUnitTypes.DistanceMeters) if strategy == 0: strategy = QgsNetworkDistanceStrategy() else: strategy = QgsNetworkSpeedStrategy(speedField, defaultSpeed, multiplier * 1000.0 / 3600.0) multiplier = 3600 director.addStrategy(strategy) builder = QgsGraphBuilder( iface.mapCanvas().mapSettings().destinationCrs(), True, tolerance) feedback.pushInfo(self.tr('Loading end points...')) request = QgsFeatureRequest() request.setFlags(request.flags() ^ QgsFeatureRequest.SubsetOfAttributes) features = QgsProcessingUtils.getFeatures(endPoints, context, request) count = QgsProcessingUtils.featureCount(endPoints, context) points = [startPoint] for f in features: points.append(f.geometry().asPoint()) feedback.pushInfo(self.tr('Building graph...')) snappedPoints = director.makeGraph(builder, points) feedback.pushInfo(self.tr('Calculating shortest paths...')) graph = builder.graph() idxStart = graph.findVertex(snappedPoints[0]) tree, cost = QgsGraphAnalyzer.dijkstra(graph, idxStart, 0) route = [] total = 100.0 / count if count else 1 for i in range(1, count + 1): idxEnd = graph.findVertex(snappedPoints[i]) if tree[idxEnd] == -1: msg = self.tr( 'There is no route from start point ({}) to end point ({}).' .format(startPoint.toString(), points[i].toString())) feedback.setProgressText(msg) QgsMessageLog.logMessage(msg, self.tr('Processing'), QgsMessageLog.WARNING) continue cost = 0.0 current = idxEnd while current != idxStart: cost += graph.edge(tree[current]).cost(0) route.append( graph.vertex(graph.edge(tree[current]).inVertex()).point()) current = graph.edge(tree[current]).outVertex() route.append(snappedPoints[0]) route.reverse() geom = QgsGeometry.fromPolyline(route) feat.setGeometry(geom) feat['start'] = startPoint.toString() feat['end'] = points[i].toString() feat['cost'] = cost / multiplier writer.addFeature(feat, QgsFeatureSink.FastInsert) route[:] = [] feedback.setProgress(int(i * total)) del writer
def load(self, reader): """Load input data by specified reader. :param reader: reader class used for reading input data """ if self._loaded: return # data already loaded # create progress bar widget progressMessageBar = iface.messageBar().createMessage( self.tr("Loading data...")) progress = QtWidgets.QProgressBar() progress.setMaximum(100) progressMessageBar.layout().addWidget(progress) iface.messageBar().pushWidget(progressMessageBar, Qgis.Info) # load items as new point features (inform user about progress) i = 0 count = reader.count() start = time.perf_counter() prev = None # previous feature self.startEditing() for item in reader: i += 1 if i == 1 and not self._aliases: # set attributes from data item if needed self._aliases = self._setAttrbsDefs( limit=item.keys(), defs=reader.attributeDefs()) feat = self._item2feat(item) if not feat: # error appeared continue feat.setId(i) self.addFeature(feat) if i % 100 == 0: percent = i / float(count) * 100 progress.setValue(percent) # add features (attributes recalculated if requested) self.commitChanges() if self.storageFormat != "memory": self._writeToOgrDataSource() self.reload() # finish import endtime = time.perf_counter() - start progress.setValue(100) iface.messageBar().clearWidgets() if self._errs: # report errors if any iface.messageBar().pushMessage( self.tr("Info"), self. tr("{} invalid measurement(s) skipped (see message log for details)" ).format(sum(self._errs.values())), level=Qgis.Info, duration=5) for attr in list(self._errs.keys()): QgsMessageLog.logMessage( "{}: {} measurement(s) skipped (invalid {})".format( self._fileName, self._errs[attr], attr), tag="Radiation Toolbox") # inform user about successful import iface.messageBar().pushMessage( self.tr("Data loaded"), self.tr("{} features loaded (in {:.2f} sec).").format( self.featureCount(), endtime), level=Qgis.Success, duration=3) # data loaded (avoid multiple imports) self._loaded = True # switch to read-only mode self.setReadOnly(True)
def on_convertButton_clicked(self): """ Performs the actual conversion """ if not self.widget.abstractDb: QtGui.QMessageBox.warning(self, self.tr('Error!'), self.tr('Enter input database!')) return if not self.widget_2.abstractDb: QtGui.QMessageBox.warning(self, self.tr('Error!'), self.tr('Enter output database!')) return if self.widget.dbVersion <> self.widget_2.dbVersion: QtGui.QMessageBox.warning( self, self.tr('Error!'), self. tr('Version mismatch!\nConversion must be between databases with the same version!' )) return type = '' if self.allDataRadioButton.isChecked(): type = 'untouchedData' if self.fixDataRadioButton.isChecked(): type = 'fixData' if not self.widget.abstractDb.slotConnected: self.widget.abstractDb.signals.updateLog.connect(self.logUpdated) self.widget.abstractDb.signals.clearLog.connect(self.logCleared) self.widget.abstractDb.slotConnected = True converted = False self.logCleared() try: if self.widget.crs <> self.widget_2.crs: if QtGui.QMessageBox.question( self, self.tr('Question'), self. tr('Databases CRS are different. Conversor will reproject spatial data. Do you want to proceed?' ), QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel) == QtGui.QMessageBox.Ok: QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) converted = self.widget.abstractDb.convertDatabase( self.widget_2.abstractDb, type) QApplication.restoreOverrideCursor() else: QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) converted = self.widget.abstractDb.convertDatabase( self.widget_2.abstractDb, type) QApplication.restoreOverrideCursor() except Exception as e: QApplication.restoreOverrideCursor() converted = False QgsMessageLog.logMessage(':'.join(e.args), 'DSG Tools Plugin', QgsMessageLog.CRITICAL) if converted: QtGui.QMessageBox.warning( self, self.tr('Success!'), self.tr('Conversion successfully completed!')) else: QtGui.QMessageBox.warning( self, self.tr('Error!'), self.tr('Conversion not performed! Check log for details.'))
def runAlgorithm(algOrName, onFinish, *args, **kwargs): if isinstance(algOrName, GeoAlgorithm): alg = algOrName else: alg = Processing.getAlgorithm(algOrName) if alg is None: print 'Error: Algorithm not found\n' QgsMessageLog.logMessage( Processing.tr('Error: Algorithm {0} not found\n').format( algOrName), Processing.tr("Processing")) return alg = alg.getCopy() if len(args) == 1 and isinstance(args[0], dict): # Set params by name and try to run the alg even if not all parameter values are provided, # by using the default values instead. setParams = [] for (name, value) in args[0].items(): param = alg.getParameterFromName(name) if param and param.setValue(value): setParams.append(name) continue output = alg.getOutputFromName(name) if output and output.setValue(value): continue print 'Error: Wrong parameter value %s for parameter %s.' % ( value, name) QgsMessageLog.logMessage( Processing.tr( 'Error: Wrong parameter value {0} for parameter {1}.'). format(value, name), Processing.tr("Processing")) ProcessingLog.addToLog( ProcessingLog.LOG_ERROR, Processing. tr('Error in %s. Wrong parameter value %s for parameter %s.' ) % (alg.name, value, name)) return # fill any missing parameters with default values if allowed for param in alg.parameters: if param.name not in setParams: if not param.setDefaultValue(): print( 'Error: Missing parameter value for parameter %s.' % (param.name)) QgsMessageLog.logMessage( Processing. tr('Error: Missing parameter value for parameter {0}.' ).format(param.name), Processing.tr("Processing")) ProcessingLog.addToLog( ProcessingLog.LOG_ERROR, Processing. tr('Error in %s. Missing parameter value for parameter %s.' ) % (alg.name, param.name)) return else: if len(args) != alg.getVisibleParametersCount( ) + alg.getVisibleOutputsCount(): print 'Error: Wrong number of parameters' QgsMessageLog.logMessage( Processing.tr('Error: Wrong number of parameters'), Processing.tr("Processing")) processing.alghelp(algOrName) return i = 0 for param in alg.parameters: if not param.hidden: if not param.setValue(args[i]): print 'Error: Wrong parameter value: ' \ + unicode(args[i]) QgsMessageLog.logMessage( Processing.tr('Error: Wrong parameter value: ') + unicode(args[i]), Processing.tr("Processing")) return i = i + 1 for output in alg.outputs: if not output.hidden: if not output.setValue(args[i]): print 'Error: Wrong output value: ' + unicode(args[i]) QgsMessageLog.logMessage( Processing.tr('Error: Wrong output value: ') + unicode(args[i]), Processing.tr("Processing")) return i = i + 1 msg = alg._checkParameterValuesBeforeExecuting() if msg: print 'Unable to execute algorithm\n' + msg QgsMessageLog.logMessage( Processing.tr('Unable to execute algorithm\n{0}').format(msg), Processing.tr("Processing")) return if not alg.checkInputCRS(): print 'Warning: Not all input layers use the same CRS.\n' \ + 'This can cause unexpected results.' QgsMessageLog.logMessage( Processing. tr('Warning: Not all input layers use the same CRS.\nThis can cause unexpected results.' ), Processing.tr("Processing")) # Don't set the wait cursor twice, because then when you # restore it, it will still be a wait cursor. overrideCursor = False if iface is not None: cursor = QApplication.overrideCursor() if cursor is None or cursor == 0: QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) overrideCursor = True elif cursor.shape() != Qt.WaitCursor: QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) overrideCursor = True progress = None if kwargs is not None and "progress" in kwargs.keys(): progress = kwargs["progress"] elif iface is not None: progress = MessageBarProgress() ret = runalg(alg, progress) if ret: if onFinish is not None: onFinish(alg, progress) else: QgsMessageLog.logMessage( Processing.tr("There were errors executing the algorithm."), Processing.tr("Processing")) if overrideCursor: QApplication.restoreOverrideCursor() if isinstance(progress, MessageBarProgress): progress.close() return alg
def _log(*args, **kw): """ Log messages to the QgsMessageLog viewer """ QgsMessageLog.logMessage(" ".join(map(str, args)), "Factory")
def associateFeatures(self): """ Associates all features selected in the map canvas to a complex. """ #case no item is selected we should warn the user if len(self.treeWidget.selectedItems()) == 0: QMessageBox.warning(self.iface.mainWindow(), self.tr("Warning!"), self.tr("Please, select a complex.")) return item = self.treeWidget.selectedItems()[0] #checking if the item is a complex (it should have depth = 2) if self.depth(item) != 2: QMessageBox.warning(self.iface.mainWindow(), self.tr("Warning!"), self.tr("Please, select a complex.")) return complex = self.complexCombo.currentText() #uuid to be adjust on the selected features uuid = item.text(1) #getting the selected features forbiddenLayers = [] self.layers = self.iface.mapCanvas().layers() for layer in self.layers: if layer.type() != QgsMapLayer.VectorLayer: continue #case no fetures selected we proceed to the next one selectedFeatures = layer.selectedFeatures() if len(selectedFeatures) == 0: continue #obtaining the link column column_name = '' try: column_name = self.abstractDb.obtainLinkColumn( complex, layer.name()) except Exception as e: QMessageBox.critical( self.iface.mainWindow(), self.tr('Critical'), self.tr('A problem occurred! Check log for details.')) QgsMessageLog.logMessage(':'.join(e.args), 'DSG Tools Plugin', QgsMessageLog.CRITICAL) #storing the names of the incompatible layers if column_name == '': forbiddenLayers.append(layer.name()) continue for feature in selectedFeatures: fieldIndex = [ i for i in range(len(layer.dataProvider().fields())) if layer.dataProvider().fields()[i].name() == column_name ] #feature id that will be updated id = feature.id() #attribute pair that will be changed attrs = {fieldIndex[0]: uuid} #actual update in the database layer.dataProvider().changeAttributeValues({id: attrs}) #show the message of incompatible classes to associate if len(forbiddenLayers) > 0: message = "" message += self.tr( "The following layers cannot be associated to complexes from " ) + self.complexCombo.currentText() + ":\n" for text in forbiddenLayers: message += text + "\n" QMessageBox.warning(self.iface.mainWindow(), self.tr("Warning!"), message) #updating the tree widget self.loadAssociatedFeatures()
def cancel(self): QgsMessageLog.logMessage( f'TracingTrask {self.description()} was canceled', level=Qgis.Info) super().cancel()
def executeGrass(commands, feedback, outputCommands=None): loglines = [] loglines.append(Grass7Utils.tr('GRASS GIS 7 execution console output')) grassOutDone = False command, grassenv = Grass7Utils.prepareGrassExecution(commands) #QgsMessageLog.logMessage('exec: {}'.format(command), 'DEBUG', QgsMessageLog.INFO) # For MS-Windows, we need to hide the console window. if isWindows(): si = subprocess.STARTUPINFO() si.dwFlags |= subprocess.STARTF_USESHOWWINDOW si.wShowWindow = subprocess.SW_HIDE with subprocess.Popen(command, shell=True if isMac() else False, stdout=subprocess.PIPE, stdin=subprocess.DEVNULL, stderr=subprocess.STDOUT, universal_newlines=True, env=grassenv, startupinfo=si if isWindows() else None) as proc: for line in iter(proc.stdout.readline, ''): if 'GRASS_INFO_PERCENT' in line: try: feedback.setProgress( int(line[len('GRASS_INFO_PERCENT') + 2:])) except: pass else: if 'r.out' in line or 'v.out' in line: grassOutDone = True loglines.append(line) feedback.pushConsoleInfo(line) # Some GRASS scripts, like r.mapcalculator or r.fillnulls, call # other GRASS scripts during execution. This may override any # commands that are still to be executed by the subprocess, which # are usually the output ones. If that is the case runs the output # commands again. if not grassOutDone and outputCommands: command, grassenv = Grass7Utils.prepareGrassExecution( outputCommands) with subprocess.Popen( command, shell=True if isMac() else False, stdout=subprocess.PIPE, stdin=subprocess.DEVNULL, stderr=subprocess.STDOUT, universal_newlines=True, env=grassenv, startupinfo=si if isWindows() else None) as proc: for line in iter(proc.stdout.readline, ''): if 'GRASS_INFO_PERCENT' in line: try: feedback.setProgress( int(line[len('GRASS_INFO_PERCENT') + 2:])) except: pass else: loglines.append(line) feedback.pushConsoleInfo(line) if ProcessingConfig.getSetting(Grass7Utils.GRASS_LOG_CONSOLE): QgsMessageLog.logMessage('\n'.join(loglines), 'Processing', QgsMessageLog.INFO)
shell=True if Pyarchinit_OS_Utility.isWindows() else False) except Exception as e: if Pyarchinit_OS_Utility.isMac(): library_path = '/Library/Frameworks/Python.framework/Versions/{}/bin'.format( python_version) cmd = '{}/python{}'.format(library_path, python_version) subprocess.call([ cmd, '{}'.format( os.path.join(os.path.dirname(__file__), 'scripts', 'modules_installer.py')), ','.join(install_libraries) ]) else: error = traceback.format_exc() QgsMessageLog.logMessage(error, tag="PyArchInit", level=Qgis.Critical) else: pass s = QgsSettings() if not Pyarchinit_OS_Utility.checkGraphvizInstallation() and s.value( 'pyArchInit/graphvizBinPath'): os.environ['PATH'] += os.pathsep + os.path.normpath( s.value('pyArchInit/graphvizBinPath')) if not Pyarchinit_OS_Utility.checkRInstallation() and s.value( 'pyArchInit/rBinPath'): os.environ['PATH'] += os.pathsep + os.path.normpath( s.value('pyArchInit/rBinPath')) packages = [
def processAlgorithm(self, parameters, context, feedback): network = self.parameterAsSource(parameters, self.INPUT, context) startPoints = self.parameterAsSource(parameters, self.START_POINTS, context) endPoint = self.parameterAsPoint(parameters, self.END_POINT, context) strategy = self.parameterAsEnum(parameters, self.STRATEGY, context) directionFieldName = self.parameterAsString(parameters, self.DIRECTION_FIELD, context) forwardValue = self.parameterAsString(parameters, self.VALUE_FORWARD, context) backwardValue = self.parameterAsString(parameters, self.VALUE_BACKWARD, context) bothValue = self.parameterAsString(parameters, self.VALUE_BOTH, context) defaultDirection = self.parameterAsEnum(parameters, self.DEFAULT_DIRECTION, context) speedFieldName = self.parameterAsString(parameters, self.SPEED_FIELD, context) defaultSpeed = self.parameterAsDouble(parameters, self.DEFAULT_SPEED, context) tolerance = self.parameterAsDouble(parameters, self.TOLERANCE, context) fields = QgsFields() fields.append(QgsField('start', QVariant.String, '', 254, 0)) fields.append(QgsField('end', QVariant.String, '', 254, 0)) fields.append(QgsField('cost', QVariant.Double, '', 20, 7)) feat = QgsFeature() feat.setFields(fields) (sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context, fields, QgsWkbTypes.LineString, network.sourceCrs()) directionField = -1 if directionFieldName: directionField = network.fields().lookupField(directionFieldName) speedField = -1 if speedFieldName: speedField = network.fields().lookupField(speedFieldName) director = QgsVectorLayerDirector(network, directionField, forwardValue, backwardValue, bothValue, defaultDirection) distUnit = context.project().crs().mapUnits() multiplier = QgsUnitTypes.fromUnitToUnitFactor( distUnit, QgsUnitTypes.DistanceMeters) if strategy == 0: strategy = QgsNetworkDistanceStrategy() else: strategy = QgsNetworkSpeedStrategy(speedField, defaultSpeed, multiplier * 1000.0 / 3600.0) multiplier = 3600 director.addStrategy(strategy) builder = QgsGraphBuilder(context.project().crs(), True, tolerance) feedback.pushInfo(self.tr('Loading start points...')) request = QgsFeatureRequest() request.setFlags(request.flags() ^ QgsFeatureRequest.SubsetOfAttributes) request.setDestinationCrs(network.sourceCrs()) features = startPoints.getFeatures(request) total = 100.0 / startPoints.featureCount() if startPoints.featureCount( ) else 0 points = [endPoint] for current, f in enumerate(features): if feedback.isCanceled(): break points.append(f.geometry().asPoint()) feedback.setProgress(int(current * total)) feedback.pushInfo(self.tr('Building graph...')) snappedPoints = director.makeGraph(builder, points, feedback) feedback.pushInfo(self.tr('Calculating shortest paths...')) graph = builder.graph() idxEnd = graph.findVertex(snappedPoints[0]) route = [] nPoints = len(snappedPoints) total = 100.0 / nPoints if nPoints else 1 for i in range(1, count + 1): if feedback.isCanceled(): break idxStart = graph.findVertex(snappedPoints[i]) tree, cost = QgsGraphAnalyzer.dijkstra(graph, idxStart, 0) if tree[idxEnd] == -1: msg = self.tr( 'There is no route from start point ({}) to end point ({}).' .format(points[i].toString(), endPoint.toString())) feedback.setProgressText(msg) QgsMessageLog.logMessage(msg, self.tr('Processing'), QgsMessageLog.WARNING) continue cost = 0.0 current = idxEnd while current != idxStart: cost += graph.edge(tree[current]).cost(0) route.append( graph.vertex(graph.edge(tree[current]).inVertex()).point()) current = graph.edge(tree[current]).outVertex() route.append(snappedPoints[i]) route.reverse() geom = QgsGeometry.fromPolyline(route) feat.setGeometry(geom) feat['start'] = points[i].toString() feat['end'] = endPoint.toString() feat['cost'] = cost / multiplier sink.addFeature(feat, QgsFeatureSink.FastInsert) route[:] = [] feedback.setProgress(int(i * total)) return {self.OUTPUT: dest_id}