本文整理匯總了Python中rasterio.windows.Window.from_slices方法的典型用法代碼示例。如果您正苦於以下問題:Python Window.from_slices方法的具體用法?Python Window.from_slices怎麽用?Python Window.from_slices使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類rasterio.windows.Window
的用法示例。
在下文中一共展示了Window.from_slices方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_window_fromslices
# 需要導入模塊: from rasterio.windows import Window [as 別名]
# 或者: from rasterio.windows.Window import from_slices [as 別名]
def test_window_fromslices(col_off, row_off, col_stop, row_stop):
"""Empty and non-empty absolute windows from slices, tuples, or lists
are valid"""
# Constrain windows to >= 0 in each dimension
assume(col_stop >= col_off)
assume(row_stop >= row_off)
rows = (row_off, row_stop)
cols = (col_off, col_stop)
expected = (col_off, row_off, col_stop - col_off, row_stop - row_off)
assert np.allclose(
Window.from_slices(rows=slice(*rows), cols=slice(*cols)).flatten(),
expected
)
assert np.allclose(
Window.from_slices(rows=rows, cols=cols).flatten(),
expected
)
assert np.allclose(
Window.from_slices(rows=list(rows), cols=list(cols)).flatten(),
expected
)
示例2: test_window_fromslices_negative_start_missing_dim_err
# 需要導入模塊: from rasterio.windows import Window [as 別名]
# 或者: from rasterio.windows.Window import from_slices [as 別名]
def test_window_fromslices_negative_start_missing_dim_err():
"""Should raise error if width or height are not provided"""
with pytest.raises(WindowError):
Window.from_slices(rows=(-10, 4), cols=(0, 4))
with pytest.raises(WindowError):
Window.from_slices(rows=(0, 4), cols=(-10, 4))
示例3: test_window_fromslices_implicit_err
# 需要導入模塊: from rasterio.windows import Window [as 別名]
# 或者: from rasterio.windows.Window import from_slices [as 別名]
def test_window_fromslices_implicit_err():
""" height and width are required if stop index is None; failing to
provide them will result in error"""
with pytest.raises(WindowError):
Window.from_slices(rows=(1, None), cols=(1, 4))
with pytest.raises(WindowError):
Window.from_slices(rows=(1, 4), cols=(1, None))
示例4: test_window_fromslices_negative_stop
# 需要導入模塊: from rasterio.windows import Window [as 別名]
# 或者: from rasterio.windows.Window import from_slices [as 別名]
def test_window_fromslices_negative_stop():
# TODO: Should negative stops even allowed?? Limited to boundless case?
assert np.allclose(
Window.from_slices(rows=(-4, -1), cols=(0, 4), height=10).flatten(),
(0, 6, 4, 3)
)
assert np.allclose(
Window.from_slices(rows=(0, 4), cols=(-4, -1), width=10).flatten(),
(6, 0, 3, 4)
)
示例5: test_window_fromslices_stops_lt_starts
# 需要導入模塊: from rasterio.windows import Window [as 別名]
# 或者: from rasterio.windows.Window import from_slices [as 別名]
def test_window_fromslices_stops_lt_starts():
"""Should produce empty windows if stop indexes are less than start
indexes"""
assert np.allclose(
Window.from_slices(rows=(4, 2), cols=(0, 4)).flatten(),
(0, 4, 4, 0)
)
assert np.allclose(
Window.from_slices(rows=(0, 4), cols=(4, 2)).flatten(),
(4, 0, 0, 4)
)
示例6: test_data_window_maskedarray
# 需要導入模塊: from rasterio.windows import Window [as 別名]
# 或者: from rasterio.windows.Window import from_slices [as 別名]
def test_data_window_maskedarray():
"""Get window of masked arr."""
arr = np.ones((3, 3))
arr[0, :] = 0
arr = np.ma.masked_array(arr, arr == 0)
window = get_data_window(arr)
assert window == Window.from_slices((1, 3), (0, 3))
示例7: test_window_fromslices_invalid_rows_cols
# 需要導入模塊: from rasterio.windows import Window [as 別名]
# 或者: from rasterio.windows.Window import from_slices [as 別名]
def test_window_fromslices_invalid_rows_cols():
"""Should raise error if rows or cols are not slices, lists, or tuples
of length 2"""
invalids = (
np.array([0, 4]), # wrong type, but close
'04', # clearly the wrong type but right length
(1, 2, 3) # wrong length
)
for invalid in invalids:
with pytest.raises(WindowError):
Window.from_slices(rows=invalid, cols=(0, 4))
with pytest.raises(WindowError):
Window.from_slices(rows=(0, 4), cols=invalid)
示例8: test_window_fromslices_negative_start
# 需要導入模塊: from rasterio.windows import Window [as 別名]
# 或者: from rasterio.windows.Window import from_slices [as 別名]
def test_window_fromslices_negative_start():
# TODO: if passing negative start, what are valid values for stop?
assert np.allclose(
Window.from_slices(rows=(-4, None), cols=(0, 4), height=10).flatten(),
(0, 6, 4, 4)
)
assert np.allclose(
Window.from_slices(rows=(0, 4), cols=(-4, None), width=10).flatten(),
(6, 0, 4, 4)
)
assert np.allclose(
Window.from_slices(rows=(-6, None), cols=(-4, None),
height=8, width=10).flatten(),
(6, 2, 4, 6)
)
示例9: test_window_fromslices_implicit
# 需要導入模塊: from rasterio.windows import Window [as 別名]
# 或者: from rasterio.windows.Window import from_slices [as 別名]
def test_window_fromslices_implicit(abs_off, imp_off, stop, dim):
""" providing None for start index will default to 0
and providing None for stop index will default to width or height """
assume(stop >= abs_off)
assume(dim >= imp_off)
absolute = (abs_off, stop)
implicit_start = (None, stop) # => (0, stop)
implicit_stop = (imp_off, None) # => (implicit_offset, dim)
implicit_both = (None, None) # => (implicit_offset, dim)
# Implicit start indexes resolve to 0
assert np.allclose(
Window.from_slices(rows=implicit_start, cols=absolute).flatten(),
(abs_off, 0, stop - abs_off, stop)
)
assert np.allclose(
Window.from_slices(rows=absolute, cols=implicit_start).flatten(),
(0, abs_off, stop, stop - abs_off)
)
# Implicit stop indexes resolve to dim (height or width)
assert np.allclose(
Window.from_slices(
rows=implicit_stop, cols=absolute, height=dim).flatten(),
(abs_off, imp_off, stop - abs_off, dim - imp_off)
)
assert np.allclose(
Window.from_slices(
rows=absolute, cols=implicit_stop, width=dim).flatten(),
(imp_off, abs_off, dim - imp_off, stop - abs_off)
)
# Both can be implicit
assert np.allclose(
Window.from_slices(
rows=implicit_both, cols=implicit_both,
width=dim, height=dim).flatten(),
(0, 0, dim, dim)
)
示例10: test_window_float
# 需要導入模塊: from rasterio.windows import Window [as 別名]
# 或者: from rasterio.windows.Window import from_slices [as 別名]
def test_window_float(path_rgb_byte_tif):
"""Test window float values"""
with rasterio.open(path_rgb_byte_tif) as src:
left, bottom, right, top = src.bounds
dx, dy = src.res
height = src.height
width = src.width
assert_window_almost_equals(from_bounds(
left, top - 400, left + 400, top, src.transform,
height, width), Window.from_slices((0, 400 / src.res[1]), (0, 400 / src.res[0])))
示例11: test_window_fromslices_boundless
# 需要導入模塊: from rasterio.windows import Window [as 別名]
# 或者: from rasterio.windows.Window import from_slices [as 別名]
def test_window_fromslices_boundless(col_off, row_off, col_stop, row_stop):
# Constrain windows to >= 0 in each dimension
assume(col_stop >= col_off)
assume(row_stop >= row_off)
assert np.allclose(
Window.from_slices(
rows=(-row_off, row_stop), cols=(col_off, col_stop),
boundless=True).flatten(),
(col_off, -row_off, col_stop - col_off, row_stop + row_off)
)
示例12: test_window_from_bounds
# 需要導入模塊: from rasterio.windows import Window [as 別名]
# 或者: from rasterio.windows.Window import from_slices [as 別名]
def test_window_from_bounds(path_rgb_byte_tif):
# TODO: break this test up.
with rasterio.open(path_rgb_byte_tif) as src:
left, bottom, right, top = src.bounds
dx, dy = src.res
height = src.height
width = src.width
assert_window_almost_equals(from_bounds(
left + EPS, bottom + EPS, right - EPS, top - EPS, src.transform,
height, width), Window.from_slices((0, height), (0, width)))
assert_window_almost_equals(from_bounds(
left, top - 2 * dy - EPS, left + 2 * dx - EPS, top, src.transform,
height, width), Window.from_slices((0, 2), (0, 2)))
# boundless
assert_window_almost_equals(
from_bounds(left - 2 * dx, top - 2 * dy, left + 2 * dx,
top + 2 * dy, src.transform, height=height,
width=width),
Window.from_slices((-2, 2), (-2, 2), boundless=True, height=height,
width=width))
示例13: test_window_union
# 需要導入模塊: from rasterio.windows import Window [as 別名]
# 或者: from rasterio.windows.Window import from_slices [as 別名]
def test_window_union():
"""Window union works."""
window = union(Window(0, 0, 1, 1), Window(1, 1, 2, 2))
assert window == Window.from_slices((0, 3), (0, 3))
示例14: test_data_window_nodata_3d
# 需要導入模塊: from rasterio.windows import Window [as 別名]
# 或者: from rasterio.windows.Window import from_slices [as 別名]
def test_data_window_nodata_3d():
"""Get window of 3d arr with nodata."""
arr = np.ones((3, 3, 3))
arr[:, 0, :] = 0
window = get_data_window(arr, nodata=0)
assert window == Window.from_slices((1, 3), (0, 3))
示例15: test_intersection
# 需要導入模塊: from rasterio.windows import Window [as 別名]
# 或者: from rasterio.windows.Window import from_slices [as 別名]
def test_intersection():
"""Window intersection works."""
window = intersection(Window(0, 0, 10, 10), Window(8, 8, 12, 12))
assert window == Window.from_slices((8, 10), (8, 10))