Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import os
import sys
import argparse
from carto.auth import APIKeyAuthClient
from carto.maps import NamedMapManager
parser = argparse.ArgumentParser(description=(
'Example of CopySQLClient usage to stream data from NEXRAD Level 2'
' data to CARTO.'
))
parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL',
default=os.environ.get('CARTO_API_URL', ''),
help=('Set the base URL. For example:'
' https://username.carto.com/'
' (defaults to env variable CARTO_API_URL)'))
parser.add_argument('--api_key', dest='CARTO_API_KEY',
default=os.environ.get('CARTO_API_KEY', ''),
help=('Api key of the account'
' (defaults to env variable CARTO_API_KEY)'))
parser.add_argument('--datasets', dest='DATASETS',
default=os.environ.get('DATASETS', ''),
help=('Name of the datasets to download'
' (defaults to None)'))
args = parser.parse_args()
if not args.CARTO_BASE_URL or not args.CARTO_API_KEY or not args.DATASETS:
sys.exit(parser.print_usage())
auth_client = APIKeyAuthClient(args.CARTO_BASE_URL, args.CARTO_API_KEY)
def create_named_map(dataset_name):
template = {
"version": "0.0.1",
"name": 'tpl_' + dataset_name,
"auth": {
"method": "open"
},
"placeholders": {},
"view": {},
"layergroup": {
"version": "1.0.1",
"layers": [
{
"type": "plain",
"options": {
"color": "#ffffff"
}
},
{
"type": "cartodb",
"options": {
"cartocss_version": "2.1.1",
"cartocss": "#layer { polygon-fill: #000000; polygon-opacity: 0.9;} #layer::outline { line-width: 0.1; line-color: #000000; line-opacity: 1;}",
"sql": "select * from buildings"
}
}
]
}
}
named_map_manager = NamedMapManager(auth_client)
try:
named_map = named_map_manager.get('tpl_' + dataset_name)
if named_map is not None:
named_map.client = auth_client
named_map.delete()
except Exception as e:
#ignore
print(e)
return named_map_manager.create(template=template)
create_named_map('buildings')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment