# `Polygon`

An area on a map, with any holes it has.

Use it for a delivery zone, a field or a building's outline. A simple rectangle for a map view is a [`BoundingBox`](/docs/types/type-bounding-box.md), and a distance around a place is a [`Circle`](/docs/types/type-circle.md).

## Shape

| Field | Type | Required? |
|---|---|---|
| `outer` | `List<`[`Point`](/docs/types/type-point.md)`>` | Yes |
| `holes` | `List<List<`[`Point`](/docs/types/type-point.md)`>>` | Yes |

Its parts keep these rules, and a value that breaks one fails with the rule's name:

- `rings_valid`: each ring closed and simple, no rings touching, holes inside the outer ring, every edge under half the globe
- `altitudes_agree`: every position with an altitude, or none

## Example

```jadpo
Polygon {
    outer: [
        Point { latitude: 0.0 longitude: 0.0 altitude: none },
        Point { latitude: 0.0 longitude: 1.0 altitude: none },
        Point { latitude: 1.0 longitude: 1.0 altitude: none },
        Point { latitude: 1.0 longitude: 0.0 altitude: none },
        Point { latitude: 0.0 longitude: 0.0 altitude: none }
    ]
    holes: []
}
```

## Good to know

- Close each ring by repeating its first point, so a triangle has four points. List the outer ring counterclockwise and each hole clockwise.
- A ring can't cross itself, rings can't touch, and holes sit inside the outer ring. A value built from data can fail these checks, so handle `rings_valid`.
- `==` tells you whether two are the same. `<` and `list.sort_by` don't take it, so sort by one of its parts instead.

## In JSON

A GeoJSON geometry, with each position's longitude first, in a request, a response or `json.write`:

```json
{"type":"Polygon","coordinates":[[[0,0],[1,0],[1,1],[0,1],[0,0]]]}
{"type":"Polygon","coordinates":[[[0,0],[1,0],[1,1],[0,1],[0,0]],[[0.25,0.25],[0.25,0.75],[0.75,0.75],[0.75,0.25],[0.25,0.25]]]}
```

## Details

<details>
<summary>Text form, storage, how it is shown, and operations</summary>

`Polygon` is in the group Geospatial, and has been in Jadpo since 0.1.0.

### Text form

It has no single text form, so `text.from` and `text.parse` don't take it.

### How it is stored

It is stored in one column named for the field, of type `geography`. `{"type":"Polygon","coordinates":[[[0,0],[1,0],[1,1],[0,1],[0,0]],[[0.25,0.25],[0.25,0.75],[0.75,0.75],[0.75,0.25],[0.25,0.25]]]}` is stored as `'SRID=4326;POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0), (0.25 0.25, 0.25 0.75, 0.75 0.75, 0.75 0.25, 0.25 0.25))'::geography`.

### How it is shown to people

`text.format(value, locale:)` shows it to people in their locale.

With `locale: Locale.en_gb`:

| Value | Shown |
|---|---|
| `{"type":"Polygon","coordinates":[[[0,0],[1,0],[1,1],[0,1],[0,0]]]}` | `((0°, 0°; 0°, 1° E; 1° N, 1° E; 1° N, 0°; 0°, 0°))` |
| `{"type":"Polygon","coordinates":[[[0,0],[1,0],[1,1],[0,1],[0,0]],[[0.25,0.25],[0.25,0.75],[0.75,0.75],[0.75,0.25],[0.25,0.25]]]}` | `((0°, 0°; 0°, 1° E; 1° N, 1° E; 1° N, 0°; 0°, 0°), (0.25° N, 0.25° E; 0.75° N, 0.25° E; 0.75° N, 0.75° E; 0.25° N, 0.75° E; 0.25° N, 0.25° E))` |

### Operations

- `geo.area(shape: T) -> Area`: A `Polygon`'s or `MultiPolygon`'s area on the ellipsoid, each edge a geodesic and each ring the smaller of the two sides it bounds, holes taken away, rounded half to even to the square millimetre.
- `geo.bounding_box(shape: T) -> BoundingBox`: The smallest box of parallels and meridians holding the shape, an edge's highest or lowest point counted, across the antimeridian when that box is narrower; a shape holding a pole runs from -180 to 180.
- `geo.centroid(shape: T) -> Point`: The shape's centre on the sphere, as PostGIS's `ST_Centroid(x, false)` finds it: each point a unit vector, weighted by area, length or count by the shape's kind, summed, and back to latitude and longitude. It may lie outside a concave polygon.
- `geo.contains(a: T, b: U) -> Bool`: Whether no point of `b` lies outside `a`, edges included. `a` may be a `BoundingBox`, whose sides are compared directly, or a `Circle`, holding the points within its radius of its centre.
- `geo.intersects(a: T, b: U) -> Bool`: Whether the two share at least one point, edges included.
- `geo.to_wkt(shape: T) -> Text`: The shape as WKT, longitude first, as PostGIS's `ST_AsText` writes it: `POINT(-0.1276 51.5072)`, `POINT Z (-0.1276 51.5072 35)` with an altitude, each ring in the order it holds its points.
- `geo.within(a: T, b: U, distance: Length?) -> Bool`: `geo.contains(b, a)`; or, with `distance:`, whether two points are at most that far apart, as `geo.distance` measures them.

It also takes `text.format`, `json.read`, `json.write` and `==`. [How every standard type behaves](/docs/every-standard-type-meets-one-contract.md) says how they work.

</details>
