In this guide you will learn how to use different data sources in your CARTO VL visualizations. After finishing this guide, you will be able to connect to datasets in several ways, and know which option is best for you.
This guide assumes that you have previously gone through the Getting Started Guide and know how to make a basic map.
By the end of this guide, you will be able to create this CARTO VL map:
CARTO VL is a library that visualizes geographical datasets in a powerful and flexible way. Those datasets can be yours or they can be served by some other provider, but the first step to know is where they are hosted and how they can be accessed.
CARTO VL currently supports these three options:
Every option is a different kind of Source, and CARTO VL provides you with a suitable object in its API to connect to them under the namespace carto.source
(for example carto.source.Dataset
).
Both Dataset and SQL are based in Vector Tiles, following the Mapbox Vector Tile Specification or MVT. This advanced technology allows transferring geographic data from the server to your browser in small chunks, allowing good performance and powerful dynamic styling.
In fact, there is a fourth type of source in CARTO VL called carto.source.MVT but it is not meant to be used directly by the users, except in very precise / advanced cases.
In the following section you will see how to use three main source types. Before getting started with that, you will need to create a basic map.
You can start from this basemap. Go ahead and copy its source code into a new file called sources.html
.
To copy the source code from an example, you just have to navigate to it with your browser, click right button >
View source
and copy the whole text, from<html>
to</html>
tags.
Next, to warm up, add navigation controls, just after the map creation:
1
2
const nav = new mapboxgl.NavigationControl();
map.addControl(nav, 'top-left');
A Dataset
can be managed using carto.source.Dataset. It is a source with geographic information related to a specific topic (such as stores, streets or counties). If you have a GIS background, this is like a local vector file with points, lines or polygons, hosted in CARTO. If you don’t, you can think of it as a table on the server with a geometry field you can use to draw it on a map.
You already know how to add a Dataset
thanks to the Getting Started guide:
1
const aSource = new carto.source.Dataset('name_of_your_dataset');
That was using carto.setDefaultAuth
method, but now you will see how to include custom credentials for a specific dataset. Add this to your current working file (sources.html if you followed our suggestion), just after map creation.
1
2
3
4
const citiesSource = new carto.source.Dataset('populated_places', {
username: 'cartovl',
apiKey: 'default_public'
});
As with any Source
, you should then pass it to a Layer
to visualize it, but first let’s create its Viz with a simple style:
1
2
3
4
const citiesViz = new carto.Viz(`
color: grey
width: 4
`);
Now you’re ready for the layer creation:
1
const citiesLayer = new carto.Layer('cities', citiesSource, citiesViz);
It is a good practice to give them a short but clear name, like ‘cities’
And now you can add that layer to the map, by including this code:
1
citiesLayer.addTo(map);
The result should look like this:
You have a CARTO account, with several custom datasets, and you want to visualize one of them on a map, with all its rows.
A GeoJSON
can be used in CARTO VL with carto.source.GeoJSON. GeoJSON is a standard format to encode geographic data using JavaScript. It is indeed a common JSON, extended with spatial features, and you can create some .geojson contents online at geojson.io.
In the next set of steps, you’ll create a new GeoJSON layer, to visualize the main CARTO offices around the world.
You can include GeoJSON content and embed it directly in your JavaScript, like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const offices = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-73.944158, 40.678178]
},
"properties": {
"address": "Brooklyn, New York"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-3.70379, 40.416775]
},
"properties": {
"address": "Madrid, Spain"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-0.127758, 51.507351]
},
"properties": {
"address": "London, United Kingdom"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-77.036871, 38.907192]
},
"properties": {
"address": "Washington, DC"
}
}
]
};
If your dataset is much bigger, you would probably store that content in an external file (see this External GeoJSON layer example).
And then use it within a GeoJSON source, like this:
1
const officesSource = new carto.source.GeoJSON(offices);
Create a custom style for the layer:
1
2
3
4
const officesViz = new carto.Viz(`
color: red
width: 20
`);
Define a map layer:
1
const officesLayer = new carto.Layer('offices', officesSource, officesViz);
And finally add that layer to the map:
1
officesLayer.addTo(map);
Now the map should look like this:
You already have your data in GeoJSON format, and you don’t have access to a CARTO account (if you do have a CARTO account, you should import it to get a better performance and more capabilities, and then use a Dataset
source). This can also be a useful format for some quick tests, using inline GeoJSON if you are managing just a few rows or a .geojson file just next to your .html.
SQL is a very common language to make queries in databases and geospatial software. It provides a flexible mechanism to adapt your dataset to your specific needs.
Let’s see how to add a SQL source to your map!
Define a query (select only the largest cities (megacity
) in the world from the populated_places
dataset):
1
const query = 'SELECT * FROM populated_places WHERE megacity = 1';
This is a simple query but the SQL runs on CARTO’s backend, which is powered by PostGIS, so you could also execute more sophisticated queries and even spatial analysis.
Create a SQL source:
1
2
3
4
const megacitiesSource = new carto.source.SQL(query, {
username: 'cartovl',
apiKey: 'default_public'
});
Define the new style:
1
const megacitiesViz = new carto.Viz('color: blue');
Create a common layer with those selected megacities
:
1
const megacitiesLayer = new carto.Layer('megacities', megacitiesSource, megacitiesViz);
And finally add this layer to the map:
1
megacitiesLayer.addTo(map);
You have a CARTO account, with several custom datasets, and you want to visualize them in a layer, applying some kind of transformation to the source, from a filter to more advanced analysis.
Congrats! You have finished this guide. The final map should look like this:
You can explore the final step here
This is the complete code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://libs.cartocdn.com/carto-vl/v0.10.0/carto-vl.js"></script>
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.50.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.50.0/mapbox-gl.css' rel='stylesheet' />
<link href="https://carto.com/developers/carto-vl/v0.10.0/examples/maps/style.css" rel="stylesheet">
</head>
<body>
<!-- Add map container -->
<div id="map"></div>
<script>
// Add basemap and set properties
const map = new mapboxgl.Map({
container: 'map',
style: carto.basemaps.voyager,
center: [0, 30],
zoom: 2
});
// Add zoom controls
const nav = new mapboxgl.NavigationControl();
map.addControl(nav, 'top-left');
//** CARTO VL functionality begins here **//
// DATASET
// Define Dataset source with custom credentials
const citiesSource = new carto.source.Dataset('populated_places', {
username: 'cartovl',
apiKey: 'default_public'
});
// Define Viz object with custom style
const citiesViz = new carto.Viz(`
color: grey
width: 4
`);
// Define map Layer
const citiesLayer = new carto.Layer('cities', citiesSource, citiesViz);
// Add map Layer
citiesLayer.addTo(map);
// GEOJSON
// Create GeoJSON content
const offices = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-73.944158, 40.678178]
},
"properties": {
"address": "Brooklyn, New York"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-3.70379, 40.416775]
},
"properties": {
"address": "Madrid, Spain"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-0.127758, 51.507351]
},
"properties": {
"address": "London, United Kingdom"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-77.036871, 38.907192]
},
"properties": {
"address": "Washington, DC"
}
}
]
};
// Define GeoJSON source
const officesSource = new carto.source.GeoJSON(offices);
// Define Viz object with custom style
const officesViz = new carto.Viz(`
color: red
width: 20
`);
// Define map Layer
const officesLayer = new carto.Layer('offices', officesSource, officesViz);
// Add map Layer
officesLayer.addTo(map);
// SQL
// Define query
const query = 'SELECT * FROM populated_places WHERE megacity = 1';
// Define SQL source with query and custom credentials
const megacitiesSource = new carto.source.SQL(query, {
username: 'cartovl',
apiKey: 'default_public'
});
// Define Viz object with custom style
const megacitiesViz = new carto.Viz('color: blue');
// Define map Layer
const megacitiesLayer = new carto.Layer('megacities', megacitiesSource, megacitiesViz);
// Add map Layer
megacitiesLayer.addTo(map);
</script>
</body>
</html>