Skip to content

Commit

Permalink
FIX: Add half-pixel shift to img_transform y coordinate
Browse files Browse the repository at this point in the history
The y-coordinates input to the bounds calculation are the centers.
The x-coordinates are shifted by half a pixel already, but we
also need to do that to the y-coordinates otherwise we get some
anomalous masking at the bottom/top of the images.
  • Loading branch information
greglucas committed Oct 24, 2024
1 parent 1a36e4e commit 18901f8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
4 changes: 3 additions & 1 deletion lib/cartopy/img_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ def _determine_bounds(x_coords, y_coords, source_cs):
bounds['x'].append([x_coords.min() - half_px,
x_coords.max() + half_px])

bounds['y'] = [y_coords.min(), y_coords.max()]
# y_coords are the centers, so adjust a half-pixel out in y too
half_px = abs(np.diff(y_coords, axis=0)).max() / 2.
bounds['y'] = [y_coords.min() - half_px, y_coords.max() + half_px]
return bounds


Expand Down
2 changes: 1 addition & 1 deletion lib/cartopy/tests/mpl/test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def test_imshow():

@pytest.mark.natural_earth
@pytest.mark.mpl_image_compare(filename='imshow_regional_projected.png',
tolerance=0.8)
tolerance=1.96)
def test_imshow_projected():
source_proj = ccrs.PlateCarree()
img_extent = (-120.67660000000001, -106.32104523100001,
Expand Down
16 changes: 8 additions & 8 deletions lib/cartopy/tests/test_img_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ def test_gridding_data_std_range():
mask_extrapolated=True)

# The expected image. n.b. on a map the data is reversed in the y axis.
expected = np.array([[3, 3, 3, 3, 3, 3, 3, 3],
[3, 1, 2, 2, 2, 3, 3, 3],
[1, 1, 1, 2, 2, 2, 3, 1],
[1, 1, 1, 1, 1, 1, 1, 1]], dtype=np.float64)
expected = np.array([[1, 1, 2, 2, 3, 3, 3, 3],
[1, 1, 2, 2, 2, 3, 3, 3],
[1, 1, 1, 2, 2, 2, 3, 3],
[1, 1, 1, 2, 2, 2, 3, 3]], dtype=np.float64)

expected_mask = np.array(
[[True, True, True, True, True, True, True, True],
[[True, False, False, False, False, False, False, True],
[True, False, False, False, False, False, False, True],
[True, False, False, False, False, False, False, True],
[True, True, True, True, True, True, True, True]])
[True, False, False, False, False, False, False, True]])

assert_array_equal([-180, 180, -90, 90], extent)
assert_array_equal(expected, image)
Expand Down Expand Up @@ -108,10 +108,10 @@ def test_gridding_data_outside_projection():
[3, 3, 3, 1, 1, 1, 1, 1]], dtype=np.float64)

expected_mask = np.array(
[[True, True, True, True, True, True, True, True],
[[False, False, True, True, True, True, False, False],
[False, False, True, True, True, True, False, False],
[False, False, True, True, True, True, False, False],
[True, True, True, True, True, True, True, True]])
[False, False, True, True, True, True, False, False]])

assert_array_equal([-180, 180, -90, 90], extent)
assert_array_equal(expected, image)
Expand Down

0 comments on commit 18901f8

Please sign in to comment.