<commit_before>rasterstats
===========

|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 <https://gist.github.com/sgillies/2217756>`_
      -  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:

.. figure:: https://github.com/perrygeo/python-raster-stats/raw/master/docs/img/zones_elevation.png
   :align: center
   :alt: zones elevation

::

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

    >>> stats[1].keys()
        ['__fid__', 'count', 'min', 'max', 'mean']

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

Statistics
^^^^^^^^^^

By default, the ``zonal_stats`` function will return the following statistics

- min
- max
- mean
- count

Optionally, these statistics are also available

- sum
- std
- median
- majority
- minority
- unique
- range
- percentile (see note below for details)

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

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

    >>> # also takes space-delimited string
    >>> stats = zonal_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.

*New in 0.6*. 
You can use a percentile statistic by specifying 
``percentile_<q>`` where ``<q>`` can be a floating point number between 0 and 100.

User-defined Statistics
^^^^^^^^^^^^^^^^^^^^^^^
*New in 0.6*. 
You can define your own aggregate functions using the ``add_stats`` argument. 
This is a dictionary with the name(s) of your statistic as keys and the function(s)
as values. For example, to reimplement the `mean` statistic::

    from __future__ import division
    import numpy as np

    def mymean(x):<commit_msg>fix syntax error in examples<commit_after>rasterstats
===========

|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 <https://gist.github.com/sgillies/2217756>`_
      -  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:

.. figure:: https://github.com/perrygeo/python-raster-stats/raw/master/docs/img/zones_elevation.png
   :align: center
   :alt: zones elevation

::

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

    >>> stats[1].keys()
        ['__fid__', 'count', 'min', 'max', 'mean']

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

Statistics
^^^^^^^^^^

By default, the ``zonal_stats`` function will return the following statistics

- min
- max
- mean
- count

Optionally, these statistics are also available

- sum
- std
- median
- majority
- minority
- unique
- range
- percentile (see note below for details)

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

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

    >>> # also takes space-delimited string
    >>> stats = zonal_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.

*New in 0.6*. 
You can use a percentile statistic by specifying 
``percentile_<q>`` where ``<q>`` can be a floating point number between 0 and 100.

User-defined Statistics
^^^^^^^^^^^^^^^^^^^^^^^
*New in 0.6*. 
You can define your own aggregate functions using the ``add_stats`` argument. 
This is a dictionary with the name(s) of your statistic as keys and the function(s)
as values. For example, to reimplement the `mean` statistic::

    from __future__ import division
    import numpy as np

    def mymean(x):
{'fraction_non_alphanumeric': 0.11652087803267866, 'fraction_numerical': 0.018484898498101997, 'mean_word_length': 3.0918298446995274, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 51, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6902065', 'n_tokens_mistral': 1937, 'n_tokens_neox': 1730, 'n_words': 631}