 # Spatial Extension for BigQuery

### BBOX

Description

Returns an array with the boundary box of a given quadint. This boundary box contains the minimum and maximum longitude and latitude. The output format is [West-South, East-North] or [min long, min lat, max long, max lat].

• `quadint`: `INT64` quadint to get the bbox from.

Return type

`ARRAY<FLOAT64>`

Example

 ``````1 2 3 4 5 `````` ``````SELECT carto-os.quadkey.BBOX(4388); -- 22.5 -- -21.943045533438177 -- 45.0 -- 0.0 ``````

### KRING

Description

Returns an array containing all the quadints directly next to the given quadint at the same level of zoom. Diagonal, horizontal and vertical nearby quadints plus the current quadint are considered, so KRING always returns `(distance*2 + 1)^2` quadints.

• `quadint`: `INT64` quadint to get the KRING from.
• `distance`: `INT64` distance (in cells) to the source.

Return type

`ARRAY<INT64>`

Example

 `````` 1 2 3 4 5 6 7 8 9 10 `````` ``````SELECT carto-os.quadkey.KRING(4388, 1); -- 3844 -- 3876 -- 3908 -- 4356 -- 4388 -- 4420 -- 4868 -- 4900 -- 4932 ``````

### KRING_INDEXED

Description

Returns an array containing all the quadints and their relative position to the given quadint in term of x and y. Quadints returned are directly next to the given quadint at the same level of zoom. Diagonal, horizontal and vertical nearby quadints plus the current quadint are considered, so KRING_INDEXED always returns `(distance*2 + 1)^2` quadints.

• `quadint`: `INT64` quadint to get the KRING_INDEXED from.
• `distance`: `INT64` distance (in cells) to the source.

Return type

`ARRAY<STRUCT<x INT64, y INT64, idx INT64>>`

Example

 `````` 1 2 3 4 5 6 7 8 9 10 `````` ``````SELECT carto-os.quadkey.KRING_INDEXED(4388, 1); -- [{"x": "-1", "y": "-1", "idx": "3844"}, -- {"x": "0", "y": "-1", "idx": "3876"}, -- {"x": "1", "y": "-1", "idx": "3908"}, -- {"x": "-1", "y": "0", "idx": "4356"}, -- {"x": "0", "y": "0", "idx": "4388"}, -- {"x": "1", "y": "0", "idx": "4420"}, -- {"x": "-1", "y": "1", "idx": "4868"}, -- {"x": "0", "y": "1", "idx": "4900"}, -- {"x": "1", "y": "1", "idx": "4932"}] ``````

Description

Returns the quadint representation for a given level of detail and geographic coordinates.

• `longitude`: `FLOAT64` horizontal coordinate of the map.
• `latitude`: `FLOAT64` vertical coordinate of the map.
• `resolution`: `INT64` level of detail or zoom.

Return type

`INT64`

Example

 ``````1 2 `````` ``````SELECT carto-os.quadkey.LONGLAT_ASQUADINT(40.4168, -3.7038, 4); -- 4388 ``````

Description

Returns the quadint index for the given point for each zoom level requested, at the specified resolution (computed as the current zoom level + the value of `resolution`). The output is an array of structs with the following elements: quadint `id`, zoom level (`z`), and horizontal (`x`) and vertical (`y`) position of the tile. These quadint indexes can be used for grouping and generating aggregations of points throughout the zoom range requested. Notice the use of an additional variable `resolution` for adjusting the desired level of granularity.

• `longitude`: `FLOAT64` horizontal coordinate of the map.
• `latitude`: `FLOAT64` vertical coordinate of the map.
• `zoom_min`: `INT64` minimum zoom to get the quadints from.
• `zoom_max`: `INT64` maximum zoom to get the quadints from.
• `zoom_step`: `INT64` used for skipping levels of zoom.
• `resolution`: `INT64` resolution added to the current zoom to extract the quadints.

Return type

`ARRAY<STRUCT<INT64, INT64, INT64>>`

Example

 ``````1 2 3 4 5 6 `````` ``````SELECT carto-os.quadkey.LONGLAT_ASQUADINTLIST_RESOLUTION(40.4168, -3.7038, 3, 6, 1, 4); -- id z x y -- 268743 3 4 4 -- 1069960 4 9 8 -- 4286249 5 19 16 -- 17124938 6 39 32 ``````

Description

• `quadkey`: `STRING` quadkey to be converted to quadint.

Return type

`INT64`

Example

 ``````1 2 `````` ``````SELECT carto-os.quadkey.QUADINT_FROMQUADKEY("3001"); -- 4388 ``````

Description

Returns a quadint from `z`, `x`, `y` coordinates.

• `z`: `INT64` zoom level.
• `x`: `INT64` horizontal position of a tile.
• `y`: `INT64` vertical position of a tile.

Constraints

Tile coordinates `x` and `y` depend on the zoom level `z`. For both coordinates, the minimum value is 0, and the maximum value is two to the power of `z`, minus one (`2^z - 1`).

Return type

`INT64`

Example

 ``````1 2 `````` ``````SELECT carto-os.quadkey.QUADINT_FROMZXY(4, 9, 8); -- 4388 ``````

Description

• `quadint`: `INT64` quadint to be converted to quadkey.

Return type

`STRING`

Example

 ``````1 2 `````` ``````SELECT carto-os.quadkey.QUADKEY_FROMQUADINT(4388); -- 3001 ``````

### SIBLING

Description

Returns the quadint directly next to the given quadint at the same zoom level. The direction must be sent as argument and currently only horizontal/vertical movements are allowed.

• `quadint`: `INT64` quadint to get the sibling from.
• `direction`: `STRING` `‘right’|‘left’|‘up’|‘down’` direction to move in to extract the next sibling.

Return type

`INT64`

Example

 ``````1 2 `````` ``````SELECT carto-os.quadkey.SIBLING(4388, 'up'); -- 3876 ``````

Description

Returns the quadint of a given point at a given level of detail.

• `point`: `GEOGRAPHY` point to get the quadint from.
• `resolution`: `INT64` level of detail or zoom.

Return type

`INT64`

Example

 ``````1 2 `````` ``````SELECT carto-os.quadkey.ST_ASQUADINT(ST_GEOGPOINT(40.4168, -3.7038), 4); -- 4388 ``````

Description

Returns an array of quadints that intersect with the given geography at a given level of detail.

• `geography`: `GEOGRAPHY` geography to extract the quadints from.
• `resolution`: `INT64` level of detail or zoom.

Return type

`ARRAY<INT64>`

Example

 ``````1 2 3 4 5 6 7 8 9 `````` ``````SELECT carto-os.quadkey.ST_ASQUADINT_POLYFILL( ST_MAKEPOLYGON(ST_MAKELINE([ST_GEOGPOINT(-363.71219873428345, 40.413365349070865), ST_GEOGPOINT(-363.7144088745117, 40.40965661286395), ST_GEOGPOINT(-363.70659828186035, 40.409525904775634), ST_GEOGPOINT(-363.71219873428345, 40.413365349070865)])), 17); -- 207301334801 -- 207305529105 -- 207305529073 -- 207305529137 -- 207305529169 -- 207301334833 ``````

### ST_BOUNDARY

Description

Returns the boundary for a given quadint. We extract the boundary in the same way as when we calculate its BBOX, then enclose it in a GeoJSON and finally transform it into a geography.

• `quadint`: `INT64` quadint to get the boundary geography from.

Return type

`GEOGRAPHY`

Example

 ``````1 2 `````` ``````SELECT carto-os.quadkey.ST_BOUNDARY(4388); -- POLYGON((22.5 0, 22.5 -21.9430455334382, 22.67578125 ... ``````

Description

Returns the centroid for a given quadint.

• `quadint`: `INT64` quadint to get the centroid geography from.

Return type

`GEOGRAPHY`

Example

 ``````1 2 `````` ``````SELECT carto-os.quadkey.ST_GEOGPOINTFROMQUADINT(4388); -- POINT(33.75 22.2982994295938) ``````

### TOCHILDREN

Description

Returns an array with the children quadints of a given quadint for a specific resolution. A children quadint is a quadint of higher level of detail that is contained by the current quadint. Each quadint has four children by definition.

• `quadint`: `INT64` quadint to get the children from.
• `resolution`: `INT64` resolution of the desired children.

Return type

`ARRAY<INT64>`

Example

 ``````1 2 3 4 5 `````` ``````SELECT carto-os.quadkey.TOCHILDREN(1155, 4); -- 4356 -- 4868 -- 4388 -- 4900 ``````

### TOPARENT

Description

• `quadint`: `INT64` quadint to get the parent from.
• `resolution`: `INT64` resolution of the desired parent.

Return type

`INT64`

Example

 ``````1 2 `````` ``````SELECT carto-os.quadkey.TOPARENT(4388, 3); -- 1155 ``````

### VERSION

Description

Returns the current version of the quadkey module.

Return type

`STRING`

Example

 ``````1 2 `````` ``````SELECT carto-os.quadkey.VERSION(); -- 1.0.4 ``````

Description

Returns the zoom level `z` and coordinates `x`, `y` for a given quadint.

• `quadint`: `INT64` quadint we want to extract tile information from.

Return type

`STRUCT<INT64, INT64, INT64>`

Example

 ``````1 2 3 `````` ``````SELECT carto-os.quadkey.ZXY_FROMQUADINT(4388); -- z x y -- 4 9 8 ``````