Skip to content

Commit

Permalink
Merge pull request #83 from avalentino/fontpath
Browse files Browse the repository at this point in the history
  • Loading branch information
djhoese committed Jan 9, 2023
2 parents 1e530ff + c2f9e43 commit 8589e6d
Showing 1 changed file with 35 additions and 49 deletions.
84 changes: 35 additions & 49 deletions pycoast/tests/test_pycoast.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
test_file = "test_image.png"
grid_file = "test_grid.png"
p_file_coasts = "test_coasts_p_mode.png"
font_path = os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf")


def fft_proj_rms(a1, a2):
Expand Down Expand Up @@ -326,7 +327,7 @@ def test_grid(self, cw_pil):
area_def = EUROPE

cw_pil.add_coastlines(img, area_def, resolution="l", level=4)
font = ImageFont.truetype(os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf"), 16)
font = ImageFont.truetype(font_path, 16)
cw_pil.add_grid(
img,
area_def,
Expand All @@ -351,7 +352,7 @@ def test_grid_germ(self, cw_pil):
area_def = GERM

cw_pil.add_coastlines(img, area_def, resolution="l", level=4)
font = ImageFont.truetype(os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf"), 16)
font = ImageFont.truetype(font_path, 16)
cw_pil.add_grid(
img,
area_def,
Expand Down Expand Up @@ -415,7 +416,7 @@ def test_grid_file(self, cw_pil):
area_def = EUROPE

cw_pil.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)
font = ImageFont.truetype(font_path, 16)
cw_pil.add_grid_to_file(
grid_file,
area_def,
Expand All @@ -440,7 +441,7 @@ def test_dateline_cross(self, cw_pil):
area_def = DATELINE_1

cw_pil.add_coastlines(img, area_def, resolution="l", level=4)
font = ImageFont.truetype(os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf"), 16)
font = ImageFont.truetype(font_path, 16)
cw_pil.add_grid(
img,
area_def,
Expand All @@ -466,7 +467,7 @@ def test_dateline_boundary_cross(self, cw_pil):
area_def = DATELINE_2

cw_pil.add_coastlines(img, area_def, resolution="l", level=4)
font = ImageFont.truetype(os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf"), 16)
font = ImageFont.truetype(font_path, 16)
cw_pil.add_grid(
img,
area_def,
Expand All @@ -491,7 +492,7 @@ def test_grid_nh(self, cw_pil):
area_def = NH

cw_pil.add_coastlines(img, area_def, resolution="l", level=4)
font = ImageFont.truetype(os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf"), 10)
font = ImageFont.truetype(font_path, 10)
cw_pil.add_grid(
img,
area_def,
Expand Down Expand Up @@ -563,7 +564,6 @@ def test_add_polygon(self, cw_pil):
assert fft_metric(grid_data, res), "Writing of nh polygons failed"

def test_add_points_pil(self, cw_pil):
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)

Expand All @@ -579,7 +579,7 @@ def test_add_points_pil(self, cw_pil):
img,
area_def,
points_list=points_list,
font_file=font_file,
font_file=font_path,
symbol="asterisk",
ptsize=6,
outline="red",
Expand All @@ -591,7 +591,7 @@ def test_add_points_pil(self, cw_pil):
img,
area_def,
points_list=points_list,
font_file=font_file,
font_file=font_path,
symbol="square",
ptsize=6,
outline="blue",
Expand All @@ -604,8 +604,6 @@ def test_add_points_pil(self, cw_pil):

def test_add_points_coordinate_conversion(self, cw_pil):
"""Check that a point added with lonlat coordinates matches the same point in pixel coordinates."""
font_file = os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf")

shape = (512, 1024)
area_def = nh_def(shape)
lonlat_coords = (13.4050, 52.5200)
Expand All @@ -619,7 +617,7 @@ def test_add_points_coordinate_conversion(self, cw_pil):
img1,
area_def,
points_list=points_list,
font_file=font_file,
font_file=font_path,
symbol="asterisk",
ptsize=6,
outline="red",
Expand All @@ -636,7 +634,7 @@ def test_add_points_coordinate_conversion(self, cw_pil):
img2,
area_def,
points_list=points_list,
font_file=font_file,
font_file=font_path,
symbol="asterisk",
ptsize=6,
outline="red",
Expand All @@ -647,8 +645,6 @@ def test_add_points_coordinate_conversion(self, cw_pil):
np.testing.assert_allclose(res1, res2)

def test_add_points_bad_image_coords(self, cw_pil):
font_file = os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf")

shape = (512, 1024)
area_def = nh_def(shape)
for pixel_colrow in (shape[::-1], (-10000, -10000)):
Expand All @@ -659,7 +655,7 @@ def test_add_points_bad_image_coords(self, cw_pil):
img1,
area_def,
points_list=points_list,
font_file=font_file,
font_file=font_path,
symbol="asterisk",
ptsize=6,
outline="red",
Expand All @@ -669,8 +665,6 @@ def test_add_points_bad_image_coords(self, cw_pil):
np.testing.assert_allclose(res1, 255) # no added points

def test_add_points_bad_coord_ref(self, cw_pil):
font_file = os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf")

shape = (512, 1024)
area_def = nh_def(shape)
img1 = Image.new("RGB", shape[::-1], (255, 255, 255))
Expand All @@ -680,7 +674,7 @@ def test_add_points_bad_coord_ref(self, cw_pil):
img1,
area_def,
points_list=points_list,
font_file=font_file,
font_file=font_path,
symbol="asterisk",
ptsize=6,
outline="red",
Expand Down Expand Up @@ -754,7 +748,6 @@ def test_config_file_points_and_borders_pil(self, cw_pil):
assert fft_metric(grid_data, res), "Writing of nh points failed"

def test_add_cities_pil(self, cw_pil):
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_cities_pil.png"))
grid_data = np.array(grid_img)

Expand All @@ -770,7 +763,7 @@ def test_add_cities_pil(self, cw_pil):
img,
area_def,
cities_list=cities_list,
font_file=font_file,
font_file=font_path,
font_size=20,
symbol="square",
ptsize=16,
Expand Down Expand Up @@ -813,7 +806,6 @@ def test_add_cities_from_dict_pil(self, cw_pil):
overlays = {}
overlays["coasts"] = {"level": 4, "resolution": "l", "outline": "black"}
overlays["borders"] = {"level": 1, "outline": "black", "resolution": "c"}
font = "pycoast/tests/test_data/DejaVuSerif.ttf"
cities_list1 = [
"Berlin",
"Paris",
Expand All @@ -828,7 +820,7 @@ def test_add_cities_from_dict_pil(self, cw_pil):
cities_list3 = ["Belp", "Bad Schwalbach", "Edinburgh", "Hilversum"]
cities_type1 = {
"cities_list": cities_list1,
"font": font,
"font": font_path,
"font_size": 26,
"symbol": "circle",
"ptsize": 24,
Expand All @@ -838,7 +830,7 @@ def test_add_cities_from_dict_pil(self, cw_pil):
}
cities_type2 = {
"cities_list": cities_list2,
"font": font,
"font": font_path,
"font_size": 24,
"symbol": "pentagon",
"ptsize": 24,
Expand All @@ -848,7 +840,7 @@ def test_add_cities_from_dict_pil(self, cw_pil):
}
cities_type3 = {
"cities_list": cities_list3,
"font": font,
"font": font_path,
"font_size": 22,
"symbol": "star5",
"ptsize": 35,
Expand Down Expand Up @@ -917,7 +909,7 @@ def test_add_grid_from_dict_pil(self, cw_pil):
img = Image.new("RGB", (800, 800))
area_def = uk_and_ireland()

font = ImageFont.truetype(os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf"), 40)
font = ImageFont.truetype(font_path, 40)

overlays = {}
overlays["coasts"] = {"width": 3.0, "level": 4, "resolution": "l"}
Expand Down Expand Up @@ -950,9 +942,8 @@ def test_western_shapes_pil(self, cw_pil):
grid_data = np.array(grid_img)
img = Image.new("RGB", (1000, 560))
area_def = north_atlantic()

cw_pil.add_coastlines(img, area_def, resolution="l", level=2)
font = ImageFont.truetype(os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf"), 16)
font = ImageFont.truetype(font_path, 16)

cw_pil.add_grid(
img,
Expand All @@ -979,7 +970,7 @@ def test_eastern_shapes_pil(self, cw_pil):
area_def = eurasia()

cw_pil.add_coastlines(img, area_def, resolution="l", level=2)
font = ImageFont.truetype(os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf"), 20)
font = ImageFont.truetype(font_path, 20)

cw_pil.add_grid(
img,
Expand Down Expand Up @@ -1007,7 +998,7 @@ def test_no_h_scratch_pil(self, cw_pil):
area_def = bering_straight()

cw_pil.add_coastlines(img, area_def, resolution="l", level=2)
font = ImageFont.truetype(os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf"), 20)
font = ImageFont.truetype(font_path, 20)

cw_pil.add_grid(
img,
Expand Down Expand Up @@ -1037,7 +1028,7 @@ def test_no_v_scratch_pil(self, cw_pil):
area_def = hawaii()

cw_pil.add_coastlines(img, area_def, resolution="l", level=2)
font = ImageFont.truetype(os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf"), 20)
font = ImageFont.truetype(font_path, 20)

cw_pil.add_grid(
img,
Expand Down Expand Up @@ -1167,7 +1158,7 @@ def test_grid_agg_txt(self):
cw.add_coastlines(img, area_def, resolution="l", level=4)
font = aggdraw.Font(
"blue",
os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf"),
font_path,
size=16,
opacity=200,
)
Expand Down Expand Up @@ -1267,7 +1258,7 @@ def test_grid_nh_agg(self):
cw.add_coastlines(img, area_def, resolution="l", level=4)
font = aggdraw.Font(
"blue",
os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf"),
font_path,
size=10,
)
cw.add_grid(
Expand Down Expand Up @@ -1362,8 +1353,6 @@ def test_add_points_agg(self):

from pycoast import ContourWriterAGG

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_agg.png"))
grid_data = np.array(grid_img)

Expand All @@ -1382,7 +1371,7 @@ def test_add_points_agg(self):
img,
area_def,
points_list=points_list,
font_file=font_file,
font_file=font_path,
symbol="circle",
ptsize=16,
outline="black",
Expand Down Expand Up @@ -1516,8 +1505,6 @@ def test_add_cities_agg(self):

from pycoast import ContourWriterAGG

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_cities_agg.png"))
grid_data = np.array(grid_img)

Expand All @@ -1536,7 +1523,7 @@ def test_add_cities_agg(self):
img,
area_def,
cities_list=cities_list,
font_file=font_file,
font_file=font_path,
font_size=20,
symbol="square",
ptsize=16,
Expand Down Expand Up @@ -1595,7 +1582,6 @@ def test_add_cities_from_dict_agg(self):
overlays = {}
overlays["coasts"] = {"level": 4, "resolution": "l", "outline": "black"}
overlays["borders"] = {"level": 1, "outline": "black", "resolution": "c"}
font = "pycoast/tests/test_data/DejaVuSerif.ttf"
cities_list1 = [
"Berlin",
"Paris",
Expand All @@ -1610,7 +1596,7 @@ def test_add_cities_from_dict_agg(self):
cities_list3 = ["Belp", "Bad Schwalbach", "Edinburgh", "Hilversum"]
cities_type1 = {
"cities_list": cities_list1,
"font": font,
"font": font_path,
"font_size": 26,
"symbol": "circle",
"ptsize": 24,
Expand All @@ -1623,7 +1609,7 @@ def test_add_cities_from_dict_agg(self):
}
cities_type2 = {
"cities_list": cities_list2,
"font": font,
"font": font_path,
"font_size": 24,
"symbol": "pentagon",
"ptsize": 24,
Expand All @@ -1636,7 +1622,7 @@ def test_add_cities_from_dict_agg(self):
}
cities_type3 = {
"cities_list": cities_list3,
"font": font,
"font": font_path,
"font_size": 22,
"symbol": "star5",
"ptsize": 35,
Expand Down Expand Up @@ -1738,7 +1724,7 @@ def test_add_grid_from_dict_agg(self):

font = aggdraw.Font(
"yellow",
os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf"),
font_path,
opacity=255,
size=40,
)
Expand Down Expand Up @@ -1787,7 +1773,7 @@ def test_western_shapes_agg(self):
cw.add_coastlines(img, area_def, resolution="l", level=2)
font = aggdraw.Font(
"orange",
os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf"),
font_path,
size=20,
)

Expand Down Expand Up @@ -1830,7 +1816,7 @@ def test_eastern_shapes_agg(self):
cw.add_coastlines(img, area_def, resolution="l", level=2)
font = aggdraw.Font(
"orange",
os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf"),
font_path,
size=20,
)

Expand Down Expand Up @@ -1874,7 +1860,7 @@ def test_no_h_scratch_agg(self):
cw.add_coastlines(img, area_def, resolution="l", level=2)
font = aggdraw.Font(
"yellow",
os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf"),
font_path,
opacity=255,
size=20,
)
Expand Down Expand Up @@ -1921,7 +1907,7 @@ def test_no_v_scratch_agg(self):
cw.add_coastlines(img, area_def, resolution="l", level=2)
font = aggdraw.Font(
"yellow",
os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf"),
font_path,
opacity=255,
size=20,
)
Expand Down Expand Up @@ -2069,7 +2055,7 @@ def test_caching_with_param_changes(self, tmpdir):
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"))
font = ImageFont.truetype(font_path)
overlays = {
"cache": {"file": os.path.join(tmpdir, "pycoast_cache")},
"grid": {"font": font},
Expand Down

0 comments on commit 8589e6d

Please sign in to comment.