Skip to content

Commit

Permalink
Downloads now only include tables selected in the table browser. Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
cgroskopf committed Jun 9, 2011
1 parent 320cbbe commit 7ada0aa
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 61 deletions.
122 changes: 70 additions & 52 deletions censusweb/api/views.py
Expand Up @@ -14,7 +14,7 @@
import utils
from datetime import datetime

DATA_ALTERNATIVES = ['2000','2010','delta','pct_change']
DATA_ALTERNATIVES = ['2000', '2010', 'delta', 'pct_change']

def homepage(request):
return render_to_response('homepage.html', {
Expand Down Expand Up @@ -43,24 +43,67 @@ def download_data_for_region(request, sumlev='', containerlev='', container='',
elif datatype == 'json':
return data_as_json(request, geoids)

def get_tables_for_request(request):
tables = request.GET.get("tables", None)

if tables:
tables = tables.split(",")
else:
tables = mongoutils.get_tables()

return tables

# --- JSON ---
def data_as_json(request, geoids):
tables = get_tables_for_request(request)

geographies = {}

geoids_list = filter(lambda g: bool(g), geoids.split(','))

for g in utils.fetch_geographies(geoids_list):
del g['xrefs']

for table in g["data"]["2010"].keys():
if table not in tables:
del g["data"]["2010"][table]

# Not all data has 2000 values
try:
del g["data"]["2000"][table]
del g["data"]["delta"][table]
del g["data"]["pct_change"][table]
except KeyError:
continue

geographies[g['geoid']] = g

return HttpResponse(simplejson.dumps(geographies), mimetype='application/json')

def _csv_row_header(tables=None):
if not tables:
tables_list = mongoutils.get_tables()
else:
tables_list = tables
# --- CSV ---
def data_as_csv(request, geoids):
tables = get_tables_for_request(request)

response = HttpResponse(mimetype="text/csv")
w = csv.writer(response)
w.writerow(_csv_row_header(tables))

geoids_list = filter(lambda g: bool(g), geoids.split(','))

for g in utils.fetch_geographies(geoids_list):
csvrow = _csv_row_for_geography(g, tables)
w.writerow(csvrow)

now = datetime.now()
date_string = "%s-%s-%s-%s" % (now.year, now.month, now.day, now.microsecond)
response['Content-Disposition'] = "attachment; filename=ire-census-%s.csv" % date_string

return response

def _csv_row_header(tables):
row = ["sumlev", "geoid", "name"]
for table in tables_list:

for table in tables:
labels = mongoutils.get_labels_for_table(table)
for statistic in sorted(labels['labels']):
for alternative in DATA_ALTERNATIVES:
Expand All @@ -71,18 +114,14 @@ def _csv_row_header(tables=None):

return row

def _csv_row_for_geography(geography, tables=None):
if not tables:
tables_list = mongoutils.get_tables()
else:
tables_list = tables

def _csv_row_for_geography(geography, tables):
row = [
geography['sumlev'],
geography['geoid'],
geography['metadata']['NAME']
]
for table in tables_list:

for table in tables:
labels = mongoutils.get_labels_for_table(table)
for statistic in sorted(labels['labels']):
for alternative in DATA_ALTERNATIVES:
Expand All @@ -93,35 +132,16 @@ def _csv_row_for_geography(geography, tables=None):

return row

def data_as_csv(request, geoids):
tables = request.GET.get("tables", None)
if tables:
tables = tables.split(",")

response = HttpResponse(mimetype="text/csv")
w = csv.writer(response)
w.writerow(_csv_row_header(tables))

geoids_list = filter(lambda g: bool(g), geoids.split(','))
for g in utils.fetch_geographies(geoids_list):
csvrow = _csv_row_for_geography(g, tables)
w.writerow(csvrow)

now = datetime.now()
date_string = "%s-%s-%s-%s" % (now.year, now.month, now.day, now.microsecond)
response['Content-Disposition'] = "attachment; filename=ire-census-%s.csv" % date_string

return response


# --- KML BEGIN ---
# --- KML ---
def data_as_kml(request, geoids,format='kml'):
tables = get_tables_for_request(request)

geoid_list = filter(lambda g: bool(g), geoids.split(','))
boundaries = dict((b.external_id,b) for b in Boundary.objects.filter(external_id__in=geoid_list))
json_data = dict((j['geoid'],j) for j in mongoutils.get_geographies_list(geoid_list))
boundaries = dict((b.external_id, b) for b in Boundary.objects.filter(external_id__in=geoid_list))
json_data = dict((j['geoid'], j) for j in mongoutils.get_geographies_list(geoid_list))

placemarks = [
_create_placemark_dict(boundaries[geoid],json_data[geoid]) for geoid in geoid_list
_create_placemark_dict(boundaries[geoid], json_data[geoid], tables) for geoid in geoid_list
]

if format == 'kmz':
Expand All @@ -130,20 +150,17 @@ def data_as_kml(request, geoids,format='kml'):
render = render_to_kml
return render('gis/kml/placemarks.kml', {'places' : placemarks})

def _create_placemark_dict(b,j,tables=None):
"""each thing should have a name, a description, and kml which includes <ExtraData>"""
def _create_placemark_dict(b, j, tables):
"""
Each placemark should have a name, a description, and kml which includes <ExtraData>
"""
p = {
'name': b.display_name,
'description': 'Summary Level: %(sumlev)s; GeoID: %(geoid)s' % (j),
}

if not tables:
tables_list = mongoutils.get_tables()
else:
tables_list = tables

kml_context = _build_kml_context_for_template(b,j,tables_list)
shape = b.simple_shape.transform(4326,clone=True)
kml_context = _build_kml_context_for_template(b, j, tables)
shape = b.simple_shape.transform(4326, clone=True)
p['kml'] = shape.kml + KML_EXTENDED_DATA_TEMPLATE.render(Context(kml_context))

return p
Expand All @@ -158,9 +175,10 @@ def _create_placemark_dict(b,j,tables=None):
{% endfor %}
</ExtendedData>""")

def _build_kml_context_for_template(b,j,tables_list):
def _build_kml_context_for_template(b, j, tables):
kml_context = { 'data': [] }
for table in tables_list:

for table in tables:
labels = mongoutils.get_labels_for_table(table)
for statistic in sorted(labels['labels']):
for alternative in DATA_ALTERNATIVES:
Expand All @@ -170,10 +188,10 @@ def _build_kml_context_for_template(b,j,tables_list):
if alternative == '2010':
datum['name'] = statistic
else:
datum['name'] = "%s.%s" % (statistic,alternative)
datum['name'] = "%s.%s" % (statistic, alternative)
datum['display_name'] = labels['labels'][statistic]['text']
kml_context['data'].append(datum)
except KeyError: pass

return kml_context

# --- KML END ---
5 changes: 2 additions & 3 deletions censusweb/media/js/browser.js
Expand Up @@ -20,7 +20,8 @@ $(function(){
this.saveLocation($.cookie('show_tables'))
var show_ids = $.cookie('show_tables').split(',')
} else {
var show_ids = ['H1']
// TODO
var show_ids = ["P1", "H1"]
}

$("table.report").hide()
Expand All @@ -33,8 +34,6 @@ $(function(){
// Report doesn't exist, create it
} else {
var labelset = window.labels_data["tables"][id];
console.log(id);
console.log(labelset);
var report = makeReport(id, labelset, window.geoids, window.geographies);
window.renderReport(report);
window.configureEvents(id);
Expand Down
22 changes: 16 additions & 6 deletions censusweb/media/js/data.js
Expand Up @@ -63,26 +63,36 @@ $(function(){
return geoids;
}

window.downloadData = function(format) {
var url = "/data/" + window.geoids.join(",") + "." + format;

if (window.location.hash != "") {
url += "?tables=" + window.location.hash.substring(1);
}

window.location = url;
}

window.loadLabels = function() {
apiRequest("/" + window.DATASET + "_labels.jsonp", "labels_" + window.DATASET, function(labels_data) {
window.labels_data = labels_data;
window.geoids = parseGeoids();

window.tables = _.keys(labels_data["tables"])
window.tables.sort();

$('nav .csv').click(function () {
window.location = "/data/" + geoids.join(",") + ".csv";
downloadData("csv");
});

$('nav .json').click(function () {
window.location = "/data/" + geoids.join(",") + ".json";
downloadData("json");
});

$('nav .kml').click(function () {
window.location = "/data/" + geoids.join(",") + ".kml";
downloadData("kml");
});

window.tables = _.keys(labels_data["tables"])
window.tables.sort();

_.each(window.tables, function(table) {
$('#reports').append($('<div id="report-wrapper-' + table + '"></div>'));
});
Expand Down

0 comments on commit 7ada0aa

Please sign in to comment.