Hey! This content applies only to previous CARTO products

Please check if it's relevant to your use case. On October 2021 we released a new version of our platform.
You can learn more and read the latest documentation at docs.carto.com

Questions  /  Working with Data  /  PostGIS

How to apply routing functions in CARTO

Learn how to generate routes from origins to destinations with CARTO routing functions.

In order to generate a route from one point to another, CARTO users can use the CARTO Data Services API. The routing functions will return the geometry, and also the trip duration (in seconds) and trip length (in kilometers).

If you have a table with latitude and longitude coordinates for both origin and destination, you can use a query like this one:

SELECT
    o.cartodb_id,
    o.origin,
    o.destination,
    r.duration/60 as minutes,
    r.length,
    r.shape as the_geom
FROM
    table_name o,
    table_name d,
    cdb_route_point_to_point(
        cdb_latlng(o.lat_o, o.long_o),
        cdb_latlng(d.lat_d, d.long_d),
        'car') as r
WHERE
    o.cartodb_id = d.cartodb_id

route