Function for Exporting Tiles per Geom Where zoom level is 9

WITH c AS (SELECT CDB_XYZ_Extent(x, y, 9) g, x, y FROM GENERATE_SERIES(0,pow(2, 9)::int-1) x, GENERATE_SERIES(0,pow(2, 9)::int-1) y), states AS (SELECT cartodb_id, the_geom_webmercator, c.x, c.y, 9 as zoom FROM andrew.ne_50m_admin_1_states_2, c WHERE ST_Intersects(andrew.ne_50m_admin_1_states_2.the_geom_webmercator, c.g)) SELECT x, y, ARRAY_AGG(cartodb_id::int) FROM states GROUP BY x,y, the_geom_webmercator ORDER BY x asc,y desc


In [ ]:
Turn CSV into a "spatial index"

In [1]:
import csv
import json

name = "tl_2014_census_tracts"
file = name+".csv"
f=0
reader = csv.reader(open(file),delimiter=',')
next(reader, None)
data = {}
for row in reader:
    if int(row[0]) in data.keys():
        pass
    else:
        data[int(row[0])] ={}
        if int(row[0])==125:
            print row
    data[int(row[0])][int(row[1])] = json.loads(row[2].replace('{','[').replace('}',']'))
# print data


['125', '314', '{1223}']

In [2]:
with open(name+'.json', 'w') as outfile:
    json.dump(data, outfile)

In [30]:
Turn our Geojson into a directory of shapes


  File "<ipython-input-30-cc782923012e>", line 1
    Turn our Geojson into a directory of shapes
           ^
SyntaxError: invalid syntax

In [6]:
with open(name+'.geojson') as data_file:    
    data = json.load(data_file)
for f in data['features']:
    out = {'type': 'FeatureCollection', 'features': [f]}
    fip = int(f['properties']['cartodb_id'])
    with open('census/'+str(fip)+'.json', 'w') as outfile:
        json.dump(out, outfile)
# print data

In [ ]: