Metadata-Version: 2.4
Name: vector2dggs
Version: 0.28.0
Summary: CLI DGGS indexer for vector geospatial data
License: LGPL-3.0-or-later
License-File: COPYING
License-File: COPYING.LESSER
Keywords: dggs,vector,h3,rHEALPix,cli
Author: James Ardo
Author-email: ardoj@landcareresearch.co.nz
Maintainer: Richard Law
Maintainer-email: lawr@landcareresearch.co.nz
Requires-Python: >=3.11,<4.0
Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Scientific/Engineering :: GIS
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Provides-Extra: a5
Provides-Extra: all
Provides-Extra: dggal
Provides-Extra: geohash
Provides-Extra: gnosis
Provides-Extra: h3
Provides-Extra: healpix
Provides-Extra: isea3h
Provides-Extra: isea4r
Provides-Extra: isea7h
Provides-Extra: isea7h-z7
Provides-Extra: isea9r
Provides-Extra: ivea3h
Provides-Extra: ivea4r
Provides-Extra: ivea7h
Provides-Extra: ivea7h-z7
Provides-Extra: ivea9r
Provides-Extra: postgres
Provides-Extra: rhp
Provides-Extra: rtea3h
Provides-Extra: rtea4r
Provides-Extra: rtea7h
Provides-Extra: rtea7h-z7
Provides-Extra: rtea9r
Provides-Extra: s2
Requires-Dist: antimeridian (>=0.4.8,<0.5.0)
Requires-Dist: click (>=8.1.7,<9.0.0)
Requires-Dist: click-log (>=0.4.0,<0.5.0)
Requires-Dist: dggal (>=0.0.7,<0.0.8) ; extra == "isea4r" or extra == "ivea4r" or extra == "rtea4r" or extra == "isea9r" or extra == "ivea9r" or extra == "rtea9r" or extra == "healpix" or extra == "gnosis" or extra == "isea7h" or extra == "ivea7h" or extra == "rtea7h" or extra == "isea7h-z7" or extra == "ivea7h-z7" or extra == "rtea7h-z7" or extra == "isea3h" or extra == "ivea3h" or extra == "rtea3h" or extra == "dggal" or extra == "all"
Requires-Dist: geopandas (>=1.1.2,<2.0.0)
Requires-Dist: h3 (>=4.1,<5.0) ; extra == "h3" or extra == "all"
Requires-Dist: numpy (>=2,<3)
Requires-Dist: psycopg2-binary (>=2.9.9,<3.0.0) ; extra == "postgres" or extra == "all"
Requires-Dist: pya5 (>=1.0.0,<2.0.0) ; extra == "a5" or extra == "all"
Requires-Dist: pyarrow (>=23.0.1,<24.0.0)
Requires-Dist: pyogrio (>=0.13,<0.14)
Requires-Dist: pyproj (>=3.7,<4.0)
Requires-Dist: python-geohash (>=0.8.5,<0.9.0) ; extra == "geohash" or extra == "all"
Requires-Dist: rhealpixdggs (>=1.0.0,<2.0.0) ; extra == "rhp" or extra == "all"
Requires-Dist: rusty-polygon-geohasher (>=0.2.3,<0.3.0) ; extra == "geohash" or extra == "all"
Requires-Dist: s2geometry (>=0.14.0,<0.15.0) ; extra == "s2" or extra == "all"
Requires-Dist: shapely (>=2.1,<3.0)
Requires-Dist: sqlalchemy (>=2.0.32,<3.0.0)
Requires-Dist: tqdm (>=4.67,<5.0)
Project-URL: Repository, https://github.com/manaakiwhenua/vector2dggs
Description-Content-Type: text/markdown

# vector2dggs

[![pypi](https://img.shields.io/pypi/v/vector2dggs?label=vector2dggs)](https://pypi.org/project/vector2dggs/) [![DOI](https://zenodo.org/badge/619451613.svg)](https://doi.org/10.5281/zenodo.24149888)

Python-based CLI tool to index vector files to DGGS in parallel, writing out to Parquet.

This is the vector equivalent of [raster2dggs](https://github.com/manaakiwhenua/raster2dggs).

Currently this tool supports the following DGGSs:

- [H3](https://h3geo.org/)
- [rHEALPix](https://datastore.landcareresearch.co.nz/dataset/rhealpix-discrete-global-grid-system) (the [OGC-registered grid](https://www.opengis.net/def/dggrs/OGC/1.0/rHEALPix) by default; see `--grid`)
- [S2](https://s2geometry.io/)
- [A5](https://a5geo.org/)
- Via [DGGAL](https://dggal.org): ISEA4R, IVEA4R, RTEA4R, ISEA9R, IVEA9R, RTEA9R, HEALPix, the GNOSIS Global Grid, and the hexagonal ISEA7H, IVEA7H, RTEA7H (each also under Z7 indexing), ISEA3H, IVEA3H and RTEA3H

... and the following geocode systems:

- [Geohash](https://en.wikipedia.org/wiki/Geohash)

Contributions (especially for other DGGSs), suggestions, bug reports and strongly worded letters are all welcome.

![Land cover polygons indexed to H3 resolutions 10 and 12, with the hive-partitioned Parquet output they produce](./docs/imgs/indexing-example.png "Land cover polygons indexed to H3 resolutions 10 and 12, with the hive-partitioned Parquet output they produce")

## Installation

This tool makes use of optional extras to allow you to install a limited subset of DGGSs.

If you want all possible:

```bash
pip install vector2dggs[all]
```

If you want only a subset, use the pattern `pip install vector2dggs[rhp]` (for one) or `pip install vector2dggs[h3,s2]` (for multiple). The DGGAL grids have one extra each (`isea9r`, `healpix`, ...) or `dggal` for all of them.

A bare `pip install vector2dggs` **will not install any DGGS backends**.

PostgreSQL/PostGIS input requires the `postgres` extra (e.g. `pip install vector2dggs[h3,postgres]`); it is included in `all`.

The `a5` extra uses the [A5 v1 index](https://a5geo.org/blog/2026/10/14/a5-v1) (from vector2dggs 0.26.0). A5 cell IDs written by earlier versions used the v0 index and are not compatible; [`a5.migrate`](https://a5geo.org/docs/api-reference/miscellaneous#migrate) converts them.


## Usage

All commands (`h3`, `rhp`, `s2`, `a5`, `geohash`, and the DGGAL grids `isea4r`, `ivea4r`, `rtea4r`, `isea9r`, `ivea9r`, `rtea9r`, `healpix`, `gnosis`, `isea7h`, `ivea7h`, `rtea7h`, `isea7h_z7`, `ivea7h_z7`, `rtea7h_z7`, `isea3h`, `ivea3h`, `rtea3h`) share the same interface:

```bash
vector2dggs <dggs> [OPTIONS] VECTOR_INPUT OUTPUT_DIRECTORY
```

`VECTOR_INPUT` may be a local file (anything GDAL can read), a remote URI or GDAL virtual path (e.g. `https://…`, `/vsizip/…`), or a PostgreSQL/PostGIS connection URL (with `-lyr` naming the table). `OUTPUT_DIRECTORY` is written as an Apache Parquet data store: a directory with one file per partition.

- `-r`/`--resolution`: the target DGGS resolution. Each output row is one (feature, cell) pair; by default a cell is included when its centre point falls inside the feature, uniformly across all backends (see `-m`/`--mode`).
- `-pr`/`--parent_res`: a coarser resolution used to partition the output (hive directories such as `h3_03=…`); defaults to a fixed offset below the target resolution.
- `-id`/`--id_field`: the feature identifier carried into the output. Repeat it for a composite identifier (`-id region -id parcel_no`): each field becomes an output column, in the order given, and rows sharing the whole key are treated as one feature. Defaults to the input's own internal ID where one exists (a GPKG's FID column, or a DB table's primary key, every column of a composite one); otherwise falls back to a synthetic index tied to row position in the read order — stable across repeated runs of the same unchanged input, but not portable to a different export/copy of the same data.
- `-co`/`--compact`: merges complete sets of sibling cells belonging to one feature (grouped by whichever id_field is in play, explicit, auto-detected, or synthetic, and by the joint key when it is composite), to no coarser than the parent resolution. Compacted output expands back to exactly the full-resolution result. On the hexagonal DGGAL grids it follows the tree of primary parents, as H3 does (see [Compaction](#compaction)).
- `--geo`: plain Parquet by default; `point` or `polygon` writes GeoParquet (v1.1.0) cell geometries instead.
- `-m`/`--mode`: which of a polygon's cells are indexed. See [Containment modes](#containment-modes) below.
- `--grid` (rHEALPix only): which rHEALPix grid to index to, as a registered DGGRS URI or a parameter spec. The default is the [OGC-registered rHEALPix DGGRS](https://www.opengis.net/def/dggrs/OGC/1.0/rHEALPix): WGS84, N_side 3, polar squares 0 and 0, central meridian 50° E, as DGGAL and OGC API - DGGS servers use it. A spec `<north square><south square><N_side>[@<central meridian>]` builds a WGS84 grid with those parameters: `003` is the OGC layout with the meridian at 0° (the default before 0.28), `113` and `123@50` move the polar squares, and `003@50` is the OGC grid itself. N_side 2 or 3 (the finest resolution is 15 for N_side 3 and 24 for 2). Grids name different cells (between `003` and the default, equatorial cells coincide from resolution 2 up under different identifiers; polar cells differ), so output is only meaningful with its grid.
- `--cell-id`: `string` (default) or `uint64`. DGGS with an integer cell form (A5, H3, S2, rHEALPix) can write cell IDs as unsigned 64-bit integers instead of text — useful where downstream tools take integer cell IDs directly (e.g. DuckDB's `h3` extension). Cell IDs are worked in each backend's native form internally regardless of this flag; it only controls the final output rendering. For rHEALPix the integer is `level << 59 | row << 30 | column`: on the default grid the OGC-registered integer identifier (the same value DGGAL gives), on any other `--grid` rhealpixdggs' packing for that grid, which the file's metadata identifies. HEALPix's two forms are two schemes rather than two spellings: strings are DGGAL's hierarchical identifiers, integers are NUNIQ (`4 * 4^level + nested index`, what healpy's `uniq2nest` and MOC files read; subtract `4 * 4^level` for the nested index at a known level). Geohash, with no integer form, rejects `--cell-id uint64`. Readers that go through GDAL, QGIS among them, have no unsigned 64-bit type and show a `uint64` column as a lossy Real, and type the hive partition key from the directory name as String; use `string` output with those.
- `--drop-spikes METRES`: off by default. Removes polygon spikes — places where a ring runs out to a vertex and back along almost the same line — narrower than the given width, before indexing. A spike encloses practically nothing, but S2 reads each edge as a great-circle arc, which bows away from the straight line between its ends; a spike returning within that bow makes the ring cross itself on the sphere, though it is valid to a planar reading.

A polygon a backend cannot index correctly is skipped with a warning naming its feature, rather than indexed wrongly: S2 refuses one whose edges cross as great-circle arcs (a spike, or a narrow inlet close to a long edge, whose arc bows further), and rHEALPix one that is invalid to shapely. Near-duplicate vertices, such as reprojection leaves, are merged first, which changes nothing a geometry means. If the feature has other parts (multipart, or cut by bisection), those are still indexed.

If nothing is indexed — usually because the resolution is too coarse for the input — the run warns and writes an empty dataset: one zero-row file carrying the schema the run would have produced, so a reader sees an empty layer rather than an unreadable directory.

The full reference (`vector2dggs h3 --help`; the other commands differ only in their resolution ranges):

```
Usage: vector2dggs h3 [OPTIONS] VECTOR_INPUT OUTPUT_DIRECTORY

  Ingest a vector dataset and index it to the H3 DGGS.

  VECTOR_INPUT is the path to input vector geospatial data. OUTPUT_DIRECTORY
  should be a directory, not a file or database table, as it will instead be the
  write location for an Apache Parquet data store.

Options:
  -v, --verbosity LVL             Either CRITICAL, ERROR, WARNING, INFO or DEBUG
                                  [default: INFO]
  -r, --resolution INTEGER RANGE  H3 resolution to index  [0<=x<=15; required]
  -pr, --parent_res INTEGER RANGE
                                  H3 parent resolution for the output partition.
                                  Defaults to resolution - 6  [0<=x<=15]
  -id, --id_field TEXT            Field to use as an ID; repeat for a composite
                                  ID (one output column per field, in the order
                                  given). Defaults to the input's own internal
                                  ID if it has one (e.g. a GPKG's FID column, or
                                  a DB table's primary key, every column of a
                                  composite key), otherwise falls back to a
                                  constructed 0...n index on the original
                                  feature order.
  -k, --keep_attributes           Retain attributes in output. The default is to
                                  create an output that only includes H3 cell ID
                                  and the ID given by the -id field (or the
                                  default index ID).
  -ka, --keep_attribute TEXT      Retain only this attribute in output; repeat
                                  for multiple. Takes precedence over
                                  -k/--keep_attributes.
  -p, -t, --processes, --threads INTEGER RANGE
                                  Number of parallel workers: process pools for
                                  indexing and for merging/compaction, and the
                                  thread pool for bisection.  [default: (CPU
                                  count - 1, capped by available memory); x>=1]
  -cp, --compression TEXT         Compression method to use for the output
                                  Parquet files. Options include 'snappy',
                                  'gzip', 'brotli', 'lz4', 'zstd', etc. Use
                                  'none' for no compression.  [default: snappy]
  -lyr, --layer TEXT              Name of the layer or table to read when using
                                  an input that supports layers or tables
  -g, --geom_col TEXT             Column name to use when using a spatial
                                  database connection as input  [default: geom]
  --geo [none|point|polygon]      Select geometry encoding for the output:
                                  'none' for regular Parquet (no GeoParquet
                                  metadata), or 'point'/'polygon' to write
                                  GeoParquet (v1.1.0) with the corresponding
                                  geometry type.  [default: none]
  --cell-id [string|uint64]       Cell ID output form: 'string' (default) or
                                  'uint64' (unsigned 64-bit integer; e.g. for
                                  DuckDB interop).  [default: string]
  -m, --mode [centre|intersects|within]
                                  How a polygon's cells are chosen, named for
                                  the DE-9IM predicate each applies: 'centre'
                                  takes each cell whose centre point falls
                                  inside the feature; 'intersects' takes every
                                  cell whose area meets the feature, covering it
                                  completely, so a feature smaller than a cell
                                  still produces one (rasterio's all_touched);
                                  'within' takes only cells whose area lies
                                  wholly inside it. Linestrings and points are
                                  indexed the same way in every mode.  [default:
                                  centre]
  --drop-spikes METRES            Remove polygon spikes - where a ring runs out
                                  and back along almost the same line - narrower
                                  than this many metres, before indexing. Off by
                                  default, as it changes the input geometry. A
                                  spike is harmless to a planar reading of a
                                  polygon, but where edges are read as great-
                                  circle arcs (S2) the ring can cross itself,
                                  and the polygon is then skipped with a
                                  warning.  [x>0]
  --tempdir PATH                  Temporary data is created during the execution
                                  of this program. This parameter allows you to
                                  control where this data will be written.
                                  [default: (system temp dir)]
  -co, --compact                  Compact the H3 cells up to the parent
                                  resolution, grouping by id_field (explicit,
                                  auto-detected, or the default 0...n sequence).
  -o, --overwrite
  --version                       Show the version and exit.
  --help                          Show this message and exit.
```

vector2dggs is a command-line tool; the underlying Python API (`vector2dggs.common.index`) can be called directly but is not a stable, supported interface.

## Containment modes

Filling a polygon means deciding, cell by cell, whether the cell belongs to the feature — and that needs a rule for what counts. `-m`/`--mode` chooses it, and each mode is named for the [DE-9IM](https://en.wikipedia.org/wiki/DE-9IM) predicate it applies between the cell and the feature:

| Mode | A cell is indexed when… | Use it for |
| --- | --- | --- |
| `centre` (default) | its **centre point** falls inside the feature | one cell per place: cells partition the plane, so a coverage input gives a coverage output, with no cell claimed by two neighbouring features |
| `intersects` | its **area** meets the feature anywhere | complete coverage: querying by any cell that intersects a feature finds it, and a feature smaller than a cell still gets one (in `centre` mode such features produce nothing and are reported as omitted) |
| `within` | its **area** lies wholly inside the feature | conservative selection: every cell returned is unambiguously the feature's, at the cost of dropping the boundary |

The modes are nested: `within` ⊆ `centre` ⊆ `intersects`. Only polygons are affected — a traced line already covers every cell it meets, and a point has exactly one cell.

![The three containment modes compared across H3, S2, A5, rHEALPix and Geohash on the same land cover polygons](./docs/imgs/containment-modes.gif "The three containment modes compared across H3, S2, A5, rHEALPix and Geohash on the same land cover polygons")

Each backend applies the same rule to its own cells, so the three modes differ in the same way everywhere: `centre` tiles the plane, `intersects` spills past the feature's edge and gives a shared edge's cells to both neighbours, and `within` opens a gap along every boundary. (The same animation is also available as [an MP4](./docs/imgs/containment-modes.mp4), which GitHub will not play inline but which is crisper for slides.)

`within` is computed as the intersecting cells minus the cells the feature's own boundary passes through, rather than asked of the backend directly. The two are equivalent for a whole feature, but only the first survives bisection: vector2dggs cuts large polygons into pieces to bound memory, and a cell wholly inside a feature need not be wholly inside any single piece the cut lines cross. Both terms of the subtraction are taken before cutting, so neither depends on where the cuts fall.

`within` is unavailable for A5, whose library (`pya5`) offers no wholly-within test; `vector2dggs a5 -m within` is rejected rather than silently indexed some other way.

If you have arrived from raster tooling, `intersects` is what rasterio's `all_touched=True` and GDAL's `ALL_TOUCHED` do. It is deliberately **not** called `touched`, because DE-9IM already has a `touches` predicate meaning something close to the opposite — geometries meeting only at their boundaries, with their interiors disjoint. The backends' own vocabularies map straight across: H3's `center`/`overlap`/`full` and rHEALPix's `center`/`overlapping`/`full` are `centre`/`intersects`/`within`.

### What "cell" means here

A DGGS cell has no canonical rendering as either a point or an area, so each mode above is explicit about which it tests, and each backend answers with its own:

- **Centre point.** rHEALPix uses the cell's nucleus; geohash the midpoint of its box; H3, S2, A5 and the DGGAL grids their own cell centres.
- **Area.** A geohash cell is a longitude/latitude aligned box, bounded by parallels and meridians, so it is exactly what it appears to be; H3's cells are decided in longitude/latitude too; S2 and A5 work on a sphere, where cell edges are great-circle arcs; a rHEALPix cell is an exact quadrilateral in its own projection, which on the ellipsoid stays longitude/latitude aligned for equatorial cells but gives polar cells curved edges that are sampled. A DGGAL zone's edges are curves in longitude/latitude; `intersects` and `within` test DGGAL's refined outline (120 points), while `--geo polygon` writes only the zone's corners joined by straight edges, so neighbouring output polygons can show slivers of gap or overlap at coarse resolutions.

So `intersects` and `within` are exact with respect to each backend's own model of a cell's extent, and near a feature's boundary different backends will not necessarily agree about the same cell — which is a property of the grids, not a defect in any one of them. The feature's own edges are read under the same model: as straight lines in longitude/latitude by H3, geohash and rHEALPix, and as great-circle arcs by S2 and A5. For short edges the two readings barely differ, but they are not interchangeable at length: a 40° edge at 60°N passes 170 km from where the great circle between its endpoints runs.

## Output metadata

Every output file carries a `dggrs` key in its Parquet schema metadata (JSON) saying what its cell IDs mean: the DGGS, the cell and parent columns and their resolutions, the containment mode, whether the output is compacted, the cell ID form, the grid (its DGGRS codespace URI where OGC has registered one: rHEALPix's default grid, and ISEA3H, ISEA7H, ISEA9R, IVEA3H, IVEA7H, IVEA9R, HEALPix and the GNOSIS Global Grid among the DGGAL grids; for rHEALPix also the `--grid` value as given, and the full DGGRS definition in OGC API - DGGS's schema, which spells out the central meridian and polar squares of a custom grid), and the versions of vector2dggs and the DGGS library that produced it. The cell ID columns carry the grid, DGGS and resolution as Arrow field metadata too, so a column selected into another tool keeps its grid with it. An rHEALPix identifier is only meaningful with its grid, and the `codespace` (or `grid`) value is a valid `--grid` for reproducing the same one.

```python
>>> import json, pyarrow.parquet as pq
>>> json.loads(pq.read_schema("out/rhp_05=S33375/part.0-….parquet").metadata[b"dggrs"])["codespace"]
'https://www.opengis.net/def/dggrs/OGC/1.0/rHEALPix'
```

## Visualising output

Output is in the Apache Parquet format, a directory with one file per partition. With `--geo point` or `--geo polygon` output will be written as GeoParquet (v1.1.0) with the respective geometry types. GeoParquet can be visualised using desktop GIS tools.

Within each file, rows are sorted by cell ID and the cell ID column is written with Parquet's delta encoding (`DELTA_BINARY_PACKED` for `--cell-id uint64`, `DELTA_BYTE_ARRAY` for strings). Sorted cell IDs differ from their neighbours by small, regular increments, so the identifier column, which would otherwise dominate a file, costs a few bits per row. These are standard Parquet (v2) encodings, read by pyarrow, DuckDB, Polars, Spark, GDAL and R's arrow among others.

The Apache Parquet output is indexed by an ID column (which you can specify), so it should be ready for two intended use-cases:
- Joining attribute data from the original feature-level data onto computed DGGS cells.
- Joining other data to this output on the DGGS cell ID. (The output has a column like `{dggs}_\d`, e.g. `h3_09` or `h3_12` according to the target resolution, zero-padded to account for the maximum resolution of the DGGS).

## Compaction

Compaction is supported with the `-co/--compact` argument. A complete set of sibling cells is replaced by their parent, as far up as the partition's parent resolution, so a feature's interior coarsens while its boundary stays at the target resolution. The coverage is unchanged; only the row count falls.

![The same land cover indexed to rHEALPix resolution 13, before and after compaction](docs/imgs/compaction-example.png "The same land cover indexed to rHEALPix resolution 13, before and after compaction")

Compaction respects overlapping polygons by considering each feature independently, a feature being the rows that share an id (the whole key, for a composite `-id`). This does mean that the index of the result is not necessarily unique (unless your input is a vector _coverage_, i.e. it does not have overlaps — as the example above is, so each of its cells belongs to exactly one feature). Where features do overlap, a cell covered by two of them appears once per feature.

The output's `dggrs` metadata records how a compacted cell expands, under `compaction`: `expansion` is `children` for every grid above, where a cell stands for its children at each finer level, and `step` is how many levels one compaction merges (1 everywhere except the 3H grids).

### Tree compaction on the hexagonal grids (7H, Z7 and 3H)

A hexagon's children do not nest inside it, and DGGAL gives every hexagonal zone several parents. For partitioning and compaction vector2dggs uses the tree of *primary* parents, one per zone: on 7H a zone's seven primary children are the one centred on it and the six around that, which is exactly what the digits of a Z7 identifier spell out, and on 3H a zone has between one and seven. Compaction is then H3's rule, a parent replaces its primary children once all are present, and `compaction.expansion` is `primary-children`. On 3H a third of zones have seven primary children and the rest just one, which would make a parent stand for a third of its own area, so 3H compacts two levels at a time instead, as an aperture-9 grid: every zone's primary grandchildren are a full set of seven or thirteen. Compacted 3H output therefore holds every second level below the resolution, and with an odd gap between `-r` and `-pr` the coarsest compacted cell is one level finer than `-pr` (the run warns). Three things follow:

- Expand a compacted cell by its primary children, level by level: for Z7 identifiers that is prefix matching, as with H3. This is not DGGAL's notion of sub-zones (the 13 zones touching a 7H zone), so `dgg decompact` and `getSubZones` would expand this output wrongly, by a fringe; the metadata says which rule applies.
- Compacted output does not overlap itself, and a feature compacts the same way whether taken whole or partition by partition. DGGAL's own `compactZones` rule has neither property: its output overlaps along every feature edge, and because 6 of a zone's 13 sub-zones belong to neighbouring partitions, no partition's own cell can ever be emitted. That is why it is not used here.
- A compacted cell's hexagon is not the union of what it stands for; about 7% lies outside its primary descendants and as much of theirs lies outside it, as with H3. Expand before measuring area.

### For development

In brief, to get started:

- Install [Poetry](https://python-poetry.org/docs/basic-usage/)
- Create and populate the virtual environment with `poetry install`. This will install necessary dependencies.

No system GDAL is required: vector data is read via [pyogrio](https://pyogrio.readthedocs.io/), whose wheels bundle GDAL. If you need a GDAL driver that pyogrio's bundled build lacks, build pyogrio from source against your own GDAL.
- Subsequently, activate the virtual environment with `eval "$(poetry env activate)"`.

If you run `poetry install -E all --with dev` and activate the environment with `eval "$(poetry env activate)"`, the CLI tool will be aliased so you can simply use `vector2dggs` rather than `poetry run vector2dggs`.

For partial backend support you can consider `poetry install --with dev -E h3 -E s2` etc. To check what is installed: `poetry show --tree`.

Alternatively, it is also possible to install using pip with `pip install -e .`, and bypass Poetry.

#### Code formatting, linting and type checking

[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

Please run `black .`, `ruff check .` and `mypy vector2dggs/` before committing. All three are enforced in CI.

#### Tests

Tests are included. Some tests (covering the PostgreSQL/PostGIS input path) spin up a throwaway PostGIS container via Docker; they'll be skipped automatically if Docker isn't available, rather than failing the run.

To run them, activate the Poetry environment first (`eval "$(poetry env activate)"`), then run:

```bash
python -m pytest -n auto tests/
```

Or without activating the shell:

```bash
poetry run pytest -n auto tests/
```

To test a specific DGGS:

```bash
python -m pytest tests/test_runthrough.py -k "a5" -v
```

Test data are included at `tests/data/`.

## Example commands

With a local GPKG:

```bash
vector2dggs h3 -v DEBUG -id title_no -r 12 -o ~/Downloads/nz-property-titles.gpkg ~/Downloads/nz-property-titles.parquet

```

Indexing small polygons without losing any of them (see [Containment modes](#containment-modes)):

```bash
vector2dggs s2 -id parcel_id -r 16 -m intersects ./parcels.gpkg ./parcels.parquet
```

With a composite identifier, compacting per feature on the joint key:

```bash
vector2dggs h3 -id region -id parcel_no -co -r 12 ./parcels.gpkg ./parcels.parquet
```

With a PostgreSQL/PostGIS connection:

```bash
vector2dggs h3 -v DEBUG -id ogc_fid -r 9 -pr 5 -t 4 --overwrite -lyr topo50_lake postgresql://user:password@host:port/db ./topo50_lake.parquet
```

## Citation

Citation metadata is maintained in [`CITATION.cff`](CITATION.cff). GitHub renders this as a **"Cite this repository"** button on the repository page (top-right of the About panel), which provides ready-to-copy BibTeX and APA formats.

[![manaakiwhenua-standards](https://github.com/manaakiwhenua/vector2dggs/workflows/manaakiwhenua-standards/badge.svg)](https://github.com/manaakiwhenua/manaakiwhenua-standards)

