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, block_size=1): """Read all bands from raster""" bands = range(1, get_band_count(path) + 1) 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} -- Multiplier for block size (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.1.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.1.0.dist-info/WHEEL A н#J;/"d&F]xzw>@Zpy3F ]n2H%_60{8&baPa>PK!HgH&dask_rasterio-0.1.0.dist-info/METADATAUn7+$M+8A; 9D펴w ɵ~Kov*2>Xrg͛y$3w8;TGp?:DƩNDԾєuFf&2uJ{]\P]tuBnuJTz <*õyLRWԏ@,2 k1)ۀu`uQr,LԦQYې_ײ 5`ݫk!a"hD_fc*85j}>x,DſCK/x∟^v+b:~RyIn M-OgϸE?t$'mqzSEhdpldwMYD~C>s)zA2LqERɠZ9O;z"Nߎt{`{T$ݥjH&ίnBG5J%I֠ 2   5v _%?pS.7A:`.eCgȲJ#jz xWSȺH~`2qW g} 8MW1_R[ש4/mV-!=4m2\ *Lt-3EB7J1q\c.[XJ ʟKѬ Q!.,%X6emj]^I[x.>=Wjg弔)֡mmmи[Js>x]/1A1N3 ]o<0 lm7hqdJ]}.x ۧlt,\ZjmQPfҸ95CYՅm JJ(E>Է2tsRfߎl4vM)S2r$N>Cc$:F~ `3v ;X}h 'H#ʪKy74L>M}^vƚb Z/Q-jTx۞$PK![uЀdask_rasterio/__init__.pyPK!`__dask_rasterio/read.pyPK!$,$ Idask_rasterio/write.pyPK!P(dask_rasterio-0.1.0.dist-info/LICENSE.mdPK!HƣxST#Bdask_rasterio-0.1.0.dist-info/WHEELPK!HgH&dask_rasterio-0.1.0.dist-info/METADATAPK!Het]/$dask_rasterio-0.1.0.dist-info/RECORDPKy