PK![uЀdask_rasterio/__init__.py__version__ = '0.1.0' from dask_rasterio.read import read_raster, read_raster_band from dask_rasterio.write import write_rasterPK!ٳ dask_rasterio/read.pyimport dask.array as da import rasterio from dask import is_dask_collection from dask.base import tokenize from rasterio.windows import Window def read_raster(path, band=None, block_size=1): """Read all or some bands from raster Arguments: path {string} -- path to raster file Keyword Arguments: band {int, iterable(int)} -- band number or iterable of bands. When passing None, it reads all bands (default: {None}) block_size {int} -- block size multiplier (default: {1}) Returns: dask.array.Array -- a Dask array """ if isinstance(band, int): return read_raster_band(path, band=band, block_size=block_size) else: if band is None: bands = range(1, get_band_count(path) + 1) else: bands = list(band) return da.stack([ read_raster_band(path, band=band, block_size=block_size) for band in bands ]) def read_raster_band(path, band=1, block_size=1): """Read a raster band and return a Dask array Arguments: path {string} -- path to the raster file Keyword Arguments: band {int} -- number of band to read (default: {1}) block_size {int} -- block size multiplier (default: {1}) """ def read_window(raster_path, window, band): with rasterio.open(raster_path) as src: return src.read(band, window=window) def resize_window(window, block_size): return Window( col_off=window.col_off * block_size, row_off=window.row_off * block_size, width=window.width * block_size, height=window.height * block_size) def block_windows(dataset, band, block_size): return [(pos, resize_window(win, block_size)) for pos, win in dataset.block_windows(band)] with rasterio.open(path) as src: h, w = src.block_shapes[band - 1] chunks = (h * block_size, w * block_size) name = 'raster-{}'.format(tokenize(path, band, chunks)) dtype = src.dtypes[band - 1] shape = src.shape blocks = block_windows(src, band, block_size) dsk = {(name, i, j): (read_window, path, window, band) for (i, j), window in blocks} return da.Array(dsk, name, chunks, dtype, shape) def get_band_count(raster_path): """Read raster band count""" with rasterio.open(raster_path) as src: return src.count PK!$,$ dask_rasterio/write.pyimport dask.array as da import rasterio from dask import is_dask_collection from dask.base import tokenize from rasterio.windows import Window def write_raster(path, array, **kwargs): """Write a dask array to a raster file If array is 2d, write array on band 1. If array is 3d, write data on each band Arguments: path {string} -- path of raster to write array {dask.array.Array} -- band array kwargs {dict} -- keyword arguments to delegate to rasterio.open Examples: # Write a single band raster >> red_band = read_raster_band("test.tif", band=1) >> write_raster("new.tif", red_band) # Write a multiband raster >> img = read_raster("test.tif") >> new_img = process(img) >> write_raster("new.tif", new_img) """ if len(array.shape) != 2 and len(array.shape) != 3: raise TypeError('invalid shape (must be either 2d or 3d)') if is_dask_collection(array): with RasterioDataset(path, 'w', **kwargs) as dst: da.store(array, dst, lock=True) else: with rasterio.open(path, 'w', **kwargs) as dst: if len(array.shape) == 2: dst.write(array, 1) else: dst.write(array) class RasterioDataset: """Rasterio wrapper to allow dask.array.store to do window saving. Example: >> rows = cols = 21696 >> a = da.ones((4, rows, cols), dtype=np.float64, chunks=(1, 4096, 4096) ) >> a = a * np.array([255., 255., 255., 255.])[:, None, None] >> a = a.astype(np.uint8) >> with RasterioDataset('test.tif', 'w', driver='GTiff', width=cols, height=rows, count=4, dtype=np.uint8) as r_file: .. da.store(a, r_file, lock=True) """ def __init__(self, *args, **kwargs): self.args = args self.kwargs = kwargs self.dataset = None def __setitem__(self, key, item): """Put the data chunk in the image""" if len(key) == 3: index_range, y, x = key indexes = list( range(index_range.start + 1, index_range.stop + 1, index_range.step or 1)) else: indexes = 1 y, x = key chy_off = y.start chy = y.stop - y.start chx_off = x.start chx = x.stop - x.start self.dataset.write( item, window=Window(chx_off, chy_off, chx, chy), indexes=indexes) def __enter__(self): """Enter method""" self.dataset = rasterio.open(*self.args, **self.kwargs) return self def __exit__(self, exc_type, exc_value, traceback): """Exit method""" self.dataset.close() PK!P(dask_rasterio-0.2.0.dist-info/LICENSE.mdBSD 2-Clause License Copyright (c) 2018, Dymaxion Labs All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of AP-Latam nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. PK!HƣxST#dask_rasterio-0.2.0.dist-info/WHEEL A н#J;/"d&F]xzw>@Zpy3F ]n2H%_60{8&baPa>PK!H6I&dask_rasterio-0.2.0.dist-info/METADATAWn6ϧ8C.lَ&1tMҦ4H QĴDK%R%)Yb;|I/a/u.߹wxo!Z`7+DN['lIv6-ynfF:% 熧HRpQ6{3yrkbb:kEF)&2"ysy p6=Vp+)WriBdb~F “~qPr,\h^^<7z*"zF kcólU\D_jUw[`ok9oqrṴA oaЮ?$I=4wAN⎂&saC#s9< w‰Ϯ9L1 i!nwP>@ibd ~ PG"b#'?n[)OZ#łշXu0VHJa0=@.L(^D$lm#Yei햙Rn 2iYk;AhjwA|啵|?VGl@]qEN|N2 is0xoickKR.7P |)/+Of6Λv_1WNXGpusqNNW,̃cx8iMx.n:sԩ7 rXB*[aR_ӀnIs$Wpڹqį1wbi)~jm(@R@ TwG}rᦗ)gQ?uq{ɾ#?a^m OQN ?nG}W?Z8N]ߤZwZp}b čߒ A} kEt/3,E E茰cTk&5\W[a(rl0:/Ȱ!bYj&yCH\^#Ę#=LyZ: 6Kd~((.L)8v1)y8tQ&|* >@%HBl.BjBN0#2&R %zF{ș6s4x %,V`ʗ ^2X H M.+<+WC"p."9;2#Qܚqhk-t9 DrJiWM c1.C<)B}Th̕a1Zq6"V:r%a҅Cm11}7J 2pO[Py#~tO'#͚҉ ktH#1}@:ISU%jְv8kו?}$*Mx8k`ʗ39B7& " .Iծ2tPYCER?qǛ1Cyľ;.ҪE-J!JjAYl>ߢgn.O]^]Y`*fn 4M#|x&/䘾כo/^]ݾV-=RpyPK!HՎ}]/$dask_rasterio-0.2.0.dist-info/RECORDv0}%Pd1 L8"K*@Ayn