def test_config_file_points_and_borders_pil(self): from pycoast import ContourWriterPIL from pyresample.geometry import AreaDefinition config_file = os.path.join(os.path.dirname(__file__), 'nh_points_pil.ini') grid_img = Image.open( os.path.join(os.path.dirname(__file__), 'nh_points_cfg_pil.png')) grid_data = np.array(grid_img) img = Image.new('RGB', (1024, 1024), (255, 255, 255)) proj4_string = '+proj=laea +lat_0=90 +lon_0=0 +a=6371228.0 +units=m' area_extent = (-5326849.0625, -5326849.0625, 5326849.0625, 5326849.0625) area_def = AreaDefinition('nh', 'nh', 'nh', proj4_string, 1024, 1024, area_extent) cw = ContourWriterPIL(gshhs_root_dir) cw.add_overlay_from_config(config_file, area_def, img) res = np.array(img) self.assertTrue(fft_metric(grid_data, res), 'Writing of nh points failed')
def test_foreground(self): """Test generating a transparent foreground.""" from pycoast import ContourWriterPIL euro_img = Image.open(os.path.join(os.path.dirname(__file__), 'contours_europe_alpha.png')) euro_data = np.array(euro_img) # img = Image.new('RGB', (640, 480)) proj4_string = \ '+proj=stere +lon_0=8.00 +lat_0=50.00 +lat_ts=50.00 +ellps=WGS84' area_extent = (-3363403.31, -2291879.85, 2630596.69, 2203620.1) area_def = FakeAreaDef(proj4_string, area_extent, 640, 480) cw = ContourWriterPIL(gshhs_root_dir) config_file = os.path.join(os.path.dirname(__file__), 'test_data', 'test_config.ini') img = cw.add_overlay_from_config(config_file, area_def) res = np.array(img) self.assertTrue(fft_metric(euro_data, res), 'Writing of contours failed') overlays = {'coasts': {'level': [1, 2, 3, 4], 'resolution': 'l'}, 'borders': {'outline': (255, 0, 0), 'resolution': 'c'}, 'rivers': {'outline': 'blue', 'resolution': 'c', 'level': 5}} img = cw.add_overlay_from_dict(overlays, area_def) res = np.array(img) self.assertTrue(fft_metric(euro_data, res), 'Writing of contours failed')
def test_config_file_coasts_and_grid(self): from pycoast import ContourWriterPIL from pyresample.geometry import AreaDefinition overlay_config = os.path.join(os.path.dirname(__file__), "coasts_and_grid.ini") grid_img = Image.open( os.path.join(os.path.dirname(__file__), 'grid_nh.png')) grid_data = np.array(grid_img) proj_dict = { 'proj': 'laea', 'lat_0': 90.0, 'lon_0': 0.0, 'a': 6371228.0, 'units': 'm' } area_extent = (-5326849.0625, -5326849.0625, 5326849.0625, 5326849.0625) area_def = AreaDefinition('nh', 'nh', 'nh', proj_dict, 425, 425, area_extent) cw = ContourWriterPIL(gshhs_root_dir) overlay = cw.add_overlay_from_config(overlay_config, area_def) img = Image.new('RGB', (425, 425)) img.paste(overlay, mask=overlay) res = np.array(img) self.assertTrue(fft_metric(grid_data, res), 'Writing of nh grid failed')
def test_add_shapefile_shapes(self): from pycoast import ContourWriterPIL grid_img = Image.open(os.path.join(os.path.dirname(__file__), 'brazil_shapefiles.png')) grid_data = np.array(grid_img) img = Image.new('RGB', (425, 425)) proj4_string = '+proj=merc +lon_0=-60 +lat_ts=-30.0 +a=6371228.0 +units=m' area_extent = (-2000000.0, -5000000.0, 5000000.0, 2000000.0) area_def = (proj4_string, area_extent) cw = ContourWriterPIL(gshhs_root_dir) cw.add_coastlines(img, area_def, resolution='l', level=4) cw.add_shapefile_shapes(img, area_def, os.path.join( os.path.dirname(__file__), 'test_data/shapes/Metareas.shp'), outline='red') cw.add_shapefile_shape(img, area_def, os.path.join(os.path.dirname(__file__), 'test_data/shapes/divisao_politica/BR_Regioes.shp'), 3, outline='blue') cw.add_shapefile_shape(img, area_def, os.path.join(os.path.dirname(__file__), 'test_data/shapes/divisao_politica/BR_Regioes.shp'), 4, outline='blue', fill='green') res = np.array(img) self.assertTrue( fft_metric(grid_data, res), 'Writing of Brazil shapefiles failed')
def test_add_points_pil(self): from pycoast import ContourWriterPIL from pyresample.geometry import AreaDefinition font_file = os.path.join(os.path.dirname(__file__), 'test_data', 'DejaVuSerif.ttf') grid_img = Image.open( os.path.join(os.path.dirname(__file__), 'nh_points_pil.png')) grid_data = np.array(grid_img) img = Image.new('RGB', (1024, 1024), (255, 255, 255)) proj4_string = '+proj=laea +lat_0=90 +lon_0=0 +a=6371228.0 +units=m' area_extent = (-5326849.0625, -5326849.0625, 5326849.0625, 5326849.0625) area_def = AreaDefinition('nh', 'nh', 'nh', proj4_string, 1024, 1024, area_extent) cw = ContourWriterPIL(gshhs_root_dir) cw.add_coastlines(img, area_def, outline='black', resolution='l', level=4) cw.add_borders(img, area_def, outline='black', level=1, resolution='c') points_list = [((13.4050, 52.5200), 'Berlin')] cw.add_points(img, area_def, points_list=points_list, font_file=font_file, symbol='asterisk', ptsize=6, outline='red', box_outline='black') points_list = [((12.4964, 41.9028), 'Rome')] cw.add_points(img, area_def, points_list=points_list, font_file=font_file, symbol='square', ptsize=6, outline='blue', fill='yellow', box_outline='black') res = np.array(img) self.assertTrue(fft_metric(grid_data, res), 'Writing of nh points failed')
def test_add_polygon(self): from pycoast import ContourWriterPIL grid_img = Image.open(os.path.join(os.path.dirname(__file__), 'nh_polygons.png')) grid_data = np.array(grid_img) img = Image.new('RGB', (425, 425)) proj4_string = '+proj=laea +lat_0=90 +lon_0=0 +a=6371228.0 +units=m' area_extent = (-5326849.0625, -5326849.0625, 5326849.0625, 5326849.0625) area_def = (proj4_string, area_extent) cw = ContourWriterPIL(gshhs_root_dir) polygons = { 'REYKJAVIK_ATC_A': ((-20.0, 73.0), (0.0, 73.0), (0.0, 61.0), (-30.0, 61.0), (-39.0, 63.5), (-20, 70)), 'REYKJAVIK_ATC_B': ( (-39, 63.5), (-55 + 4 / 6.0, 63.5), (-57 + 45 / 60.0, 65), (-76, 76), (-75, 78), (-60, 82), (0, 90), (30, 82), (0, 82), (0, 73), (-20, 73), (-20, 70)), 'REYKJAVIK_ATC': ( (0.0, 73.0), (0.0, 61.0), (-30.0, 61.0), (-39, 63.5), (-55 + 4 / 6.0, 63.5), (-57 + 45 / 60.0, 65), (-76, 76), (-75, 78), (-60, 82), (0, 90), (30, 82), (0, 82)), 'ICELAND_BOX': ((-25, 62.5), (-25, 67), (-13, 67), (-13, 62.5)) } cw.add_polygon(img, area_def, polygons['REYKJAVIK_ATC'], outline='red') cw.add_polygon(img, area_def, polygons['ICELAND_BOX'], outline='green', fill='gray') cw.add_coastlines(img, area_def, resolution='l', level=4) res = np.array(img) self.assertTrue(fft_metric(grid_data, res), 'Writing of nh polygons failed')
def test_geos(self): from pycoast import ContourWriterPIL geos_img = Image.open( os.path.join(os.path.dirname(__file__), 'contours_geos.png')) geos_data = np.array(geos_img) img = Image.new('RGB', (425, 425)) proj4_string = \ '+proj=geos +lon_0=0.0 +a=6378169.00 +b=6356583.80 +h=35785831.0' area_extent = (-5570248.4773392612, -5567248.074173444, 5567248.074173444, 5570248.4773392612) area_def = (proj4_string, area_extent) cw = ContourWriterPIL(gshhs_root_dir) cw.add_coastlines(img, area_def, resolution='l') res = np.array(img) self.assertTrue(fft_metric(geos_data, res), 'Writing of geos contours failed')
def test_grid_nh(self): from pycoast import ContourWriterPIL grid_img = Image.open( os.path.join(os.path.dirname(__file__), 'grid_nh.png')) grid_data = np.array(grid_img) img = Image.new('RGB', (425, 425)) proj4_string = '+proj=laea +lat_0=90 +lon_0=0 +a=6371228.0 +units=m' area_extent = (-5326849.0625, -5326849.0625, 5326849.0625, 5326849.0625) area_def = (proj4_string, area_extent) cw = ContourWriterPIL(gshhs_root_dir) cw.add_coastlines(img, area_def, resolution='l', level=4) font = ImageFont.truetype( os.path.join(os.path.dirname(__file__), 'test_data', 'DejaVuSerif.ttf'), 10) cw.add_grid(img, area_def, (10.0, 10.0), (2.0, 2.0), font=font, fill='blue', outline='blue', minor_outline='blue', write_text=False, lon_placement='tblr', lat_placement='') res = np.array(img) self.assertTrue(fft_metric(grid_data, res), 'Writing of nh grid failed')
def test_dateline_boundary_cross(self): from pycoast import ContourWriterPIL dl_img = Image.open( os.path.join(os.path.dirname(__file__), 'dateline_boundary_cross.png')) dl_data = np.array(dl_img) img = Image.new('RGB', (640, 480)) proj4_string = \ '+proj=stere +lon_0=140.00 +lat_0=60.00 +lat_ts=50.00 +ellps=WGS84' area_extent = (-3363403.31, -2291879.85, 2630596.69, 2203620.1) area_def = (proj4_string, area_extent) cw = ContourWriterPIL(gshhs_root_dir) cw.add_coastlines(img, area_def, resolution='l', level=4) font = ImageFont.truetype( os.path.join(os.path.dirname(__file__), 'test_data', 'DejaVuSerif.ttf'), 16) cw.add_grid(img, area_def, (10.0, 10.0), (2.0, 2.0), font=font, fill='blue', outline='blue', minor_outline='blue', write_text=False, lon_placement='b', lat_placement='lr') res = np.array(img) self.assertTrue(fft_metric(dl_data, res), 'Writing of dateline boundary crossing data failed')
def test_grid_file(self): from pycoast import ContourWriterPIL grid_img = Image.open( os.path.join(os.path.dirname(__file__), 'grid_europe.png')) grid_data = np.array(grid_img) proj4_string = \ '+proj=stere +lon_0=8.00 +lat_0=50.00 +lat_ts=50.00 +ellps=WGS84' area_extent = (-3363403.31, -2291879.85, 2630596.69, 2203620.1) area_def = (proj4_string, area_extent) cw = ContourWriterPIL(gshhs_root_dir) cw.add_coastlines_to_file(grid_file, area_def, resolution='l', level=4) font = ImageFont.truetype( os.path.join(os.path.dirname(__file__), 'test_data', 'DejaVuSerif.ttf'), 16) cw.add_grid_to_file(grid_file, area_def, (10.0, 10.0), (2.0, 2.0), font=font, fill='blue', write_text=False, outline='blue', minor_outline='blue') img = Image.open(grid_file) res = np.array(img) self.assertTrue(fft_metric(grid_data, res), 'Writing of grid failed')
def test_grid_germ(self): """Check that issue #26 is fixed.""" from pycoast import ContourWriterPIL result_file = os.path.join(os.path.dirname(__file__), 'grid_germ.png') grid_img = Image.open(result_file) grid_data = np.array(grid_img) img = Image.new('RGB', (1024, 1024)) proj4_string = \ '+proj=stere +ellps=bessel +lat_0=90.0 +lon_0=5.0 +lat_ts=50.0 +a=6378144.0 +b=6356759.0' area_extent = [ -155100.436345, -4441495.37946, 868899.563655, -3417495.37946 ] area_def = (proj4_string, area_extent) cw = ContourWriterPIL(gshhs_root_dir) cw.add_coastlines(img, area_def, resolution='l', level=4) font = ImageFont.truetype( os.path.join(os.path.dirname(__file__), 'test_data', 'DejaVuSerif.ttf'), 16) cw.add_grid(img, area_def, (10.0, 10.0), (2.0, 2.0), font=font, fill='yellow', write_text=True, outline='red', minor_outline='white') res = np.array(img) self.assertTrue(fft_metric(grid_data, res), 'Writing of grid to germ failed')
def test_europe(self): from pycoast import ContourWriterPIL euro_img = Image.open( os.path.join(os.path.dirname(__file__), 'contours_europe.png')) euro_data = np.array(euro_img) img = Image.new('RGB', (640, 480)) proj4_string = \ '+proj=stere +lon_0=8.00 +lat_0=50.00 +lat_ts=50.00 +ellps=WGS84' area_extent = (-3363403.31, -2291879.85, 2630596.69, 2203620.1) area_def = (proj4_string, area_extent) cw = ContourWriterPIL(gshhs_root_dir) cw.add_coastlines(img, area_def, resolution='l', level=4) cw.add_rivers(img, area_def, level=5, outline='blue') cw.add_borders(img, area_def, outline=(255, 0, 0)) res = np.array(img) self.assertTrue(fft_metric(euro_data, res), 'Writing of contours failed')
def test_caching_with_param_changes(self, tmpdir): """Testing caching when changing parameters.""" from pycoast import ContourWriterPIL # img = Image.new('RGB', (640, 480)) proj4_string = \ '+proj=stere +lon_0=8.00 +lat_0=50.00 +lat_ts=50.00 +ellps=WGS84' area_extent = (-3363403.31, -2291879.85, 2630596.69, 2203620.1) area_def = FakeAreaDef(proj4_string, area_extent, 640, 480) cw = ContourWriterPIL(gshhs_root_dir) font = ImageFont.truetype( os.path.join(os.path.dirname(__file__), 'test_data', 'DejaVuSerif.ttf')) overlays = { 'cache': { 'file': os.path.join(tmpdir, 'pycoast_cache') }, 'grid': { 'font': font } } # Create the original cache file cw.add_overlay_from_dict(overlays, area_def) cache_glob = glob(os.path.join(tmpdir, 'pycoast_cache_*.png')) assert len(cache_glob) == 1 cache_filename = cache_glob[0] assert os.path.isfile(cache_filename) mtime = os.path.getmtime(cache_filename) # Reuse the generated cache file cw.add_overlay_from_dict(overlays, area_def) cache_glob = glob(os.path.join(tmpdir, 'pycoast_cache_*.png')) assert len(cache_glob) == 1 assert os.path.isfile(cache_filename) assert os.path.getmtime(cache_filename) == mtime # Remove the font option, should produce the same result # font is not considered when caching del overlays['grid']['font'] cw.add_overlay_from_dict(overlays, area_def) cache_glob = glob(os.path.join(tmpdir, 'pycoast_cache_*.png')) assert len(cache_glob) == 1 assert os.path.isfile(cache_filename) assert os.path.getmtime(cache_filename) == mtime # Changing a parameter should create a new cache file overlays = { 'cache': { 'file': os.path.join(tmpdir, 'pycoast_cache') }, 'grid': { 'width': 2.0 } } cw.add_overlay_from_dict(overlays, area_def) cache_glob = glob(os.path.join(tmpdir, 'pycoast_cache_*.png')) assert len(cache_glob) == 2 assert os.path.isfile(cache_filename) new_cache_filename = cache_glob[ 0] if cache_glob[0] != cache_filename else cache_glob[1] # original cache file should be unchanged assert os.path.getmtime(cache_filename) == mtime # new cache file should be...new assert os.path.getmtime(new_cache_filename) != mtime
def test_cache(self, tmpdir): """Test generating a transparent foreground and cache it.""" from pycoast import ContourWriterPIL euro_img = Image.open( os.path.join(os.path.dirname(__file__), 'contours_europe_alpha.png')) euro_data = np.array(euro_img) proj4_string = \ '+proj=stere +lon_0=8.00 +lat_0=50.00 +lat_ts=50.00 +ellps=WGS84' area_extent = (-3363403.31, -2291879.85, 2630596.69, 2203620.1) area_def = FakeAreaDef(proj4_string, area_extent, 640, 480) cw = ContourWriterPIL(gshhs_root_dir) overlays = { 'cache': { 'file': os.path.join(tmpdir, 'pycoast_cache') }, 'coasts': { 'level': 4, 'resolution': 'l' }, 'borders': { 'outline': (255, 0, 0), 'resolution': 'c' }, 'rivers': { 'outline': 'blue', 'resolution': 'c', 'level': 5 } } # Create the original cache file img = cw.add_overlay_from_dict(overlays, area_def) res = np.array(img) cache_glob = glob(os.path.join(tmpdir, 'pycoast_cache_*.png')) assert len(cache_glob) == 1 cache_filename = cache_glob[0] assert fft_metric(euro_data, res), 'Writing of contours failed' assert os.path.isfile(cache_filename) mtime = os.path.getmtime(cache_filename) # Reuse the generated cache file img = cw.add_overlay_from_dict(overlays, area_def) res = np.array(img) assert fft_metric(euro_data, res), 'Writing of contours failed' assert os.path.isfile(cache_filename) assert os.path.getmtime(cache_filename) == mtime # Regenerate cache file current_time = time.time() cw.add_overlay_from_dict(overlays, area_def, current_time) mtime = os.path.getmtime(cache_filename) assert mtime > current_time assert fft_metric(euro_data, res), 'Writing of contours failed' cw.add_overlay_from_dict(overlays, area_def, current_time) assert os.path.getmtime(cache_filename) == mtime assert fft_metric(euro_data, res), 'Writing of contours failed' overlays['cache']['regenerate'] = True cw.add_overlay_from_dict(overlays, area_def) assert os.path.getmtime(cache_filename) != mtime assert fft_metric(euro_data, res), 'Writing of contours failed' overlays.pop('cache') overlays['grid'] = { 'outline': (255, 255, 255), 'outline_opacity': 175, 'minor_outline': (200, 200, 200), 'minor_outline_opacity': 127, 'width': 1.0, 'minor_width': 0.5, 'minor_is_tick': True, 'write_text': True, 'lat_placement': 'lr', 'lon_placement': 'b' } cw.add_overlay_from_dict(overlays, area_def) os.remove(cache_filename)
def test_cache(self): """Test generating a transparent foreground and cache it.""" from pycoast import ContourWriterPIL from tempfile import gettempdir euro_img = Image.open( os.path.join(os.path.dirname(__file__), 'contours_europe_alpha.png')) euro_data = np.array(euro_img) # img = Image.new('RGB', (640, 480)) proj4_string = \ '+proj=stere +lon_0=8.00 +lat_0=50.00 +lat_ts=50.00 +ellps=WGS84' area_extent = (-3363403.31, -2291879.85, 2630596.69, 2203620.1) area_def = FakeAreaDef(proj4_string, area_extent, 640, 480) cw = ContourWriterPIL(gshhs_root_dir) tmp = gettempdir() overlays = { 'cache': { 'file': os.path.join(tmp, 'pycoast_cache') }, 'coasts': { 'level': 4, 'resolution': 'l' }, 'borders': { 'outline': (255, 0, 0), 'resolution': 'c' }, 'rivers': { 'outline': 'blue', 'resolution': 'c', 'level': 5 } } cache_filename = os.path.join(tmp, 'pycoast_cache_fakearea.png') img = cw.add_overlay_from_dict(overlays, area_def) res = np.array(img) self.assertTrue(fft_metric(euro_data, res), 'Writing of contours failed') self.assertTrue(os.path.isfile(cache_filename)) current_time = time.time() img = cw.add_overlay_from_dict(overlays, area_def, current_time) mtime = os.path.getmtime(cache_filename) self.assertGreater(mtime, current_time) self.assertTrue(fft_metric(euro_data, res), 'Writing of contours failed') img = cw.add_overlay_from_dict(overlays, area_def, current_time) self.assertEqual(os.path.getmtime(cache_filename), mtime) self.assertTrue(fft_metric(euro_data, res), 'Writing of contours failed') overlays['cache']['regenerate'] = True img = cw.add_overlay_from_dict(overlays, area_def) self.assertNotEqual(os.path.getmtime(cache_filename), mtime) self.assertTrue(fft_metric(euro_data, res), 'Writing of contours failed') overlays.pop('cache') overlays['grid'] = { 'outline': (255, 255, 255), 'outline_opacity': 175, 'minor_outline': (200, 200, 200), 'minor_outline_opacity': 127, 'width': 1.0, 'minor_width': 0.5, 'minor_is_tick': True, 'write_text': True, 'lat_placement': 'lr', 'lon_placement': 'b' } img = cw.add_overlay_from_dict(overlays, area_def) os.remove(os.path.join(tmp, 'pycoast_cache_fakearea.png'))