[GIS] How to Add CARTO(db) Map Tiles to QGIS

cartoqgis

How can I add my 'slippy map' (the XYZ tiles) designed in carto(db) into QGIS?

I am aware of the CARTO plugin for QGIS but it cannot import a carto layer with the styling done in CARTO; it only imports the data and I would have to restyle the data again in QGIS.

The Quickservicesmap plugin for qgis only includes the CARTO base maps designed by Carto; I am trying to add a custom map that I already heavily styled in carto.

I've done some digging From Carto's maps API and think I may be able to obtain the tiles from carto. From reading that link above, I'm not sure if I have to initialize an API call to create the XYZ tile layer first and if I do, how I'd do that in QGIS (or if there's another way to do it).

Best Answer

I'm not certain how to get it into QGIS, but in a browser you need to use the CartoDB.js Core library, here's an example: http://blockbuilder.org/clhenrick/058552f400c6afaf0ec8aeac6ab106a0

Relevant JS code:

var baselayer = L.tileLayer('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png', { attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, &copy; <a href="https://carto.com/attributions">CARTO</a>' }); 

var map = L.map('map', {
  center: [40.7127, -74.0059],
  zoom: 13,
  layers: [baselayer]
});

function cartoTileLayer(callback) {
  cartodb.Tiles.getTiles({
    type: 'cartodb',
    user_name: 'chenrick',
    sublayers: [{
      sql: 'SELECT * FROM map_pluto_likely_rs_2016v1',
      cartocss: '#map_pluto_likely_rs_2016v1{polygon-fill: #FF6600;polygon-opacity: 0.7;line-color: #FFFFFF;line-width: 0.4;line-opacity: 0.5;}'
    }]
  },
  function (tiles, error) {
    if (!tiles || error) {
      if (!error) {
        error = 'Empty response.';
      }
      callback(error, tiles);
    } else {
      callback(null, tiles);
    }
  });
}

function callback(error, tiles) {
  if (error) {
    console.warn(error);
    return;
  }
  var tileURL = tiles.tiles[0];
  map.addLayer(L.tileLayer(tileURL));
}

cartoTileLayer(callback);

Hope this helps somewhat!