Skip to content

Commit

Permalink
Fixes #2
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
vfdev-5 committed Nov 9, 2019
1 parent 6299385 commit 46ef998
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 24 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 5 additions & 3 deletions docs/source/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions examples/example_const_size_tiling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
14 changes: 7 additions & 7 deletions tests/test_const_size.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

import unittest
import math

from tiling import ConstSizeTiles

Expand Down Expand Up @@ -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)]:
Expand All @@ -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)
Expand All @@ -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" \
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tiling/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from six import with_metaclass


__version__ = '0.2.0'
__version__ = '0.3.0'


class BaseTiles(with_metaclass(ABCMeta, object)):
Expand Down
20 changes: 18 additions & 2 deletions tiling/const_size.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
"""
Expand All @@ -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):
Expand Down
21 changes: 20 additions & 1 deletion tiling/const_stride.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 46ef998

Please sign in to comment.