CARTO for deck.gl

CARTO for deck.gl

Trips Layer

This example shows how to use the TripsLayer to render animated paths that represent vehicle trips.

 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
<html>
  <head>
    <script src="https://unpkg.com/deck.gl@^8.5.0/dist.min.js"></script>
    <script src="https://unpkg.com/@deck.gl/carto@^8.5.0/dist.min.js"></script>

    <script src="https://libs.cartocdn.com/mapbox-gl/v1.13.0/mapbox-gl.js"></script>
    <link href="https://libs.cartocdn.com/mapbox-gl/v1.13.0/mapbox-gl.css" rel="stylesheet" />
  </head>

  <body style="margin: 0; padding: 0;">
    <div id="map" style="width: 100vw; height: 100vh"></div>
  </body>

  <script type="text/javascript">
    const LOOP_LENGTH = 1800;
    const ANIMATION_SPEED = 0.4;

    async function initialize() {
      deck.carto.setDefaultCredentials({
        apiBaseUrl: 'https://gcp-us-east1.api.carto.com',
        apiVersion: deck.carto.API_VERSIONS.V3,
        accessToken: 'eyJhbGciOiJIUzI1NiJ9.eyJhIjoiYWNfbHFlM3p3Z3UiLCJqdGkiOiI1YjI0OWE2ZCJ9.Y7zB30NJFzq5fPv8W5nkoH5lPXFWQP0uywDtqUg8y8c'
      });

      // Fetch Data from CARTO
      const geojsonData =  await deck.carto.getData({
        type: deck.carto.MAP_TYPES.QUERY,
        source: `SELECT geom, vendor, 
                        TO_JSON_STRING(timestamps) AS timestamps 
                   FROM cartobq.public_account.new_york_trips`,
        connection: 'bqconn',
        format: deck.carto.FORMATS.GEOJSON
      });

      // TripsLayer needs data in the following format
      const layerData = geojsonData.features.map(f => ({
        vendor: f.properties.vendor,
        timestamps: JSON.parse(f.properties.timestamps),
        path: f.geometry.coordinates
      }));

      const deckgl = new deck.DeckGL({
        container: 'map',
        mapStyle: deck.carto.BASEMAP.DARK_MATTER_NOLABELS,
        initialViewState: {
          longitude: -74,
          latitude: 40.73,
          zoom: 12,
          pitch: 45,
          bearing: 0
        },
        controller: true
      });

      let time = 0;

      function animate() {
        time = (time + ANIMATION_SPEED) % LOOP_LENGTH;
        window.requestAnimationFrame(animate);
      }

      setInterval(() => {
        deckgl.setProps({
          layers: [
            new deck.TripsLayer({
              id: 'trips-layer',
              data: layerData,
              getPath: d => d.path,
              getTimestamps: d => d.timestamps,
              getColor: d => (d.vendor === 0 ? [253, 128, 93] : [23, 184, 190]),
              opacity: 0.3,
              widthMinPixels: 2,
              rounded: true,
              trailLength: 180,
              currentTime: time,
              shadowEnabled: false
            })
          ]
        });
      }, 50);

      window.requestAnimationFrame(animate);
    }

    initialize();    
  </script>
</html>