Soil Aggregation Tool
Soil_aggregation_tool is an installable Python package for aggregating SWAT
usersoil classes and reclassifying a matching soil raster. It groups comparable
soils, selects an appropriate number of k-means clusters, and produces a smaller
usersoil table, a matching raster, and a two-column lookup table.
Developed by Yashas Kumar. Packaged and maintained by chandankr014.
Outputs
Each run creates exactly these three files:
| Output file | Contents |
|---|---|
Soil_usersoil.csv |
Aggregated usersoil table with the same columns as the input usersoil CSV. |
Soil_lookup.csv |
Lookup containing only MUID and SNAM, copied from Soil_usersoil.csv. |
Soil.tif |
Input soil raster reclassified to the new aggregated MUID values. |
Generated soil names are Soil_1, Soil_2, Soil_3, and so on. Therefore,
the MUID and SNAM pairs in Soil_lookup.csv always match those in
Soil_usersoil.csv.
The program does not delete unrelated files already present in the output folder. For a folder containing only these three files, use a new or empty output folder.
How the tool works
- It reads the unique MUID values and cell areas from the input soil raster.
- It selects usersoil rows whose
MUIDvalues occur in that raster. - It divides the selected soils into comparable strata using
HYDGRP,TEXTURE,SOL_ZMX, andNLAYERS. - Within each stratum, it clusters soils using
SOL_K1,SOL_AWC1,SOL_BD1, andUSLE_K1. - It compares candidate cluster counts using the silhouette score.
- It aggregates each selected cluster into one usersoil row and assigns new
sequential MUIDs beginning with
1. - It reclassifies the raster from the original MUID values to the new MUIDs.
- It copies
MUIDandSNAMfrom the output usersoil into the lookup file.
Input requirements
Usersoil CSV
The input CSV must contain these columns:
| Column | Use |
|---|---|
MUID |
Soil identifier corresponding to raster cell values. |
SNAM |
Soil name. Output names are replaced with Soil_1, Soil_2, etc. |
HYDGRP |
Hydrologic soil group used to define strata. |
TEXTURE |
Soil texture used to define strata. |
SOL_ZMX |
Maximum soil depth used to define strata. |
NLAYERS |
Number of soil layers used to define strata. |
SOL_K1 |
Layer-one saturated hydraulic conductivity used for clustering. |
SOL_AWC1 |
Layer-one available water capacity used for clustering. |
SOL_BD1 |
Layer-one bulk density used for clustering. |
USLE_K1 |
Layer-one soil erodibility factor used for clustering. |
All other input usersoil columns are retained in Soil_usersoil.csv. If the
input includes every column from SOL_Z1 through SOL_Z10, the tool corrects
an inconsistent NLAYERS value from the number of positive layer depths.
Soil raster
The raster must:
- be a single-band GeoTIFF;
- contain cell values corresponding to
MUIDvalues in the usersoil CSV; and - contain valid raster dimensions, transform, and coordinate-system metadata.
Values 0 and 65535 are treated as background or no-data by default. Raster
MUIDs that do not occur in the usersoil CSV are written as 0 in Soil.tif
and reported in the terminal.
Software requirements
- Python 3.10 or newer
- NumPy
- pandas
- Rasterio
- scikit-learn
Python installs the package dependencies from pyproject.toml.
Download and install from GitHub
First, install Git and Python 3.10 or newer. Then follow these steps.
1. Download the repository
Open PowerShell or a terminal and run:
git clone https://github.com/chandankr014/swatplus-soil-aggregation-tool.git
2. Enter the downloaded folder
cd swatplus-soil-aggregation-tool
3. Create a virtual environment
python -m venv .venv
Activate it on Windows PowerShell:
.\.venv\Scripts\Activate.ps1
On macOS or Linux, activate it with:
source .venv/bin/activate
4. Install the package
python -m pip install .
The required Python packages are installed automatically. Confirm that the installation succeeded:
soil-aggregation-tool --help
Install directly without cloning
The package can also be installed directly from GitHub:
python -m pip install "git+https://github.com/chandankr014/swatplus-soil-aggregation-tool.git"
Install in Jupyter Notebook
Run this command in a notebook cell to install the package into the environment used by the current notebook kernel:
%pip install "git+https://github.com/chandankr014/swatplus-soil-aggregation-tool.git"
Restart the notebook kernel after installation. The Python API can then be used directly in another cell:
from soil_aggregation_tool import aggregate_soils
result = aggregate_soils(
usersoil_path="usersoil.csv",
raster_path="soil_input.tif",
output_dir="soil_output",
)
print(result.usersoil_path)
print(result.lookup_path)
print(result.raster_path)
Running with interactive input
Run the command without file arguments:
soil-aggregation-tool
The program asks for the inputs as follows:
Usersoil CSV path:
Soil raster path:
Output folder path:
Enter a complete or relative path after each prompt. The prompts do not use default filenames or square brackets.
Example:
Usersoil CSV path: D:\data\usersoil.csv
Soil raster path: D:\data\soil_input.tif
Output folder path: D:\data\soil_output
Running with command-line arguments
File paths can be supplied directly to avoid interactive prompts:
soil-aggregation-tool `
--usersoil "D:\data\usersoil.csv" `
--raster "D:\data\soil_input.tif" `
--output-dir "D:\data\soil_output"
The Python module form is equivalent:
python -m soil_aggregation_tool `
--usersoil "D:\data\usersoil.csv" `
--raster "D:\data\soil_input.tif" `
--output-dir "D:\data\soil_output"
Paths are quoted so paths containing spaces work correctly. The output folder is created automatically when it does not exist.
Optional settings
| Argument | Default | Description |
|---|---|---|
--max-optimal-clusters |
15 |
Maximum number of clusters evaluated within each soil stratum. |
--seed |
123 |
Random seed that makes k-means results reproducible. |
--ignore-raster-values |
0 65535 |
Space-separated raster values treated as background. |
Example using 0 and -9999 as ignored values:
soil-aggregation-tool `
--usersoil "D:\data\usersoil.csv" `
--raster "D:\data\soil_input.tif" `
--output-dir "D:\data\soil_output" `
--ignore-raster-values 0 -9999
Using the package from Python
from soil_aggregation_tool import aggregate_soils
result = aggregate_soils(
usersoil_path="usersoil.csv",
raster_path="soil_input.tif",
output_dir="soil_output",
)
print(result.usersoil_path)
print(result.lookup_path)
print(result.raster_path)
print(result.aggregated_soil_count)
Running the tests
Run the automated test from the repository folder:
python -m unittest discover -s tests -v
The tests create a small usersoil CSV and GeoTIFF, run the complete workflow, and check that:
- only the three documented output files are created;
Soil_lookup.csvhas only theMUIDandSNAMcolumns;- lookup values exactly match
Soil_usersoil.csv; - the new MUID values are written into
Soil.tif; - raster MUIDs missing from the usersoil CSV are written as
0; and - a missing required column raises a clear error.
The same tests run in GitHub Actions before each release is published.
Publishing a release to PyPI
Pushing a tag that starts with v runs .github/workflows/publish.yml, which
tests, builds, and uploads the package to PyPI. The package version is taken
from the tag, so there is no version number to edit by hand.
One-time setup: create a PyPI API token and add it on GitHub under
Settings → Secrets and variables → Actions as PYPI_API_TOKEN.
To release a new version:
git add .
git commit -m "Release v0.1.0"
git push origin main
git tag v0.1.0
git push origin v0.1.0
Check progress in the repository's Actions tab. After it finishes, install with:
pip install --no-cache-dir soil-aggregation-tool
License
Released under the MIT License. See LICENSE.
Repository structure
Soil_aggregation_tool/
|-- .github/
| `-- workflows/
| `-- publish.yml
|-- .gitignore
|-- LICENSE
|-- README.md
|-- pyproject.toml
|-- src/
| `-- soil_aggregation_tool/
| |-- __init__.py
| |-- __main__.py
| |-- cli.py
| `-- core.py
`-- tests/
`-- test_aggregation.py
Large CSV and TIFF datasets are excluded by .gitignore. Keep input data
outside the repository, or check that it is not staged before committing.
Common errors
- Usersoil CSV not found: check the path entered at
Usersoil CSV pathor supplied to--usersoil. - Soil raster not found: check the path entered at
Soil raster pathor supplied to--raster. - Missing required columns: add or rename the columns listed in the usersoil input section.
- No raster MUID values matched: confirm that raster cell values and the
usersoil
MUIDcolumn use the same identifiers. - Output file is open: close the file in other software and run the tool again. When an output is locked, the tool warns and uses a timestamped filename rather than overwriting the open file.
- Arguments are required when input is not interactive: when running from
a script or scheduled job, pass
--usersoil,--raster, and--output-dir.
Metadata
Release files for soil-aggregation-tool 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| soil_aggregation_tool-0.1.0.tar.gz | 16.8 kB | Without provenance | Details for soil_aggregation_tool-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| soil_aggregation_tool-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for soil_aggregation_tool-0.1.0-py3-none-any.whl |
Total release size: 29.2 kB
Release files / soil_aggregation_tool-0.1.0.tar.gz
| Download URL | soil_aggregation_tool-0.1.0.tar.gz |
|---|---|
| Size | 16.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3b3b114efdee4f8b991c0a898f6b43a1ff9809c88d03d66fd2eedb173fe2dbe8
|
|
BLAKE2b-256 checksum How to use checksums |
b72f0f58e2a6488b699a2a4b551a45a0c5ad1cfd5bfe3876e69c1bbcdcfd306a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.14
|
Release files / soil_aggregation_tool-0.1.0-py3-none-any.whl
| Download URL | soil_aggregation_tool-0.1.0-py3-none-any.whl |
|---|---|
| Size | 12.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fa15004eb667dd94bb596fc38f2e5fe59e1e3f4b86bd7ccc817e60ec755f6523
|
|
BLAKE2b-256 checksum How to use checksums |
8b3b32b8ee89587ab145de11d230391059fe850ace95188f712a7d7dcad41226
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.14
|
Security for soil-aggregation-tool 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.