Sunday, 13 August 2017

carto - Can I get a .jpg from a CartoDB map created in the editor without using a browser?


Edit


I'm actually starting to suspect that I can't get a .jpg image from a map created with the UI editor without using a browser. I think I might have to use the API to re-create the map my colleague created so that the map has a layergroupid. An acceptable answer to this question might be to confirm those suspicions.




Original question


I'm a complete beginner with CartoDB.


Somebody in our organisation has created a map in the CartoDB editor (described here), and I need to get a .jpg image from it. The map covers the whole of the country and shows property boundaries. The person who created it is responsible for the styling of the map and for the data the map uses. The datasets are private.


I am writing an API which needs to get aerial images of properties from the CartoDB map, zoomed in so that each image shows a few properties. My API is written in C# and runs on a server. I mention those details to emphasise that there is no browser involved in the interactions with the map, and no Javascript.


The map need only be accessible to trusted entities. The two I can think of are: (1) the person maintaining the map and (2) my code. The code I'll be writing runs in a totally private context on our server. It would therefore be OK for my code to use the api_key when retrieving the .jpg. I can also set a password for the map and use that. (The images retrieved will be stored on our company's server, and those stored images served up to our API users in a completely different security context.) So access to the map is totally private.



I've seen some information about getting .jpg images, but it all seems not to work for me because it requires a layergroupid, which it seems is only available if the map was created through a CartoDB API. Since my map was created using the editor, I don't think there is a layergroupid, and so those methods don't apply.


It might be possible to create the map using the CartoDB API instead of using the editor, so that the map has a layergroupid. This approach has drawbacks though, because my colleague would need to learn a different way to create the maps.


In summary



  • complete beginner with CartoDB

  • map created using CartoDB editor, private datasets

  • no browser, no Javascript

  • method of retrieving .jpg totally private, so could use api_key

  • prefer not to re-create existing editor-created map via CartoDB API



Question


Given those constraints, how can I get the .jpg images I need from the map?



Answer



If you have a Map (named map if it has private data) created already in the Editor, you will be able to find it's template map name in the viz.json file of the map. (Available at the third link in the Publish button under the CartoDB.js title).


Inside your viz.json, look for "named_map" and "name"


{
type: "namedmap",
order: 1,
options: {
type: "namedmap",

user_name: "iriberri",
maps_api_template: "https://{user}.cartodb.com:443",
sql_api_template: "https://{user}.cartodb.com:443",
tiler_protocol: "http",
tiler_domain: "cartodb.com",
tiler_port: "80",
filter: "mapnik",
named_map: {
name: "tpl_59dc29e0_f006_11e5_9245_0e674067d321",
stat_tag: "59dc29e0-f006-11e5-9245-0e674067d321",

params: {
layer0: 1,
layer1: 1
},

Now you have your named map name which you can use in the Static Maps API options that allow the template_id (synonym to named map name) directly or you can instantiate it in order to get the layergroup and use this token with the Static Maps API.


Notice that the token will expire after some time, so each time that you need to retrieve an image you'll have to instantiate it. In order to instantiate it, no browser or javascript is required, just:


CURL -X POST \
-H 'Content-Type: application/json' \
-d '' \

'https://USERNAME.cartodb.com/api/v1/map/named/THE_NAME_IN_THE_VIZJSON?api_key=APIKEY'

So, in short:


If you know the ID of the map created in the Editor, you can request the viz.json file to:


https://USERNAME.cartodb.com/api/v2/viz/MAP_ID/viz.json


and from the JSON file retrieved, just look into it for the named map object and its name.


There's other question that you posted which is pretty related to my response here and might include extra details also useful: Create a named map with a base map - without Javascript or browser


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...