0

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).

1

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!

0

Thanks to Keith Jenkins for providing the answer offline and Chris with an additional insight.

Steps to do this for future reference for anyone reading this.

  1. Find the tilelayer URL to your styled map styles by viewing your CARTO web map as an HTML file in a browser and using an inspector tool (like Firebug for Firefox). Depending how you styled your data (If you styled it completely in the carto editor on carto.com, just input the VIZ url in in this cartodb.js template or if you styled it in cartodb.js, follow clhenrick's example above.

You'll find the carto url by inspecting the elements in firebug or another html inspector.

In my case, the URL was http://1.ashbu.cartocdn.com/skorasaurus/api/v1/map/7164929f37e89aa80fe873bb60a4cb91:1420169079879/14/84229/233.png

  1. Using QGIS' TileLayer Plugin, create an entry using your tilelayer URL, but replace the last 3 portions with {z}, {x}, and {y}. (so the TileLayer URL is my example is http://1.ashbu.cartocdn.com/skorasaurus/api/v1/map/7164929f37e89aa80fe873bb60a4cb91:1420169079879/{z}/{x}/{y}.png).

Optional step 3: Repeat steps 1 and 2 with the tilelayer URL of the carto(db) basemap or use a carto map in the quickservicesmap plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.