def most_recent_upload(cartodb_table, socrata_resource):
    """Get the most recent upload by table key"""
    cl = CartoDBAPIKey(cartodb_table.apiKey, cartodb_table.domain)
    sql = 'SELECT * FROM {} ORDER BY {} DESC LIMIT 1'.format(
        cartodb_table.table,
        socrata_resource.unique_key
    )
    try:
        return cl.sql(sql)['rows'][0]
    except CartoDBException as e:
        traceback.print_exc()
        print('Exception while inserting:', e)
    except IndexError:
        return None
def upload_data(cartodb_table, data, adapter=None):
    cl = CartoDBAPIKey(cartodb_table.apiKey, cartodb_table.domain)
    adapted = adapter(data)

    values = []
    for values_row in adapted['values']:
        values.append(','.join([sql_str(v) for v in values_row]))

    sql = 'INSERT INTO %s (%s) VALUES %s' % (
        cartodb_table.table,
        ','.join(adapted['column_names']),
        ','.join(['(%s)' % v for v in values]),
    )
    try:
        cl.sql(sql)
    except CartoDBException as e:
        traceback.print_exc()
        print('Exception while inserting:', e)
示例#3
0
# coding=utf-8

import re

from django.conf import settings

from cartodb.cartodb import CartoDBAPIKey

cl = CartoDBAPIKey(
    settings.CARTODB_KEY,
    settings.CARTODB_DOMAIN)


def custom_sql(sql):
    """
        Query cartodb to get ID ordered by distance to the point
    """
    result = cl.sql(sql)
    objects = result['rows']
    return objects


def get_nearest(table_name, lat, lon, limit=20, offset=0, **kwargs):
    """
        Query cartodb to get ID ordered by distance to the point
    """
    join_sql = _build_joins(table_name, **kwargs)

    sql = u"""
                SELECT %(tablename)s.cartodb_id,
                    ST_Distance(%(tablename)s.the_geom::geometry,