def testLogger(self): (myFileHandle, myFilename) = tempfile.mkstemp() try: myFile = os.fdopen(myFileHandle, "w") myFile.write("QGIS Logger Unit Test\n") myFile.close() os.environ['QGIS_DEBUG'] = '2' os.environ['QGIS_LOG_FILE'] = myFilename myLogger = QgsLogger() myLogger.debug('This is a debug') myLogger.warning('This is a warning') myLogger.critical('This is critical') #myLogger.fatal('Aaaargh...fatal'); #kills QGIS not testable myFile = open(myFilename, 'rt') myText = myFile.readlines() myFile.close() myExpectedText = ['QGIS Logger Unit Test\n', 'This is a debug\n', 'This is a warning\n', 'This is critical\n'] myMessage = ('Expected:\n---\n%s\n---\nGot:\n---\n%s\n---\n' % (myExpectedText, myText)) self.assertEquals(myText, myExpectedText, myMessage) finally: pass os.remove(myFilename)
def testLogger(self): (myFileHandle, myFilename) = tempfile.mkstemp() try: myFile = os.fdopen(myFileHandle, "w") myFile.write("QGIS Logger Unit Test\n") myFile.close() os.environ['QGIS_DEBUG'] = '2' os.environ['QGIS_LOG_FILE'] = myFilename myLogger = QgsLogger() myLogger.debug('This is a debug') myLogger.warning('This is a warning') myLogger.critical('This is critical') #myLogger.fatal('Aaaargh...fatal'); #kills QGIS not testable myFile = open(myFilename, 'rt') myText = myFile.readlines() myFile.close() myExpectedText = [ 'QGIS Logger Unit Test\n', 'This is a debug\n', 'This is a warning\n', 'This is critical\n' ] myMessage = ('Expected:\n---\n%s\n---\nGot:\n---\n%s\n---\n' % (myExpectedText, myText)) self.assertEquals(myText, myExpectedText, myMessage) finally: pass os.remove(myFilename)
def _log(self, message: str, level: LoggerLevel) -> None: """ Print message to QGIS log, depending on release or debug mode. Print either to the QGIS error console, if in release mode, (https://qgis.org/pyqgis/master/core/QgsLogger.html#qgis.core.QgsLogger) or to the QGIS GUI message logger (Log Message Panel) (https://qgis.org/pyqgis/master/core/QgsMessageLog.html#qgis.core.QgsMessageLog). Always use the above thread-safe classess to print out messages. """ message = str(message) # Get caller info callerframerecord = inspect.stack()[1] frame = callerframerecord[0] info = inspect.getframeinfo(frame) # prepare level message msg = 'file=%s, func=%s, line=%s: %s' % ( info.filename, info.function, str(info.lineno), message) # Print to Log Message Panel # (https://qgis.org/pyqgis/master/core/QgsMessageLog.html#qgis.core.QgsMessageLog.logMessage) if self.debug_flag: if level == self.DEBUG: QgsMessageLog.logMessage(msg, self.tag, level=Qgis.Info) elif level == self.INFO: QgsMessageLog.logMessage(msg, self.tag, level=Qgis.Success) elif level == self.ERROR: QgsMessageLog.logMessage(msg, self.tag, level=Qgis.Critical) elif level == self.WARNING: QgsMessageLog.logMessage(msg, self.tag, level=Qgis.Warning) else: raise Exception('Invalid log level: %s' % level) # Print to QGIS Python console else: # Do not use QgsLogger.fatal(msg), as it will crash QGIS process if level == self.ERROR: QgsLogger.critical(msg) elif level == self.WARNING: QgsLogger.warning(msg) elif level in (self.DEBUG, self.INFO): QgsLogger.debug( msg=message, file=info.filename, function=info.function, line=info.lineno) # always print to QGIS log file # must configure the QGIS_LOG_FILE environment variable QgsLogger.logMessageToFile("Picterra plugin - " + msg)
def testLogger(self): try: myFile = os.fdopen(myFileHandle, "w") myFile.write("QGIS Logger Unit Test\n") myFile.close() myLogger = QgsLogger() myLogger.debug("This is a debug") myLogger.warning("This is a warning") myLogger.critical("This is critical") # myLogger.fatal('Aaaargh...fatal'); #kills QGIS not testable myFile = open(myFilename, "rt") myText = myFile.readlines() myFile.close() myExpectedText = [ "QGIS Logger Unit Test\n", "This is a debug\n", "This is a warning\n", "This is critical\n", ] myMessage = "Expected:\n---\n%s\n---\nGot:\n---\n%s\n---\n" % (myExpectedText, myText) self.assertEqual(myText, myExpectedText, myMessage) finally: pass os.remove(myFilename)
def getSupportedVectors(self): if self.supportedVectors is not None: return self.supportedVectors # first get the OGR driver manager QgsApplication.registerOgrDrivers() self.supportedVectors = dict() # for each loaded OGR driver for i in range(ogr.GetDriverCount()): driver = ogr.GetDriver(i) if driver is None: QgsLogger.warning("unable to get driver " + unicode(i)) continue driverName = driver.GetName() longName = '' glob = [] if driverName.startswith("AVCBin"): pass # myDirectoryDrivers += "Arc/Info Binary Coverage,AVCBin" elif driverName.startswith("AVCE00"): longName = "Arc/Info ASCII Coverage" glob.append("*.e00") elif driverName.startswith("BNA"): longName = "Atlas BNA" glob.append("*.bna") elif driverName.startswith("CSV"): longName = "Comma Separated Value" glob.append("*.csv") elif driverName.startswith("DODS"): pass # myProtocolDrivers += "DODS/OPeNDAP,DODS" elif driverName.startswith("PGeo"): pass # myDatabaseDrivers += "ESRI Personal GeoDatabase,PGeo" # on Windows add a pair to the dict for this driver if platform.system() == "Windows": longName = "ESRI Personal GeoDatabase" glob.append("*.mdb") elif driverName.startswith("SDE"): pass # myDatabaseDrivers += "ESRI ArcSDE,SDE" elif driverName.startswith("ESRI"): longName = "ESRI Shapefiles" glob.append("*.shp") elif driverName.startswith("FMEObjects Gateway"): longName = "FMEObjects Gateway" glob.append("*.fdd") elif driverName.startswith("GeoJSON"): pass # myProtocolDrivers += "GeoJSON,GeoJSON" longName = "GeoJSON" glob.append("*.geojson") elif driverName.startswith("GeoRSS"): longName = "GeoRSS" glob.append("*.xml") elif driverName.startswith("GML"): longName = "Geography Markup Language" glob.append("*.gml") elif driverName.startswith("GMT"): longName = "GMT" glob.append("*.gmt") elif driverName.startswith("GPX"): longName = "GPX" glob.append("*.gpx") elif driverName.startswith("GRASS"): pass # myDirectoryDrivers += "Grass Vector,GRASS" elif driverName.startswith("IDB"): pass # myDatabaseDrivers += "Informix DataBlade,IDB" elif driverName.startswith("Interlis 1"): longName = "INTERLIS 1" glob.append("*.itf") glob.append("*.xml") glob.append("*.ili") elif driverName.startswith("Interlis 2"): longName = "INTERLIS 2" glob.append("*.itf") glob.append("*.xml") glob.append("*.ili") elif driverName.startswith("INGRES"): pass # myDatabaseDrivers += "INGRES,INGRES" elif driverName.startswith("KML"): longName = "KML" glob.append("*.kml") elif driverName.startswith("MapInfo File"): longName = "Mapinfo File" glob.append("*.mif") glob.append("*.tab") elif driverName.startswith("DGN"): longName = "Microstation DGN" glob.append("*.dgn") elif driverName.startswith("MySQL"): pass # myDatabaseDrivers += "MySQL,MySQL" elif driverName.startswith("OCI"): pass # myDatabaseDrivers += "Oracle Spatial,OCI" elif driverName.startswith("ODBC"): pass # myDatabaseDrivers += "ODBC,ODBC" elif driverName.startswith("OGDI"): pass # myDatabaseDrivers += "OGDI Vectors,OGDI" elif driverName.startswith("PostgreSQL"): pass # myDatabaseDrivers += "PostgreSQL,PostgreSQL" elif driverName.startswith("S57"): longName = "S-57 Base file" glob.append("*.000") elif driverName.startswith("SDTS"): longName = "Spatial Data Transfer Standard" glob.append("*catd.ddf") elif driverName.startswith("SQLite"): longName = "SQLite" glob.append("*.sqlite") elif driverName.startswith("UK .NTF"): pass # myDirectoryDrivers += "UK. NTF,UK. NTF" elif driverName.startswith("TIGER"): pass # myDirectoryDrivers += "U.S. Census TIGER/Line,TIGER" elif driverName.startswith("VRT"): longName = "VRT - Virtual Datasource " glob.append("*.vrt") elif driverName.startswith("XPlane"): longName = "X-Plane/Flighgear" glob.append("apt.dat") glob.append("nav.dat") glob.append("fix.dat") glob.append("awy.dat") longName = string.strip(longName) if longName == '': continue self.supportedVectors[longName] = { 'EXTENSIONS': glob, 'LONGNAME': longName, 'SHORTNAME': driverName } return self.supportedVectors
def getSupportedRasters(self): if self.supportedRasters is not None: return self.supportedRasters # first get the GDAL driver manager if gdal.GetDriverCount() == 0: gdal.AllRegister() self.supportedRasters = dict() jp2Driver = None # for each loaded GDAL driver for i in range(gdal.GetDriverCount()): driver = gdal.GetDriver(i) if driver is None: QgsLogger.warning("unable to get driver " + unicode(i)) continue # now we need to see if the driver is for something currently # supported; if not, we give it a miss for the next driver longName = string.strip(re.sub('\(.*$', '', driver.LongName)) shortName = string.strip(re.sub('\(.*$', '', driver.ShortName)) extensions = '' description = driver.GetDescription() glob = [] metadata = driver.GetMetadata() if gdal.DMD_EXTENSION in metadata: extensions = unicode(metadata[gdal.DMD_EXTENSION]) if longName != '': if extensions != '': # XXX add check for SDTS; in that case we want (*CATD.DDF) #TODO fix and test #glob.append( QString("*." + extensions.replace("/", " *.")).split(" ")) glob.append( string.split("*." + string.replace(extensions, "/", " *."), sep=(" "))) # Add only the first JP2 driver found to the filter list (it's the one GDAL uses) if description == "JPEG2000" or description.startswith( "JP2"): # JP2ECW, JP2KAK, JP2MrSID if jp2Driver is not None: continue # skip if already found a JP2 driver jp2Driver = driver # first JP2 driver found glob.append("*.j2k") # add alternate extension elif description == "GTiff": glob.append("*.tiff") elif description == "JPEG": glob.append("*.jpeg") else: # USGS DEMs use "*.dem" if description.startswith("USGSDEM"): glob.append("*.dem") elif description.startswith("DTED"): # DTED use "*.dt0" glob.append("*.dt0") elif description.startswith("MrSID"): # MrSID use "*.sid" glob.append("*.sid") else: continue self.supportedRasters[longName] = { 'EXTENSIONS': glob, 'LONGNAME': longName, 'SHORTNAME': shortName, 'DESCRIPTION': description } return self.supportedRasters
def getSupportedVectors(self): if self.supportedVectors is not None: return self.supportedVectors # first get the OGR driver manager QgsApplication.registerOgrDrivers() self.supportedVectors = dict() # for each loaded OGR driver for i in range(ogr.GetDriverCount()): driver = ogr.GetDriver(i) if driver is None: QgsLogger.warning("unable to get driver " + str(i)) continue driverName = driver.GetName() longName = '' glob = [] if driverName.startswith("AVCBin"): pass # myDirectoryDrivers += "Arc/Info Binary Coverage,AVCBin" elif driverName.startswith("AVCE00"): longName = "Arc/Info ASCII Coverage" glob.append("*.e00") elif driverName.startswith("BNA"): longName = "Atlas BNA" glob.append("*.bna") elif driverName.startswith("CSV"): longName = "Comma Separated Value" glob.append("*.csv") elif driverName.startswith("DODS"): pass # myProtocolDrivers += "DODS/OPeNDAP,DODS" elif driverName.startswith("PGeo"): pass # myDatabaseDrivers += "ESRI Personal GeoDatabase,PGeo" # on Windows add a pair to the dict for this driver if platform.system() == "Windows": longName = "ESRI Personal GeoDatabase" glob.append("*.mdb") elif driverName.startswith("SDE"): pass # myDatabaseDrivers += "ESRI ArcSDE,SDE" elif driverName.startswith("ESRI"): longName = "ESRI Shapefiles" glob.append("*.shp") elif driverName.startswith("FMEObjects Gateway"): longName = "FMEObjects Gateway" glob.append("*.fdd") elif driverName.startswith("GeoJSON"): pass # myProtocolDrivers += "GeoJSON,GeoJSON" longName = "GeoJSON" glob.append("*.geojson") elif driverName.startswith("GeoRSS"): longName = "GeoRSS" glob.append("*.xml") elif driverName.startswith("GML"): longName = "Geography Markup Language" glob.append("*.gml") elif driverName.startswith("GMT"): longName = "GMT" glob.append("*.gmt") elif driverName.startswith("GPX"): longName = "GPX" glob.append("*.gpx") elif driverName.startswith("GRASS"): pass # myDirectoryDrivers += "Grass Vector,GRASS" elif driverName.startswith("IDB"): pass # myDatabaseDrivers += "Informix DataBlade,IDB" elif driverName.startswith("Interlis 1"): longName = "INTERLIS 1" glob.append("*.itf") glob.append("*.xml") glob.append("*.ili") elif driverName.startswith("Interlis 2"): longName = "INTERLIS 2" glob.append("*.itf") glob.append("*.xml") glob.append("*.ili") elif driverName.startswith("INGRES"): pass # myDatabaseDrivers += "INGRES,INGRES" elif driverName.startswith("KML"): longName = "KML" glob.append("*.kml") elif driverName.startswith("MapInfo File"): longName = "Mapinfo File" glob.append("*.mif") glob.append("*.tab") elif driverName.startswith("DGN"): longName = "Microstation DGN" glob.append("*.dgn") elif driverName.startswith("MySQL"): pass # myDatabaseDrivers += "MySQL,MySQL" elif driverName.startswith("OCI"): pass # myDatabaseDrivers += "Oracle Spatial,OCI" elif driverName.startswith("ODBC"): pass # myDatabaseDrivers += "ODBC,ODBC" elif driverName.startswith("OGDI"): pass # myDatabaseDrivers += "OGDI Vectors,OGDI" elif driverName.startswith("PostgreSQL"): pass # myDatabaseDrivers += "PostgreSQL,PostgreSQL" elif driverName.startswith("S57"): longName = "S-57 Base file" glob.append("*.000") elif driverName.startswith("SDTS"): longName = "Spatial Data Transfer Standard" glob.append("*catd.ddf") elif driverName.startswith("SQLite"): longName = "SQLite" glob.append("*.sqlite") elif driverName.startswith("UK .NTF"): pass # myDirectoryDrivers += "UK. NTF,UK. NTF" elif driverName.startswith("TIGER"): pass # myDirectoryDrivers += "U.S. Census TIGER/Line,TIGER" elif driverName.startswith("VRT"): longName = "VRT - Virtual Datasource " glob.append("*.vrt") elif driverName.startswith("XPlane"): longName = "X-Plane/Flighgear" glob.append("apt.dat") glob.append("nav.dat") glob.append("fix.dat") glob.append("awy.dat") longName = string.strip(longName) if longName == '': continue self.supportedVectors[longName] = {'EXTENSIONS': glob, 'LONGNAME': longName, 'SHORTNAME': driverName} return self.supportedVectors
def getSupportedRasters(self): if self.supportedRasters is not None: return self.supportedRasters # first get the GDAL driver manager if gdal.GetDriverCount() == 0: gdal.AllRegister() self.supportedRasters = dict() jp2Driver = None # for each loaded GDAL driver for i in range(gdal.GetDriverCount()): driver = gdal.GetDriver(i) if driver is None: QgsLogger.warning("unable to get driver " + str(i)) continue # now we need to see if the driver is for something currently # supported; if not, we give it a miss for the next driver longName = string.strip(re.sub('\(.*$', '', driver.LongName)) shortName = string.strip(re.sub('\(.*$', '', driver.ShortName)) extensions = '' description = driver.GetDescription() glob = [] metadata = driver.GetMetadata() if gdal.DMD_EXTENSION in metadata: extensions = str(metadata[gdal.DMD_EXTENSION]) if longName != '': if extensions != '': # XXX add check for SDTS; in that case we want (*CATD.DDF) #TODO fix and test #glob.append( QString("*." + extensions.replace("/", " *.")).split(" ")) glob.append(string.split("*." + string.replace(extensions, "/", " *."), sep=(" "))) # Add only the first JP2 driver found to the filter list (it's the one GDAL uses) if description == "JPEG2000" or description.startswith("JP2"): # JP2ECW, JP2KAK, JP2MrSID if jp2Driver is not None: continue # skip if already found a JP2 driver jp2Driver = driver # first JP2 driver found glob.append("*.j2k") # add alternate extension elif description == "GTiff": glob.append("*.tiff") elif description == "JPEG": glob.append("*.jpeg") else: # USGS DEMs use "*.dem" if description.startswith("USGSDEM"): glob.append("*.dem") elif description.startswith("DTED"): # DTED use "*.dt0" glob.append("*.dt0") elif description.startswith("MrSID"): # MrSID use "*.sid" glob.append("*.sid") else: continue self.supportedRasters[longName] = {'EXTENSIONS': glob, 'LONGNAME': longName, 'SHORTNAME': shortName, 'DESCRIPTION': description} return self.supportedRasters