Skip to main content

xagg

Build Status codecov pypi conda-forge Documentation Status

A package to aggregate gridded data in xarray to polygons in geopandas using area-weighting from the relative area overlaps between pixels and polygons.

Installation

The easiest way to install the latest version of xagg is using conda:

conda install -c conda-forge xagg

(There may be a version issue with numba - in which case downgrading numpy using pip install numpy==1.21.0 should fix the problem)

Alternatively, you can use pip, though current dependency issues (esmpy is no longer updated on PyPI) is limiting pip to only installing xagg<=0.1.4:

pip install xagg

Documentation

See the latest documentation at https://xagg.readthedocs.io/en/latest/index.html

Intro

Science often happens on grids - gridded weather products, interpolated pollution data, night time lights, remote sensing all approximate the continuous real world for reasons of data resolution, processing time, or ease of calculation.

However, living things don't live on grids, and rarely play, act, or observe data on grids either. Instead, humans tend to work on the county, state, township, okrug, or city level; birds tend to fly along complex migratory corridors; and rain- and watersheds follow valleys and mountains.

So, whenever we need to work with both gridded and geographic data products, we need ways of getting them to match up. We may be interested for example what the average temperature over a county is, or the average rainfall rate over a watershed.

Enter xagg.

xagg provides an easy-to-use (2 lines!), standardized way of aggregating raster data to polygons. All you need is some gridded data in an xarray Dataset or DataArray and some polygon data in a geopandas GeoDataFrame. Both of these are easy to use for the purposes of xagg - for example, all you need to use a shapefile is to open it:

   import xarray as xr
   import geopandas as gpd
    
   # Gridded data file (netcdf/climate data)
   ds = xr.open_dataset('file.nc')

   # Shapefile
   gdf = gpd.open_dataset('file.shp')

xagg will then figure out the geographic grid (lat/lon) in ds, create polygons for each pixel, and then generate intersects between every polygon in the shapefile and every pixel. For each polygon in the shapefile, the relative area of each covering pixel is calculated - so, for example, if a polygon (say, a US county) is the size and shape of a grid pixel, but is split halfway between two pixels, the weight for each pixel will be 0.5, and the value of the gridded variables on that polygon will just be the average of both.

Here is a sample code run, using the loaded files from above:


   import xagg as xa

   # Get overlap between pixels and polygons
   weightmap = xa.pixel_overlaps(ds,gdf)

   # Aggregate data in [ds] onto polygons
   aggregated = xa.aggregate(ds,weightmap)

   # aggregated can now be converted into an xarray dataset (using aggregated.to_dataset()), 
   # or a geopandas geodataframe (using aggregated.to_dataframe()), or directly exported 
   # to netcdf, csv, or shp files using aggregated.to_csv()/.to_netcdf()/.to_shp()

Researchers often need to weight your data by more than just its relative area overlap with a polygon (for example, do you want to weight pixels with more population more?). xagg has a built-in support for adding an additional weight grid (another xarray DataArray) into xagg.pixel_overlaps().

Finally, xagg allows for direct exporting of the aggregated data in several commonly used data formats:

  • NetCDF
  • CSV for STATA, R
  • Shapefile for QGIS, further spatial processing

Best of all, xagg is flexible. Multiple variables in your dataset? xagg will aggregate them all, as long as they have at least lat/lon dimensions. Fields in your shapefile that you'd like to keep? xagg keeps all fields (for example FIPS codes from county datasets) all the way through the final export. Weird dimension names? xagg is trained to recognize all versions of "lat", "Latitude", "Y", "nav_lat", "Latitude_1"... etc. that the author has run into over the years of working with climate data; and this list is easily expandable as a keyword argument if needed.

Use Cases

Climate econometrics

Many climate econometrics studies use societal data (mortality, crop yields, etc.) at a political or administrative level (for example, counties) but climate and weather data on grids. Oftentimes, further weighting by population or agricultural density is needed.

Area-weighting of pixels onto polygons ensures that aggregating weather and climate data onto polygons occurs in a robust way. Consider a (somewhat contrived) example: an administrative region is in a relatively flat lowlands, but a pixel that slightly overlaps the polygon primarily covers a wholly different climate (mountainous, desert, etc.). Using a simple mask would weight that pixel the same, though its information is not necessarily relevant to the climate of the region. Population-weighting may not always be sufficient either; consider Los Angeles, which has multiple significantly different climates, all with high densities.

xagg allows a simple population and area-averaging, in addition to export functions that will turn the aggregated data into output easily used in STATA or R for further calculations.


Project based on the cookiecutter science project template.

Metadata

Release files for xagg-no-xesmf-deps 0.3.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xagg-no-xesmf-deps 0.3.0.4
File Size Uploaded
xagg_no_xesmf_deps-0.3.0.4.tar.gz 13.3 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for xagg-no-xesmf-deps 0.3.0.4
File Interpreter ABI Platform
xagg_no_xesmf_deps-0.3.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 13.3 MB

Release files / xagg_no_xesmf_deps-0.3.0.4.tar.gz

Download URL xagg_no_xesmf_deps-0.3.0.4.tar.gz
Size 13.3 MB
Tags Source
SHA-256 checksum
How to use checksums
25cb9011fb86955e2410104657763132927c03f5f36bf8762c681a8b0606659f
BLAKE2b-256 checksum
How to use checksums
3cffa49b408243d59c727d55c44886a7e55db6536ef495782fc6486b8d9b286a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.9

Release files / xagg_no_xesmf_deps-0.3.0.4-py3-none-any.whl

Download URL xagg_no_xesmf_deps-0.3.0.4-py3-none-any.whl
Size 38.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3ba07fa610f5c8b858fd4a98ce2d4eb583c5dcb8db0fbe44b888eff3194fbb4e
BLAKE2b-256 checksum
How to use checksums
57d340dfc254e3cac8989147897abba1d8f06602b0a19e7867dda18a3dec2214
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.9

Release history Release notifications | RSS feed

This release

0.3.0.4 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page