# CARTO User Manual

## Tutorials

In this section we provide a set of tutorials that showcase how to get the most out of the CARTO platform, in particular through the Workspace and Builder interfaces that are detailed in this User Manual.

These tutorials leverage the demo data provided by default via the CARTO Data Warehouse connection, so you can start creating maps and exploring our tools from the very beginning.

The data you need for these tutorials are available and ready to be used in the Data Explorer and Builder and you can start using them with all other components of the CARTO platform.

July 12, 2021

## New police stations based on Chicago crime location clusters

In this example we are going to use points clustering to analyze where to locate five new police stations in Chicago based on 5000 samples of crime locations.

### Generating the clusters

First, we calculate crime locations clusters using the `ST_CLUSTERKMEANS` function:

šŗ
 ``````1 2 3 4 5 6 `````` ``````WITH clustered_points AS ( SELECT `carto-un`.carto.ST_CLUSTERKMEANS(ARRAY_AGG(ST_GEOGPOINT(longitude, latitude)), 5) AS cluster_arr FROM cartobq.docs.chicago_crime_sample ) SELECT cluster_element.cluster, cluster_element.geom AS geom FROM clustered_points, UNNEST(cluster_arr) AS cluster_element ``````

This query takes the crimes samples data, gathers their geometries in order to establish clusters and then groups the different geometry clusters. In the following visualization we can easily identify the different clusters by colors.

### Calculating the clusters' centers

Once we have split the sample of points into clusters, we can easily work with them to calculate their centers, envelopes, concave/convex hulls and other different transformations. In this particular example we are interested in finding the center of the clusters, since that is where we are going to place the police stations. The Analytics Toolbox offers different functions for this task: `ST_CENTERMEAN`, `ST_CENTERMEDIAN` and `ST_CENTEROFMASS`. In this case let’s use `ST_CENTERMEDIAN` to calculate the location of the new police stations:

šŗ
 ``````1 2 3 4 5 6 `````` ``````WITH clustered_points AS ( SELECT `carto-un`.carto.ST_CLUSTERKMEANS(ARRAY_AGG(ST_GEOGPOINT(longitude, latitude)), 5) AS cluster_arr FROM cartobq.docs.chicago_crime_sample ) SELECT cluster_element.cluster, `carto-un`.carto.ST_CENTERMEDIAN(ST_UNION_AGG(cluster_element.geom)) AS geom FROM clustered_points, UNNEST(cluster_arr) AS cluster_element GROUP BY cluster_element.cluster ``````

We can see the result in the following visualization, where the bigger dots represent the police stations we have decided to open based on our analysis:

This project has received funding from the European Unionās Horizon 2020 research and innovation programme under grant agreement No 960401.

July 12, 2021

## New supplier offices based on store locations clusters

In this example we are going to use points clustering to analyze where to locate 10 new supplier offices in US so they can best serve all Starbucks locations.

### Generating the clusters

First, we calculate Starbucks locations clusters using theĀ `ST_CLUSTERKMEANS`Ā function:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 `````` ``````WITH data AS( SELECT geog FROM sfcarto.public.starbucks_locations_usa WHERE geog IS NOT null ORDER BY id ), clustered_points AS ( SELECT carto.ST_CLUSTERKMEANS(ARRAY_AGG(ST_ASGEOJSON(geog)::STRING), 10) AS cluster_arr FROM data ) SELECT GET(VALUE, 'cluster') AS cluster, TO_GEOGRAPHY(GET(VALUE, 'geom')) AS geom FROM clustered_points, lateral FLATTEN(input => cluster_arr) ``````

This query gathers the geometries of the Starbucks locations in order to establish clusters and then groups the different geometry clusters. Each of them is represented in a different colour in the following visualization.

### Calculating the clusters' centers

Once we have split the sample of points into clusters, we can easily work with them to calculate their centers, envelopes, concave/convex hulls and other different transformations. In this particular example we are interested in finding the center of the clusters, since that is where we are going to place the offices. The Analytics Toolbox offers different functions for this task, for exampleĀ `ST_CENTERMEAN`, `ST_CENTERMEDIAN`Ā andĀ `ST_CENTEROFMASS`.

In this case we are going to useĀ `ST_CENTEROFMASS`Ā to calculate the location of the new offices:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 `````` ``````WITH data AS( SELECT geog FROM sfcarto.public.starbucks_locations_usa WHERE geog IS NOT null ORDER BY id ), clustered_points AS ( SELECT carto.ST_CLUSTERKMEANS(ARRAY_AGG(ST_ASGEOJSON(geog)::STRING), 10) AS cluster_arr FROM data ) SELECT GET(VALUE, 'cluster') AS cluster, carto.ST_CENTEROFMASS(ST_COLLECT(TO_GEOGRAPHY(GET(VALUE, 'geom')))) AS geom FROM clustered_points, lateral FLATTEN(input => cluster_arr) GROUP BY cluster ``````

We can see the result in the following visualization, where the bigger dots represent the supplier offices locations we have decided to open based on our analysis.