2

I'm using the static maps API to retrieve a .jpg image from a CartoDB named map. It looks like this.

map with parcels

It's a bunch of residential land parcels.

I need to add a base map such as Here Nokia Reduced Day to this so that the street names can be shown, like this

map with streets

How would I do that without using a Browser or Javascript? My earlier question gives some background for those curious about the "no browser, no Javascript" constraints, and my answer describes what I'm currently doing. I may be wrong about these constraints, but I don't know how to get around them.

6

To create/instantiate a Named Map and obtain an image from it you don't need Javascript or a Browser.

The MapConfig file in which you define your layer allows you to add HTTP layers which are mostly meant to include your own basemaps directly. Here's an example of the definition of the layers of a map (my basemap included, note about the credential tokens information I mention below):

{
    "version": "0.0.1",
    "name": "named_map_with_basemap2",
    "auth": {
        "method": "open"
    },
    "layergroup": {

        "layers": [{
            "type": "http",
            "options": {

                "urlTemplate": "https://2.maps.nlp.nokia.com/maptile/2.1/maptile/newest/reduced.day/{z}/{x}/{y}/256/png8?lg=eng&token=YOUR_TOKEN&app_id=YOUR_APP_ID"
            }
        }, {
            "type": "cartodb",
            "options": {
                "cartocss_version": "2.1.1",
                "sql": "select * from ne_10m_populated_places_simple_14",
                "cartocss": "#ne_10m_populated_places_simple_14{ marker-fill-opacity: 0.9; marker-line-color: #FFF; marker-line-width: 1.5; marker-line-opacity: 1; marker-placement: point; marker-type: ellipse; marker-width: 4; marker-fill: #FF6600; marker-allow-overlap: true;}"
            }
        }]
    }
}

Note: At this step you need to make sure that the URL for the basemap you're using is valid -- or that CartoDB is using it conveniently. If after declaring the map this way you're not seeing the basemap, something might be wrong.

Then create it:

curl -X POST \
-H 'Content-Type: application/json' \
-d @template.json \
'https://USERNAME.cartodb.com/api/v1/map/named?api_key=APIKEY'

After a successful response (which will return the named map name) you can use the Static Maps API methods that work with named maps names or instantiate the map to get the layergroup ID and use the Static Maps API with the token functionality.

Example:

http://iriberri.cartodb.com/api/v1/map/static/named/named_map_with_basemap2/600/700.png

(From: http://docs.cartodb.com/cartodb-platform/maps-api/static-maps-api/#named-map)

world

Or instantiate it:

CURL -X POST \
-H 'Content-Type: application/json' \
-d '' \
'https://USERNAME.cartodb.com/api/v1/map/named/named_map_with_basemap2'

Response example:

{"layergroupid":"iriberri@2c2684f3@d9e1692c0fd89ce0ecfedeb44a861c66:1424793167596","metadata":{"layers":[{"type":"http","meta":{"stats":[],"cartocss":{}}},{"type":"mapnik","meta":{"stats":[],"cartocss":"#ne_10m_populated_places_simple_14{ marker-fill-opacity: 0.9; marker-line-color: #FFF; marker-line-width: 1.5; marker-line-opacity: 1; marker-placement: point; marker-type: ellipse; marker-width: 4; marker-fill: #FF6600; marker-allow-overlap: true;}"}}]},"cdn_url":{"http":"ashbu.cartocdn.com","https":"cartocdn-ashbu.global.ssl.fastly.net"},"last_updated":"2015-02-24T15:52:47.596Z"}% 

Whose response would return a layergroupid, among others information from which you can use any of the request to retrieve images that are documented at: http://docs.cartodb.com/cartodb-platform/maps-api/static-maps-api/

Example (whose token will expire soon, but for the record):

http://iriberri.cartodb.com/api/v1/map/static/center/iriberri@2c2684f3@d9e1692c0fd89ce0ecfedeb44a861c66:1424793167596/3/40.71502926732618/-4.96039009094238/600/400.png

second request

Note about basemaps:

If you want to use Here basemaps outside of the CartoDB Editor, you will need an account for their API and the tokens needed to request their maps. They allow thousands of free requests per month so you would not have to worry about pricing if your use case is not going to be extensive. For the explicit URL of your basemap, if you want to use HERE, you might want to take a look here: https://developer.here.com/rest-apis/documentation/enterprise-map-tile/topics/example-normal-day-grey.html

If you don't require to use HERE basemaps, you can use for example the Positron CartoDB basemap which has a similar colour and whose data comes from OSM. Its URL is http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png.

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.