1

I want to download a static image of a map to my local PC via a Python script.

I'm using carto-print: carto-print

I have tried this method:

from xlwings import Book, Range
from carto.auth import APIKeyAuthClient
from carto.exceptions import CartoException
from carto.datasets import DatasetManager
from carto.export import ExportJob
from carto.maps import NamedMapManager, NamedMap
from carto.resources import AsyncResource
from carto.print import Printer

import json
import os
import urllib
import urllib.parse
import urllib.request as urllib2
import base64
import sys
import argparse
from _ast import If
try:
    import requests
except ImportError:
    print('The requests package is required: http://docs.python-requests.org/en/latest/user/install/#install')
    sys.exit()

user = "{username}"
key = "{api key}"
map_id = '{map id}'
width = 30
hight = 20


############ GET TEMPLATE NAME and BOUNDS ############
img_url = 'https://%s.carto.com/api/v2/viz/%s/viz.json/?api_key=%s' %(user, map_id, key)
r = requests.get(img_url)
#print(json.dumps(r.json(), indent=2)) # Prints json response

#center = r.json()['center']
#lat, long = center.replace(']','').replace('[','').split(",")
zoom = r.json()['zoom']
tpl_name = r.json()['layers'][1]['options']['named_map']['name']
bounds_temp = r.json()['bounds']
bounds = bounds_temp[0]
bounds.append(bounds_temp[1][0])
bounds.append(bounds_temp[1][1])
bounds = str(bounds).replace(' ','').replace('[','').replace(']','')

print(bounds)

############ GET IMAGE ###########
printer = Printer(user, tpl_name, key, width, hight, zoom, bounds, 300, 'CMYK')
printer.export("D:/{folder}/{other_folder}")


print('done')

UPDATE: I fixed the error but the image I get is a background with no map or visualization layers. blank map

Any idea how to get the visualizations and map layers? Perhaps I am getting the wrong template id?

  • Could you share what getMap.py does? We need to check the code in order to replicate the error. – ramiroaznar Jan 21 at 9:18
  • @ramiroaznar Okay, I added it. – Brandon Hoffmann Jan 23 at 22:17

Your Answer

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

Browse other questions tagged or ask your own question.