Skip to main content

AGRS – Agricultural Remote Sensing Library

Project description

AGRS – Agricultural Remote Sensing Library

PyPI DOI Socket Downloads License: MIT

Description

AGRS is a domain-focused Python library that turns Sentinel-2 imagery into agronomy-ready features for tasks such as yield modeling, stress analysis, and site-specific fertilizer (NPK) recommendation.

Instead of re-implementing STAC queries, cloud filters, band math, and geometry clipping in every project, AGRS provides opinionated, agriculture-centric pipelines:

  • Search & fetch Sentinel-2 L2A scenes (via Microsoft Planetary Computer STAC).
  • Compute a comprehensive set of classic vegetation, water, moisture, and burn indices.
  • Select snapshots using agronomic strategies (fractions of season, specific dates, all scenes, best cloud-free scenes).
  • Aggregate to field-level features per index and time-fraction of the season.
  • Optionally return per-band statistics (mean, median, percentiles, …) for QA and custom modeling.
  • Return tidy pandas.DataFrame objects ready to join with yield, NPK, and management tables.

The goal is to maximize value for agricultural ML workflows: field trial analysis, site-specific NPK optimization, and crop monitoring.

Key features

  • Sentinel-2 access

    • Microsoft Planetary Computer STAC API.
    • Automatic bounding-box search from field geometries (GeoDataFrame).
  • Band handling

    • Automatic band detection (default: all B* assets) or user-defined band list.
    • Robust geometry clipping for each field.
  • Built-in index formulas (Sentinel-2 friendly)
    (only computed if required bands are present)

    • Vegetation / chlorophyll:
      • NDVI, EVI, EVI2, SAVI, MSAVI, OSAVI
      • GNDVI, VARI, GCI
      • NDRE, RECI, ARVI
      • MCARI, MCARI2
    • Water / moisture / burn:
      • NDWI (McFeeters), MNDWI
      • NDMI (a.k.a moisture index)
      • NBR, NBR2
    • Safe division with NaNs for invalid cases (no hard crashes on zero / missing).
  • Snapshot selection strategies

    • fractional – nearest scenes to temporal fractions (e.g. [0.3, 0.7] of the season).
    • fixed_date – snapshot closest to a single target date.
    • dates – snapshots closest to a list of key dates.
    • top_n_cloudfree – N scenes with lowest eo:cloud_cover.
    • all – use all scenes between start_date and end_date.
  • Return modes

    • return_mode="features" (default):
      • Field-level aggregated index features (per field, per index, per time-fraction).
    • return_mode="bands":
      • Per-field, per-date statistics for each band:
        • mean, median, min, max, std, p10, p90.
    • return_mode="both":
      • Tuple (features_df, bands_df) with both outputs.
  • Field-level aggregation

    • For each field and index:
      • Aggregation over the selected snapshots with statistics such as mean, median, min, max, std.
    • Uses time fractions to encode position within the season (0–1).

Installation

pip install agrs

AGRS requires Python 3.9+ and depends on geopandas, shapely, rasterio, pystac-client, and planetary-computer.

Quick start (features only)

import geopandas as gpd
from shapely.geometry import Point
from agrs.client import s2agc

# 1) Single test field
field_geom = Point(-8.0, 32.0)

fields = gpd.GeoDataFrame(
    {"field_id": [1], "geometry": [field_geom]},
    crs="EPSG:4326",
)

# 2) Create client
client = s2agc(
    source="planetary_computer",
    max_cloud=0.2,
)

# 3) Get field-level index features with fractional snapshots (e.g. 30%, 60%, 90%, 100% of season)
features_df = client.get_features(
    fields=fields,
    field_id_col="field_id",
    start_date="2018-10-01",
    end_date="2019-06-30",
    crop="wheat",
    snapshot_strategy="fractional",
    fractions=[0.3, 0.6, 0.9, 1.0],
    return_mode="features",  # default
)

print(features_df.head())

Examples of other behaviours

1. Custom fractional snapshots (e.g. 30% and 70% of season)

features_df = client.get_features(
    fields=fields,
    field_id_col="field_id",
    start_date="2018-10-01",
    end_date="2019-06-30",
    crop="wheat",
    snapshot_strategy="fractional",
    fractions=[0.3, 0.7],
    return_mode="features",
)

2. Bands only (all snapshots between two dates)

bands_df = client.get_features(
    fields=fields,
    field_id_col="field_id",
    start_date="2018-10-01",
    end_date="2019-06-30",
    crop="wheat",
    snapshot_strategy="all",   # use all available scenes
    return_mode="bands",       # per-band stats per date
)

print(bands_df.head())

3. Features + bands together

features_df, bands_df = client.get_features(
    fields=fields,
    field_id_col="field_id",
    start_date="2018-10-01",
    end_date="2019-06-30",
    crop="wheat",
    snapshot_strategy="dates",
    key_dates=["2019-02-15", "2019-04-01"],
    return_mode="both",
)

More usage patterns (fixed-date, top-N cloud-free, multi-field examples) are available in the examples/ folder.

Contributing

Contributions, bug reports, and feature requests are very welcome.

Please see CONTRIBUTING.md for guidelines on how to:

  • Set up a development environment
  • Run tests and examples
  • Propose new indices, snapshot strategies, or data sources
  • Open issues and pull requests

License

AGRS is released under the MIT License. See LICENSE for details.

Project details


Download files

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

Source Distribution

agrs-0.1.4.tar.gz (18.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agrs-0.1.4-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file agrs-0.1.4.tar.gz.

File metadata

  • Download URL: agrs-0.1.4.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for agrs-0.1.4.tar.gz
Algorithm Hash digest
SHA256 ba160bcea1882ace554bc2da26e34bc4e5b5ba3cec3c32278c1c2d6dbc97da0c
MD5 0ca4ea609078901c19506bab0115476f
BLAKE2b-256 43e2ded860375889be1b6d94e38ab01dd54af1ef7f33aa9c5d4c98821ad82e77

See more details on using hashes here.

File details

Details for the file agrs-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: agrs-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for agrs-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 793ea4699f7b5f1565da1dafe4e5e3b4a161a89516783916eaae63ce6d930987
MD5 49fe9ec104be3ef3f8db249f2be17938
BLAKE2b-256 97adc9110a016ff99f55325b000e29e317d1fa1054732ca7dde5ee11619ff9a7

See more details on using hashes here.

Supported by

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