Skip to content

Instantly share code, notes, and snippets.

import click
import csv
import json
import requests
from io import StringIO
from datetime import datetime
from kafka import KafkaConsumer
import atexit
def flush(endpoint, datasource_name, token, rows):
@rochoa
rochoa / index.html
Last active February 1, 2019 17:33
Manhattan buildings by number of floors over the years | CARTO VL
<!DOCTYPE html>
<html>
<head>
<title>Manhattan buildings by number of floors over the years | CARTO VL</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="UTF-8">
<!-- Include CARTO VL JS -->
<script src="https://libs.cartocdn.com/carto-vl/v0.4.0/carto-vl.js"></script>
<!-- Include Mapbox GL JS -->
@rochoa
rochoa / tile.sql
Created November 8, 2017 15:22 — forked from javisantana/tile.sql
CREATE OR REPLACE FUNCTION tile (z integer, x integer, y integer, query text) RETURNS TABLE(id int8, geom geometry)
AS $$
DECLARE
sql TEXT;
BEGIN
sql := 'with _conf as (
select
CDB_XYZ_resolution(' || z || ') as res,
1.0/CDB_XYZ_resolution(' || z || ') as invres,
st_xmin(CDB_XYZ_Extent(' || x || ',' || y || ',' || z ||')) as tile_x,
@rochoa
rochoa / README.md
Last active June 21, 2022 18:42
CARTO Node.js code test

CARTO Node.js code test

Introduction

At CARTO, among other things, we render maps, just check this example of Map with countries and USA states.

In order to limit the scope of the challenge, we are gonna use Mapnik and Node.js. Within this repository, we are providing all the pieces you need to reproduce that map. Well, all pieces except the map tile server.

An example of how to create an image with Mapnik:

@rochoa
rochoa / .gitignore
Last active May 16, 2017 14:04
node-mapnik variables not used in Grids
result.*
node_modules/

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.

@rochoa
rochoa / dot_density.sql
Last active February 14, 2017 16:58 — forked from stuartlynn/dot_density.sql
dot_density.sql
-- Source code from https://github.com/CartoDB/crankshaft/blob/develop/src/pg/sql/30_dot_density.sql
CREATE OR REPLACE FUNCTION cdb_dot_density(geom geometry , no_points Integer, max_iter_per_point Integer DEFAULT 1000)
RETURNS GEOMETRY AS $$
DECLARE
extent GEOMETRY;
test_point Geometry;
width NUMERIC;
height NUMERIC;
x0 NUMERIC;
y0 NUMERIC;
@rochoa
rochoa / .gitignore
Last active March 16, 2017 13:14
npm shrinkwrap error
a/
b/
@rochoa
rochoa / index.html
Last active March 16, 2017 13:02
[CARTO] Tangram basic
<!doctype html>
<!--
Tangram: real-time WebGL rendering for OpenStreetMap
http://github.com/tangrams/tangram
http://mapzen.com
-->
<html lang="en-us">
<head>
<meta charset="utf-8">
@rochoa
rochoa / app.js
Last active August 2, 2018 11:08
[CARTO] basic viewer
'use strict';
var DEFAULTS = {
endpoint: 'https://rochoa.carto.com',
sql: 'select * from airbnb_madrid_oct_2015_listings',
cartocss: `#layer['mapnik::geometry_type'=1] {
marker-width: 7;
marker-fill: #EE4D5A;
marker-fill-opacity: 0.9;
marker-line-color: #FFFFFF;