rasterio 0.7.1

Rasterio employs GDAL under the hood for file I/O and raster formatting. Its
functions typically accept and return Numpy ndarrays. Rasterio is designed to
make working with geospatial raster data more productive and more fun.

Example

Here’s an example of the basic features rasterio provides. Three bands are
read from an image and summed to produce something like a panchromatic band.
This new band is then written to a new single band TIFF.

importnumpyimportrasterioimportsubprocess# Register format drivers with a context managerwithrasterio.drivers():# Read raster bands directly to Numpy arrays.#withrasterio.open('rasterio/tests/data/RGB.byte.tif')assrc:b,g,r=map(src.read_band,(1,2,3))# Combine arrays using the 'iadd' ufunc. Expecting that the sum# will exceed the 8-bit integer range, initialize it as 16-bit.# Adding other arrays to it in-place converts those arrays up# and preserves the type of the total array.total=numpy.zeros(r.shape,dtype=rasterio.uint16)forbandin(r,g,b):total+=bandtotal/=3asserttotal.dtype==rasterio.uint16# Write the product as a raster band to a new 8-bit file. For# keyword arguments, we start with the meta attributes of the# source file, but then change the band count to 1, set the# dtype to uint8, and specify LZW compression.kwargs=src.metakwargs.update(dtype=rasterio.uint8,count=1,compress='lzw')withrasterio.open('example-total.tif','w',**kwargs)asdst:dst.write_band(1,total.astype(rasterio.uint8))# At the end of the ``with rasterio.drivers()`` block, context# manager exits and all drivers are de-registered.# Dump out gdalinfo's report card and open the image.info=subprocess.check_output(['gdalinfo','-stats','example-total.tif'])print(info)subprocess.call(['open','example-total.tif'])

The rasterio.drivers() function and context manager are new in 0.5. The
example above shows the way to use it to register and de-register
drivers in a deterministic and efficient way. Code written for rasterio
0.4 will continue to work: opened raster datasets may manage the global
driver registry if no other manager is present.

The Numpy headers are required to run the rasterio setup script. Numpy has to
be installed (via the indicated requirements file) before rasterio can be
installed. See rasterio’s Travis configuration for more guidance.