Skip to main content

Fast and direct raster I/O for use with Numpy and SciPy

Project description

Rasterio reads and writes geospatial raster data.

https://travis-ci.org/mapbox/rasterio.png?branch=master https://coveralls.io/repos/github/mapbox/rasterio/badge.svg?branch=master

Geographic information systems use GeoTIFF and other formats to organize and store gridded, or raster, datasets. Rasterio reads and writes these formats and provides a Python API based on N-D arrays.

Rasterio supports Python 2.7 and 3.3-3.6 on Linux and Mac OS X.

Read the documentation for more details: https://rasterio.readthedocs.io/en/latest/.

Example

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

import numpy as np
import rasterio

# Read raster bands directly to Numpy arrays.
#
with rasterio.open('tests/data/RGB.byte.tif') as src:
    r, g, b = src.read()

# Combine arrays in place. Expecting that the sum will
# temporarily exceed the 8-bit integer range, initialize it as
# a 64-bit float (the numpy default) array. Adding other
# arrays to it in-place converts those arrays "up" and
# preserves the type of the total array.
total = np.zeros(r.shape)
for band in r, g, b:
    total += band
total /= 3

# Write the product as a raster band to a new 8-bit file. For
# the new file's profile, 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.
profile = src.profile
profile.update(dtype=rasterio.uint8, count=1, compress='lzw')

with rasterio.open('example-total.tif', 'w', **profile) as dst:
    dst.write(total.astype(rasterio.uint8), 1)

The output:

http://farm6.staticflickr.com/5501/11393054644_74f54484d9_z_d.jpg

API Overview

Rasterio gives access to properties of a geospatial raster file.

with rasterio.open('tests/data/RGB.byte.tif') as src:
    print(src.width, src.height)
    print(src.crs)
    print(src.transform)
    print(src.count)
    print(src.indexes)

# Printed:
# (791, 718)
# {u'units': u'm', u'no_defs': True, u'ellps': u'WGS84', u'proj': u'utm', u'zone': 18}
# Affine(300.0379266750948, 0.0, 101985.0,
#        0.0, -300.041782729805, 2826915.0)
# 3
# [1, 2, 3]

A rasterio dataset also provides methods for getting extended array slices given georeferenced coordinates.

with rasterio.open('tests/data/RGB.byte.tif') as src:
    print src.window(**src.window_bounds(((100, 200), (100, 200))))

# Printed:
# ((100, 200), (100, 200))

Rasterio CLI

Rasterio’s command line interface, named “rio”, is documented at cli.rst. Its rio insp command opens the hood of any raster dataset so you can poke around using Python.

$ rio insp tests/data/RGB.byte.tif
Rasterio 0.10 Interactive Inspector (Python 3.4.1)
Type "src.meta", "src.read(1)", or "help(src)" for more information.
>>> src.name
'tests/data/RGB.byte.tif'
>>> src.closed
False
>>> src.shape
(718, 791)
>>> src.crs
{'init': 'epsg:32618'}
>>> b, g, r = src.read()
>>> b
masked_array(data =
 [[-- -- -- ..., -- -- --]
 [-- -- -- ..., -- -- --]
 [-- -- -- ..., -- -- --]
 ...,
 [-- -- -- ..., -- -- --]
 [-- -- -- ..., -- -- --]
 [-- -- -- ..., -- -- --]],
             mask =
 [[ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]
 ...,
 [ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]],
       fill_value = 0)

>>> np.nanmin(b), np.nanmax(b), np.nanmean(b)
(0, 255, 29.94772668847656)

Rio Plugins

Rio provides the ability to create subcommands using plugins. See cli.rst for more information on building plugins.

See the plugin registry for a list of available plugins.

Installation

Please install Rasterio in a virtual environment so that its requirements don’t tamper with your system’s Python.

SSL certs

The Linux wheels on PyPI are built on CentOS and libcurl expects certs to be in /etc/pki/tls/certs/ca-bundle.crt. Ubuntu’s certs, for example, are in a different location. You may need to use the CURL_CA_BUNDLE environment variable to specify the location of SSL certs on your computer. On an Ubuntu system set the variable as shown below.

$ export CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt

Dependencies

Rasterio has a C library dependency: GDAL >=1.11. GDAL itself depends on some other libraries provided by most major operating systems and also depends on the non standard GEOS and PROJ4 libraries. How to meet these requirement will be explained below.

Rasterio’s Python dependencies are listed in its requirements.txt file.

Development also requires (see requirements-dev.txt) Cython and other packages.

Binary Distributions

Use a binary distributions that directly or indirectly provide GDAL if possible.

Linux

Rasterio distributions are available from UbuntuGIS and Anaconda’s conda-forge channel.

Manylinux1 distributions may be available in the future.

OS X

Binary distributions with GDAL, GEOS, and PROJ4 libraries included are available for OS X versions 10.7+ starting with Rasterio version 0.17. To install, run pip install rasterio. These binary wheels are preferred by newer versions of pip.

If you don’t want these wheels and want to install from a source distribution, run pip install rasterio --no-binary rasterio instead.

The included GDAL library is fairly minimal, providing only the format drivers that ship with GDAL and are enabled by default. To get access to more formats, you must build from a source distribution (see below).

Windows

Binary wheels for rasterio and GDAL are created by Christoph Gohlke and are available from his website.

To install rasterio, simply download both binaries for your system (rasterio and GDAL) and run something like this from the downloads folder:

$ pip install -U pip
$ pip install GDAL-2.0.2-cp27-none-win32.whl
$ pip install rasterio-0.34.0-cp27-cp27m-win32.whl

Source Distributions

Rasterio is a Python C extension and to build you’ll need a working compiler (XCode on OS X etc). You’ll also need Numpy preinstalled; 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.

Linux

The following commands are adapted from Rasterio’s Travis-CI configuration.

$ sudo add-apt-repository ppa:ubuntugis/ppa
$ sudo apt-get update
$ sudo apt-get install gdal-bin libgdal-dev
$ pip install -U pip
$ pip install rasterio

Adapt them as necessary for your Linux system.

OS X

For a Homebrew based Python environment, do the following.

$ brew update
$ brew install gdal
$ pip install -U pip
$ pip install --no-use-wheel rasterio

Alternatively, you can install GDAL binaries from kyngchaos. You will then need to add the installed location /Library/Frameworks/GDAL.framework/Programs to your system path.

Windows

You can download a binary distribution of GDAL from here. You will also need to download the compiled libraries and headers (include files).

When building from source on Windows, it is important to know that setup.py cannot rely on gdal-config, which is only present on UNIX systems, to discover the locations of header files and libraries that rasterio needs to compile its C extensions. On Windows, these paths need to be provided by the user. You will need to find the include files and the library files for gdal and use setup.py as follows.

$ python setup.py build_ext -I<path to gdal include files> -lgdal_i -L<path to gdal library>
$ python setup.py install

We have had success compiling code using the same version of Microsoft’s Visual Studio used to compile the targeted version of Python (more info on versions used here.).

Note: The GDAL dll (gdal111.dll) and gdal-data directory need to be in your Windows PATH otherwise rasterio will fail to work.

Development and Testing

See CONTRIBUTING.rst.

Documentation

See docs/.

License

See LICENSE.txt.

Authors

See AUTHORS.txt.

Changes

See CHANGES.txt.

Who is Using Rasterio?

See here.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rasterio-1.0b4.tar.gz (1.8 MB view details)

Uploaded Source

Built Distributions

rasterio-1.0b4-cp36-cp36m-manylinux1_x86_64.whl (18.7 MB view details)

Uploaded CPython 3.6m

rasterio-1.0b4-cp36-cp36m-macosx_10_9_intel.macosx_10_9_x86_64.whl (23.0 MB view details)

Uploaded CPython 3.6m macOS 10.9+ intel macOS 10.9+ x86-64

rasterio-1.0b4-cp35-cp35m-manylinux1_x86_64.whl (18.6 MB view details)

Uploaded CPython 3.5m

rasterio-1.0b4-cp35-cp35m-macosx_10_9_intel.macosx_10_9_x86_64.whl (22.9 MB view details)

Uploaded CPython 3.5m macOS 10.9+ intel macOS 10.9+ x86-64

rasterio-1.0b4-cp34-cp34m-manylinux1_x86_64.whl (18.6 MB view details)

Uploaded CPython 3.4m

rasterio-1.0b4-cp34-cp34m-macosx_10_9_intel.macosx_10_9_x86_64.whl (23.0 MB view details)

Uploaded CPython 3.4m macOS 10.9+ intel macOS 10.9+ x86-64

rasterio-1.0b4-cp27-cp27mu-manylinux1_x86_64.whl (18.2 MB view details)

Uploaded CPython 2.7mu

rasterio-1.0b4-cp27-cp27m-manylinux1_x86_64.whl (18.2 MB view details)

Uploaded CPython 2.7m

rasterio-1.0b4-cp27-cp27m-macosx_10_9_x86_64.whl (23.1 MB view details)

Uploaded CPython 2.7m macOS 10.9+ x86-64

File details

Details for the file rasterio-1.0b4.tar.gz.

File metadata

  • Download URL: rasterio-1.0b4.tar.gz
  • Upload date:
  • Size: 1.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for rasterio-1.0b4.tar.gz
Algorithm Hash digest
SHA256 d81d6bfdecf4f2158066ddcaa1bf512914ec162ede543e52eddd9dcf897e1b3c
MD5 095d70fa32d0381f5fd75b180c4eb954
BLAKE2b-256 93f82b93143f1ead2a990fe71bc34e0c0ad0fffd9e7fff458ccb00dae7f12382

See more details on using hashes here.

File details

Details for the file rasterio-1.0b4-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for rasterio-1.0b4-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 09f6a59ade468f3d1ffb763977994f6a2a2fdff4bc84b73fcf51f9e40d2c4cd8
MD5 c3e8dbc403a67061b19fb9d6e0a78dff
BLAKE2b-256 35cc4a171bdae7455280a671e78c9306b2a0423bc0d9784965eaa197424eeb7c

See more details on using hashes here.

File details

Details for the file rasterio-1.0b4-cp36-cp36m-macosx_10_9_intel.macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for rasterio-1.0b4-cp36-cp36m-macosx_10_9_intel.macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f9a480d45972b907f9a9a4162d28743099a5ae638b5a58552271894baac7550b
MD5 e866a4ff5df004ad4b62629694024a45
BLAKE2b-256 1973e2ce7f32805cf6f64078576ab4e5bda58a1c8680bf1d94ff3d3e29a7a20b

See more details on using hashes here.

File details

Details for the file rasterio-1.0b4-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for rasterio-1.0b4-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d7ec93cbbe35b433dc921b6d64268b58fd9ec13f4c3c81ebbd74f64300561de2
MD5 1eb38d8293e9f0bcddba0ea1fd69f6ba
BLAKE2b-256 31d456ad629a43ab0267646225d90922ab09988217f9c4711816290e54e848e0

See more details on using hashes here.

File details

Details for the file rasterio-1.0b4-cp35-cp35m-macosx_10_9_intel.macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for rasterio-1.0b4-cp35-cp35m-macosx_10_9_intel.macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 4947e76a0e4783508a47ba33ceceee3a79ee93a1f3cb5d32c3fbb2d50b57fe7f
MD5 c75b9e1cb295221ab5b745040ed178f6
BLAKE2b-256 3e92d1cf76bf70d4ae0bd5517c63142b47b24d44a3ff7a9889acbd3fadd3c65e

See more details on using hashes here.

File details

Details for the file rasterio-1.0b4-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for rasterio-1.0b4-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0c9564664183deb42ebd1b4d232f467e1ddd8218e0844ff1e005facae6b5e412
MD5 9524993e7ed91c35744ab68ff3ae79e9
BLAKE2b-256 2df4041cf97fb502dee290018ec57c69bd890968c5126017b6075e154b5d241c

See more details on using hashes here.

File details

Details for the file rasterio-1.0b4-cp34-cp34m-macosx_10_9_intel.macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for rasterio-1.0b4-cp34-cp34m-macosx_10_9_intel.macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f71ebe42471329a419b9368460948f31d23ea29d1a9f073a1727a13c5d7c18fe
MD5 bdd4d82a1f81db23704bfc9bbb67022b
BLAKE2b-256 c59ea7029d18f411ede0ceff0009df5b26b4a7e7123c6c54552364a6b7637eaa

See more details on using hashes here.

File details

Details for the file rasterio-1.0b4-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for rasterio-1.0b4-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7575334980bca96cacd84c3ae72862cd8dc0c5822b28fbe32051e0844ca1f418
MD5 31f63abdf0267c5a0a0ef1ee1bc10e98
BLAKE2b-256 96fa4bb0ffa719d271920b90d521c968adb6137f2bc59acb9b69b0e206f0f26e

See more details on using hashes here.

File details

Details for the file rasterio-1.0b4-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for rasterio-1.0b4-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e2fe6a2c92f01ea822afe3c3e9e51e66ebccd0054317312c968f7fa0ed818108
MD5 17d5600f8782d78734906d136326fbe3
BLAKE2b-256 f1dbccc849f1f517310a01d65084cccc0cc12006eefc94ae9bd9a96e46513479

See more details on using hashes here.

File details

Details for the file rasterio-1.0b4-cp27-cp27m-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for rasterio-1.0b4-cp27-cp27m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 cb3a12709f3e038e069a4fd870489bfc2373390d8db62f1879575e01f35a8460
MD5 fecb2143aa5e7dde92d05d2b6bd906b5
BLAKE2b-256 62fd3ae03831ca14cbff3d4de25e463a17b36b91ac6c327636f242a2f06a4235

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page