Skip to main content

No project description provided

Project description

BuildStatus CoverageStatus PyPiVersion PyPiDownloads

The rasterstats python module provides a fast and flexible tool to summarize geospatial raster datasets based on vector geometries (i.e. zonal statistics).

  • 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()
    ['__fid__', 'count', 'min', 'max', 'mean']

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

Statistics

By default, the raster_stats function will return the following statistics

  • min

  • max

  • mean

  • count

Optionally, these statistics are also available

  • sum

  • std

  • median

  • majority

  • minority

  • unique

  • range

You can specify the statistics to calculate using the stats argument:

>>> stats = raster_stats("tests/data/polygons.shp",
                         "tests/data/elevation.tif"
                         stats=['min', 'max', 'median', 'majority', 'sum'])

>>> # also takes space-delimited string
>>> stats = raster_stats("tests/data/polygons.shp",
                         "tests/data/elevation.tif"
                         stats="min max median majority sum")

Note that the more complex statistics may require significantly more processing so performance can be impacted based on which statistics you choose to calculate.

Specifying Geometries

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')
...

Feature Properties

By default, an __fid__ property is added to each feature’s results. None of the other feature attributes/proprties are copied over unless copy_properties is set to True:

>>> stats = raster_stats("tests/data/polygons.shp",
                         "tests/data/elevation.tif"
                         copy_properties=True)

>>> stats[0].has_key('name')  # name field from original shapefile is retained
True

Working with categorical rasters

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

For example, you may have a raster vegetation dataset and want to summarize vegetation by polygon. Statistics such as mean, median, sum, etc. don’t make much sense in this context (What’s the sum of oak + grassland?).

The polygon below 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

Project details


Download files

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

Source Distributions

rasterstats-0.3.2.zip (16.6 kB view details)

Uploaded Source

rasterstats-0.3.2.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

rasterstats-0.3.2.win32.exe (211.7 kB view details)

Uploaded Source

File details

Details for the file rasterstats-0.3.2.zip.

File metadata

  • Download URL: rasterstats-0.3.2.zip
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for rasterstats-0.3.2.zip
Algorithm Hash digest
SHA256 b9aed62836a32cff2ae140d9d31314a651e87f1bb6bc22c9a264c333c78c16bc
MD5 74bb284a3fed82083346f6d6ade620b4
BLAKE2b-256 11722111c9ff96fe3bcd54321a7ebd4e500b5d5f6f92f1cc151d54277ddfa56a

See more details on using hashes here.

File details

Details for the file rasterstats-0.3.2.tar.gz.

File metadata

  • Download URL: rasterstats-0.3.2.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for rasterstats-0.3.2.tar.gz
Algorithm Hash digest
SHA256 3349240b62319d2e67c1434d018afb798f76a3a0189e676146e1dc00674942b5
MD5 d885f1df02351a0dcc43ce5f83f7ca3c
BLAKE2b-256 b5011bfc0222fba1dec4a3edddc7a40b05cab0bc739633fcb2ddcaf5629cc844

See more details on using hashes here.

File details

Details for the file rasterstats-0.3.2.win32.exe.

File metadata

File hashes

Hashes for rasterstats-0.3.2.win32.exe
Algorithm Hash digest
SHA256 e9b6981b86e30bdad5f11af17b1100a555436d8b8dc012d87311a9a485ea0be9
MD5 5a760d5b63a48e2224ba81f3b0d8c9d1
BLAKE2b-256 2a7d0d1dc1a2ccc27d60c5d6a376bb2f77c167655b2766106bab1e78180b004a

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