From 46ef998ace034ecad184a10d3b42e07bca12bae2 Mon Sep 17 00:00:00 2001 From: vfdev-5 Date: Sat, 9 Nov 2019 17:29:01 +0100 Subject: [PATCH] Fixes #2 - updated version to v0.3.0 as API changed - ConstSizeTiles.__getitem__ -> (x_offset, y_offset, x_extent, y_extent), (x_out_size, y_out_size) --- README.md | 8 +++++--- docs/source/quickstart.rst | 8 +++++--- examples/example_const_size_tiling.ipynb | 14 +++++++------- tests/test_const_size.py | 14 +++++++------- tiling/__init__.py | 2 +- tiling/const_size.py | 20 ++++++++++++++++++-- tiling/const_stride.py | 21 ++++++++++++++++++++- 7 files changed, 63 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index fc7f5a3..e697c05 100644 --- a/README.md +++ b/README.md @@ -60,11 +60,13 @@ from tiling import ConstSizeTiles tiles = ConstSizeTiles(image_size=(500, 500), tile_size=(256, 256), min_overlapping=15, scale=1.0) print("Number of tiles: %i" % len(tiles)) -for extent in tiles: +for extent, out_size in tiles: + assert out_size[0] == tiles.tile_size[0] + assert out_size[1] == tiles.tile_size[1] x, y, width, height = extent data = read_data(x, y, width, height, - out_width=tiles.tile_size[0], - out_height=tiles.tile_size[1]) + out_width=out_size[0], + out_height=out_size[1]) print("data.shape: {}".format(data.shape)) # Access a tile: diff --git a/docs/source/quickstart.rst b/docs/source/quickstart.rst index 0a92ec5..91357b3 100644 --- a/docs/source/quickstart.rst +++ b/docs/source/quickstart.rst @@ -73,11 +73,13 @@ Thus, overlapping tiles can be extracted as tiles = ConstSizeTiles(image_size=(500, 500), tile_size=(256, 256), min_overlapping=15, scale=1.0) print("Number of tiles: %i" % len(tiles)) - for extent in tiles: + for extent, out_size in tiles: + assert out_size[0] == tiles.tile_size[0] + assert out_size[1] == tiles.tile_size[1] x, y, width, height = extent data = read_data(x, y, width, height, - out_width=tiles.tile_size[0], - out_height=tiles.tile_size[1]) + out_width=out_size[0], + out_height=out_size[1]) print("data.shape: {}".format(data.shape)) # Access a tile: diff --git a/examples/example_const_size_tiling.ipynb b/examples/example_const_size_tiling.ipynb index a490052..e506019 100644 --- a/examples/example_const_size_tiling.ipynb +++ b/examples/example_const_size_tiling.ipynb @@ -11,7 +11,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -153,9 +153,9 @@ "fig = plt.figure(figsize=(20, 20))\n", "grid = ImageGrid(fig, 111, nrows_ncols=(tiles.ny, tiles.nx), axes_pad=0.1)\n", "\n", - "for i, extent in enumerate(tiles):\n", + "for i, (extent, out_size) in enumerate(tiles):\n", " x, y, w, h = extent\n", - " tile = read_data(image_data, x, y, w, h, tiles.tile_size[0], tiles.tile_size[1])\n", + " tile = read_data(image_data, x, y, w, h, out_size[0], out_size[1])\n", " ax = grid[i]\n", " im = ax.imshow(tile)\n", " ax.text(2, 2, '%i %i' % (x, y), color='w', backgroundcolor='m', alpha=0.7)\n", @@ -193,9 +193,9 @@ "fig = plt.figure(figsize=(20, 20))\n", "grid = ImageGrid(fig, 111, nrows_ncols=(tiles.ny, tiles.nx), axes_pad=0.1)\n", "\n", - "for i, extent in enumerate(tiles):\n", + "for i, (extent, out_size) in enumerate(tiles):\n", " x, y, w, h = extent\n", - " tile = read_data(image_data, x, y, w, h, tiles.tile_size[0], tiles.tile_size[1])\n", + " tile = read_data(image_data, x, y, w, h, out_size[0], out_size[1])\n", " ax = grid[i]\n", " im = ax.imshow(tile)\n", " ax.text(2, 2, '%i %i' % (x, y), color='w', backgroundcolor='m', alpha=0.7)\n", @@ -231,9 +231,9 @@ "fig = plt.figure(figsize=(20, 20))\n", "grid = ImageGrid(fig, 111, nrows_ncols=(tiles.ny, tiles.nx), axes_pad=0.1)\n", "\n", - "for i, extent in enumerate(tiles):\n", + "for i, (extent, out_size) in enumerate(tiles):\n", " x, y, w, h = extent\n", - " tile = read_data(image_data, x, y, w, h, tiles.tile_size[0], tiles.tile_size[1])\n", + " tile = read_data(image_data, x, y, w, h, out_size[0], out_size[1])\n", " ax = grid[i]\n", " im = ax.imshow(tile)\n", " ax.text(2, 2, '%i %i' % (x, y), color='w', backgroundcolor='m', alpha=0.7)\n", diff --git a/tests/test_const_size.py b/tests/test_const_size.py index 5217729..8449bbe 100644 --- a/tests/test_const_size.py +++ b/tests/test_const_size.py @@ -1,6 +1,5 @@ import unittest -import math from tiling import ConstSizeTiles @@ -43,9 +42,10 @@ def test__compute_float_overlapping(self): def test_as_iterator(self): tiles = ConstSizeTiles((100, 120), (10, 10), min_overlapping=5) counter = 0 - for extent in tiles: - _extent = tiles[counter] + for extent, _ in tiles: + _extent, _out_size = tiles[counter] self.assertEqual(extent, _extent) + self.assertEqual(tiles.tile_size, _out_size) counter += 1 for i, j in [(len(tiles) - 1, -1), (len(tiles) - 2, -2), (len(tiles) - 3, -3)]: @@ -67,7 +67,7 @@ def _test(im_size, ts, scale, min_overlapping): debug_msg += "n={}\n".format(len(tiles)) self.assertGreater(len(tiles), 0, debug_msg) - extent0 = tiles[0] + extent0, out_size = tiles[0] # Start at origin but should be positive debug_msg += "extent0={}\n".format(extent0) self.assertEqual((extent0[0], extent0[1]), (0, 0), debug_msg) @@ -76,8 +76,8 @@ def _test(im_size, ts, scale, min_overlapping): self.assertLessEqual((extent0[2], extent0[3]), (ts / scale, ts / scale), debug_msg) for i in range(1, len(tiles)): - extent = tiles[i] - prev_extent = tiles[i - 1] + extent, _ = tiles[i] + prev_extent, _ = tiles[i - 1] var_debug_msg = "i={} extent={}\n" \ .format(i, extent) var_debug_msg += "prev_extent={}\n" \ @@ -89,7 +89,7 @@ def _test(im_size, ts, scale, min_overlapping): debug_msg + var_debug_msg) # Check the last tile ends at the boundary - extent = tiles[-1] + extent, _ = tiles[-1] debug_msg += "extent={}\n".format(extent) for j in [0, 1]: self.assertLess(extent[j], im_size, debug_msg) diff --git a/tiling/__init__.py b/tiling/__init__.py index 3318842..abc3a9f 100644 --- a/tiling/__init__.py +++ b/tiling/__init__.py @@ -9,7 +9,7 @@ from six import with_metaclass -__version__ = '0.2.0' +__version__ = '0.3.0' class BaseTiles(with_metaclass(ABCMeta, object)): diff --git a/tiling/const_size.py b/tiling/const_size.py index c20f46e..ff1409c 100644 --- a/tiling/const_size.py +++ b/tiling/const_size.py @@ -10,6 +10,22 @@ class ConstSizeTiles(BaseTiles): """Class provides constant size tile parameters (offset, extent) to extract data from image. Generated tile extents can overlap and do not includes nodata paddings. + Examples: + + .. code-block:: python + + from tiling import ConstSizeTiles + + tiles = ConstSizeTiles(image_size=(500, 500), tile_size=(256, 256), min_overlapping=15, scale=1.0) + + print("Number of tiles: %i" % len(tiles)) + for extent, out_size in tiles: + x, y, width, height = extent + data = read_data(x, y, width, height, + out_width=out_size[0], + out_height=out_size[1]) + print("data.shape: {}".format(data.shape)) + Args: image_size (list/tuple of int): input image size in pixels (width, height) tile_size (int or list/tuple of int): output tile size in pixels (width, height) @@ -62,7 +78,7 @@ def __getitem__(self, idx): idx: (int) tile index between `0` and `len(tiles)` Returns: - (tuple) tile extent in pixels: x offset, y offset, x tile extent, y tile extent + (tuple) tile extent, output size in pixels If scale is 1.0, then x tile extent, y tile extent are equal to tile size """ @@ -77,7 +93,7 @@ def __getitem__(self, idx): self.float_overlapping_x) y_tile_offset, y_tile_extent = self._compute_tile_extent(y_tile_index, self.tile_extent[1], self.float_overlapping_y) - return x_tile_offset, y_tile_offset, x_tile_extent, y_tile_extent + return (x_tile_offset, y_tile_offset, x_tile_extent, y_tile_extent), (self.tile_size[0], self.tile_size[1]) @staticmethod def _compute_number_of_tiles(tile_extent, image_size, min_overlapping): diff --git a/tiling/const_stride.py b/tiling/const_stride.py index 50632ca..a458b6f 100644 --- a/tiling/const_stride.py +++ b/tiling/const_stride.py @@ -15,6 +15,25 @@ class ConstStrideTiles(BaseTiles): """Class provides tile parameters (offset, extent) to extract data from image. + Examples: + + .. code-block:: python + + from tiling import ConstStrideTiles + + tiles = ConstStrideTiles(image_size=(500, 500), tile_size=(256, 256), stride=(100, 100), + origin=(-100, -100), + scale=1.0, + include_nodata=True) + + print("Number of tiles: %i" % len(tiles)) + for extent, out_size in tiles: + x, y, width, height = extent + data = read_data(x, y, width, height, + out_width=out_size[0], + out_height=out_size[1]) + print("data.shape: {}".format(data.shape)) + Args: image_size (list/tuple of int): input image size in pixels (width, height) tile_size (int or list/tuple of int): output tile size in pixels (width, height) @@ -85,7 +104,7 @@ def __getitem__(self, idx): idx: (int) tile index between `0` and `len(tiles)` Returns: - (tuple) tile extent, output size + (tuple) tile extent, output size in pixels Tile extent in pixels: x offset, y offset, x tile extent, y tile extent. If scale is 1.0, then x tile extent, y tile extent are equal to tile size