# lds

This module contains functions and procedures that make use of location data services, such as geocoding, reverse geocoding, isolines and routing computation.

For manual installations of the CARTO Analytics Toolbox, after installing it for the first time, and before using any LDS function you need to call the `SETUP` procedure to configure the LDS and gateway functions. It also optionally sets default credentials.

## Credentials <a href="#credentials" id="credentials"></a>

Your CARTO account has monthly quotas assigned for each LDS service that are used up by every call to the LDS functions and procedures in the Analytics Toolbox.

Accordingly, the use of these functions and procedures requires providing authorization credentials to prevent fraudulent usage. Two parameters are needed:

* `api_base_url` The API base url is simply the address through which you can access all the services of your CARTO account, and it depends on the region or premises where your account is located. Usually it will be this one: `https://gcp-us-east1.api.carto.com`.
* `api_access_token` This token is an [API Access Token](https://docs.carto.com/carto-user-manual/developers/api-access-tokens) that is allowed to use the LDS API. You must keep this secret! Anyone that has access to this token can use up the LDS quota assigned to your account.

Both the API base url and your API access token can be accessed through the developers section of the CARTO user interface. The API base url is displayed inside this section while for the API access token you will have to create a new API access token allowing the LDS API.

For more information about CARTO for developers, please check our [documentation for Developers](https://docs.carto.com/carto-user-manual/developers).

If default credentials are provided to the `SETUP` procedure, you can pass NULL values for the credentials in the LDS functions, and the credentials configured during SETUP will be used.

{% hint style="info" %}
**tip**

To check that everything works correctly, without spending any credits, make a call to the `GET_LDS_QUOTA_INFO` procedure. You can enter the following in the GCP Bigquery console having selected the project where the Analytics Toolbox is installed:

{% tabs %}
{% tab title="carto-un" %}

```sql
SELECT `carto-un`.carto.GET_LDS_QUOTA_INFO(NULL, NULL);
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
SELECT `carto-un-eu`.carto.GET_LDS_QUOTA_INFO(NULL, NULL);
```

{% endtab %}

{% tab title="manual" %}

```sql
SELECT carto.GET_LDS_QUOTA_INFO(NULL, NULL);
```

{% endtab %}
{% endtabs %}

Note that if you haven't set up default LDS credentials with `SETUP` you'll need to provide them when calling the LDS fucntions:

{% tabs %}
{% tab title="carto-un" %}

```sql
SELECT `carto-un`.carto.GET_LDS_QUOTA_INFO(
  '<my-api-base-url>',
  '<my-api-access-token>'
)
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
SELECT `carto-un-eu`.carto.GET_LDS_QUOTA_INFO(
  '<my-api-base-url>',
  '<my-api-access-token>'
)
```

{% endtab %}

{% tab title="manual" %}

```sql
SELECT carto.GET_LDS_QUOTA_INFO(
  '<my-api-base-url>',
  '<my-api-access-token>'
)
```

{% endtab %}
{% endtabs %}

You should get a JSON response like this, with the available services and the quotas:

```json
[
  {
    "used_quota": 10,
    "annual_quota": 100000,
    "providers": {
        "geocoding": "tomtom",
        "isolines": "here",
        "routing":"tomtom"
    }
  }
]
```

LDS quota is an annual quota that defines how much geocoding and isolines you can compute. Each geocoded row or computed isolines counts as one LDS quota unit. The single element in the result of LDS\_QUOTA\_INFO will show your LDS quota for the current annual period (annual\_quota), how much you’ve spent (used\_quota), and which LDS providers are in use.

This also will allow you to verify that you have the right credentials, the AT is installed correctly and the service is working.
{% endhint %}

## GEOCODE\_TABLE <a href="#geocode_table" id="geocode_table"></a>

```sql
GEOCODE_TABLE(api_base_url, api_access_token, input_table, address_column, geom_column, country, options)
```

{% hint style="warning" %}
**warning**

This function consumes LDS quota. Each call consumes as many units of quota as the number of rows of your input table. Before running, we recommend checking the size of the data to be geocoded and your available quota using the [`GET_LDS_QUOTA_INFO`](#get_lds_quota_info) function.
{% endhint %}

**Description**

Geocodes an input table by adding an user defined column `geom_column` with the geographic coordinates (latitude and longitude) of a given address column. This procedure also adds a `carto_geocode_metadata` column with additional information of the geocoding result in JSON format. It geocodes sequentially the table in chunks of 100.

**Input parameters**

* `api_base_url`: `STRING`|`NULL` url of the API where the customer account is stored. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `api_access_token`: `STRING`|`NULL` an [API Access Token](https://docs.carto.com/carto-user-manual/developers/api-access-tokens) that is allowed to use the LDS API. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `input_table`: `STRING` name of the table to be geocoded. Please make sure you have enough permissions to alter this table, as this procedure will add two columns to it to store the geocoding result.
* `address_column`: `STRING` name of the column from the input table that contains the addresses to be geocoded.
* `geom_column`: `STRING`|`NULL` column name for the geometry column. Defaults to `'geom'`. Set to `NULL` to use the default value.
* `country`: `STRING`|`NULL` name of the country in [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2). Defaults to `''`. Set to `NULL` to use the default value.
* `options`: `STRING`|`NULL` containing a valid JSON with the different options. In addition to the options targeted at the geocoding service described below, a boolean option `carto_force_geocode` (false by default) can be used to force geocoding rows that already have a non-null value in `geom_column`. Valid geocoding service options are described in the table below. If no options are indicated then 'default' values would be applied.

  | Provider | Option     | Description                                                                 |
  | -------- | ---------- | --------------------------------------------------------------------------- |
  | `All`    | `language` | A `STRING` that specifies the language of the geocoding in RFC 4647 format. |

If the input table already contains a geometry column with the name defined by `geom_column`, only those rows with `NULL` values will be geocoded.

{% hint style="info" %}
If you plan to repeat the geocoding process, bear in mind that if you drop columns from your table you won't be able to create columns with the same name for a period of time (7 days) because BigQuery reserves the deleted columns names for [*time travel*](https://cloud.google.com/bigquery/docs/time-travel) purposes. So, for example, instead of dropping the `geom` column to re-geocode all rows, update the table and set it to `NULL`.
{% endhint %}

**Examples**

{% tabs %}
{% tab title="carto-un" %}

```sql
CALL `carto-un`.carto.GEOCODE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_address_column', NULL, NULL, NULL);
-- The table `my-project.my-schema.my-table` will be updated
-- adding the columns: geom, carto_geocode_metadata.
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
CALL `carto-un-eu`.carto.GEOCODE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_address_column', NULL, NULL, NULL);
-- The table `my-project.my-schema.my-table` will be updated
-- adding the columns: geom, carto_geocode_metadata.
```

{% endtab %}

{% tab title="manual" %}

```sql
CALL carto.GEOCODE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_address_column', NULL, NULL, NULL);
-- The table `my-project.my-schema.my-table` will be updated
-- adding the columns: geom, carto_geocode_metadata.
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="carto-un" %}

```sql
CALL `carto-un`.carto.GEOCODE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_address_column', 'my_geom_column', NULL, NULL);
-- The table `my-project.my-schema.my-table` will be updated
-- adding the columns: my_geom_column, carto_geocode_metadata.
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
CALL `carto-un-eu`.carto.GEOCODE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_address_column', 'my_geom_column', NULL, NULL);
-- The table `my-project.my-schema.my-table` will be updated
-- adding the columns: my_geom_column, carto_geocode_metadata.
```

{% endtab %}

{% tab title="manual" %}

```sql
CALL carto.GEOCODE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_address_column', 'my_geom_column', NULL, NULL);
-- The table `my-project.my-schema.my-table` will be updated
-- adding the columns: my_geom_column, carto_geocode_metadata.
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="carto-un" %}

```sql
CALL `carto-un`.carto.GEOCODE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_address_column', 'my_geom_column', 'my_country', NULL);
-- The table `my-project.my-schema.my-table` will be updated
-- adding the columns: my_geom_column, carto_geocode_metadata.
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
CALL `carto-un-eu`.carto.GEOCODE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_address_column', 'my_geom_column', 'my_country', NULL);
-- The table `my-project.my-schema.my-table` will be updated
-- adding the columns: my_geom_column, carto_geocode_metadata.
```

{% endtab %}

{% tab title="manual" %}

```sql
CALL carto.GEOCODE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_address_column', 'my_geom_column', 'my_country', NULL);
-- The table `my-project.my-schema.my-table` will be updated
-- adding the columns: my_geom_column, carto_geocode_metadata.
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="carto-un" %}

```sql
CALL `carto-un`.carto.GEOCODE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_address_column', 'my_geom_column', 'my_country', '{"language":"en-US"}');
-- The table `my-project.my-schema.my-table` will be updated
-- adding the columns: my_geom_column, carto_geocode_metadata.
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
CALL `carto-un-eu`.carto.GEOCODE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_address_column', 'my_geom_column', 'my_country', '{"language":"en-US"}');
-- The table `my-project.my-schema.my-table` will be updated
-- adding the columns: my_geom_column, carto_geocode_metadata.
```

{% endtab %}

{% tab title="manual" %}

```sql
CALL carto.GEOCODE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_address_column', 'my_geom_column', 'my_country', '{"language":"en-US"}');
-- The table `my-project.my-schema.my-table` will be updated
-- adding the columns: my_geom_column, carto_geocode_metadata.
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="carto-un" %}

```sql
CALL `carto-un`.carto.GEOCODE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_address_column', 'my_geom_column', 'my_country', '{"carto_force_geocode":true}');
-- The table `my-project.my-schema.my-table` will be updated
-- adding the columns: my_geom_column, carto_geocode_metadata.
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
CALL `carto-un-eu`.carto.GEOCODE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_address_column', 'my_geom_column', 'my_country', '{"carto_force_geocode":true}');
-- The table `my-project.my-schema.my-table` will be updated
-- adding the columns: my_geom_column, carto_geocode_metadata.
```

{% endtab %}

{% tab title="manual" %}

```sql
CALL carto.GEOCODE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_address_column', 'my_geom_column', 'my_country', '{"carto_force_geocode":true}');
-- The table `my-project.my-schema.my-table` will be updated
-- adding the columns: my_geom_column, carto_geocode_metadata.
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
**Additional examples**

* [Geocoding your address data](https://academy.carto.com/advanced-spatial-analytics/spatial-analytics-for-bigquery/step-by-step-tutorials/geocoding-your-address-data)
  {% endhint %}

## GEOCODE\_REVERSE\_TABLE <a href="#geocode_reverse_table" id="geocode_reverse_table"></a>

```sql
GEOCODE_REVERSE_TABLE(api_base_url, api_access_token, input_table, geom_column, address_column, language, options)
```

{% hint style="warning" %}
**warning**

This function consumes LDS quota. Each call consumes as many units of quota as the number of rows your input table has. Before running, we recommend checking the size of the data to be geocoded and your available quota using the [`GET_LDS_QUOTA_INFO`](#get_lds_quota_info) function.
{% endhint %}

**Description**

Reverse-geocodes an input table by adding an user defined column `address_column` with the address coordinates corresponding to a given point location column. It geocodes sequentially the table in chunks of 100 rows.

**Input parameters**

* `api_base_url`: `STRING`|`NULL` url of the API where the customer account is stored. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `api_access_token`: `STRING`|`NULL` an [API Access Token](https://docs.carto.com/carto-user-manual/developers/api-access-tokens) that is allowed to use the LDS API. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `input_table`: `STRING` name of the table to be reverse-geocoded. Please make sure you have enough permissions to alter this table, as this procedure will add two columns to it to store the geocoding result.
* `geom_column`: `STRING`|`NULL` column name for the geometry column that contains the points to be reverse-geocoded. Defaults to `'geom'`.
* `address_column`: `STRING` name of the column where the computed addresses will be stored. It defaults to `'address'`, and it is created on the input table if it doesn't exist.
* `language`: `STRING`|`NULL` language in which results should be returned. Defaults to `''`. The effect and interpretation of this parameter depends on the LDS provider assigned to your account.
* `options`: `STRING`|`NULL` containing a valid JSON with the different options. No options are allowed currently, so this value will not be taken into account.

If the input table already contains a column with the name defined by `address_column`, only those rows with NULL values will be reverse-geocoded.

{% hint style="info" %}
If you plan to repeat the reverse-geocoding process, bear in mind that if you drop columns from your table you won't be able to create columns with the same name for a period of time (7 days) because BigQuery reserves the deleted columns names for [*time travel*](https://cloud.google.com/bigquery/docs/time-travel) purposes. So, for example, instead of dropping the `address_column` column to re-process all rows, update the table and set it to `NULL`.
{% endhint %}

**Examples**

{% tabs %}
{% tab title="carto-un" %}

```sql
CALL `carto-un`.carto.GEOCODE_REVERSE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', NULL, NULL, NULL, NULL);
-- The table `my-project.my-schema.my-table` with a column `geom` will be updated
-- adding the column `address`.
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
CALL `carto-un-eu`.carto.GEOCODE_REVERSE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', NULL, NULL, NULL, NULL);
-- The table `my-project.my-schema.my-table` with a column `geom` will be updated
-- adding the column `address`.
```

{% endtab %}

{% tab title="manual" %}

```sql
CALL carto.GEOCODE_REVERSE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', NULL, NULL, NULL, NULL);
-- The table `my-project.my-schema.my-table` with a column `geom` will be updated
-- adding the column `address`.
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="carto-un" %}

```sql
CALL `carto-un`.carto.GEOCODE_REVERSE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_geom_column', NULL, NULL, NULL);
-- The table `my-project.my-schema.my-table` with a column `my_geom_column` will be updated
-- adding the column `address`.
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
CALL `carto-un-eu`.carto.GEOCODE_REVERSE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_geom_column', NULL, NULL, NULL);
-- The table `my-project.my-schema.my-table` with a column `my_geom_column` will be updated
-- adding the column `address`.
```

{% endtab %}

{% tab title="manual" %}

```sql
CALL carto.GEOCODE_REVERSE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_geom_column', NULL, NULL, NULL);
-- The table `my-project.my-schema.my-table` with a column `my_geom_column` will be updated
-- adding the column `address`.
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="carto-un" %}

```sql
CALL `carto-un`.carto.GEOCODE_REVERSE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_geom_column', 'my_address_column', NULL, NULL);
-- The table `my-project.my-schema.my-table` with a column `my_geom_column` will be updated
-- adding the column `my_address_column`.
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
CALL `carto-un-eu`.carto.GEOCODE_REVERSE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_geom_column', 'my_address_column', NULL, NULL);
-- The table `my-project.my-schema.my-table` with a column `my_geom_column` will be updated
-- adding the column `my_address_column`.
```

{% endtab %}

{% tab title="manual" %}

```sql
CALL carto.GEOCODE_REVERSE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_geom_column', 'my_address_column', NULL, NULL);
-- The table `my-project.my-schema.my-table` with a column `my_geom_column` will be updated
-- adding the column `my_address_column`.
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="carto-un" %}

```sql
CALL `carto-un`.carto.GEOCODE_REVERSE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_geom_column', 'my_address_column', 'en-US', NULL);
-- The table `my-project.my-schema.my-table` with a column `my_geom_column` will be updated
-- adding the column `my_address_column`.
-- The addresses will be in the (US) english language, if supported by the account LDS provider.
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
CALL `carto-un-eu`.carto.GEOCODE_REVERSE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_geom_column', 'my_address_column', 'en-US', NULL);
-- The table `my-project.my-schema.my-table` with a column `my_geom_column` will be updated
-- adding the column `my_address_column`.
-- The addresses will be in the (US) english language, if supported by the account LDS provider.
```

{% endtab %}

{% tab title="manual" %}

```sql
CALL carto.GEOCODE_REVERSE_TABLE('my_api_base_url', 'my_api_access_token', 'my-project.my-schema.my-table', 'my_geom_column', 'my_address_column', 'en-US', NULL);
-- The table `my-project.my-schema.my-table` with a column `my_geom_column` will be updated
-- adding the column `my_address_column`.
-- The addresses will be in the (US) english language, if supported by the account LDS provider.
```

{% endtab %}
{% endtabs %}

## CREATE\_ISOLINES <a href="#create_isolines" id="create_isolines"></a>

```sql
CREATE_ISOLINES(api_base_url, api_access_token, input, output_table, geom_column, mode, range_value, range_type, options)
```

{% hint style="warning" %}
**warning**

This function consumes LDS quota. Each call consumes as many units of quota as the number of rows your input table or query has. Before running, we recommend checking the size of data for which isolines will be created and your available quota using the [`GET_LDS_QUOTA_INFO`](#get_lds_quota_info) function.
{% endhint %}

**Description**

Calculates the isolines (polygons) from given origins (points) in a table or query. It creates a new table with the columns of the input table or query except the `geom_column` plus the isolines in the column `geom` (if the input already contains a `geom` column, it will be overwritten). It calculates isolines sequentially in chunks of N rows, N being the optimal batch size for this datawarehouse and the specific LDS provider that you are using.

The output table will contain a column named `carto_isoline_metadata` with error information for each isoline result. Rows with errors will have a NULL `geom`.

Note that The term *isoline* is used here in a general way to refer to the areas that can be reached from a given origin point within the given travel time or distance (depending on the `range_type` parameter).

**Input parameters**

* `api_base_url`: `STRING`|`NULL` url of the API where the customer account is stored. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `api_access_token`: `STRING`|`NULL` an [API Access Token](https://docs.carto.com/carto-user-manual/developers/api-access-tokens) that is allowed to use the LDS API. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `input`: `STRING` name of the input table or query.
* `output_table`: `STRING` qualified name of the output table, e.g. `<my-project>.<my-dataset>.<my-output-table>`. The process will fail if the table already exists.
* `geom_column`: `STRING` column name for the origin geometry column.
* `mode`: `STRING` type of transport. The supported modes depend on the provider:
  * `HERE`: 'walk', 'car', 'truck', 'taxi', 'bus', 'private\_bus'.
  * `TomTom`: 'walk', 'car', 'bike', 'motorbike', 'truck', 'taxi', 'bus', 'van'.
  * `TravelTime`: 'walk', 'car', 'bike', 'public\_transport', 'coach', 'bus', 'train', 'ferry'.
  * `Mapbox`: 'walk', 'car', 'bike'.
* `range_value`: `INT64` range of the isoline in seconds (for `range_type` 'time') or meters (for `range_type` 'distance').
* `range_type`: `STRING` type of range. Supported: 'time' (for isochrones), 'distance' (for isodistances).
* `options`: `STRING`|`NULL` containing a valid JSON with the different options. Valid options are described in the table below. If no options are indicated then 'default' values would be applied.

  | Provider     | Option            | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
  | ------------ | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
  | `HERE`       | `arrival_time`    | A `STRING` that specifies the time of arrival. If the value is set, a reverse isoline is calculated. If set to `"any"`, then time-dependent effects will not be taken into account. It cannot be used in combination with `departure_time`. Supported: `"any"`, `"now"` and date-time as `"<YYYY-MM-DD>T<hh:mm:ss>"`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
  | `HERE`       | `departure_time`  | Default: `"any"`. A `STRING` that specifies the time of departure. If set to `"any"`, then time-dependent effects will not be taken into account. It cannot be used in combination with `arrival_time`. Supported: `"any"`, `"now"` and date-time as `"<YYYY-MM-DD>T<hh:mm:ss>"`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
  | `HERE`       | `optimize_for`    | Default: `"balanced"`. A `STRING` that specifies how isoline calculation is optimized. Supported: `"quality"` (calculation of isoline focuses on quality, that is, the graph used for isoline calculation has higher granularity generating an isoline that is more precise), `"performance"` (calculation of isoline is performance-centric, quality of isoline is reduced to provide better performance) and `"balanced"` (calculation of isoline takes a balanced approach averaging between quality and performance).                                                                                                                                                                                                                                                                                                                       |
  | `HERE`       | `routing_mode`    | Default: `"fast"`. A `STRING` that specifies which optimization is applied during isoline calculation. Supported: `"fast"` (route calculation from start to destination optimized by travel time. In many cases, the route returned by the fast mode may not be the route with the fastest possible travel time. For example, the routing service may favor a route that remains on a highway, even if a faster travel time can be achieved by taking a detour or shortcut through an inconvenient side road) and `"short"` (route calculation from start to destination disregarding any speed information. In this mode, the distance of the route is minimized, while keeping the route sensible. This includes, for example, penalizing turns. Because of that, the resulting route will not necessarily be the one with minimal distance). |
  | `TomTom`     | `departure_time`  | Default: `"now"`. A `STRING` that specifies the time of departure. Supported: `"now"` and date-time as `"<YYYY-MM-DD>T<hh:mm:ss>"`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
  | `TomTom`     | `traffic`         | Default: `false`. A `BOOL` that specifies if all available traffic information will be taken into consideration. Supported: `true` and `false`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
  | `TravelTime` | `level_of_detail` | A JSON string. In the most typical case, you will want to use a string in the form `{ scale_type: 'simple_numeric', level: -N }`, with `N` being the detail level (-8 by default). Higher Ns (more negative levels) will simplify the polygons more but will reduce performance. There are other ways of setting the level of detail. Check the \[<https://docs.traveltime.com/api/reference/isochrones#arrival\\_searches-level\\_of\\_detail]\\(TravelTime> docs) for more info.                                                                                                                                                                                                                                                                                                                                                              |
  | `TravelTime` | `departure_time`  | Default: `"now"`. A `STRING` that specifies the time of departure. Supported: `"now"` and date-time as `"<YYYY-MM-DD>T<hh:mm:ss>Z"`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |

{% hint style="warning" %}
**warning**

Before running, we recommend checking your provider using the [`GET_LDS_QUOTA_INFO`](#get_lds_quota_info) function. Notice that some of the parameters are provider dependant. Please contact your CARTO representative if you have questions regarding the service provider configured in your organization.
{% endhint %}

**Examples**

{% tabs %}
{% tab title="carto-un" %}

```sql
CALL `carto-un`.carto.CREATE_ISOLINES(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-schema.my-table',
    'my-project.my-schema.my-output-table',
    'my_geom_column',
    'car', 300, 'time',
    NULL
);
-- The table `my-project.my-schema.my-output-table` will be created
-- with the columns of the input table except `my_geom_column`.
-- Isolines will be added in the "geom" column.
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
CALL `carto-un-eu`.carto.CREATE_ISOLINES(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-schema.my-table',
    'my-project.my-schema.my-output-table',
    'my_geom_column',
    'car', 300, 'time',
    NULL
);
-- The table `my-project.my-schema.my-output-table` will be created
-- with the columns of the input table except `my_geom_column`.
-- Isolines will be added in the "geom" column.
```

{% endtab %}

{% tab title="manual" %}

```sql
CALL carto.CREATE_ISOLINES(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-schema.my-table',
    'my-project.my-schema.my-output-table',
    'my_geom_column',
    'car', 300, 'time',
    NULL
);
-- The table `my-project.my-schema.my-output-table` will be created
-- with the columns of the input table except `my_geom_column`.
-- Isolines will be added in the "geom" column.
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="carto-un" %}

```sql
CALL `carto-un`.carto.CREATE_ISOLINES(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-schema.my-table',
    'my-project.my-schema.my-output-table',
    'my_geom_column',
    'car', 1000, 'distance',
    '{"polygons_filter": {"limit": 1}}'
);
-- The table `my-project.my-schema.my-output-table` will be created
-- with the columns of the input table except `my_geom_column`.
-- Isolines will be added in the "geom" column.
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
CALL `carto-un-eu`.carto.CREATE_ISOLINES(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-schema.my-table',
    'my-project.my-schema.my-output-table',
    'my_geom_column',
    'car', 1000, 'distance',
    '{"polygons_filter": {"limit": 1}}'
);
-- The table `my-project.my-schema.my-output-table` will be created
-- with the columns of the input table except `my_geom_column`.
-- Isolines will be added in the "geom" column.
```

{% endtab %}

{% tab title="manual" %}

```sql
CALL carto.CREATE_ISOLINES(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-schema.my-table',
    'my-project.my-schema.my-output-table',
    'my_geom_column',
    'car', 1000, 'distance',
    '{"polygons_filter": {"limit": 1}}'
);
-- The table `my-project.my-schema.my-output-table` will be created
-- with the columns of the input table except `my_geom_column`.
-- Isolines will be added in the "geom" column.
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
**Additional examples**

* [Generating trade areas based on drive/walk-time isolines](https://academy.carto.com/advanced-spatial-analytics/spatial-analytics-for-bigquery/step-by-step-tutorials/generating-trade-areas-based-on-drive-walk-time-isolines)
  {% endhint %}

## CREATE\_ROUTES <a href="#create_routes" id="create_routes"></a>

```sql
CREATE_ROUTES(api_base_url, api_access_token, input, output_table, geom_column, mode, options)
```

{% hint style="warning" %}
**warning**

This function consumes LDS quota. Each call consumes as many units of quota as the number of rows your input query has. Before running, we recommend checking the size of the data used to create the routes and your available quota using the [`GET_LDS_QUOTA_INFO`](#get_lds_quota_info) function.
{% endhint %}

**Description**

Calculates the routes (line strings) between given origins and destinations (points) in a query. It creates a new table with the columns of the input query with the resulting route in the user defined column `geom_column` (if the input already contains a column named `geom_column`, it will be overwritten) and a `carto_routing_metadata` column with the response of the service provider except for the route geometry. It calculates routes sequentially in chunks of 100 rows.

Note that routes are calculated using the external LDS provider assigned to your CARTO account. Currently TomTom, HERE, and TravelTime are supported.

**Input parameters**

* `api_base_url`: `STRING`|`NULL` url of the API where the customer account is stored. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `api_access_token`: `STRING`|`NULL` an [API Access Token](https://docs.carto.com/carto-user-manual/developers/api-access-tokens) that is allowed to use the LDS API. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `input`: `STRING` name of the input query, which must have columns named `origin` and `destination` of type `GEOGRAPHY` and containing points. If a column named `waypoints` is also present, it should contain a STRING with the coordinates of the desired intermediate points with the format `"lon1,lat1:lon2,lat2..."`.
* `output_table`: `STRING` qualified name of the output table, e.g. `<my-project>.<my-dataset>.<my-output-table>`. The process will fail if the table already exists.
* `geom_column`: `STRING` column name for the generated geography column that will contain the resulting routes.
* `mode`: `STRING` type of transport. The supported modes depend on the provider:
  * `TomTom`: 'car', 'pedestrian', 'bicycle', 'motorcycle', 'truck', 'taxi', 'bus', 'van'.
  * `HERE`: 'car', 'truck', 'pedestrian', 'bicycle', 'scooter', 'taxi', 'bus', 'privateBus'.
  * `TravelTime`: 'cycling', 'driving', 'walking', 'public\_transport', 'coach', 'bus', 'train', 'ferry', 'driving+train', 'driving+ferry', 'cycling+ferry', 'cycling+public\_transport'.
* `options`: `STRING`|`NULL` containing a valid JSON with optional parameters. This is intended for advanced use: additional parameters can be passed directly to the Routing provider by placing them in this JSON string. To find out what your provider is, check the [`GET_LDS_QUOTA_INFO`](#get_lds_quota_info) function. The following are some of the most common parameters for each provider:

  * `TomTom`:
    * `avoid`: Specifies something that the route calculation should try to avoid when determining the route. Possible values (several of them can be used at the same time):
      * `tollRoads`
      * `motorways`
      * `ferries`
      * `unpavedRoads`
      * `carpools`
      * `alreadyUsedRoads`
      * `borderCrossings`
      * `tunnels`
      * `carTrains`
      * `lowEmissionZones`
    * `routeType`: Specifies the type of optimization used when calculating routes. Possible values: `fastest`, `shortest`, `short` `eco`, `thrilling`
    * `traffic`: Set to true `true` to consider all available traffic information during routing. Set to `false` otherwise
    * `departAt`: The date and time of departure at the departure point. It should be specified in RFC 3339 format with an optional time zone offset.
    * `arriveAt`: The date and time of arrival at the destination point. It should be specified in RFC 3339 format with an optional time zone offset.
    * `vehicleMaxspeed`: Maximum speed of the vehicle in kilometers/hour.
  * `HERE`
    * `avoid`: Elements or areas to avoid. Information about avoidance can be found [here](https://developer.here.com/documentation/routing-api/dev_guide/topics/use-cases/avoid.html)
    * `departureTime`: The date and time of departure at the departure point. It should be specified in RFC 3339 format with an optional time zone offset.
    * `arrivalTime`: The date and time of arrival at the destination point. It should be specified in RFC 3339 format with an optional time zone offset.
    * `language`: The language to use. Supported language codes can be found [here](https://developer.here.com/documentation/routing-api/dev_guide/topics/languages.html)
  * `TravelTime`
    * `snap_penalty`: Controls whether walking time and distance from the departure location to the nearest road and from the nearest road to the arrival location are included in the route. Possible values: `enabled` (walking segments are added to the total travel time and distance), `disabled` (journey effectively starts and ends at the nearest points on the road network). Defaults to `disabled` for driving modes and `enabled` for other modes.

  For more advanced usage, check the documentation of your provider's routing API.

  * [`TomTom`](https://developer.tomtom.com/routing-api/documentation/routing/common-routing-parameters)
  * [`HERE`](https://developer.here.com/documentation/routing-api/dev_guide/index.html)
  * [`TravelTime`](https://docs.traveltime.com/api/reference/routes)

{% hint style="warning" %}
**warning**

Before running, we recommend checking your provider using the [`GET_LDS_QUOTA_INFO`](#get_lds_quota_info) function. Notice that some of the parameters are provider dependant. Please contact your CARTO representative if you have questions regarding the service provider configured in your organization.
{% endhint %}

**Examples**

{% tabs %}
{% tab title="carto-un" %}

```sql
CALL `carto-un`.carto.CREATE_ROUTES(
    'my_api_base_url', 'my_api_access_token',
    'SELECT id, origin, destination FROM my-project.my-schema.my-table',
    'my-project.my-schema.my-output-table',
    'my_geom_column',
    'car',
    NULL
);
-- The table `my-project.my-schema.my-output-table` will be created
-- with the columns `id`, `origin`, `destination`, `geom` and `carto_routing_metadata`.
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
CALL `carto-un-eu`.carto.CREATE_ROUTES(
    'my_api_base_url', 'my_api_access_token',
    'SELECT id, origin, destination FROM my-project.my-schema.my-table',
    'my-project.my-schema.my-output-table',
    'my_geom_column',
    'car',
    NULL
);
-- The table `my-project.my-schema.my-output-table` will be created
-- with the columns `id`, `origin`, `destination`, `geom` and `carto_routing_metadata`.
```

{% endtab %}

{% tab title="manual" %}

```sql
CALL carto.CREATE_ROUTES(
    'my_api_base_url', 'my_api_access_token',
    'SELECT id, origin, destination FROM my-project.my-schema.my-table',
    'my-project.my-schema.my-output-table',
    'my_geom_column',
    'car',
    NULL
);
-- The table `my-project.my-schema.my-output-table` will be created
-- with the columns `id`, `origin`, `destination`, `geom` and `carto_routing_metadata`.
```

{% endtab %}
{% endtabs %}

Example with TomTom specific parameters:

{% tabs %}
{% tab title="carto-un" %}

```sql
CALL `carto-un`.carto.CREATE_ROUTES(
    'my_api_base_url', 'my_api_access_token',
    'SELECT id, origin, destination FROM my-project.my-schema.my-table',
    'my-project.my-schema.my-output-table',
    'my_geom_column',
    'car',
    '{"departAt":"now"}'
);
-- The table `my-project.my-schema.my-output-table` will be created
-- with the columns `id`, `origin`, `destination`, `geom` and `carto_routing_metadata`.
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
CALL `carto-un-eu`.carto.CREATE_ROUTES(
    'my_api_base_url', 'my_api_access_token',
    'SELECT id, origin, destination FROM my-project.my-schema.my-table',
    'my-project.my-schema.my-output-table',
    'my_geom_column',
    'car',
    '{"departAt":"now"}'
);
-- The table `my-project.my-schema.my-output-table` will be created
-- with the columns `id`, `origin`, `destination`, `geom` and `carto_routing_metadata`.
```

{% endtab %}

{% tab title="manual" %}

```sql
CALL carto.CREATE_ROUTES(
    'my_api_base_url', 'my_api_access_token',
    'SELECT id, origin, destination FROM my-project.my-schema.my-table',
    'my-project.my-schema.my-output-table',
    'my_geom_column',
    'car',
    '{"departAt":"now"}'
);
-- The table `my-project.my-schema.my-output-table` will be created
-- with the columns `id`, `origin`, `destination`, `geom` and `carto_routing_metadata`.
```

{% endtab %}
{% endtabs %}

## GEOCODE <a href="#geocode" id="geocode"></a>

```sql
GEOCODE(api_base_url, api_access_token, address , country, options)
```

{% hint style="warning" %}
**warning**

This function consumes LDS quota. Each call consumes one unit of quota. Before running, check the size of the data to be geocoded and make sure you store the result in a table to avoid misuse of the quota. To check the information about available and consumed quota use the function [`GET_LDS_QUOTA_INFO`](#get_lds_quota_info).

**We recommend using this function only with an input of up to 10 records. In order to geocode larger sets of addresses, we strongly recommend using the** [**`GEOCODE_TABLE`**](#geocode_table) **procedure. Likewise, in order to materialize the results in a table.**
{% endhint %}

**Description**

Geocodes an address into a point with its geographic coordinates (latitude and longitude).

**Input parameters**

* `api_base_url`: `STRING`|`NULL` url of the API where the customer account is stored. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `api_access_token`: `STRING`|`NULL` an [API Access Token](https://docs.carto.com/carto-user-manual/developers/api-access-tokens) that is allowed to use the LDS API. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `address`: `STRING` input address to geocode.
* `country`: `STRING`|`NULL` name of the country in [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2). Defaults to `''`. Set to `NULL` to use the default value.
* `options`: `STRING`|`NULL` containing a valid JSON with the different options.

  | Provider | Option     | Description                                                                 |
  | -------- | ---------- | --------------------------------------------------------------------------- |
  | `All`    | `language` | A `STRING` that specifies the language of the geocoding in RFC 4647 format. |

**Return type**

`GEOGRAPHY`

**Constraints**

This function performs requests to the CARTO Location Data Services API. BigQuery makes parallel requests depending on the number of records you are processing, potentially hitting the limit of the number of requests per seconds allowed for your account. The payload size of these requests depends on the number of records and could cause a timeout in the external function, with the error message `External function timeout`. The limit is around 500 records but could vary with the provider. To avoid this error, please try geocoding smaller volumes of data or using the procedure [`GEOCODE_TABLE`](#geocode_table) instead. This procedure manages concurrency and payload size to avoid exceeding this limit.

**Examples**

{% tabs %}
{% tab title="carto-un" %}

```sql
SELECT `carto-un`.carto.GEOCODE(NULL, NULL, 'Madrid', NULL, NULL);
-- POINT(109.590465335923 34.1733770650093)
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
SELECT `carto-un-eu`.carto.GEOCODE(NULL, NULL, 'Madrid', NULL, NULL);
-- POINT(109.590465335923 34.1733770650093)
```

{% endtab %}

{% tab title="manual" %}

```sql
SELECT carto.GEOCODE(NULL, NULL, 'Madrid', NULL, NULL);
-- POINT(109.590465335923 34.1733770650093)
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="carto-un" %}

```sql
SELECT `carto-un`.carto.GEOCODE(NULL, NULL, 'Madrid', 'es', NULL);
-- POINT(51.405967078794 20.3365500266832)
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
SELECT `carto-un-eu`.carto.GEOCODE(NULL, NULL, 'Madrid', 'es', NULL);
-- POINT(51.405967078794 20.3365500266832)
```

{% endtab %}

{% tab title="manual" %}

```sql
SELECT carto.GEOCODE(NULL, NULL, 'Madrid', 'es', NULL);
-- POINT(51.405967078794 20.3365500266832)
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="carto-un" %}

```sql
SELECT `carto-un`.carto.GEOCODE(NULL, NULL, 'Madrid', 'es', '{"language":"es-ES"}');
-- POINT(51.405967078794 20.3365500266832)
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
SELECT `carto-un-eu`.carto.GEOCODE(NULL, NULL, 'Madrid', 'es', '{"language":"es-ES"}');
-- POINT(51.405967078794 20.3365500266832)
```

{% endtab %}

{% tab title="manual" %}

```sql
SELECT carto.GEOCODE(NULL, NULL, 'Madrid', 'es', '{"language":"es-ES"}');
-- POINT(51.405967078794 20.3365500266832)
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="carto-un" %}

```sql
SELECT `carto-un`.carto.GEOCODE('my_api_base_url', 'my_api_access_token', 'Madrid', 'es', '{"language":"es-ES"}');
-- POINT(51.405967078794 20.3365500266832)
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
SELECT `carto-un-eu`.carto.GEOCODE('my_api_base_url', 'my_api_access_token', 'Madrid', 'es', '{"language":"es-ES"}');
-- POINT(51.405967078794 20.3365500266832)
```

{% endtab %}

{% tab title="manual" %}

```sql
SELECT carto.GEOCODE('my_api_base_url', 'my_api_access_token', 'Madrid', 'es', '{"language":"es-ES"}');
-- POINT(51.405967078794 20.3365500266832)
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="carto-un" %}

```sql
CREATE TABLE `<my-project>.<my-dataset>.<my-geocoded-table>` AS
SELECT address, `carto-un`.carto.GEOCODE(NULL, NULL, address, NULL, NULL) AS geom FROM `<my-project>.<my-dataset>.<my-table>`
-- The table `<my-project>.<my-dataset>.<my-geocoded-table>` will be created
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
CREATE TABLE `<my-project>.<my-dataset>.<my-geocoded-table>` AS
SELECT address, `carto-un-eu`.carto.GEOCODE(NULL, NULL, address, NULL, NULL) AS geom FROM `<my-project>.<my-dataset>.<my-table>`
-- The table `<my-project>.<my-dataset>.<my-geocoded-table>` will be created
```

{% endtab %}

{% tab title="manual" %}

```sql
CREATE TABLE `<my-project>.<my-dataset>.<my-geocoded-table>` AS
SELECT address, carto.GEOCODE(NULL, NULL, address, NULL, NULL) AS geom FROM `<my-project>.<my-dataset>.<my-table>`
-- The table `<my-project>.<my-dataset>.<my-geocoded-table>` will be created
```

{% endtab %}
{% endtabs %}

## GEOCODE\_REVERSE <a href="#geocode_reverse" id="geocode_reverse"></a>

```sql
GEOCODE_REVERSE(api_base_url, api_access_token, geom, language, options)
```

{% hint style="warning" %}
**warning**

This function consumes LDS quota. Each call consumes one unit of quota. Before running, check the size of the data to be reverse geocoded and make sure you store the result in a table to avoid misuse of the quota. To check the information about available and consumed quota use the function \[`GET_LDS_QUOTA_INFO`]\(lds.md#get\_ lds\_quota\_info)

**We recommend using this function only with an input of up to 10 records. In order to reverse-geocode larger sets of locations, we strongly recommend using the** [**`GEOCODE_REVERSE_TABLE`**](#geocode_reverse_table) **procedure. Likewise, in order to materialize the results in a table.**
{% endhint %}

**Description**

Performs a reverse geocoding of the point received as input.

**Input parameters**

* `api_base_url`: `STRING`|`NULL` url of the API where the customer account is stored. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `api_access_token`: `STRING`|`NULL` an [API Access Token](https://docs.carto.com/carto-user-manual/developers/api-access-tokens) that is allowed to use the LDS API. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `geom`: `GEOGRAPHY` input point for which to obtain the address.
* `language`: `STRING`|`NULL` language in which results should be returned. Defaults to `''`. The effect and interpretation of this parameter depends on the LDS provider assigned to your account.
* `options`: `STRING`|`NULL` containing a valid JSON with the different options. No options are allowed currently, so this value will not be taken into account.

**Return type**

`STRING`

**Constraints**

This function performs requests to the CARTO Location Data Services API. BigQuery makes parallel requests depending on the number of records you are processing, potentially hitting the limit of the number of requests per seconds allowed for your account. The payload size of these requests depends on the number of records and could cause a timeout in the external function, with the error message `External function timeout`. The limit is around 500 records but could vary with the provider. To avoid this error, please try processing smaller volumes of data.

**Examples**

{% tabs %}
{% tab title="carto-un" %}

```sql
SELECT `carto-un`.carto.GEOCODE_REVERSE(NULL, NULL, ST_GEOGPOINT(-74.0060, 40.7128), NULL, NULL);
-- 254 Broadway, New York, NY 10007, USA
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
SELECT `carto-un-eu`.carto.GEOCODE_REVERSE(NULL, NULL, ST_GEOGPOINT(-74.0060, 40.7128), NULL, NULL);
-- 254 Broadway, New York, NY 10007, USA
```

{% endtab %}

{% tab title="manual" %}

```sql
SELECT carto.GEOCODE_REVERSE(NULL, NULL, ST_GEOGPOINT(-74.0060, 40.7128), NULL, NULL);
-- 254 Broadway, New York, NY 10007, USA
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="carto-un" %}

```sql
SELECT `carto-un`.carto.GEOCODE_REVERSE(NULL, NULL, ST_GEOGPOINT(-74.0060, 40.7128), 'en-US', NULL);
-- 254 Broadway, New York, NY 10007, USA
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
SELECT `carto-un-eu`.carto.GEOCODE_REVERSE(NULL, NULL, ST_GEOGPOINT(-74.0060, 40.7128), 'en-US', NULL);
-- 254 Broadway, New York, NY 10007, USA
```

{% endtab %}

{% tab title="manual" %}

```sql
SELECT carto.GEOCODE_REVERSE(NULL, NULL, ST_GEOGPOINT(-74.0060, 40.7128), 'en-US', NULL);
-- 254 Broadway, New York, NY 10007, USA
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="carto-un" %}

```sql
SELECT `carto-un`.carto.GEOCODE_REVERSE('my_api_base_url', 'my_api_access_token', ST_GEOGPOINT(-74.0060, 40.7128), 'en-US', '{}');
-- 254 Broadway, New York, NY 10007, USA
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
SELECT `carto-un-eu`.carto.GEOCODE_REVERSE('my_api_base_url', 'my_api_access_token', ST_GEOGPOINT(-74.0060, 40.7128), 'en-US', '{}');
-- 254 Broadway, New York, NY 10007, USA
```

{% endtab %}

{% tab title="manual" %}

```sql
SELECT carto.GEOCODE_REVERSE('my_api_base_url', 'my_api_access_token', ST_GEOGPOINT(-74.0060, 40.7128), 'en-US', '{}');
-- 254 Broadway, New York, NY 10007, USA
```

{% endtab %}
{% endtabs %}

## ISOLINE <a href="#isoline" id="isoline"></a>

```sql
ISOLINE(api_base_url, api_access_token, origin, mode, range_value, range_type, options)
```

{% hint style="warning" %}
**warning**

This function consumes LDS quota. Each call consumes one unit quota. Before running, check the size of the data and make sure you store the result in a table to avoid misuse of the quota. To check the information about available and consumed quota use the function [`GET_LDS_QUOTA_INFO`](#get_lds_quota_info).

**We recommend using this function only with an input of up to 10 records. In order to calculate isolines for larger sets of locations, we strongly recommend using the** [**`CREATE_ISOLINES`**](#create_isolines) **procedure. Likewise, in order to materialize the results in a table.**
{% endhint %}

**Description**

Calculates the isoline polygon from a given point.

**Input parameters**

* `api_base_url`: `STRING`|`NULL` url of the API where the customer account is stored. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `api_access_token`: `STRING`|`NULL` an [API Access Token](https://docs.carto.com/carto-user-manual/developers/api-access-tokens) that is allowed to use the LDS API. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `origin`: `GEOGRAPHY` origin point of the isoline.
* `mode`: `STRING` type of transport. The supported modes depend on the provider:
  * `HERE`: 'walk', 'car', 'truck', 'taxi', 'bus', 'private\_bus'.
  * `TomTom`: 'walk', 'car', 'bike', 'motorbike', 'truck', 'taxi', 'bus', 'van'.
  * `TravelTime`: 'walk', 'car', 'bike', 'public\_transport', 'coach', 'bus', 'train', 'ferry'.
  * `Mapbox`: 'walk', 'car', 'bike'.
* `range_value`: `INT` range of the isoline in seconds (for `range_type` 'time') or meters (for `range_type` 'distance').
* `range_type`: `STRING` type of range. Supported: 'time' (for isochrones), 'distance' (for isodistances).
* `options` `STRING`|`NULL` containing a valid JSON with the different options. Valid options are described in the table below. If no options are indicated then 'default' values would be applied.

  | Provider     | Option            | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
  | ------------ | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
  | `HERE`       | `arrival_time`    | A `STRING` that specifies the time of arrival. If the value is set, a reverse isoline is calculated. If set to `"any"`, then time-dependent effects will not be taken into account. It cannot be used in combination with `departure_time`. Supported: `"any"`, `"now"` and date-time as `"<YYYY-MM-DD>T<hh:mm:ss>"`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
  | `HERE`       | `departure_time`  | Default: `"now"`. A `STRING` that specifies the time of departure. If set to `"any"`, then time-dependent effects will not be taken into account. It cannot be used in combination with `arrival_time`. Supported: `"any"`, `"now"` and date-time as `"<YYYY-MM-DD>T<hh:mm:ss>"`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
  | `HERE`       | `optimize_for`    | Default: `"balanced"`. A `STRING` that specifies how isoline calculation is optimized. Supported: `"quality"` (calculation of isoline focuses on quality, that is, the graph used for isoline calculation has higher granularity generating an isoline that is more precise), `"performance"` (calculation of isoline is performance-centric, quality of isoline is reduced to provide better performance) and `"balanced"` (calculation of isoline takes a balanced approach averaging between quality and performance).                                                                                                                                                                                                                                                                                                                       |
  | `HERE`       | `routing_mode`    | Default: `"fast"`. A `STRING` that specifies which optimization is applied during isoline calculation. Supported: `"fast"` (route calculation from start to destination optimized by travel time. In many cases, the route returned by the fast mode may not be the route with the fastest possible travel time. For example, the routing service may favor a route that remains on a highway, even if a faster travel time can be achieved by taking a detour or shortcut through an inconvenient side road) and `"short"` (route calculation from start to destination disregarding any speed information. In this mode, the distance of the route is minimized, while keeping the route sensible. This includes, for example, penalizing turns. Because of that, the resulting route will not necessarily be the one with minimal distance). |
  | `TomTom`     | `departure_time`  | Default: `"now"`. A `STRING` that specifies the time of departure. If set to `"any"`, then time-dependent effects will not be taken into account. Supported: `"any"`, `"now"` and date-time as `"<YYYY-MM-DD>T<hh:mm:ss>"`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
  | `TomTom`     | `traffic`         | Default: `true`. A `BOOL` that specifies if all available traffic information will be taken into consideration. Supported: `true` and `false`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
  | `TravelTime` | `level_of_detail` | A JSON string. In the most typical case, you will want to use a string in the form `{ scale_type: 'simple_numeric', level: -N }`, with `N` being the detail level (-8 by default). Higher Ns (more negative levels) will simplify the polygons more but will reduce performance. There are other ways of setting the level of detail. Check the \[<https://docs.traveltime.com/api/reference/isochrones#arrival\\_searches-level\\_of\\_detail]\\(TravelTime> docs) for more info.                                                                                                                                                                                                                                                                                                                                                              |
  | `TravelTime` | `departure_time`  | Default: `"now"`. A `STRING` that specifies the time of departure. Supported: `"now"` and date-time as `"<YYYY-MM-DD>T<hh:mm:ss>Z"`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |

{% hint style="warning" %}
**warning**

Before running, we recommend checking your provider using the [`GET_LDS_QUOTA_INFO`](#get_lds_quota_info) function. Notice that some of the parameters are provider dependant. Please contact your CARTO representative if you have questions regarding the service provider configured in your organization.
{% endhint %}

**Return type**

`GEOGRAPHY`

**Constraints**

This function performs requests to the CARTO Location Data Services API. Bigquery makes parallel requests depending on the number of records you are processing, potentially hitting the limit of the number of requests per seconds allowed for your account. The payload size of these requests depends on the number of records and could cause a timeout in the external function, with the error message `External function timeout`. The limit is around 500 records but could vary with the provider. To avoid this error, please try processing smaller volumes of data.

**Examples**

{% tabs %}
{% tab title="carto-un" %}

```sql
SELECT `carto-un`.carto.ISOLINE(NULL, NULL, ST_GEOGPOINT(-3, 40), 'car', 300, 'time');
-- POLYGON ((-3.0081322 40.00005, -3.0081322 40.0006, -3.007061 40.001423, -3.0042043 40.001423, ...
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
SELECT `carto-un-eu`.carto.ISOLINE(NULL, NULL, ST_GEOGPOINT(-3, 40), 'car', 300, 'time');
-- POLYGON ((-3.0081322 40.00005, -3.0081322 40.0006, -3.007061 40.001423, -3.0042043 40.001423, ...
```

{% endtab %}

{% tab title="manual" %}

```sql
SELECT carto.ISOLINE(NULL, NULL, ST_GEOGPOINT(-3, 40), 'car', 300, 'time');
-- POLYGON ((-3.0081322 40.00005, -3.0081322 40.0006, -3.007061 40.001423, -3.0042043 40.001423, ...
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="carto-un" %}

```sql
SELECT `carto-un`.carto.ISOLINE(NULL, NULL, ST_GEOGPOINT(-3, 40), 'car', 1000, 'distance');
-- POLYGON ((-3.0960834 39.984512, -3.094504 39.984512, -3.092925 39.983295, -3.0913458 39.983295, ...
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
SELECT `carto-un-eu`.carto.ISOLINE(NULL, NULL, ST_GEOGPOINT(-3, 40), 'car', 1000, 'distance');
-- POLYGON ((-3.0960834 39.984512, -3.094504 39.984512, -3.092925 39.983295, -3.0913458 39.983295, ...
```

{% endtab %}

{% tab title="manual" %}

```sql
SELECT carto.ISOLINE(NULL, NULL, ST_GEOGPOINT(-3, 40), 'car', 1000, 'distance');
-- POLYGON ((-3.0960834 39.984512, -3.094504 39.984512, -3.092925 39.983295, -3.0913458 39.983295, ...
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="carto-un" %}

```sql
SELECT `carto-un`.carto.ISOLINE('my_api_base_url', 'my_api_access_token', ST_GEOGPOINT(-3, 40), 'car', 300, 'time', '{"departure_time":"any"}');
-- POLYGON ((-3.0081322 40.00005, -3.0081322 40.0006, -3.007061 40.001423, -3.0042043 40.001423, ...
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
SELECT `carto-un-eu`.carto.ISOLINE('my_api_base_url', 'my_api_access_token', ST_GEOGPOINT(-3, 40), 'car', 300, 'time', '{"departure_time":"any"}');
-- POLYGON ((-3.0081322 40.00005, -3.0081322 40.0006, -3.007061 40.001423, -3.0042043 40.001423, ...
```

{% endtab %}

{% tab title="manual" %}

```sql
SELECT carto.ISOLINE('my_api_base_url', 'my_api_access_token', ST_GEOGPOINT(-3, 40), 'car', 300, 'time', '{"departure_time":"any"}');
-- POLYGON ((-3.0081322 40.00005, -3.0081322 40.0006, -3.007061 40.001423, -3.0042043 40.001423, ...
```

{% endtab %}
{% endtabs %}

## GET\_LDS\_QUOTA\_INFO <a href="#get_lds_quota_info" id="get_lds_quota_info"></a>

```sql
GET_LDS_QUOTA_INFO(api_base_url, api_access_token)
```

**Description**

Returns statistics about the LDS quota. LDS quota is an annual quota that defines how much geocoding and isolines you can compute. Each geocoded row or computed isolines counts as one LDS quota unit. The single element in the result of GET\_LDS\_QUOTA\_INFO will show your LDS quota for the current annual period (annual\_quota), how much you’ve spent (used\_quota), and which LDS providers are in use.

**Input parameters**

* `api_base_url`: `STRING`|`NULL` url of the API where the customer account is stored. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `api_access_token`: `STRING`|`NULL` an [API Access Token](https://docs.carto.com/carto-user-manual/developers/api-access-tokens) that is allowed to use the LDS API. If default credentials have been configured with `SETUP` NULL can be passed to use them.

**Return type**

`STRING`

**Example**

{% tabs %}
{% tab title="carto-un" %}

```sql
SELECT `carto-un`.carto.GET_LDS_QUOTA_INFO('my_api_base_url', 'my_api_access_token');
-- [
--   {
--     "used_quota": 10,
--     "annual_quota": 100000,
--     "providers": {
--         "geocoding": "tomtom",
--         "isolines": "here",
--         "routing":"tomtom"
--     }
--   }
-- ]
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
SELECT `carto-un-eu`.carto.GET_LDS_QUOTA_INFO('my_api_base_url', 'my_api_access_token');
-- [
--   {
--     "used_quota": 10,
--     "annual_quota": 100000,
--     "providers": {
--         "geocoding": "tomtom",
--         "isolines": "here",
--         "routing":"tomtom"
--     }
--   }
-- ]
```

{% endtab %}

{% tab title="manual" %}

```sql
SELECT carto.GET_LDS_QUOTA_INFO('my_api_base_url', 'my_api_access_token');
-- [
--   {
--     "used_quota": 10,
--     "annual_quota": 100000,
--     "providers": {
--         "geocoding": "tomtom",
--         "isolines": "here",
--         "routing":"tomtom"
--     }
--   }
-- ]
```

{% endtab %}
{% endtabs %}

## CREATE\_H3\_ISOLINES <a href="#create_h3_isolines" id="create_h3_isolines"></a>

```sql
CREATE_H3_ISOLINES(api_base_url, api_access_token, input, output_table, geom_column, mode, range_value, range_type, resolution, options)
```

{% hint style="warning" %}
**warning**

This function consumes LDS quota. Each call consumes as many units of quota as the number of rows your input table or query has. Before running, we recommend checking the size of data for which H3 isolines will be created and your available quota using the [`GET_LDS_QUOTA_INFO`](#get_lds_quota_info) function.
{% endhint %}

{% hint style="warning" %}
**warning**

This procedure requires the TravelTime provider. If your organization uses a different isolines provider, the procedure will raise an error.
{% endhint %}

**Description**

Calculates H3 isolines (isochrones represented as H3 cells) from given origins (points) in a table or query. Unlike the standard `CREATE_ISOLINES` procedure which returns polygon geometries, this procedure returns an array of H3 cells with travel time information for each cell.

It creates a new table with the columns of the input table or query except the `geom_column`, with each H3 cell unnested into a separate row containing `h3`, `h3_min`, `h3_max`, and `h3_mean` as scalar values. It calculates H3 isolines sequentially in chunks of N rows, N being the optimal batch size for this datawarehouse.

The output table will contain a column named `carto_isoline_metadata` with error information for each isoline result. Rows with errors will have NULL values in the H3 columns.

**Input parameters**

* `api_base_url`: `STRING`|`NULL` url of the API where the customer account is stored. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `api_access_token`: `STRING`|`NULL` an [API Access Token](https://docs.carto.com/carto-user-manual/developers/api-access-tokens) that is allowed to use the LDS API. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `input`: `STRING` name of the input table or query.
* `output_table`: `STRING` qualified name of the output table, e.g. `<my-project>.<my-dataset>.<my-output-table>`. The process will fail if the table already exists.
* `geom_column`: `STRING` column name for the origin geometry column.
* `mode`: `STRING` type of transport. Supported modes for TravelTime: 'walk', 'car', 'bike', 'public\_transport', 'coach', 'bus', 'train', 'ferry'.
* `range_value`: `INT64` range of the isoline in seconds (for `range_type` 'time') or meters (for `range_type` 'distance'). Valid range for time: 60-36000 (1 minute to 10 hours).
* `range_type`: `STRING` type of range. Currently only 'time' is supported for H3 isolines.
* `resolution`: `INT64` H3 resolution level for the output cells. Valid range: 6-12.
* `options`: `STRING`|`NULL` containing a valid JSON with the different options. Valid options are described in the table below. If no options are indicated then 'default' values would be applied.

  | Provider     | Option            | Description                                                                                                                                                                                                                                                                                                                                                                                                       |
  | ------------ | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
  | `TravelTime` | `level_of_detail` | A JSON string. In the most typical case, you will want to use a string in the form `{ scale_type: 'simple_numeric', level: -N }`, with `N` being the detail level (-8 by default). Higher Ns (more negative levels) will simplify the results more but will reduce performance. Check the [TravelTime docs](https://docs.traveltime.com/api/reference/isochrones#arrival_searches-level_of_detail) for more info. |
  | `TravelTime` | `departure_time`  | Default: `"now"`. A `STRING` that specifies the time of departure. Supported: `"now"` and date-time as `"<YYYY-MM-DD>T<hh:mm:ss>Z"`.                                                                                                                                                                                                                                                                              |

**Output Table Schema**

The output table will contain all columns from the input table except `geom_column`, with each H3 cell as a separate row:

| Column                   | Type     | Description                                              |
| ------------------------ | -------- | -------------------------------------------------------- |
| `h3`                     | `STRING` | H3 cell index.                                           |
| `h3_min`                 | `INT64`  | Minimum travel time in seconds for this H3 cell.         |
| `h3_max`                 | `INT64`  | Maximum travel time in seconds for this H3 cell.         |
| `h3_mean`                | `INT64`  | Average travel time in seconds for this H3 cell.         |
| `carto_isoline_metadata` | `STRING` | Error information in JSON format, or NULL if successful. |

**Examples**

{% tabs %}
{% tab title="carto-un" %}

```sql
CALL `carto-un`.carto.CREATE_H3_ISOLINES(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-schema.my-table',
    'my-project.my-schema.my-output-table',
    'my_geom_column',
    'car', 900, 'time', 6,
    NULL
);
-- The table `my-project.my-schema.my-output-table` will be created
-- with the columns of the input table except `my_geom_column`.
-- H3 cell data will be added in the "h3", "h3_min", "h3_max", and "h3_mean" columns.
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
CALL `carto-un-eu`.carto.CREATE_H3_ISOLINES(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-schema.my-table',
    'my-project.my-schema.my-output-table',
    'my_geom_column',
    'car', 900, 'time', 6,
    NULL
);
-- The table `my-project.my-schema.my-output-table` will be created
-- with the columns of the input table except `my_geom_column`.
-- H3 cell data will be added in the "h3", "h3_min", "h3_max", and "h3_mean" columns.
```

{% endtab %}

{% tab title="manual" %}

```sql
CALL carto.CREATE_H3_ISOLINES(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-schema.my-table',
    'my-project.my-schema.my-output-table',
    'my_geom_column',
    'car', 900, 'time', 6,
    NULL
);
-- The table `my-project.my-schema.my-output-table` will be created
-- with the columns of the input table except `my_geom_column`.
-- H3 cell data will be added in the "h3", "h3_min", "h3_max", and "h3_mean" columns.
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="carto-un" %}

```sql
CALL `carto-un`.carto.CREATE_H3_ISOLINES(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-schema.my-table',
    'my-project.my-schema.my-output-table',
    'my_geom_column',
    'walk', 600, 'time', 7,
    '{"departure_time": "2024-01-15T09:00:00Z"}'
);
-- The table `my-project.my-schema.my-output-table` will be created
-- with H3 cells at resolution 7 for 10-minute walking isochrones.
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
CALL `carto-un-eu`.carto.CREATE_H3_ISOLINES(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-schema.my-table',
    'my-project.my-schema.my-output-table',
    'my_geom_column',
    'walk', 600, 'time', 7,
    '{"departure_time": "2024-01-15T09:00:00Z"}'
);
-- The table `my-project.my-schema.my-output-table` will be created
-- with H3 cells at resolution 7 for 10-minute walking isochrones.
```

{% endtab %}

{% tab title="manual" %}

```sql
CALL carto.CREATE_H3_ISOLINES(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-schema.my-table',
    'my-project.my-schema.my-output-table',
    'my_geom_column',
    'walk', 600, 'time', 7,
    '{"departure_time": "2024-01-15T09:00:00Z"}'
);
-- The table `my-project.my-schema.my-output-table` will be created
-- with H3 cells at resolution 7 for 10-minute walking isochrones.
```

{% endtab %}
{% endtabs %}

## CREATE\_ROUTING\_MATRIX <a href="#create_routing_matrix" id="create_routing_matrix"></a>

```sql
CREATE_ROUTING_MATRIX(api_base_url, api_access_token, origins_table, origins_geom_column, destinations_table, destinations_geom_column, output_table, mode, options)
```

{% hint style="warning" %}
**warning**

This function consumes LDS quota. Each call consumes as many units of quota as the number of rows of the origins table multiplied by the rows of destinations table. Before running, we recommend checking the size of the data used to create the routes and your available quota using the [`GET_LDS_QUOTA_INFO`](#get_lds_quota_info) function.
{% endhint %}

**Description**

Calculates the routes (line strings) between given origins and destinations (points) in two table. It creates a new table with the cross join of the origin and destination geom columns adding the distance or time between the different routes. A `carto_routing_matrix_metadata` column will also be added containing pottential errors thrown by the service provider for particular origin-destination combinations.

Note that routes are calculated using the external LDS provider assigned to your CARTO account. Currently TomTom and TravelTime are supported.

**Input parameters**

* `api_base_url`: `STRING`|`NULL` url of the API where the customer account is stored. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `api_access_token`: `STRING`|`NULL` an [API Access Token](https://docs.carto.com/carto-user-manual/developers/api-access-tokens) that is allowed to use the LDS API. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `origins_table`: `STRING` name of the origins input table.
* `origins_geom_column`: `STRING` column name for the origin geometry column.
* `destinations_table`: `STRING` name of the destinations input table.
* `destinations_geom_column`: `STRING` column name for the destination geometry column.
* `output_table`: `STRING` qualified name of the output table, e.g. `<my-project>.<my-dataset>.<my-output-table>`. The process will fail if the table already exists.
* `geom_column`: `STRING` column name for the generated geography column that will contain the resulting routes.
* `mode`: `STRING` type of transport. The supported modes depend on the provider:
  * `TomTom`: 'car', 'truck', 'pedestrian'.
  * `TravelTime`: 'cycling', 'driving', 'driving+train', 'driving+public\_transport', 'public\_transport', 'walking', 'coach', 'bus', 'train', 'ferry', 'driving+ferry', 'cycling+ferry', 'cycling+public\_transport'.
* `options`: `STRING`|`NULL` containing a valid JSON with optional parameters. This is intended for advanced use: additional parameters can be passed directly to the Routing provider by placing them in this JSON string. To find out what your provider is, check the [`GET_LDS_QUOTA_INFO`](#get_lds_quota_info) function. The following are some of the most common parameters for each provider:

  | Provider     | Option            | Description                                                                                                                                                                                                                                                  |
  | ------------ | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
  | `TomTom`     | `avoid`           | Default: `[]`. An `ARRAY` that specifies something that the route calculation should try to avoid when determining the route. Supported: `["tollRoads"]`, `["unpavedRoads"]`.                                                                                |
  | `TomTom`     | `departAt`        | Default: `"any"`. A `STRING` that specifies the time of departure. Supported: `"now"`, `"any"` and date-time as `"<YYYY-MM-DD>T<hh:mm:ss>"`.                                                                                                                 |
  | `TomTom`     | `routeType`       | Default: `"fastest"`. A `STRING` that specifies the type of optimization used when calculating routes. Supported: `"fastest"`, `"shortest"`.                                                                                                                 |
  | `TomTom`     | `traffic`         | Default: `historical`. A `STRING` that decides how traffic is considered for computing routes. Supported: `historical` and `live`. `live` may not be used in conjunction with `departAt=any`.                                                                |
  | `TomTom`     | `vehicleMaxSpeed` | Default: `0`. A `NUMBER` that specifies the maximum speed of the vehicle in kilometers/hour. Supported: a value in the range \[0, 250]. A value of `0` means that an appropriate value for the vehicle will be determined and applied during route planning. |
  | `TravelTime` | `departure_time`  | Default: `"now"`. A `STRING` that specifies the time of departure. Supported: `"now"` and date-time as `"<YYYY-MM-DD>T<hh:mm:ss>Z"`.                                                                                                                         |
  | `TravelTime` | `travel_time`     | Default: `14400` (4 hours). A `NUMBER` that specifies the maximum travel time in seconds. Supported: a value in the range \[0, 14400].                                                                                                                       |

  For more advanced usage, check the documentation of your provider's matrix routing API.

  * [`TomTom`](https://developer.tomtom.com/matrix-routing-v2-api/documentation/asynchronous-matrix-submission)
  * [`TravelTime`](https://docs.traveltime.com/api/reference/travel-time-distance-matrix#departure_searches)

  The following are options provided by CARTO in order to adjust the procedure performance:

  | Option                          | Description                                                                                                                                                                                                                                                                                                  |
  | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
  | `carto_origins_batch_size`      | Default: `TomTom: 100`, `TravelTime: 1`. Max: `TomTom: 10000`, `TravelTime: 10`. A `NUMBER` that specifies the number of origins rows to process in each batch. Increasing this value in the case of Traveltime can cause some origins-destinations combinations to be skipped when the origin is not found. |
  | `carto_destinations_batch_size` | Default: `TomTom: 100`, `TravelTime: 2000`. Max: `TomTom: 10000`, `TravelTime: 2000`. A `NUMBER` that specifies the number of destinations rows to process in each batch.                                                                                                                                    |

  In the case of `TomTom` the next requirements must be met:

  * If `departAt=now` or `departAT=dateTime` then:
    * The size of the matrix should not be larger than 2500.
    * The number of origins should not be larger than 1000.
    * The number of destinations should not be larger than 1000.
    * The bounding box should not be larger than 400km X 400km. Example: 2X1000, 5X500, 100X25, etc.
  * If `departAt=any` then:
    * The size of the matrix can be as large as 100M.
    * The number of origins should not be larger than 10000.
    * The number of destinations should not be larger than 10000.

{% hint style="warning" %}
**warning**

We recommend the product of `carto_origins_batch_size x carto_destinations_batch_size` to be lower than 10000 to ensure that the batches sizes is small enough to be processed.
{% endhint %}

{% hint style="warning" %}
**warning**

Before running, we recommend checking your provider using the [`GET_LDS_QUOTA_INFO`](#get_lds_quota_info) function. Notice that some of the parameters are provider dependant. Please contact your CARTO representative if you have questions regarding the service provider configured in your organization.
{% endhint %}

**Return type**

The results are stored in the table named `<output_table>`, which contains the following columns:

* `origin_geom`: `STRING` the origin geometry.
* `destination_geom`: `STRING` the destination geometry.
* `route_distance`: `INT` the distance of the route in meters.
* `route_duration`: `INT` the duration of the route in seconds.
* `carto_routing_matrix_metadata`: `STRING` possible errors thrown by the service provider for particular origin-destination combinations.

When generating the output table origin-destination combinations, duplicated or null geometries will not be processed.

**Examples**

{% tabs %}
{% tab title="carto-un" %}

```sql
CALL `carto-un`.carto.CREATE_ROUTING_MATRIX(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-dataset.my-origins-table',
    'my_origins_geom_column',
    'my-project.my-dataset.my-destinations-table',
    'my_destinations_geom_column',
    'my-project.my-dataset.my-output-table',
    'car',
    NULL
);
-- The table `my-project.my-dataset.my-output-table` will be created
-- with the columns from both input tables, `route_distance`, `route_duration` and `carto_routing_matrix_metadata`.
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
CALL `carto-un-eu`.carto.CREATE_ROUTING_MATRIX(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-dataset.my-origins-table',
    'my_origins_geom_column',
    'my-project.my-dataset.my-destinations-table',
    'my_destinations_geom_column',
    'my-project.my-dataset.my-output-table',
    'car',
    NULL
);
-- The table `my-project.my-dataset.my-output-table` will be created
-- with the columns from both input tables, `route_distance`, `route_duration` and `carto_routing_matrix_metadata`.
```

{% endtab %}

{% tab title="manual" %}

```sql
CALL carto.CREATE_ROUTING_MATRIX(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-dataset.my-origins-table',
    'my_origins_geom_column',
    'my-project.my-dataset.my-destinations-table',
    'my_destinations_geom_column',
    'my-project.my-dataset.my-output-table',
    'car',
    NULL
);
-- The table `my-project.my-dataset.my-output-table` will be created
-- with the columns from both input tables, `route_distance`, `route_duration` and `carto_routing_matrix_metadata`.
```

{% endtab %}
{% endtabs %}

Example with TomTom specific parameters:

{% tabs %}
{% tab title="carto-un" %}

```sql
CALL `carto-un`.carto.CREATE_ROUTING_MATRIX(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-dataset.my-origins-table',
    'my_origins_geom_column',
    'my-project.my-dataset.my-destinations-table',
    'my_destinations_geom_column',
    'my-project.my-dataset.my-output-table',
    'car',
    '{"departAt":"now"}'
);
-- The table `my-project.my-dataset.my-output-table` will be created
-- with the columns from both input tables, `route_distance`, `route_duration` and `carto_routing_matrix_metadata`.
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
CALL `carto-un-eu`.carto.CREATE_ROUTING_MATRIX(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-dataset.my-origins-table',
    'my_origins_geom_column',
    'my-project.my-dataset.my-destinations-table',
    'my_destinations_geom_column',
    'my-project.my-dataset.my-output-table',
    'car',
    '{"departAt":"now"}'
);
-- The table `my-project.my-dataset.my-output-table` will be created
-- with the columns from both input tables, `route_distance`, `route_duration` and `carto_routing_matrix_metadata`.
```

{% endtab %}

{% tab title="manual" %}

```sql
CALL carto.CREATE_ROUTING_MATRIX(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-dataset.my-origins-table',
    'my_origins_geom_column',
    'my-project.my-dataset.my-destinations-table',
    'my_destinations_geom_column',
    'my-project.my-dataset.my-output-table',
    'car',
    '{"departAt":"now"}'
);
-- The table `my-project.my-dataset.my-output-table` will be created
-- with the columns from both input tables, `route_distance`, `route_duration` and `carto_routing_matrix_metadata`.
```

{% endtab %}
{% endtabs %}

Example with batch sizes:

{% tabs %}
{% tab title="carto-un" %}

```sql
CALL `carto-un`.carto.CREATE_ROUTING_MATRIX(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-dataset.my-origins-table',
    'my_origins_geom_column',
    'my-project.my-dataset.my-destinations-table',
    'my_destinations_geom_column',
    'my-project.my-dataset.my-output-table',
    'car',
    '{"carto_origins_batch_size": 10, "carto_destinations_batch_size": 1000}'
);
-- The table `my-project.my-dataset.my-output-table` will be created
-- with the columns from both input tables, `route_distance`, `route_duration` and `carto_routing_matrix_metadata`.
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
CALL `carto-un-eu`.carto.CREATE_ROUTING_MATRIX(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-dataset.my-origins-table',
    'my_origins_geom_column',
    'my-project.my-dataset.my-destinations-table',
    'my_destinations_geom_column',
    'my-project.my-dataset.my-output-table',
    'car',
    '{"carto_origins_batch_size": 10, "carto_destinations_batch_size": 1000}'
);
-- The table `my-project.my-dataset.my-output-table` will be created
-- with the columns from both input tables, `route_distance`, `route_duration` and `carto_routing_matrix_metadata`.
```

{% endtab %}

{% tab title="manual" %}

```sql
CALL carto.CREATE_ROUTING_MATRIX(
    'my_api_base_url', 'my_api_access_token',
    'my-project.my-dataset.my-origins-table',
    'my_origins_geom_column',
    'my-project.my-dataset.my-destinations-table',
    'my_destinations_geom_column',
    'my-project.my-dataset.my-output-table',
    'car',
    '{"carto_origins_batch_size": 10, "carto_destinations_batch_size": 1000}'
);
-- The table `my-project.my-dataset.my-output-table` will be created
-- with the columns from both input tables, `route_distance`, `route_duration` and `carto_routing_matrix_metadata`.
```

{% endtab %}
{% endtabs %}

## H3\_ISOLINE <a href="#h3_isoline" id="h3_isoline"></a>

```sql
H3_ISOLINE(api_base_url, api_access_token, origin, mode, range_value, range_type, resolution, options)
```

{% hint style="warning" %}
**warning**

This function consumes LDS quota. Each call consumes one unit quota. Before running, check the size of the data and make sure you store the result in a table to avoid misuse of the quota. To check the information about available and consumed quota use the function [`GET_LDS_QUOTA_INFO`](#get_lds_quota_info).

**We recommend using this function only with an input of up to 10 records. In order to calculate H3 isolines for larger sets of locations, we strongly recommend using the** [**`CREATE_H3_ISOLINES`**](#create_h3_isolines) **procedure. Likewise, in order to materialize the results in a table.**
{% endhint %}

{% hint style="warning" %}
**warning**

This function requires the TravelTime provider. If your organization uses a different isolines provider, the function will raise an error.
{% endhint %}

**Description**

Calculates H3 isolines (isochrones represented as H3 cells) from a given point. Unlike the standard `ISOLINE` function which returns a polygon geometry, this function returns an array of H3 cells with travel time information for each cell.

**Input parameters**

* `api_base_url`: `STRING`|`NULL` url of the API where the customer account is stored. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `api_access_token`: `STRING`|`NULL` an [API Access Token](https://docs.carto.com/carto-user-manual/developers/api-access-tokens) that is allowed to use the LDS API. If default credentials have been configured with `SETUP` NULL can be passed to use them.
* `origin`: `GEOGRAPHY` origin point of the isoline.
* `mode`: `STRING` type of transport. Supported modes for TravelTime: 'walk', 'car', 'bike', 'public\_transport', 'coach', 'bus', 'train', 'ferry'.
* `range_value`: `INT64` range of the isoline in seconds (for `range_type` 'time') or meters (for `range_type` 'distance'). Valid range for time: 60-36000 (1 minute to 10 hours).
* `range_type`: `STRING` type of range. Currently only 'time' is supported for H3 isolines.
* `resolution`: `INT64` H3 resolution level for the output cells. Valid range: 6-12.
* `options`: `STRING`|`NULL` containing a valid JSON with the different options. Valid options are described in the table below. If no options are indicated then 'default' values would be applied.

  | Provider     | Option            | Description                                                                                                                                                                                                                                                                                                                                                                                                       |
  | ------------ | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
  | `TravelTime` | `level_of_detail` | A JSON string. In the most typical case, you will want to use a string in the form `{ scale_type: 'simple_numeric', level: -N }`, with `N` being the detail level (-8 by default). Higher Ns (more negative levels) will simplify the results more but will reduce performance. Check the [TravelTime docs](https://docs.traveltime.com/api/reference/isochrones#arrival_searches-level_of_detail) for more info. |
  | `TravelTime` | `departure_time`  | Default: `"now"`. A `STRING` that specifies the time of departure. Supported: `"now"` and date-time as `"<YYYY-MM-DD>T<hh:mm:ss>Z"`.                                                                                                                                                                                                                                                                              |

**Return type**

`ARRAY<STRUCT<h3 STRING, h3_min INT64, h3_max INT64, h3_mean INT64>>`

Each element in the array represents an H3 cell within the isochrone:

| Field     | Type     | Description                                           |
| --------- | -------- | ----------------------------------------------------- |
| `h3`      | `STRING` | H3 cell index.                                        |
| `h3_min`  | `INT64`  | Minimum travel time in seconds to reach this H3 cell. |
| `h3_max`  | `INT64`  | Maximum travel time in seconds to reach this H3 cell. |
| `h3_mean` | `INT64`  | Average travel time in seconds to reach this H3 cell. |

**Constraints**

This function performs requests to the CARTO Location Data Services API. BigQuery makes parallel requests depending on the number of records you are processing, potentially hitting the limit of the number of requests per seconds allowed for your account. The payload size of these requests depends on the number of records and could cause a timeout in the external function, with the error message `External function timeout`. To avoid this error, please try processing smaller volumes of data or use the `CREATE_H3_ISOLINES` procedure instead.

**Examples**

{% tabs %}
{% tab title="carto-un" %}

```sql
SELECT `carto-un`.carto.H3_ISOLINE(NULL, NULL, ST_GEOGPOINT(-3.7038, 40.4168), 'car', 900, 'time', 6, NULL);
-- Returns an array of H3 cells at resolution 6 for a 15-minute drive from Madrid
-- [{"h3": "86390ca27ffffff", "h3_min": 0, "h3_max": 120, "h3_mean": 60}, ...]
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
SELECT `carto-un-eu`.carto.H3_ISOLINE(NULL, NULL, ST_GEOGPOINT(-3.7038, 40.4168), 'car', 900, 'time', 6, NULL);
-- Returns an array of H3 cells at resolution 6 for a 15-minute drive from Madrid
-- [{"h3": "86390ca27ffffff", "h3_min": 0, "h3_max": 120, "h3_mean": 60}, ...]
```

{% endtab %}

{% tab title="manual" %}

```sql
SELECT carto.H3_ISOLINE(NULL, NULL, ST_GEOGPOINT(-3.7038, 40.4168), 'car', 900, 'time', 6, NULL);
-- Returns an array of H3 cells at resolution 6 for a 15-minute drive from Madrid
-- [{"h3": "86390ca27ffffff", "h3_min": 0, "h3_max": 120, "h3_mean": 60}, ...]
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="carto-un" %}

```sql
SELECT `carto-un`.carto.H3_ISOLINE(NULL, NULL, ST_GEOGPOINT(-3.7038, 40.4168), 'walk', 600, 'time', 7, '{"departure_time": "2024-01-15T09:00:00Z"}');
-- Returns an array of H3 cells at resolution 7 for a 10-minute walk
-- with specific departure time
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
SELECT `carto-un-eu`.carto.H3_ISOLINE(NULL, NULL, ST_GEOGPOINT(-3.7038, 40.4168), 'walk', 600, 'time', 7, '{"departure_time": "2024-01-15T09:00:00Z"}');
-- Returns an array of H3 cells at resolution 7 for a 10-minute walk
-- with specific departure time
```

{% endtab %}

{% tab title="manual" %}

```sql
SELECT carto.H3_ISOLINE(NULL, NULL, ST_GEOGPOINT(-3.7038, 40.4168), 'walk', 600, 'time', 7, '{"departure_time": "2024-01-15T09:00:00Z"}');
-- Returns an array of H3 cells at resolution 7 for a 10-minute walk
-- with specific departure time
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="carto-un" %}

```sql
SELECT
    h3_cell.h3,
    h3_cell.h3_min,
    h3_cell.h3_max,
    h3_cell.h3_mean
FROM UNNEST(`carto-un`.carto.H3_ISOLINE(NULL, NULL, ST_GEOGPOINT(-3.7038, 40.4168), 'car', 900, 'time', 6, NULL)) AS h3_cell;
-- Unnests the array to get individual H3 cells as rows
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
SELECT
    h3_cell.h3,
    h3_cell.h3_min,
    h3_cell.h3_max,
    h3_cell.h3_mean
FROM UNNEST(`carto-un-eu`.carto.H3_ISOLINE(NULL, NULL, ST_GEOGPOINT(-3.7038, 40.4168), 'car', 900, 'time', 6, NULL)) AS h3_cell;
-- Unnests the array to get individual H3 cells as rows
```

{% endtab %}

{% tab title="manual" %}

```sql
SELECT
    h3_cell.h3,
    h3_cell.h3_min,
    h3_cell.h3_max,
    h3_cell.h3_mean
FROM UNNEST(carto.H3_ISOLINE(NULL, NULL, ST_GEOGPOINT(-3.7038, 40.4168), 'car', 900, 'time', 6, NULL)) AS h3_cell;
-- Unnests the array to get individual H3 cells as rows
```

{% endtab %}
{% endtabs %}

<img src="https://3029946802-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FybPdpmLltPkzGFvz7m8A%2Fuploads%2Fgit-blob-1c82685e4e434438152a8e3d867df996413489fe%2Feu-flag-website.png?alt=media&#x26;token=4343f6e5-973a-4e9a-8e14-50366a086f72" alt="EU flag" data-size="line">This project has received funding from the [European Union’s Horizon 2020](https://ec.europa.eu/programmes/horizon2020/en) research and innovation programme under grant agreement No 960401.
