> ## Documentation Index
> Fetch the complete documentation index at: https://docs.wherobots.com/llms.txt
> Use this file to discover all available pages before exploring further.

# ST_HilbertDistance

Maps the midpoint of a geometry's envelope to an address along a Hilbert curve over a supplied extent. Use the result as a spatial sorting, clustering, or partitioning key. It is a one-dimensional curve address, not a geometric distance.

The envelope midpoint is scaled independently on each axis to the integer grid from `0` through `2^level - 1`. Coordinates outside the supplied extent are clipped to its nearest edge. A zero-width axis maps to grid coordinate zero. Z and M coordinates do not affect the result.

Use the same extent and level for geometries whose keys you want to compare. Different geometries whose envelope midpoints map to the same grid position receive the same key.

## Signatures

```sql theme={"system"}
ST_HilbertDistance(geometry: Geometry, xmin: Double, ymin: Double, xmax: Double, ymax: Double, level: Integer)
```

## Parameters

<ParamField body="geometry" type="Geometry" required>
  The input geometry. The function uses the midpoint of its envelope, not its centroid.
</ParamField>

<ParamField body="xmin" type="Double" required>
  The minimum X coordinate of the extent used to scale the midpoint.
</ParamField>

<ParamField body="ymin" type="Double" required>
  The minimum Y coordinate of the extent used to scale the midpoint.
</ParamField>

<ParamField body="xmax" type="Double" required>
  The maximum X coordinate of the extent used to scale the midpoint.
</ParamField>

<ParamField body="ymax" type="Double" required>
  The maximum Y coordinate of the extent used to scale the midpoint.
</ParamField>

<ParamField body="level" type="Integer" required>
  The curve level. Levels 1 through 16 provide increasingly fine ordering keys. For a non-empty geometry, a level at or below zero returns zero. A level above 16 raises an error.
</ParamField>

## Return type

<ResponseField type="Long">
  A non-negative Hilbert address. At level 16, the maximum value is `4294967295`.
</ResponseField>

If any argument is `NULL`, the result is `NULL`. An empty geometry raises an error because it has no envelope midpoint, including when the level is zero or negative.

## How addresses are assigned

At level 2, the extent becomes a 4-by-4 grid. The Hilbert curve visits every cell once and assigns addresses from 0 through 15 along that continuous path:

<img src="https://mintcdn.com/wherobots/qgskT70dgmj4LJ8Z/images/sql-functions/ST_HilbertDistance/ST_HilbertDistance_curve.svg?fit=max&auto=format&n=qgskT70dgmj4LJ8Z&q=85&s=93fead7c9efa9cea7a06112cdf33e9c4" alt="Level-2 Hilbert curve visiting a 4-by-4 grid in address order from 0 through 15" width="1120" height="680" data-path="images/sql-functions/ST_HilbertDistance/ST_HilbertDistance_curve.svg" />

*Consecutive addresses share a grid edge, which is why sorting by the returned key tends to keep nearby records together. The key is an ordering address, not a geometric distance.*

For each input geometry, the function uses the midpoint of its envelope, normalizes that point into the supplied extent, and looks up the cell's Hilbert address. This example also shows how a midpoint outside the extent is clipped before lookup:

<img src="https://mintcdn.com/wherobots/qgskT70dgmj4LJ8Z/images/sql-functions/ST_HilbertDistance/ST_HilbertDistance_workflow.svg?fit=max&auto=format&n=qgskT70dgmj4LJ8Z&q=85&s=5f88d70cf3217bde679355b471403e7f" alt="Five geometry envelopes and midpoints normalized onto a Hilbert grid, then sorted by addresses 0, 2, 4, 13, and 15" width="1200" height="760" data-path="images/sql-functions/ST_HilbertDistance/ST_HilbertDistance_workflow.svg" />

*Only the envelope midpoint determines the address. Different geometries with midpoints in the same grid cell receive the same value at that level.*

## Examples

### Envelope midpoint

The polygon's envelope midpoint is `(0.5, 0.5)`. Within the unit-square extent at level 2, it maps to address `2`:

```sql theme={"system"}
SELECT ST_HilbertDistance(
    ST_GeomFromWKT('POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))'),
    0.0, 0.0, 1.0, 1.0, 2
) AS hilbert_key;
```

```text theme={"system"}
2
```

### Maximum address

The return type can represent the maximum level-16 address without becoming negative:

```sql theme={"system"}
SELECT ST_HilbertDistance(
    ST_GeomFromWKT('POINT (1 0)'),
    0.0, 0.0, 1.0, 1.0, 16
) AS hilbert_key;
```

```text theme={"system"}
4294967295
```
