# `BoundingBox`

A rectangle on a map, given by its four sides.

Use it for the visible part of a map or a rough search area. For an exact outline, use [`Polygon`](/docs/types/type-polygon.md). For a distance around a place, use [`Circle`](/docs/types/type-circle.md).

## Shape

| Field | Type | Required? |
|---|---|---|
| `south` | [`Latitude`](/docs/types/type-latitude.md) | Yes |
| `west` | [`Longitude`](/docs/types/type-longitude.md) | Yes |
| `north` | [`Latitude`](/docs/types/type-latitude.md) | Yes |
| `east` | [`Longitude`](/docs/types/type-longitude.md) | Yes |

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

- `south_not_above_north`: south <= north

## Example

```jadpo
BoundingBox { south: 51.28 west: -0.51 north: 51.69 east: 0.33 }
```

This operation gives one: `geo.bounding_box`.

## Good to know

- A box may cross the 180° meridian: its `west` is then further east than its `east`. But `south` can't be above `north`.
- `geo.bounding_box` gives the box around any shape.
- `==` 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

An object of its parts, in a request, a response or `json.write`:

```json
{"south":"51.28","west":"-0.51","north":"51.69","east":"0.33"}
{"south":"-20","west":"170","north":"-10","east":"-170"}
```

## Details

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

`BoundingBox` 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 4 columns, each named for the field, `_` and its suffix. This is `{"south":"-20","west":"170","north":"-10","east":"-170"}`:

| Column | Type | Holds |
|---|---|---|
| `<field>_south` | `numeric` | `-20` |
| `<field>_west` | `numeric` | `170` |
| `<field>_north` | `numeric` | `-10` |
| `<field>_east` | `numeric` | `-170` |

### How it is shown to people

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

With `locale: Locale.en_gb`:

| Value | Shown |
|---|---|
| `{"south":"51.28","west":"-0.51","north":"51.69","east":"0.33"}` | `51.28° N, 0.51° W – 51.69° N, 0.33° E` |
| `{"south":"-20","west":"170","north":"-10","east":"-170"}` | `20° S, 170° E – 10° S, 170° W` |

### Operations

- `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.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.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>
