Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions folium/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def mercator_transform(
lat_bounds : length 2 tuple
Minimal and maximal value of the latitude of the image.
Bounds must be between -85.051128779806589 and 85.051128779806589
otherwise they will be clipped to that values.
otherwise the image will be clipped to that range.

origin : ['upper' | 'lower'], optional, default 'upper'
Place the [0,0] index of the array in the upper left or lower left
Expand Down Expand Up @@ -261,8 +261,9 @@ def mercator(x):
if origin == "upper":
array = array[::-1, :, :]

lats = lat_min + np.linspace(0.5 / height, 1.0 - 0.5 / height, height) * (
lat_max - lat_min
# The input rows span the full lat_bounds; only the output is clipped.
lats = lat_bounds[0] + np.linspace(0.5 / height, 1.0 - 0.5 / height, height) * (
lat_bounds[1] - lat_bounds[0]
)
latslats = mercator(lat_min) + np.linspace(
0.5 / height_out, 1.0 - 0.5 / height_out, height_out
Expand Down
2 changes: 1 addition & 1 deletion tests/test_raster_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_image_overlay():
url = (
"data:image/png;base64,"
"iVBORw0KGgoAAAANSUhEUgAAAAMAAAACCAYAAACddGYaAAA"
"AF0lEQVR42mP4z8AARFDw/z/DeiA5H4QBV60H6ABl9ZIAAAAASUVORK5CYII="
"AF0lEQVR42mP4z8AARFDw/z/DciC5DoQBV78H/mpWZYkAAAAASUVORK5CYII="
)
assert io.url == url

Expand Down
19 changes: 19 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
get_obj_in_upper_tree,
if_pandas_df_convert_to_numpy,
javascript_identifier_path_to_array_notation,
mercator_transform,
normalize_bounds_type,
parse_font_size,
parse_options,
Expand Down Expand Up @@ -292,3 +293,21 @@ def test_parse_font_size_valid(value, expected):
def test_parse_font_size_invalid(value, error_message):
with pytest.raises(ValueError, match=error_message):
parse_font_size(value)


def test_mercator_transform_clips_bounds_beyond_mercator_limit():
# Each row holds the latitude of its centre, from -89.5 (bottom) to 89.5.
height = 180
lats = np.linspace(-90, 90, height + 1)[:-1] + 0.5
data = np.repeat(lats[:, None], 2, axis=1)

out = mercator_transform(data, (-90, 90), origin="lower")

# The output only covers the Web Mercator range, so every output row must
# hold (up to interpolation, well within one 1 degree input pixel) the
# latitude of its own centre: rows beyond +-85.05 are dropped, not
# squeezed into the output.
# Output row centres in Web Mercator y (degrees); +-85.05 maps to +-180.
y = np.linspace(-180, 180, height + 1)[:-1] + 180 / height
expected = np.degrees(np.arctan(np.sinh(np.radians(y))))
np.testing.assert_allclose(out[:, 0, 0], expected, atol=0.05)