Using the CartoLayer
Introduction
The CartoLayer is a deck.gl CompositeLayer
used to visualize geospatial data from the CARTO platform.
It is compatible with the different versions of the CARTO Maps API (v1, v2, and v3) and is responsible for connecting to the platform in order to retrieve the data for visualization.
Connecting to CARTO 3
Go to the Workspace and create a new connection: Tip:
carto_dw
connection is also available for users who don’t have a data warehouse.Create an API Access Token using that connection. Learn how to create API Access Tokens using CARTO Workspace:
Make sure at least Maps API is enabled,
Add grants as needed for your application.
Copy the token and paste it into your deck.gl application. A function
setDefaultCredentials
is provided to define the connection parameters to CARTO:For more info about the parameters of this function, check the reference.
Create a layer using the previously created connection:
View the complete example here
If you are using CARTO 2, there are some differences you need to take into account when specifying the properties:
setDefaultCredentials needs an
username
andapiKey
. For more info check the reference or our guide for Managing your API keysThe
connection
parameter is not required.
Geometry column
By default, the data source you specify in the data
property must contain a column named “geom” with the geometry. If the column containing the geometry has a different name or you are using spatial indices instead of regular geometries, you can still use it but you need to do the following:
If the
type
isMAP_TYPES.QUERY
, you need to alias the geometry column togeom
If the
type
isMAP_TYPES.TABLE
, you need to specify the name of the column containing the geometry or the spatial index using thegeoColumn
property.
Support for other deck.gl layers
The CartoLayer
uses the GeoJsonLayer
for rendering but you can also use any other deck.gl layer for rendering using the fetchLayerData
function from the CARTO module.
This function receives an object with the following properties:
source (equivalent to the
data
property forCartoLayer
)
If the format is not explicitly specified, fetchLayerData
will pick a format automatically, depending on what is available from the CARTO API. The fetchLayerData
function returns an Object
with the following properties:
format
: the format of the returned datadata
: the actual data response
If you are using CARTO 2, there isn’t an equivalent function to the fetchLayerData
function but you can use the SQL API to retrieve the data in JSON or GeoJSON format depending on the layer you want to use. Please check the ArcLayer
example to see how it works.
Last updated