lds

ADVANCED BETA

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

GEOCODE_TABLE

GEOCODE_TABLE(input_table, address_column [, geom_column] [, country] [, options])

Description

Geocodes an input table by adding a column geom with the geographic coordinates (latitude and longitude) corresponding to 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 500.

  • input_table: VARCHAR 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: VARCHAR name of the column from the input table that contains the addresses to be geocoded.

  • geom_column (optional): VARCHAR column name for the geometry column. Defaults to 'geom'.

  • country (optional): VARCHAR name of the country in ISO 3166-1 alpha-2. Defaults to ''.

  • options (optional): VARCHAR|OBJECT 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

    All

    language

    A VARCHAR that specifies the language of the geocoding in RFC 4647 format.

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

Examples

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

Troubleshooting

For the GEOCODE_TABLE procedure to work, the input table should be owned by the same role as the procedure. The procedure may not have access to update the table, for example, if the table was created with the ACCOUNTADMIN role, but the procedure is owned by the SYSADMIN role. In such scenario, the following error will be raised:

SQL access control error: Insufficient privileges to operate on table 'my-table'

You can check the OWNERSHIP of the table with the following query:

SHOW GRANTS ON TABLE "my-schema"."my-table";

To change the OWNERSHIP of to table to the SYSADMIN role, execute:

GRANT OWNERSHIP ON TABLE "my-schema"."my-table" TO ROLE SYSADMIN COPY CURRENT GRANTS;

After performing this operation, you will be able to run GEOCODE_TABLE without running into privilege issues.

Additional examples

GEOCODE_REVERSE_TABLE

GEOCODE_REVERSE_TABLE(input_table [, geom_column] [, address_column] [, language] [, options])

Description

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

  • input_table: VARCHAR name of the table to be reverse-geocoded. Please make sure you have enough permissions to alter this table, as this procedure will add a columns to it to store the geocoding result.

  • geom_column (optional): GEOGRAPHY column name from the input table that contains the points to be reverse-geocoded. Defaults to 'geom'.

  • address_column (optional): VARCHAR 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 (optional): VARCHAR 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 (optional): VARCHAR|OBJECT 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 an address column with the name specified by the address_column parameter, only those rows with NULL values in it will be reverse-geocoded.

Examples

CALL CARTO.CARTO.GEOCODE_REVERSE_TABLE('my-schema.my-table');
-- The table `my-schema.my-table`, which should have a `geom` GEOGRAPHY column, will be updated
-- adding the column: `address`.
CALL CARTO.CARTO.GEOCODE_REVERSE_TABLE('my-schema.my-table', 'my_geom_column');
-- The table `my-schema.my-table`, with a GEOGRAPHY column named `my_geom_column`, will be updated
-- adding the column: `address`.
CALL CARTO.CARTO.GEOCODE_REVERSE_TABLE('my-schema.my-table', 'my_geom_column', 'my_address_column');
-- The table `my-schema.my-table`, with a GEOGRAPHY column named `my_geom_column`, will be updated
-- adding the column: `my_address_column` if it doesn't previously exist.
CALL CARTO.CARTO.GEOCODE_REVERSE_TABLE('my-schema.my-table', 'my_geom_column', 'my_address_column', 'en-US');
-- The table `my-schema.my-table`, with a GEOGRAPHY column named `my_geom_column`, will be updated
-- adding the column: `my_address_column` if it doesn't previously exist.
-- The addresses will be in the (US) english language.

Troubleshooting

For the GEOCODE_REVERSE_TABLE procedure to work, the input table should be owned by the same role as the procedure. The procedure may not have access to update the table, for example, if the table was created with the ACCOUNTADMIN role, but the procedure is owned by the SYSADMIN role. In such scenario, the following error will be raised:

SQL access control error: Insufficient privileges to operate on table 'my-table'

You can check the OWNERSHIP of the table with the following query:

SHOW GRANTS ON TABLE "my-schema"."my-table";

To change the OWNERSHIP of to table to the SYSADMIN role, execute:

GRANT OWNERSHIP ON TABLE "my-schema"."my-table" TO ROLE SYSADMIN COPY CURRENT GRANTS;

After performing this operation, you will be able to run GEOCODE_REVERSE_TABLE without running into privilege issues.

CREATE_ISOLINES

CREATE_ISOLINES(input, output_table, geom_column, mode, range, range_type [, options])

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 100 rows.

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: VARCHAR name of the input table or query.

  • output_table: VARCHAR name of the output table. It will raise an error if the table already exists.

  • geom_column: VARCHAR column name for the origin geography column.

  • mode: VARCHAR type of transport. The supported modes depend on the provider:

    • Here: 'walk', 'car', 'truck', 'taxi', 'bus' and 'private_bus'.

    • Mapbox: 'walk', 'car' and 'bike'.

    • TomTom: 'walk', 'car', 'bike', 'motorbike', 'truck', 'taxi', 'bus' and 'van'.

  • range: INT range of the isoline in seconds (for range_type 'time') or meters (for range_type 'distance').

  • range_type: VARCHAR type of range. Supported: 'time' (for isochrones), 'distance' (for isodistances).

  • options (optional): VARCHAR|OBJECT 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 VARCHAR that specifies the time of arrival. If the value is set, a reverse isoline is calculated. If "any" is introduced, 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 VARCHAR that specifies the time of departure. If "any" is introduced, 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 VARCHAR 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 VARCHAR 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 VARCHAR that specifies the time of departure. If "any" is introduced, 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 BOOLEAN that specifies if all available traffic information will be taken into consideration. Supported: true and false.

Examples

CALL CARTO.CARTO.CREATE_ISOLINES(
    'my-schema.my-table',
    'my-schema.my-output-table',
    'my_geom_column',
    'car', 60, 'time'
);
-- The table `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.
CALL CARTO.CARTO.CREATE_ISOLINES(
    'my-schema.my-table',
    'my-schema.my-output-table',
    'my_geom_column',
    'car', 60, 'time',
    '{"departure_time":"any"}'
);
-- The table `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.
CALL CARTO.CARTO.CREATE_ISOLINES(
    'my-schema.my-table',
    'my-schema.my-output-table',
    'my_geom_column',
    'car', 60, 'time',
    {'departure_time':'any'}
);
-- The table `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.

GEOCODE

GEOCODE(address [, country] [, options])

Description

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

  • address: VARCHAR input address to geocode.

  • country (optional): VARCHAR name of the country in ISO 3166-1 alpha-2. Defaults to ''.

  • options (optional): VARCHAR|OBJECT 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

    All

    language

    A VARCHAR 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. Snowflake 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. Unexpected server errors will force Snowflake to retry the requests. 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 instead. This procedure manages concurrency and payload size to avoid exceeding this limit.

Examples

SELECT CARTO.CARTO.GEOCODE('Madrid');
-- { "coordinates": [ -3.69196, 40.41956 ], "type": "Point" }
SELECT CARTO.CARTO.GEOCODE('Madrid', 'es');
-- { "coordinates": [ -3.69196, 40.41956 ], "type": "Point" }
SELECT CARTO.CARTO.GEOCODE('Madrid', 'es', '{"language":"es-ES"}');
-- { "coordinates": [ -3.69196, 40.41956 ], "type": "Point" }
SELECT CARTO.CARTO.GEOCODE('Madrid', 'es', {'language':'en-US'});
-- { "coordinates": [ -3.69196, 40.41956 ], "type": "Point" }
CREATE TABLE my_geocoded_table AS
SELECT ADDRESS, CARTO.CARTO.GEOCODE(ADDRESS) AS GEOM FROM my_table
-- Table my_geocoded_table successfully created.

GEOCODE_REVERSE

GEOCODE_REVERSE(geom [, language] [, options])

Description

Performs a reverse geocoding of the point received as input.

  • geom: GEOGRAPHY input point to obtain the address.

  • language (optional): VARCHAR language in which results should be returned.

  • options (optional): VARCHAR|OBJECT containing a valid JSON with the different options. No options are allowed currently, so this value will not be taken into account.

Return type

VARCHAR

Constraints

This function performs requests to the CARTO Location Data Services API. Snowflake 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. Unexpected server errors will force Snowflake to retry the requests. The limit is around 500 records but could vary with the provider. To avoid this error, please try processing smaller volumes of data.

Example

SELECT CARTO.CARTO.GEOCODE_REVERSE(ST_POINT(-74.0060, 40.7128));
-- 254 Broadway, New York, NY 10007, USA

ISOLINE

ISOLINE(origin, mode, range, range_type [, options])

Description

Creates an isoline from the provided origin.

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

  • origin: GEOGRAPHY of the origin of the isoline.

  • mode: VARCHAR type of transport. The supported modes depend on the provider:

    • Here: 'walk', 'car', 'truck', 'taxi', 'bus' and 'private_bus'.

    • Mapbox: 'walk', 'car' and 'bike'.

    • TomTom: 'walk', 'car', 'bike', 'motorbike', 'truck', 'taxi', 'bus' and 'van'.

  • range: INT range of the isoline in seconds (for range_type ‘time’) or meters (for range_type ‘distance’).

  • range_type: VARCHAR of the range type. Supported: ‘time’ (for isochrones), ‘distance’ (for isodistances).

  • options (optional): VARCHAR|OBJECT 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 VARCHAR that specifies the time of arrival. If the value is set, a reverse isoline is calculated. If "any" is introduced, 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 VARCHAR that specifies the time of departure. If "any" is introduced, 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 VARCHAR 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 VARCHAR 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 VARCHAR that specifies the time of departure. If "any" is introduced, 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 BOOLEAN that specifies if all available traffic information will be taken into consideration. Supported: true and false.

Return type

GEOGRAPHY

Constraints

This function performs requests to the CARTO Location Data Services API. Snowflake 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. Unexpected server errors will force Snowflake to retry the requests. The limit is around 500 records but could vary with the provider. To avoid this error, please try processing smaller volumes of data.

Examples

SELECT CARTO.CARTO.ISOLINE(ST_MAKEPOINT(-3,40), 'car', 10, 'time', '{"departure_time":"any"}');
-- { "coordinates": [ [ [ -2.999868, 40.001907 ], [ -2.999439, 40.001736 ], [ -2.999096, 40.000706 ], [ -2.998066, 40.000362 ], [ ...
SELECT CARTO.CARTO.ISOLINE(ST_MAKEPOINT(-3,40), 'car', 10, 'time', {'departure_time':'any'});
-- { "coordinates": [ [ [ -2.999868, 40.001907 ], [ -2.999439, 40.001736 ], [ -2.999096, 40.000706 ], [ -2.998066, 40.000362 ], [ ...

LDS_QUOTA_INFO

LDS_QUOTA_INFO()

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

Return type

VARCHAR

Example

SELECT CARTO.CARTO.LDS_QUOTA_INFO();
-- [
--   {
--     "used_quota": 10,
--     "annual_quota": 1000,
--     "providers": {
--         "geocoding": "tomtom",
--         "isolines": "here"
--     }
--   }
-- ]

Last updated

Was this helpful?