Skip to content

Commit

Permalink
tiling for the edge of the raster
Browse files Browse the repository at this point in the history
  • Loading branch information
iamtekson committed Jul 4, 2022
1 parent d3a51e2 commit 61e7b15
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
16 changes: 11 additions & 5 deletions geoTile/GeoTile.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,11 @@ def generate_raster_tiles(
tile_y: int
The size of the tile in y axis, Default value is 256
stride_x: int
The stride of the x axis, Default value is 128
The stride of the x axis, Default value is 128 (1/2 overalapping)
If you want to ignore the overlap, keep it same as tile_x
stride_y: int
The stride of the y axis, Default value is 128
The stride of the y axis, Default value is 128 (1/2 overalapping)
If you want to ignore the overlap, keep it same as tile_y
Returns
-------
Expand All @@ -146,7 +148,7 @@ def generate_raster_tiles(
>>> tiler = GeoTile('/path/to/raster/file.tif')
>>> tiler.generate_raster_tiles('/path/to/output/folder')
# save the specific bands with other than default size
>>> tiler.generate_raster_tiles('/path/to/output/folder', [3, 2, 1], tile_x=512, tile_y=512, stride_x=0, stride_y=0)
>>> tiler.generate_raster_tiles('/path/to/output/folder', [3, 2, 1], tile_x=512, tile_y=512, stride_x=512, stride_y=512)
"""

self.tile_x = tile_x
Expand All @@ -167,14 +169,16 @@ def generate_raster_tiles(
col_off=col_off, row_off=row_off, width=256, height=256)
transform = windows.transform(window, self.ds.transform)
meta = self.ds.meta.copy()
nodata = meta['nodata']

# update the meta data
meta.update(
{"width": window.width, "height": window.height, "transform": transform})

# if the output bands is not None, add all bands to the output dataset
# out_bands starts from i+1 because rasterio bands start from 1
if out_bands is None:
out_bands = [i for i in range(0, self.ds.count())]
out_bands = [i+1 for i in range(0, self.ds.count)]

else:
meta.update({"count": len(out_bands)})
Expand All @@ -186,13 +190,15 @@ def generate_raster_tiles(
else:
dtype = self.ds.meta['dtype']

# tile name and path
tile_name = 'tile_' + str(col_off) + '_' + \
str(row_off) + '.' + image_format
tile_path = os.path.join(output_folder, tile_name)

# save the tiles with new metadata
with rio.open(tile_path, 'w', **meta) as outds:
outds.write(self.ds.read(
out_bands, window=window).astype(dtype))
out_bands, window=window, fill_value=nodata, boundless=True).astype(dtype))

def mosaic_rasters(self, input_folder: str, out_path: str, image_format: Optional[str] = 'tif', **kwargs):
"""Mosaic the rasters inside the input folder
Expand Down
16 changes: 11 additions & 5 deletions geotile/GeoTile.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,11 @@ def generate_raster_tiles(
tile_y: int
The size of the tile in y axis, Default value is 256
stride_x: int
The stride of the x axis, Default value is 128
The stride of the x axis, Default value is 128 (1/2 overalapping)
If you want to ignore the overlap, keep it same as tile_x
stride_y: int
The stride of the y axis, Default value is 128
The stride of the y axis, Default value is 128 (1/2 overalapping)
If you want to ignore the overlap, keep it same as tile_y
Returns
-------
Expand All @@ -146,7 +148,7 @@ def generate_raster_tiles(
>>> tiler = GeoTile('/path/to/raster/file.tif')
>>> tiler.generate_raster_tiles('/path/to/output/folder')
# save the specific bands with other than default size
>>> tiler.generate_raster_tiles('/path/to/output/folder', [3, 2, 1], tile_x=512, tile_y=512, stride_x=0, stride_y=0)
>>> tiler.generate_raster_tiles('/path/to/output/folder', [3, 2, 1], tile_x=512, tile_y=512, stride_x=512, stride_y=512)
"""

self.tile_x = tile_x
Expand All @@ -167,14 +169,16 @@ def generate_raster_tiles(
col_off=col_off, row_off=row_off, width=256, height=256)
transform = windows.transform(window, self.ds.transform)
meta = self.ds.meta.copy()
nodata = meta['nodata']

# update the meta data
meta.update(
{"width": window.width, "height": window.height, "transform": transform})

# if the output bands is not None, add all bands to the output dataset
# out_bands starts from i+1 because rasterio bands start from 1
if out_bands is None:
out_bands = [i for i in range(0, self.ds.count())]
out_bands = [i+1 for i in range(0, self.ds.count)]

else:
meta.update({"count": len(out_bands)})
Expand All @@ -186,13 +190,15 @@ def generate_raster_tiles(
else:
dtype = self.ds.meta['dtype']

# tile name and path
tile_name = 'tile_' + str(col_off) + '_' + \
str(row_off) + '.' + image_format
tile_path = os.path.join(output_folder, tile_name)

# save the tiles with new metadata
with rio.open(tile_path, 'w', **meta) as outds:
outds.write(self.ds.read(
out_bands, window=window).astype(dtype))
out_bands, window=window, fill_value=nodata, boundless=True).astype(dtype))

def mosaic_rasters(self, input_folder: str, out_path: str, image_format: Optional[str] = 'tif', **kwargs):
"""Mosaic the rasters inside the input folder
Expand Down

0 comments on commit 61e7b15

Please sign in to comment.