-
Notifications
You must be signed in to change notification settings - Fork 4
/
census-reporter.js
157 lines (132 loc) · 4.72 KB
/
census-reporter.js
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
ResidentResearch = window.ResidentResearch || { }
ResidentResearch.censusReporter = function(tracts) {
tracts = tracts;
resultData = [];
var appendData = function(tract) {
tract.data =_.reduce(getDataWithId(resultData, tract.geoid), mergeData, {});
return tract;
};
var apiUrlForTableAndGeoids = function(table, geoids) {
return CR_API_BASE+'/data/show/acs2013_5yr?table_ids='+table+'&geo_ids='+geoids.join(',');
};
var populationResult = function(d, sq_km) {
var data = estimateError(d, 'B01003', 'B01003001');
return {
'B01003001': data,
'population density': densityError(data, sq_km)
};
}
var rentalResult = function(d, sq_km) {
var ownerHousing = estimateError(d, 'B25003', 'B25003002'),
total = estimateError(d, 'B25003', 'B25003001'),
rental = total.estimate - ownerHousing.estimate;
return {
'B25003002': ownerHousing,
'B25003001': total,
'rental percentage': estimateErrorValues(percentage(rental, total.estimate), undefined)
};
}
var householdIncomeResult = function(d, sq_km) {
var median = estimateError(d, 'B19013', 'B19013001');
return {
'B19013001': median
};
}
var percapitaIncomeResult = function(d, sq_km) {
var income = estimateError(d, 'B19301', 'B19301001');
return {
'B19301001': income
};
}
var racialResult = function(d, sq_km) {
var hispanic = estimateError(d, 'B03002', 'B03002012'),
white = estimateError(d, 'B03002', 'B03002003'),
black = estimateError(d, 'B03002', 'B03002004'),
asian = estimateError(d, 'B03002', 'B03002006'),
total = estimate(d, 'B03002', 'B03002001');
return {
'B03002012': hispanic,
'B03002003': white,
'B03002004': black,
'B03002006': asian,
'hispanic density': densityError(hispanic, sq_km),
'white density': densityError(white, sq_km),
'black density': densityError(black, sq_km),
'asian density': densityError(asian, sq_km),
'hispanic percentage': percentageError(hispanic, total),
'white percentage': percentageError(white, total),
'black percentage': percentageError(black, total),
'asian percentage': percentageError(asian, total)
};
}
var getTableData = function(geoIds, tableId, promise, resultCallback) {
jQuery.ajax(apiUrlForTableAndGeoids(tableId,geoIds)).done(function(response) {
promise.resolve(censusResult(response.data, resultCallback));
}).fail(function(data) {
promise.reject(data);
});
};
var censusResult = function(data, dataFunc) {
var result = _.map(data, function(d, geoId) {
var tract = _.findWhere(tracts, { geoid: geoId });
var sq_km = tract.feature.properties.aland / 1000000;
return { id: geoId, data: dataFunc(d, sq_km)};
});
return result;
};
var estimateErrorValues = function(estimateValue, errorValue) {
return { estimate: estimateValue, error: errorValue }
}
var estimateError = function(d, tableId, objectId) {
return { estimate: estimate(d, tableId, objectId), error: error(d, tableId, objectId) }
}
var estimate = function(d, tableId, objectId) {
return d[tableId].estimate[objectId];
}
var error = function(d, tableId, objectId) {
return d[tableId].error[objectId];
}
var percentageError = function(d, total) {
return estimateErrorValues(percentage(d.estimate,total), undefined );
}
var percentage = function(value, total) {
return 100 * value / total;
}
var densityError = function(d, sq_km) {
return estimateErrorValues(density(d.estimate,sq_km), density(d.error,sq_km));
}
var density = function(value, total) {
return value/total;
}
var getDataWithId = function(list, geoId) {
return _.where(list, {id: geoId })
}
var mergeData = function(obj, element) {
return _.extend(obj, element.data)
}
return {
getData: function(callback) {
var deferred = $.Deferred();
var d1 = jQuery.Deferred();
var d2 = jQuery.Deferred();
var d3 = jQuery.Deferred();
var d4 = jQuery.Deferred();
var d5 = jQuery.Deferred();
var geoids = _.map(tracts, function(tract) { return tract.geoid });
getTableData(geoids, 'B01003', d1, populationResult);
getTableData(geoids, 'B03002', d2, racialResult);
getTableData(geoids, 'B25003', d3, rentalResult);
getTableData(geoids, 'B19013', d4, householdIncomeResult);
getTableData(geoids, 'B19301', d5, percapitaIncomeResult);
$.when( d1, d2, d3, d4, d5 ).done(function ( v1, v2, v3, v4, v5 ) {
resultData = _.flatten([v1, v2, v3, v4, v5])
tracts = _.map(tracts, appendData);
if(callback) {
callback(tracts);
}
deferred.resolve(tracts);
});
return deferred;
}
}
}