Metadata-Version: 2.5
Name: laterite-quarry
Version: 0.1.0
Summary: A polite CLI for pulling public AGS4 geotechnical datasets (BGS, RVO) into a local, citable mirror.
Project-URL: Repository, https://github.com/niko86/laterite-quarry
Project-URL: Changelog, https://github.com/niko86/laterite-quarry/blob/main/CHANGELOG.md
License-Expression: MIT
License-File: LICENSE
Keywords: AGS4,BGS,borehole,geotechnical,open data
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering
Requires-Python: >=3.12
Requires-Dist: laterite>=0.17.1
Provides-Extra: rvo
Requires-Dist: py7zr>=0.22; extra == 'rvo'
Description-Content-Type: text/markdown

# laterite-quarry

`quarry` pulls public AGS4 geotechnical datasets into a local mirror, keeps
their licence with them, and stays out of the way of the sites it reads.

[laterite-forge](https://github.com/niko86/laterite-forge) makes synthetic AGS4
data. Quarry digs up the real thing. Both feed
[laterite](https://github.com/niko86/laterite), which reads and validates it.

**Status: early release.** Commands may still change between 0.x versions.

## Install with uv

Run it once, with nothing installed:

```bash
uvx --from "laterite-quarry[rvo]" quarry rvo list
```

Install the `quarry` command for good:

```bash
uv tool install "laterite-quarry[rvo]"
quarry doctor
```

Or add it to a uv project, to use the mirror from Python:

```bash
uv add "laterite-quarry[rvo]"
```

The `[rvo]` extra adds `.7z` support, which the RVO archives need. Leave it
off if you only want BGS data. With pip: `pip install "laterite-quarry[rvo]"`.

## Sources

| Source | What | How it is reached |
|---|---|---|
| `bgs` | British Geological Survey AGS exports, by area | The BGS AGS export API |
| `rvo` | Dutch offshore wind site investigations (Fugro) | A hand-curated list of direct file links |

## Use

```bash
quarry bgs count --bbox -0.133,51.514,-0.127,51.518
quarry bgs pull tcr --bbox -0.133,51.514,-0.127,51.518 --dry-run
quarry bgs pull tcr --bbox -0.133,51.514,-0.127,51.518
quarry rvo list
quarry rvo pull hkz-wfs3-bh        # needs the [rvo] extra for .7z
quarry list
quarry holes bgs/tcr               # one row per borehole: ID, type, depth, groups, project
quarry check bgs/tcr               # validate with laterite
quarry cite bgs/tcr                # licence and attribution line
```

`--bbox` is `west,south,east,north` in decimal degrees. Add `--json` for a
provenance envelope (`meta.source` is `live` or `local`), or `--agent` for the
same in compact form.

## How the files are laid out

`--files` on a pull chooses what lands in the mirror:

| `--files` | What you get |
|---|---|
| `as-sent` (default) | Every file exactly as the source supplied it. BGS: a file per project per cell. RVO: a file per borehole. |
| `per-project` | One AGS4 file per project in `projects/`, merged with laterite. Rows are matched by their KEY headings, so a borehole supplied twice lands once. |
| `archives` (BGS only) | The downloaded zips, unopened. |

A merged file is written by laterite, so it is not byte-for-byte what the
source sent, though no value is changed. A project that will not merge cleanly
is kept as sent and named in the pull's output.

## From Python

```python
import laterite
from laterite_quarry import cli, store

cli.main(["rvo", "pull", "hkz-wfs3-bh"])  # one download, then cached
folder = store.dataset_dir("rvo", "hkz-wfs3-bh")
for path in sorted(folder.glob("*.AGS")):
    print(path.name, laterite.read(path).groups)
```

Save that as `holes.py` and run it without setting anything up:

```bash
uv run --with "laterite-quarry[rvo]" holes.py
```

## Being a good citizen

Neither site publishes a rate limit. BGS's service code has no rate limiter,
only its cap of 50 boreholes per export; neither site sends rate-limit headers,
and neither `robots.txt` sets a crawl delay. BGS's terms forbid anything
resembling a denial-of-service attack. So this tool sets its own limit, and
enforces it in one place (`http.py`):

- One request at a time, at least 2 seconds apart per host. `--interval` can
  raise the gap; it cannot go below 1 second.
- The gap holds between runs too: the time of the last request to each host is
  kept in the mirror, so a script calling `quarry` in a loop is still spaced.
- A server's `Retry-After` is honoured, and failures back off: 4, 8, then 16
  seconds, then a clean failure.
- A request budget per run (`--budget`, 40 by default); exceeding it stops the run.
- Nothing is fetched twice: a mirrored dataset is reused unless `--refresh`.
- A pull counts first and refuses an area over `--max-holes`.
- A User-Agent naming this tool and this repository.
- Only https requests to the two known hosts are made; anything else is
  refused, including a redirect that leads elsewhere.
- Responses and unpacked archives are size-capped, and a pull replaces the
  mirror only once it has finished.
- RVO's `robots.txt` disallows `/search`, so the tool never searches or crawls
  that site. It fetches only links in `rvo_catalogue.json`.
- Whatever a run drops (cells it could not split, archive members it refused,
  files it did not check) is reported in its output.

## Licences

This code is MIT. The data is not ours: each mirrored dataset records its own
licence and attribution, and `quarry cite` prints them. No data files are
distributed in this repository.

- BGS exports state the Open Government Licence v3.0 in the metadata file
  inside each download.
- RVO states CC0 1.0 for its site content unless a document says otherwise.

## Exit codes

`0` ok · `1` findings (`check`) · `2` usage error · `3` not found, or a file
could not be read or written · `4` `check` met a file it could not read as
AGS4 · `5` pull refused by its size cap, or a missing optional dependency ·
`7` upstream failure, request budget spent, or a request or archive this tool
refused.

## Known limits

- A dataset name is 1-64 characters of `a-z`, `0-9`, `-` and `_`.
- A BGS area over the export limit is fetched as several cells, so one project
  can arrive as a file per cell. `quarry holes` is the joined-up view: it lists
  each borehole once, by BGS's own identifier (`BGS_LOCA_ID`). A borehole that
  comes back in two cells is marked as a second copy, and a file holding
  nothing but second copies is removed. Files are otherwise kept exactly as
  BGS sent them.

## Developing

```bash
uv sync
uv run ruff check . && uv run ruff format --check . && uv run ty check && uv run pytest -q
```

The tests never touch the network.

To release: bump `version` in `pyproject.toml`, date the changelog entry,
merge to `main`, then run the **Release** workflow from the Actions tab. It
publishes to PyPI and then tags the commit.
