/
index.html
173 lines (143 loc) · 8.54 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Untitled</title>
<script src="https://code.jquery.com/jquery-1.11.2.min.js" charset="utf-8"></script>
<script src="turf.min.js" charset="utf-8"></script>
</head>
<body>
<ul>
<li><a href="https://github.com/censusreporter/census-api/blob/master/API.md">Census Reporter API docs</a></li>
<li><a href="http://censusreporter.org/profiles/16000US0653000-oakland-ca/">Page for single feature</a></li>
<li><a href="http://api.censusreporter.org/1.0/geo/tiger2013/16000US0653000">JSON for single feature</a></li>
<li><a href="http://api.censusreporter.org/1.0/geo/tiger2013/tiles/140/12/656/1582.geojson">Tile of tracts</a></li>
<li><a href="http://api.censusreporter.org/1.0/geo/show/tiger2013?geo_ids=14000US06001401000,14000US06001401400">Pair of selected features</a></li>
<li><a href="http://api.censusreporter.org/1.0/data/show/acs2013_5yr?table_ids=B13016&geo_ids=04000US55">Data for one feature</a></li>
</ul>
<script>
var start = (new Date()).getTime(),
api_base_href = 'http://api.censusreporter.org/1.0',
chunk_size = 10;
function announce(xhr, settings)
{
console.log('Getting ' + settings.url);
}
var q = 'Portland',
url = api_base_href+'/geo/elasticsearch?size=1&sumlevs=160&q='+escape(q),
result = jQuery.ajax(url, {async: false, beforeSend: announce}),
muni_geoid = result.responseJSON.results[0].full_geoid,
display_name = result.responseJSON.results[0].display_name;
var url = api_base_href+'/geo/show/tiger2013?geo_ids=140|'+muni_geoid,
result = jQuery.ajax(url, {async: false, beforeSend: announce}),
tracts = result.responseJSON.features;
document.write('<p>Loaded ' + tracts.length + ' census tracts in and around <a href="http://censusreporter.org/profiles/' + muni_geoid + '">' + display_name + '</a>.</p>');
function number(value)
{
var string = value.toString();
while(string.match(/^\d+\d\d\d\b/))
{
string = string.replace(/^(\d+)(\d\d\d\b)/, '$1,$2');
}
return string;
}
function percent(numerator, denominator, precision)
{
return parseFloat((100 * numerator / denominator).toPrecision(precision));
}
function show_population(estimate, error)
{
if(error == undefined)
return number(estimate);
return number(estimate) + ' ± ' + number(error) + ' (' + percent(error, estimate, 2) + '%)';
}
function show_income(estimate, error)
{
if(error == undefined)
return '$' + number(estimate);
return '$' + number(estimate) + ' ± $' + number(error) + ' (' + percent(error, estimate, 2) + '%)';
}
function show_percentage(estimate, error)
{
if(error == undefined)
return number(estimate) + '%';
return estimate + '% ± ' + error + '%';
}
for(var i = 0; i <= tracts.length; i += chunk_size)
{
var geoids = [],
chunk_tracts = {},
slice_tracts = tracts.slice(i, i + chunk_size);
while(slice_tracts.length)
{
var tract = slice_tracts.pop();
geoids.push(tract.properties.geoid);
chunk_tracts[tract.properties.geoid] = tract;
}
var url = api_base_href+'/data/show/acs2013_5yr?table_ids=B01003,B03002,B19013,B19301,B25003&geo_ids='+geoids.join(','),
result = jQuery.ajax(url, {async: false, beforeSend: announce}),
tables = result.responseJSON.tables,
data = result.responseJSON.data;
for(var geoid in data)
{
var tract = chunk_tracts[geoid];
if(tract.properties['aland'] == 0 || tract.properties['2013_population_estimate'] == 0)
{
continue;
}
document.write('<h2><a href="http://censusreporter.org/profiles/'+geoid+'">'+tract.properties.name+'</a></h2>');
document.write('<ul>');
var total_population = data[geoid]['B01003'].estimate['B01003001'],
total_population_err = data[geoid]['B01003'].error['B01003001'],
hispanic_population = data[geoid]['B03002'].estimate['B03002012'],
hispanic_population_err = data[geoid]['B03002'].error['B03002012'],
hispanic_percent = percent(hispanic_population, total_population, 2),
hispanic_percent_err = percent(hispanic_population_err, total_population, 2),
white_population = data[geoid]['B03002'].estimate['B03002003'],
white_population_err = data[geoid]['B03002'].error['B03002003'],
white_percent = percent(white_population, total_population, 2),
white_percent_err = percent(white_population_err, total_population, 2),
black_population = data[geoid]['B03002'].estimate['B03002004'],
black_population_err = data[geoid]['B03002'].error['B03002004'],
black_percent = percent(black_population, total_population, 2),
black_percent_err = percent(black_population_err, total_population, 2),
asian_population = data[geoid]['B03002'].estimate['B03002006'],
asian_population_err = data[geoid]['B03002'].error['B03002006'],
asian_percent = percent(asian_population, total_population, 2),
asian_percent_err = percent(asian_population_err, total_population, 2),
other_population = data[geoid]['B03002'].estimate['B03002005'] + data[geoid]['B03002'].estimate['B03002007'] + data[geoid]['B03002'].estimate['B03002008'] + data[geoid]['B03002'].estimate['B03002009'],
other_population_err = undefined,
other_percent = percent(other_population, total_population, 2),
other_percent_err = undefined,
medhouse_income = data[geoid]['B19013'].estimate['B19013001'],
medhouse_income_err = data[geoid]['B19013'].error['B19013001'],
percapita_income = data[geoid]['B19301'].estimate['B19301001'],
percapita_income_err = data[geoid]['B19301'].error['B19301001'],
total_housing = data[geoid]['B25003'].estimate['B25003001'],
total_housing_err = data[geoid]['B25003'].error['B25003001'],
owner_housing = data[geoid]['B25003'].estimate['B25003002'],
owner_housing_err = data[geoid]['B25003'].error['B25003002'],
owner_percent = percent(owner_housing, total_housing, 2),
owner_percent_err = percent(owner_housing_err, total_housing, 2);
document.write('<li>Total population: ' + show_population(total_population, total_population_err) + '</li>');
document.write('<li>Hispanic population: ' + show_population(hispanic_population, hispanic_population_err) + '</li>');
document.write('<li>White population: ' + show_population(white_population, white_population_err) + '</li>');
document.write('<li>Black population: ' + show_population(black_population, black_population_err) + '</li>');
document.write('<li>Asian population: ' + show_population(asian_population, asian_population_err) + '</li>');
document.write('<li>Other population: ' + show_population(other_population, other_population_err) + '</li>');
document.write('<li>Median household income: ' + show_income(medhouse_income, medhouse_income_err) + '</li>');
document.write('<li>Per capita income: ' + show_income(percapita_income, percapita_income_err) + '</li>');
document.write('<li>Hispanic percent: ' + show_percentage(hispanic_percent, hispanic_percent_err) + '</li>');
document.write('<li>White percent: ' + show_percentage(white_percent, white_percent_err) + '</li>');
document.write('<li>Black percent: ' + show_percentage(black_percent, black_percent_err) + '</li>');
document.write('<li>Asian percent: ' + show_percentage(asian_percent, asian_percent_err) + '</li>');
document.write('<li>Other percent: ' + show_percentage(other_percent, other_percent_err) + '</li>');
document.write('<li>Total housing: ' + show_population(total_housing, total_housing_err) + '</li>');
document.write('<li>Owner-occupied percent: ' + show_percentage(owner_percent, owner_percent_err) + '</li>');
document.write('</ul>');
}
break;
}
</script>
</body>
</html>