Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython

rasterstats is unavailable in PyPM, because there aren't any builds for it in the package repositories. Click the linked icons to find out why.

 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
Windows (64-bit)
Mac OS X (10.5+)
Linux (32-bit)
Linux (64-bit)
 
Links
Author
License
BSD
Dependencies

BuildStatus CoverageStatus

The rasterstats python module provides a fast, flexible and robust tool to summarize geospatial raster datasets based on vector geometries.

  • Raster data support

    • Any raster data source supported by GDAL
    • Support for continuous and categorical
    • Respects null/no-data metadata or takes argument
  • Vector data support

    • Points, Lines, Polygon and Multi-* geometries

    • Flexible input formats

      • Any vector data source supported by OGR
      • Python objects that are geojson-like mappings or support the geo_interface
      • Well-Known Text/Binary (WKT/WKB) geometries
  • Depends on GDAL, Shapely and numpy

Install

Using ubuntu 12.04:

sudo apt-get install python-numpy python-gdal
pip install rasterstats

Example Usage

Given a polygon vector layer and a digitial elevation model (DEM) raster, calculate the mean elevation of each polygon:

zones elevation
>>> from rasterstats import raster_stats
>>> stats = raster_stats("tests/data/polygons.shp", "tests/data/elevation.tif")

>>> stats[1].keys()
    ['std', 'count', 'min', 'max', 'sum', 'id', 'mean']

>>> [(f['id'], f['mean']) for f in stats]
    [(1, 756.6057470703125), (2, 114.660084635416666)]
Python interface

In addition to the basic usage above, rasterstats supports other mechanisms of specifying vector geometeries.

It integrates with other python objects that support the geo_interface (e.g. Fiona, Shapely, ArcPy, PyShp, GeoDjango):

>>> import fiona

>>> # an iterable of objects with geo_interface
>>> lyr = fiona.open('/path/to/vector.shp')
>>> features = (x for x in lyr if x['properties']['state'] == 'CT')
>>> raster_stats(features, '/path/to/elevation.tif')
...

>>> # a single object with a geo_interface
>>> lyr = fiona.open('/path/to/vector.shp')
>>> raster_stats(lyr.next(), '/path/to/elevation.tif')
...

Or by using with geometries in "Well-Known" formats:

>>> raster_stats('POINT(-124 42)', '/path/to/elevation.tif')
...
Working with categorical rasters (e.g. vegetation map)

You can treat rasters as categorical (i.e. raster values represent discrete classes) if only interested in the counts of unique pixel values.

For example, this polygon is comprised of 12 pixels of oak (raster value 32) and 78 pixels of grassland (raster value 33):

>>> raster_stats(lyr.next(), '/path/to/vegetation.tif', categorical=True)

>>> [{'id': 1, 32: 12, 33: 78}]

Keep in mind that rasterstats just reports on the pixel values as keys; It is up to the programmer to associate the pixel values with their appropriate meaning (e.g. oak == 32) for reporting.

Issues

Find a bug? Report it via github issues by providing

  • a link to download the smallest possible raster and vector dataset necessary to reproduce the error
  • python code or command to reproduce the error
  • information on your environment: versions of python, gdal and numpy and system memory

Subscribe to package updates

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.