> For the complete documentation index, see [llms.txt](https://docs.carto.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.carto.com/data-and-analysis/analytics-toolbox-for-snowflake/sql-reference/s2.md).

# s2

Our S2 module is based on a port of the official S2 geometry library created by Google. For more information about S2 check the [library's website](http://s2geometry.io/) or the [Spatial Indexes section](https://docs.carto.com/data-and-analysis/analytics-toolbox-for-snowflake/key-concepts/spatial-indexes#s2) of this documentation.

## S2\_BOUNDARY <a href="#s2_boundary" id="s2_boundary"></a>

```sql
S2_BOUNDARY(id)
```

**Description**

Returns the boundary for a given S2 cell ID. We extract the boundary by getting the corner longitudes and latitudes, then enclose it in a GeoJSON and finally transform it into geography.

**Input parameters**

* `id`: `BIGINT` S2 cell ID to get the boundary geography from.

**Return type**

`GEOGRAPHY`

**Example**

```sql
SELECT CARTO.CARTO.S2_BOUNDARY(955378847514099712);
-- { "coordinates": [ [ [ -3.743508991266907, 40.24850114133136 ], [ -3.743508991266907, 40.57421345060903 ] ...
```

{% hint style="info" %}
S2 Cell edges are spherical geodesics.
{% endhint %}

## S2\_FROMGEOGPOINT <a href="#s2_fromgeogpoint" id="s2_fromgeogpoint"></a>

```sql
S2_FROMGEOGPOINT(point, resolution)
```

**Description**

Returns the S2 cell ID of a given point at a requested resolution.

**Input parameters**

* `point`: `GEOGRAPHY` point to get the ID from.
* `resolution`: `INT` level of detail or zoom.

**Return type**

`BIGINT`

**Example**

```sql
SELECT CARTO.CARTO.S2_FROMGEOGPOINT(ST_POINT(-3.7038, 40.4168), 8);
-- 955378847514099712
```

## S2\_FROMHILBERTQUADKEY <a href="#s2_fromhilbertquadkey" id="s2_fromhilbertquadkey"></a>

```sql
S2_FROMHILBERTQUADKEY(hquadkey)
```

**Description**

Returns the conversion of a Hilbert quadkey (a.k.a Hilbert curve quadtree ID) into a S2 cell ID.

**Input parameters**

* `hquadkey`: `STRING` Hilbert quadkey to be converted.

**Return type**

`BIGINT`

**Example**

```sql
SELECT CARTO.CARTO.S2_FROMHILBERTQUADKEY('0/12220101');
-- 955378847514099712
```

## S2\_FROMLONGLAT <a href="#s2_fromlonglat" id="s2_fromlonglat"></a>

```sql
S2_FROMLONGLAT(longitude, latitude, resolution)
```

**Description**

Returns the S2 cell ID for a given longitude, latitude and zoom resolution.

**Input parameters**

* `longitude`: `DOUBLE` horizontal coordinate on the map.
* `latitude`: `DOUBLE` vertical coordinate on the map.
* `resolution`: `INT` level of detail or zoom.

**Return type**

`BIGINT`

**Example**

```sql
SELECT CARTO.CARTO.S2_FROMLONGLAT(-3.7038, 40.4168, 8);
-- 955378847514099712
```

## S2\_TOHILBERTQUADKEY <a href="#s2_tohilbertquadkey" id="s2_tohilbertquadkey"></a>

```sql
S2_TOHILBERTQUADKEY(id)
```

**Description**

Returns the conversion of a S2 cell ID into a Hilbert quadkey (a.k.a Hilbert curve quadtree ID).

**Input parameters**

* `id`: `BIGINT` S2 cell ID to be converted.

**Return type**

`STRING`

**Example**

```sql
SELECT CARTO.CARTO.S2_TOHILBERTQUADKEY(955378847514099712);
-- 0/12220101
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.carto.com/data-and-analysis/analytics-toolbox-for-snowflake/sql-reference/s2.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
