Skip to content

Commit

Permalink
Updated docs for const_size
Browse files Browse the repository at this point in the history
  • Loading branch information
vfdev-5 committed Sep 18, 2019
1 parent 1f8aff9 commit 35a7362
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 57 deletions.
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# -- Project information -----------------------------------------------------

project = 'ImageTilingUtils'
copyright = '2018, vfdev-5'
copyright = '2019, vfdev-5'
author = 'vfdev-5'

# The short X.Y version
Expand Down
2 changes: 1 addition & 1 deletion docs/source/const_size.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ Basic usage:
.. autoclass:: ConstSizeTiles
:members:
:undoc-members:
:imported-members:
2 changes: 1 addition & 1 deletion docs/source/const_stride.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ Basic usage:
.. autoclass:: ConstStrideTiles
:members:
:undoc-members:
:imported-members:
75 changes: 21 additions & 54 deletions tiling/const_size.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,16 @@


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.
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)
min_overlapping (int): minimal overlapping in pixels between tiles.
scale (float): Scaling applied to the input image parameters before extracting tile's extent
"""
Class provides constant size tile parameters (offset, extent) to extract data from image.
Generated tile extents can overlap, do not includes nodata paddings.
For example, tiling can look like this:
```
tile 0 tile 2 tile 4
|<------>| |<------>| |<------>|
tile 1 tile 3 tile 5
|<------>| |<------>| |<------>|
|<------------------------------------>|
| IMAGE |
| |
```
Usage:
```
from tiling import ConstSizeTiles
tiles = ConstSizeTiles(image_size=(500, 500), tile_size=(256, 256), min_overlapping=100)
print("Number of tiles: %i" % len(tiles))
for x, y, width, height in tiles:
data = read_data(x, y, width, height, tiles.tile_size[0], tiles.tile_size[0])
print("data.shape: {}".format(data.shape))
```
"""

def __init__(self, image_size, tile_size, min_overlapping=0, scale=1.0):
"""
Initialize tiles
:param image_size: (list/tuple of int) input image size in pixels (width, height)
:param tile_size: (int or list/tuple of int) output tile size in pixels (width, height)
:param min_overlapping: (int) minimal overlapping in pixels. Actual overlapping between tiles will not be
constant, minimal overlapping value is the minimum boundary value.
:param scale: (float) Scaling applied to the input image parameters before extracting tile's extent.
"""
super(ConstSizeTiles, self).__init__(image_size=image_size, tile_size=tile_size, scale=scale)

if not (0 <= min_overlapping < min(self.tile_extent[0], self.tile_extent[1])):
Expand All @@ -71,27 +43,28 @@ def __init__(self, image_size, tile_size, min_overlapping=0, scale=1.0):
self._max_index = self.nx * self.ny

def __len__(self):
"""
Method to get total number of tiles
:return:
"""Method to get total number of tiles
"""
return self._max_index

@staticmethod
def _compute_tile_extent(idx, tile_extent, overlapping):
"""
Method to compute tile extent: offset, extent for a given index
"""Method to compute tile extent: offset, extent for a given index
"""

offset = int(round(idx * (tile_extent - overlapping)))
return offset, int(round(tile_extent))

def __getitem__(self, idx):
"""
Method to get the tile at index `idx`
:param idx: (int) tile index between `0` and `len(tiles)`
:return: (tuple) 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
"""Method to get the tile at index `idx`
Args:
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
If scale is 1.0, then x tile extent, y tile extent are equal to tile size
"""
if idx < -self._max_index or idx >= self._max_index:
raise IndexError("Index %i is out of ranges %i and %i" % (idx, 0, self._max_index))
Expand All @@ -108,18 +81,12 @@ def __getitem__(self, idx):

@staticmethod
def _compute_number_of_tiles(tile_extent, image_size, min_overlapping):
"""
Method to compute number of overlapping tiles for a given image size
n = ceil(image_size / (tile_size - min_overlapping))
"""Method to compute number of overlapping tiles for a given image size
"""
return ceil_int(image_size * 1.0 / (tile_extent - min_overlapping + 1e-10))

@staticmethod
def _compute_float_overlapping(tile_size, image_size, n):
"""
Method to float overlapping
delta = tile_size * n - image_size
overlapping = delta / (n - 1)
"""Method to float overlapping
"""
return (tile_size * n - image_size) * 1.0 / (n - 1.0) if n > 1 else 0

0 comments on commit 35a7362

Please sign in to comment.