def test_log_to_file_from_thread(self): """Utils: Verify that we can filter from a threading.Thread.""" from natcap.invest.utils import log_to_file logfile = os.path.join(self.workspace, 'logfile.txt') def _log_from_other_thread(): thread_logger = logging.getLogger() thread_logger.info('this should be logged') local_logger = logging.getLogger() thread = threading.Thread(target=_log_from_other_thread) with log_to_file(logfile, threadname=thread.name) as handler: thread.start() local_logger.info('this should not be logged') local_logger.info('neither should this message') thread.join() handler.flush() messages = [ msg for msg in open(logfile).read().split('\n') if msg if msg ] self.assertEqual(len(messages), 1)
def test_log_to_file_no_thread(self): """Utils: Verify that we can exclude messages not from this thread.""" from natcap.invest.utils import log_to_file logfile = os.path.join(self.workspace, 'logfile.txt') def _log_from_other_thread(): thread_logger = logging.getLogger() thread_logger.info('this should not be logged') local_logger = logging.getLogger() # create the file before we log to it, so we know a warning should # be logged. with open(logfile, 'w') as new_file: new_file.write(' ') with log_to_file(logfile) as handler: thread = threading.Thread(target=_log_from_other_thread) thread.start() local_logger.info('this should be logged') local_logger.info('this should also be logged') thread.join() handler.flush() messages = [ msg for msg in open(logfile).read().split('\n') if msg if msg ] self.assertEqual(len(messages), 2)
def test_bounding_boxes(self): """Usage logger test that we can extract bounding boxes.""" from natcap.invest import utils from natcap.invest.ui import usage srs = osr.SpatialReference() srs.ImportFromEPSG(32731) # WGS84 / UTM zone 31s srs_wkt = srs.ExportToWkt() raster_path = os.path.join(self.workspace_dir, 'raster.tif') driver = gdal.GetDriverByName('GTiff') raster_array = numpy.ones((20, 20)) raster = driver.Create(raster_path, raster_array.shape[1], raster_array.shape[0], 1, gdal.GDT_Byte, options=('TILED=YES', 'BIGTIFF=YES', 'COMPRESS=LZW', 'BLOCKXSIZE=256', 'BLOCKYSIZE=256')) raster.SetProjection(srs_wkt) raster_band = raster.GetRasterBand(1) raster_band.WriteArray(raster_array) raster_band.SetNoDataValue(255) raster_geotransform = [2, 2, 0, -2, 0, -2] raster.SetGeoTransform(raster_geotransform) raster = None vector_path = os.path.join(self.workspace_dir, 'vector.gpkg') pygeoprocessing.testing.create_vector_on_disk( [shapely.geometry.LineString([(4, -4), (10, -10)])], projection=srs_wkt, vector_format='GPKG', filename=vector_path) model_args = { 'raster': raster_path, 'vector': vector_path, 'not_a_gis_input': 'foobar' } output_logfile = os.path.join(self.workspace_dir, 'logfile.txt') with utils.log_to_file(output_logfile): bb_inter, bb_union = usage._calculate_args_bounding_box(model_args) numpy.testing.assert_allclose( bb_inter, [-87.234108, -85.526151, -87.233424, -85.526205]) numpy.testing.assert_allclose( bb_union, [-87.237771, -85.526132, -87.23321, -85.526491]) # Verify that no errors were raised in calculating the bounding boxes. self.assertTrue('ERROR' not in open(output_logfile).read(), 'Exception logged when there should not have been.')
def test_log_warnings(self): """utils: test that we can capture GDAL warnings to logging.""" from natcap.invest import utils logfile = os.path.join(self.workspace, 'logfile.txt') # this warning should go to stdout. gdal.Open('this_file_should_not_exist.tif') with utils.log_to_file(logfile) as handler: with utils.capture_gdal_logging(): # warning should be captured. gdal.Open('file_file_should_also_not_exist.tif') handler.flush() # warning should go to stdout gdal.Open('this_file_should_not_exist.tif') messages = [msg for msg in open(logfile).read().split('\n') if msg] self.assertEqual(len(messages), 1)
def test_bounding_boxes(self): """Usage logger test that we can extract bounding boxes.""" from natcap.invest import utils from natcap.invest.ui import usage freshwater_dir = os.path.join(SAMPLE_DATA, 'Base_Data', 'Freshwater') model_args = { 'raster': os.path.join(freshwater_dir, 'dem'), 'vector': os.path.join(freshwater_dir, 'subwatersheds.shp'), 'not_a_gis_input': 'foobar' } output_logfile = os.path.join(self.workspace_dir, 'logfile.txt') with utils.log_to_file(output_logfile): bb_inter, bb_union = usage._calculate_args_bounding_box(model_args) numpy.testing.assert_allclose( bb_inter, [-123.584877, 44.273852, -123.400091, 44.726233]) numpy.testing.assert_allclose( bb_union, [-123.658275, 44.415778, -123.253863, 44.725814]) # Verify that no errors were raised in calculating the bounding boxes. self.assertTrue('ERROR' not in open(output_logfile).read(), 'Exception logged when there should not have been.')