Skip to content

Commit

Permalink
Use machine epsilon in transform.rowcol (#2106)
Browse files Browse the repository at this point in the history
* Use machine epsilon.

* Use window rounding methods.

* Update test checksums

* Update test expected value
  • Loading branch information
groutr committed Feb 10, 2021
1 parent f407e9a commit ceb0e45
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
11 changes: 5 additions & 6 deletions rasterio/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,15 +308,15 @@ def nullcontext(obj):
)
logger.debug("Src %s window: %r", src.name, src_window)

src_window = src_window.round_shape()

# 3. Compute the destination window
dst_window = windows.from_bounds(
int_w, int_s, int_e, int_n, output_transform, precision=precision
)

# 4. Read data in source window into temp
trows, tcols = (int(round(dst_window.height)), int(round(dst_window.width)))
src_window = src_window.round_shape(pixel_precision=0)
dst_window = dst_window.round_shape(pixel_precision=0)
trows, tcols = dst_window.height, dst_window.width
temp_shape = (src_count, trows, tcols)
temp = src.read(
out_shape=temp_shape,
Expand All @@ -328,9 +328,8 @@ def nullcontext(obj):
)

# 5. Copy elements of temp into dest
roff, coff = (
int(round(dst_window.row_off)), int(round(dst_window.col_off)))

dst_window = dst_window.round_offsets(pixel_precision=0)
roff, coff = dst_window.row_off, dst_window.col_off
region = dest[:, roff:roff + trows, coff:coff + tcols]

if math.isnan(nodataval):
Expand Down
16 changes: 5 additions & 11 deletions rasterio/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from collections.abc import Iterable
import math
import sys

from affine import Affine

Expand Down Expand Up @@ -218,20 +219,13 @@ def rowcol(transform, xs, ys, op=math.floor, precision=None):
list of column indices
"""

single_x = False
single_y = False
if not isinstance(xs, Iterable):
xs = [xs]
single_x = True
if not isinstance(ys, Iterable):
ys = [ys]
single_y = True

if precision is None:
eps = 0.0
else:
eps = 10.0 ** -precision * (1.0 - 2.0 * op(0.1))

eps = sys.float_info.epsilon

invtransform = ~transform

rows = []
Expand All @@ -241,9 +235,9 @@ def rowcol(transform, xs, ys, op=math.floor, precision=None):
cols.append(op(fcol))
rows.append(op(frow))

if single_x:
if len(xs) == 1:
cols = cols[0]
if single_y:
if len(ys) == 1:
rows = rows[0]

return rows, cols
Expand Down
6 changes: 3 additions & 3 deletions tests/test_rio_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,13 @@ def test_merge_tiny_res_bounds(tiffs, runner):
assert result.exit_code == 0

# Output should be
# [[[0 90]
# [[[120 90]
# [0 0]]]

with rasterio.open(outputname) as src:
data = src.read()
print(data)
assert data[0, 0, 0] == 0
assert data[0, 0, 0] == 120
assert data[0, 0, 1] == 90
assert data[0, 1, 0] == 0
assert data[0, 1, 1] == 0
Expand All @@ -481,7 +481,7 @@ def test_merge_rgb(tmpdir, runner):
assert result.exit_code == 0

with rasterio.open(outputname) as src:
assert [src.checksum(i) for i in src.indexes] == [33219, 35315, 45188]
assert [src.checksum(i) for i in src.indexes] == [25420, 29131, 37860]


def test_merge_tiny_intres(tiffs):
Expand Down

0 comments on commit ceb0e45

Please sign in to comment.