@dra2020/district-analytics 1.0.6 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +102 -52
- package/dist/cli.js.map +1 -1
- package/dist/district-analytics.js +102 -52
- package/dist/district-analytics.js.map +1 -1
- package/lib/HelloWorld.d.ts +3 -0
- package/lib/HelloWorld.js +11 -0
- package/lib/_api.js +91 -0
- package/lib/_api.js.map +1 -0
- package/lib/_cli.js +434 -0
- package/lib/_cli.js.map +1 -0
- package/lib/_data.js +425 -0
- package/lib/_data.js.map +1 -0
- package/lib/analyze.d.ts +3 -0
- package/lib/analyze.js +69 -0
- package/lib/analyze.js.map +1 -0
- package/lib/api.d.ts +34 -0
- package/lib/api.js +117 -0
- package/lib/api.js.map +1 -0
- package/lib/cli.d.ts +1 -0
- package/lib/cli.js +386 -0
- package/lib/cli.js.map +1 -0
- package/lib/cohesive.d.ts +4 -0
- package/lib/cohesive.js +132 -0
- package/lib/cohesive.js.map +1 -0
- package/lib/compact.d.ts +4 -0
- package/lib/compact.js +183 -0
- package/lib/compact.js.map +1 -0
- package/lib/constants.js +367 -0
- package/lib/constants.js.map +1 -0
- package/lib/data.js +188 -0
- package/lib/data.js.map +1 -0
- package/lib/equal.d.ts +4 -0
- package/lib/equal.js +59 -0
- package/lib/equal.js.map +1 -0
- package/lib/features.js +19 -0
- package/lib/features.js.map +1 -0
- package/lib/geofeature.js +112 -0
- package/lib/geofeature.js.map +1 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.js +11 -0
- package/lib/index.js.map +1 -0
- package/lib/minority.d.ts +3 -0
- package/lib/minority.js +61 -0
- package/lib/minority.js.map +1 -0
- package/lib/political.d.ts +7 -0
- package/lib/political.js +88 -0
- package/lib/political.js.map +1 -0
- package/lib/preprocess.d.ts +4 -0
- package/lib/preprocess.js +101 -0
- package/lib/preprocess.js.map +1 -0
- package/lib/report.d.ts +14 -0
- package/lib/report.js +817 -0
- package/lib/report.js.map +1 -0
- package/lib/sample.d.ts +1 -0
- package/lib/sample.js +32 -0
- package/lib/sample.js.map +1 -0
- package/lib/scorecard.d.ts +4 -0
- package/lib/scorecard.js +237 -0
- package/lib/scorecard.js.map +1 -0
- package/lib/settings.d.ts +5 -0
- package/lib/settings.js +18 -0
- package/lib/settings.js.map +1 -0
- package/lib/types.d.ts +125 -0
- package/lib/types.js +7 -0
- package/lib/types.js.map +1 -0
- package/lib/utils.d.ts +20 -0
- package/lib/utils.js +223 -0
- package/lib/utils.js.map +1 -0
- package/lib/valid.d.ts +5 -0
- package/lib/valid.js +230 -0
- package/lib/valid.js.map +1 -0
- package/package.json +2 -2
- package/src/_api.ts +1 -0
- package/src/_data.ts +42 -19
- package/src/report.ts +59 -34
package/lib/utils.js
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//
|
|
3
|
+
// UTILITIES
|
|
4
|
+
//
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const S = require("./settings");
|
|
7
|
+
// PLAN HELPERS
|
|
8
|
+
// Is a "neighbor" in state?
|
|
9
|
+
function isInState(geoID) {
|
|
10
|
+
return geoID != S.OUT_OF_STATE;
|
|
11
|
+
}
|
|
12
|
+
exports.isInState = isInState;
|
|
13
|
+
// Is a "neighbor" out of state?
|
|
14
|
+
function isOutOfState(geoID) {
|
|
15
|
+
return geoID == S.OUT_OF_STATE;
|
|
16
|
+
}
|
|
17
|
+
exports.isOutOfState = isOutOfState;
|
|
18
|
+
// Get the districtID to which a geoID is assigned
|
|
19
|
+
function getDistrict(plan, geoID) {
|
|
20
|
+
// All geoIDs in a state *should be* assigned to a district (including the
|
|
21
|
+
// dummy "unassigned" district), but "water-only" features are sometimes missing
|
|
22
|
+
// from a map. This is also a guard against a bug in which a geoID has no district.
|
|
23
|
+
if (keyExists(geoID, plan)) {
|
|
24
|
+
return plan[geoID];
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.getDistrict = getDistrict;
|
|
31
|
+
// Invert a feature assignment structure to sets of ids by district
|
|
32
|
+
// NOTE - This is here vs. _data.ts, so it can also be used in cli.ts
|
|
33
|
+
function invertPlan(plan) {
|
|
34
|
+
let invertedPlan = {};
|
|
35
|
+
// Add a dummy 'unassigned' district
|
|
36
|
+
invertedPlan[S.NOT_ASSIGNED] = new Set();
|
|
37
|
+
for (let geoID in plan) {
|
|
38
|
+
let districtID = plan[geoID];
|
|
39
|
+
// Make sure the set for the districtID exists
|
|
40
|
+
if (!(objectContains(invertedPlan, districtID))) {
|
|
41
|
+
invertedPlan[districtID] = new Set();
|
|
42
|
+
}
|
|
43
|
+
// Add the geoID to the districtID's set
|
|
44
|
+
invertedPlan[districtID].add(geoID);
|
|
45
|
+
}
|
|
46
|
+
return invertedPlan;
|
|
47
|
+
}
|
|
48
|
+
exports.invertPlan = invertPlan;
|
|
49
|
+
// WORKING WITH GEOIDS
|
|
50
|
+
function parseGeoID(geoID) {
|
|
51
|
+
let parts = {};
|
|
52
|
+
parts['state'] = geoID.substring(0, 2);
|
|
53
|
+
parts['county'] = geoID.substring(0, 5);
|
|
54
|
+
let l = geoID.length;
|
|
55
|
+
if (l >= 11) {
|
|
56
|
+
parts['tract'] = geoID.substring(0, 11);
|
|
57
|
+
}
|
|
58
|
+
if (l >= 12) {
|
|
59
|
+
parts['bg'] = geoID.substring(0, 12);
|
|
60
|
+
}
|
|
61
|
+
if (l == 15) {
|
|
62
|
+
parts['block'] = geoID;
|
|
63
|
+
}
|
|
64
|
+
return parts;
|
|
65
|
+
}
|
|
66
|
+
exports.parseGeoID = parseGeoID;
|
|
67
|
+
function getFIPSFromCountyGeoID(geoID) {
|
|
68
|
+
return geoID.substring(2, 5);
|
|
69
|
+
}
|
|
70
|
+
exports.getFIPSFromCountyGeoID = getFIPSFromCountyGeoID;
|
|
71
|
+
// NORMALIZING RESULTS
|
|
72
|
+
// Convert a raw score [0-1] into a normalized score [0-100]
|
|
73
|
+
function normalize(rawScore, scale, invertp = false) {
|
|
74
|
+
// Invert the axis if necessary to make bigger = better
|
|
75
|
+
if (invertp) {
|
|
76
|
+
rawScore = 1.0 - rawScore;
|
|
77
|
+
}
|
|
78
|
+
// Coerce the value to be w/in the given range
|
|
79
|
+
let rangeMin = scale[0];
|
|
80
|
+
let rangeMax = scale[1];
|
|
81
|
+
let coercedValue = Math.min(Math.max(rawScore, rangeMin), rangeMax);
|
|
82
|
+
// Scale the bounded value w/in the range [0 - (rangeMax - rangeMin)]
|
|
83
|
+
let scaledValue = (coercedValue - rangeMin) / (rangeMax - rangeMin);
|
|
84
|
+
// Finally, make the range [0-100]
|
|
85
|
+
return Math.round(scaledValue * S.NORMALIZED_RANGE);
|
|
86
|
+
}
|
|
87
|
+
exports.normalize = normalize;
|
|
88
|
+
// Round a fractional number [0-1] to the desired level of PRECISION.
|
|
89
|
+
function trim(fullFraction) {
|
|
90
|
+
let shiftPlaces = Math.pow(10, S.PRECISION);
|
|
91
|
+
return Math.round(fullFraction * shiftPlaces) / shiftPlaces;
|
|
92
|
+
}
|
|
93
|
+
exports.trim = trim;
|
|
94
|
+
// ARRAY HELPERS
|
|
95
|
+
function sumArray(arr) {
|
|
96
|
+
return arr.reduce((a, b) => a + b, 0);
|
|
97
|
+
}
|
|
98
|
+
exports.sumArray = sumArray;
|
|
99
|
+
function avgArray(arr) {
|
|
100
|
+
return (arr.reduce((a, b) => a + b, 0)) / arr.length;
|
|
101
|
+
}
|
|
102
|
+
exports.avgArray = avgArray;
|
|
103
|
+
function minArray(arr) {
|
|
104
|
+
return Math.min(...arr);
|
|
105
|
+
}
|
|
106
|
+
exports.minArray = minArray;
|
|
107
|
+
function maxArray(arr) {
|
|
108
|
+
return Math.max(...arr);
|
|
109
|
+
}
|
|
110
|
+
exports.maxArray = maxArray;
|
|
111
|
+
function initArray(n, value) {
|
|
112
|
+
return Array.from(Array(n), () => value);
|
|
113
|
+
}
|
|
114
|
+
exports.initArray = initArray;
|
|
115
|
+
function andArray(arr) {
|
|
116
|
+
return arr.reduce(function (a, b) { return a && b; }, true);
|
|
117
|
+
}
|
|
118
|
+
exports.andArray = andArray;
|
|
119
|
+
// WORKING WITH OBJECT KEYS/PROPERTIES
|
|
120
|
+
// TODO - Terry, is this copesetic?
|
|
121
|
+
// Does an object have a key/property?
|
|
122
|
+
function keyExists(k, o) {
|
|
123
|
+
return k in o;
|
|
124
|
+
}
|
|
125
|
+
exports.keyExists = keyExists;
|
|
126
|
+
// TODO - Terry, can these three be combined into a generic isEmpty() check?
|
|
127
|
+
// Does an object (dict) have any keys/properties?
|
|
128
|
+
function isObjectEmpty(o) {
|
|
129
|
+
return Object.keys(o).length === 0;
|
|
130
|
+
}
|
|
131
|
+
exports.isObjectEmpty = isObjectEmpty;
|
|
132
|
+
// Does a Set have any members?
|
|
133
|
+
function isSetEmpty(s) {
|
|
134
|
+
return s.size === 0;
|
|
135
|
+
}
|
|
136
|
+
exports.isSetEmpty = isSetEmpty;
|
|
137
|
+
// Does an array hold any items?
|
|
138
|
+
function isArrayEmpty(a) {
|
|
139
|
+
if (a === undefined || a.length == 0) {
|
|
140
|
+
// array empty or does not exist
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
exports.isArrayEmpty = isArrayEmpty;
|
|
148
|
+
// Get the keys for an object
|
|
149
|
+
function getObjectKeys(o) {
|
|
150
|
+
return Object.keys(o);
|
|
151
|
+
}
|
|
152
|
+
exports.getObjectKeys = getObjectKeys;
|
|
153
|
+
// TODO - Convert getNumericObjectKeys() idiom to for..of where possible
|
|
154
|
+
function getNumericObjectKeys(o) {
|
|
155
|
+
return Object.keys(o).map(Number);
|
|
156
|
+
}
|
|
157
|
+
exports.getNumericObjectKeys = getNumericObjectKeys;
|
|
158
|
+
function getSelectObjectKeys(o, v) {
|
|
159
|
+
let selectKeys = [];
|
|
160
|
+
Object.keys(o).forEach(key => {
|
|
161
|
+
if (arrayContains(v, o[key])) {
|
|
162
|
+
selectKeys.push(key);
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
return selectKeys;
|
|
166
|
+
}
|
|
167
|
+
exports.getSelectObjectKeys = getSelectObjectKeys;
|
|
168
|
+
function arrayContains(a, item) {
|
|
169
|
+
return a.some(x => x === item);
|
|
170
|
+
}
|
|
171
|
+
exports.arrayContains = arrayContains;
|
|
172
|
+
function objectContains(o, key) {
|
|
173
|
+
return (key in o);
|
|
174
|
+
}
|
|
175
|
+
exports.objectContains = objectContains;
|
|
176
|
+
// ENUM HELPERS
|
|
177
|
+
// Source: https://stackoverflow.com/questions/38034673/determine-the-number-of-enum-elements-typescript
|
|
178
|
+
function countEnumValues(enumName) {
|
|
179
|
+
let count = 0;
|
|
180
|
+
for (let item in enumName) {
|
|
181
|
+
if (isNaN(Number(item)))
|
|
182
|
+
count++;
|
|
183
|
+
}
|
|
184
|
+
return count;
|
|
185
|
+
}
|
|
186
|
+
exports.countEnumValues = countEnumValues;
|
|
187
|
+
// COPYING - Copied from dra-client/util.ts
|
|
188
|
+
function shallowCopy(src) {
|
|
189
|
+
if (Array.isArray(src))
|
|
190
|
+
return src.slice();
|
|
191
|
+
else if (typeof src === 'object') {
|
|
192
|
+
let dst = {};
|
|
193
|
+
for (var p in src)
|
|
194
|
+
if (src.hasOwnProperty(p))
|
|
195
|
+
dst[p] = src[p];
|
|
196
|
+
return dst;
|
|
197
|
+
}
|
|
198
|
+
else
|
|
199
|
+
return src;
|
|
200
|
+
}
|
|
201
|
+
exports.shallowCopy = shallowCopy;
|
|
202
|
+
function deepCopy(src) {
|
|
203
|
+
if (Array.isArray(src)) {
|
|
204
|
+
let dst = [];
|
|
205
|
+
for (let i = 0; i < src.length; i++)
|
|
206
|
+
dst.push(deepCopy(src[i]));
|
|
207
|
+
return dst;
|
|
208
|
+
}
|
|
209
|
+
else if (typeof src === 'object') {
|
|
210
|
+
let dst = {};
|
|
211
|
+
for (var p in src)
|
|
212
|
+
if (src.hasOwnProperty(p))
|
|
213
|
+
dst[p] = deepCopy(src[p]);
|
|
214
|
+
return dst;
|
|
215
|
+
}
|
|
216
|
+
else
|
|
217
|
+
return src;
|
|
218
|
+
}
|
|
219
|
+
exports.deepCopy = deepCopy;
|
|
220
|
+
// TODO - Terry: What is this the simple explanation of what this thing is doing?
|
|
221
|
+
var util_1 = require("@dra2020/util");
|
|
222
|
+
exports.depthof = util_1.depthof;
|
|
223
|
+
//# sourceMappingURL=utils.js.map
|
package/lib/utils.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";AAAA,EAAE;AACF,YAAY;AACZ,EAAE;;AAGF,gCAAgC;AAEhC,eAAe;AAEf,4BAA4B;AAC5B,SAAgB,SAAS,CAAC,KAAa;IACrC,OAAO,KAAK,IAAI,CAAC,CAAC,YAAY,CAAC;AACjC,CAAC;AAFD,8BAEC;AAED,gCAAgC;AAChC,SAAgB,YAAY,CAAC,KAAa;IACxC,OAAO,KAAK,IAAI,CAAC,CAAC,YAAY,CAAC;AACjC,CAAC;AAFD,oCAEC;AAED,kDAAkD;AAClD,SAAgB,WAAW,CAAC,IAAmB,EAAE,KAAa;IAC5D,0EAA0E;IAC1E,gFAAgF;IAChF,mFAAmF;IACnF,IAAI,SAAS,CAAC,KAAK,EAAE,IAAI,CAAC,EAAE;QAC1B,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC;KACpB;SACI;QACH,OAAO,SAAS,CAAC;KAClB;AACH,CAAC;AAVD,kCAUC;AAED,mEAAmE;AACnE,qEAAqE;AACrE,SAAgB,UAAU,CAAC,IAAmB;IAC5C,IAAI,YAAY,GAAG,EAAwB,CAAC;IAE5C,oCAAoC;IACpC,YAAY,CAAC,CAAC,CAAC,YAAY,CAAC,GAAG,IAAI,GAAG,EAAE,CA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ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import * as T from './types';
|
|
2
|
+
import { AnalyticsSession } from './api';
|
|
3
|
+
export declare function doIsComplete(s: AnalyticsSession): T.TestEntry;
|
|
4
|
+
export declare function doIsContiguous(s: AnalyticsSession): T.TestEntry;
|
|
5
|
+
export declare function doIsFreeOfHoles(s: AnalyticsSession): T.TestEntry;
|
package/lib/valid.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//
|
|
3
|
+
// MAP/PLAN VALIDATIONS
|
|
4
|
+
//
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const U = require("./utils");
|
|
7
|
+
const S = require("./settings");
|
|
8
|
+
const D = require("./_data");
|
|
9
|
+
//
|
|
10
|
+
// COMPLETE - Are all geo's assigned to a district, and do all districts have
|
|
11
|
+
// at least one geo assigned to them?
|
|
12
|
+
//
|
|
13
|
+
function doIsComplete(s) {
|
|
14
|
+
let test = s.getTest(0 /* Complete */);
|
|
15
|
+
let bAllAssigned = true;
|
|
16
|
+
let bNoneEmpty = true;
|
|
17
|
+
let unassignedFeatures = [];
|
|
18
|
+
let emptyDistricts = [];
|
|
19
|
+
// Get the by district results, including the dummy unassigned district,
|
|
20
|
+
// but ignoring the N+1 summary district
|
|
21
|
+
let bNotEmptyByDistrict = s.districts.statistics[D.DistrictField.bNotEmpty];
|
|
22
|
+
bNotEmptyByDistrict = bNotEmptyByDistrict.slice(0, -1);
|
|
23
|
+
// Are all features assigned to districts?
|
|
24
|
+
// Check the dummy district that holds any unassigned features.
|
|
25
|
+
bAllAssigned = (!bNotEmptyByDistrict[S.NOT_ASSIGNED]);
|
|
26
|
+
if (!bAllAssigned) {
|
|
27
|
+
let unassignedDistrict = s.plan.geoIDsForDistrictID(S.NOT_ASSIGNED);
|
|
28
|
+
unassignedFeatures = Array.from(unassignedDistrict);
|
|
29
|
+
unassignedFeatures = unassignedFeatures.slice(0, S.NUMBER_OF_ITEMS_TO_REPORT);
|
|
30
|
+
}
|
|
31
|
+
// Do all real districts have at least one feature assigned to them?
|
|
32
|
+
bNoneEmpty = U.andArray(bNotEmptyByDistrict.slice(1));
|
|
33
|
+
// Case 1 - One or more districts are missing:
|
|
34
|
+
// The # of enumerated districts minus the dummy NOT_ASSIGNED one should
|
|
35
|
+
// equal the number apportioned districts. This guards against a district
|
|
36
|
+
// not being included in a map that is imported.
|
|
37
|
+
//
|
|
38
|
+
// TODO - I'm no longer checking for this, but DRA should!
|
|
39
|
+
// Case 2 - Or a district is explicitly named but empty:
|
|
40
|
+
// Note, this can happen if a district is created, and then all features
|
|
41
|
+
// are removed from it (in DRA).
|
|
42
|
+
// Populate the test entry
|
|
43
|
+
test['score'] = bAllAssigned && bNoneEmpty;
|
|
44
|
+
if (!bAllAssigned) {
|
|
45
|
+
test['details']['unassignedFeatures'] = unassignedFeatures;
|
|
46
|
+
}
|
|
47
|
+
if (!bNoneEmpty) {
|
|
48
|
+
test['details']['emptyDistricts'] = emptyDistricts;
|
|
49
|
+
}
|
|
50
|
+
// Populate the N+1 summary "district" in district.statistics
|
|
51
|
+
let bNotEmpty = s.districts.statistics[D.DistrictField.bNotEmpty];
|
|
52
|
+
let summaryRow = s.districts.numberOfRows() - 1;
|
|
53
|
+
bNotEmpty[summaryRow] = test['score'];
|
|
54
|
+
return test;
|
|
55
|
+
}
|
|
56
|
+
exports.doIsComplete = doIsComplete;
|
|
57
|
+
//
|
|
58
|
+
// CONTIGUOUS - Is each district in a plan fully connected?
|
|
59
|
+
//
|
|
60
|
+
// NOTE - To check "operational contiguity," we need to use a graph, i.e.,
|
|
61
|
+
// we can't rely on just the geometric contiguity of shapes in a shapefile.
|
|
62
|
+
//
|
|
63
|
+
// To test this, load the NC 2010 map 'SAMPLE-BG-map-discontiguous.csv'.
|
|
64
|
+
//
|
|
65
|
+
function doIsContiguous(s) {
|
|
66
|
+
let test = s.getTest(1 /* Contiguous */);
|
|
67
|
+
// Get the contiguity of each district. Ignore dummy unassigned district
|
|
68
|
+
// and the N+1 summary district.
|
|
69
|
+
let bContiguousByDistrict = s.districts.statistics[D.DistrictField.bContiguous];
|
|
70
|
+
bContiguousByDistrict = bContiguousByDistrict.slice(1, -1);
|
|
71
|
+
// If any real districts aren't contiguous, mark the plan as not contiguous
|
|
72
|
+
let bMapContiguous = U.andArray(bContiguousByDistrict);
|
|
73
|
+
// If the map is not contiguous, log the offending districts.
|
|
74
|
+
let discontiguousDistricts = [];
|
|
75
|
+
let districtID = 1;
|
|
76
|
+
bContiguousByDistrict.forEach(function (bDistrictContiguous) {
|
|
77
|
+
if (!bDistrictContiguous)
|
|
78
|
+
discontiguousDistricts.push(districtID);
|
|
79
|
+
districtID += 1;
|
|
80
|
+
});
|
|
81
|
+
// Populate the test entry
|
|
82
|
+
test['score'] = bMapContiguous;
|
|
83
|
+
if (!bMapContiguous) {
|
|
84
|
+
test['details'] = { 'discontiguousDistricts': discontiguousDistricts };
|
|
85
|
+
}
|
|
86
|
+
// Populate the N+1 summary "district" in district.statistics
|
|
87
|
+
let bContiguous = s.districts.statistics[D.DistrictField.bContiguous];
|
|
88
|
+
let summaryRow = s.districts.numberOfRows() - 1;
|
|
89
|
+
bContiguous[summaryRow] = test['score'];
|
|
90
|
+
return test;
|
|
91
|
+
}
|
|
92
|
+
exports.doIsContiguous = doIsContiguous;
|
|
93
|
+
// Are the features in a district fully connected?
|
|
94
|
+
function isConnected(districtGeos, graph) {
|
|
95
|
+
// export function isConnected(districtGeos: Set<string>, graph: T.ContiguityGraph): boolean {
|
|
96
|
+
// TODO - Terry, why does this constructor need a <T> type specification?
|
|
97
|
+
let visited = new Set();
|
|
98
|
+
let toProcess = [];
|
|
99
|
+
// Start processing with the first geoID in the district
|
|
100
|
+
let iter = districtGeos.values();
|
|
101
|
+
toProcess.push(iter.next().value);
|
|
102
|
+
// While there are geoIDs in the district that haven't been processed
|
|
103
|
+
while (toProcess.length > 0) {
|
|
104
|
+
// Grab a geoID and process it
|
|
105
|
+
let node = toProcess.pop();
|
|
106
|
+
visited.add(node);
|
|
107
|
+
// Get its actual, in-state neighbors
|
|
108
|
+
let actualNeighbors = graph.peerNeighbors(node).filter(x => U.isInState(x));
|
|
109
|
+
// Add neighbors to visit, if they're in the same district Y haven't already been visited
|
|
110
|
+
let neighborsToVisit = actualNeighbors.filter(x => districtGeos.has(x) && (!visited.has(x)));
|
|
111
|
+
// TODO - Terry, is this the quickest/best way to do this?
|
|
112
|
+
toProcess.push(...neighborsToVisit);
|
|
113
|
+
}
|
|
114
|
+
// Stop when you've visited all the geoIDs in the district
|
|
115
|
+
return visited.size == districtGeos.size;
|
|
116
|
+
}
|
|
117
|
+
exports.isConnected = isConnected;
|
|
118
|
+
//
|
|
119
|
+
// FREE OF HOLES - Are any districts fully embedded w/in another district?
|
|
120
|
+
//
|
|
121
|
+
// A district is NOT a "donut hole" district:
|
|
122
|
+
// - If any neighbor is 'OUT_OF_STATE'; or
|
|
123
|
+
// - If there are 2 or more neighboring districts.
|
|
124
|
+
//
|
|
125
|
+
// To test this, load the NC 2010 map 'SAMPLE-BG-map-hole.csv'. District 1,
|
|
126
|
+
// Buncombe County (37021), is a donut hole w/in District 3.
|
|
127
|
+
//
|
|
128
|
+
// TODO - Optimize this to take advantage of district boundary info, if/when
|
|
129
|
+
// we cache one to optimize compactness.
|
|
130
|
+
//
|
|
131
|
+
function doIsFreeOfHoles(s) {
|
|
132
|
+
let test = s.getTest(2 /* FreeOfHoles */);
|
|
133
|
+
// Initialize values
|
|
134
|
+
let bFreeOfHoles = true;
|
|
135
|
+
let embeddedDistricts = [];
|
|
136
|
+
// Get the embeddedness of each district. Ignore dummy unassigned district
|
|
137
|
+
// and the N+1 summary district.
|
|
138
|
+
let bNotEmbeddedByDistrict = s.districts.statistics[D.DistrictField.bNotEmbedded];
|
|
139
|
+
bNotEmbeddedByDistrict = bNotEmbeddedByDistrict.slice(1, -1);
|
|
140
|
+
let districtID = 1;
|
|
141
|
+
bNotEmbeddedByDistrict.forEach(function (bDistrictNotEmbedded) {
|
|
142
|
+
if (!bDistrictNotEmbedded) {
|
|
143
|
+
embeddedDistricts.push(districtID);
|
|
144
|
+
bFreeOfHoles = false;
|
|
145
|
+
}
|
|
146
|
+
districtID += 1;
|
|
147
|
+
});
|
|
148
|
+
// Populate the test entry
|
|
149
|
+
test['score'] = bFreeOfHoles;
|
|
150
|
+
if (!bFreeOfHoles) {
|
|
151
|
+
test['details'] = { 'embeddedDistricts': embeddedDistricts };
|
|
152
|
+
}
|
|
153
|
+
// Populate the N+1 summary "district" in district.statistics
|
|
154
|
+
let bNotEmbedded = s.districts.statistics[D.DistrictField.bNotEmbedded];
|
|
155
|
+
let summaryRow = s.districts.numberOfRows() - 1;
|
|
156
|
+
bNotEmbedded[summaryRow] = test['score'];
|
|
157
|
+
return test;
|
|
158
|
+
}
|
|
159
|
+
exports.doIsFreeOfHoles = doIsFreeOfHoles;
|
|
160
|
+
// Test whether one district is embedded w/in any other.
|
|
161
|
+
function isEmbedded(districtID, geoIDs, plan, graph) {
|
|
162
|
+
// TODO - Make "features" = "geoIDs." These aren't "features" proper, just
|
|
163
|
+
// identifier strings.
|
|
164
|
+
let features = geoIDs;
|
|
165
|
+
let planByGeo = plan.byGeoID();
|
|
166
|
+
// Assume the district is embedded
|
|
167
|
+
let bEmbedded = true;
|
|
168
|
+
// Keep track of the neighoring districts
|
|
169
|
+
let neighboringDistricts = new Set();
|
|
170
|
+
// TODO - Use just the boundary features, when available
|
|
171
|
+
// Get the features for the real district
|
|
172
|
+
let featuresToCheck = Array.from(features);
|
|
173
|
+
// If the district has features, check whether it is embedded
|
|
174
|
+
if (!(U.isArrayEmpty(featuresToCheck))) {
|
|
175
|
+
// For each feature that needs to be checked (see above)
|
|
176
|
+
for (let feature of featuresToCheck) {
|
|
177
|
+
// Get its neighbors (including the virtual "out of state" ones)
|
|
178
|
+
let neighbors = graph.peerNeighbors(feature);
|
|
179
|
+
for (let neighbor of neighbors) {
|
|
180
|
+
if (U.isOutOfState(neighbor)) {
|
|
181
|
+
bEmbedded = false;
|
|
182
|
+
// No need to check any more neighbors
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
let neighboringDistrict = U.getDistrict(planByGeo, neighbor);
|
|
187
|
+
// Assume that a missing district assignment (= None) means that the
|
|
188
|
+
// feature is "water-only" AND part of the state border (vs. internal)
|
|
189
|
+
// and, therefore, not in the plan/map.
|
|
190
|
+
if (neighboringDistrict == undefined) {
|
|
191
|
+
bEmbedded = false;
|
|
192
|
+
// No need to check any more neighbors
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
// TODO - Since we're checking *all* features in a district right
|
|
197
|
+
// now, not just boundary features and neighbors in other districts,
|
|
198
|
+
// prune out the current district. If/when we optimize compactness
|
|
199
|
+
// to cache district boundaries (as before in my Python implementation),
|
|
200
|
+
// we won't have to guard adding "neighboring" districts in this way.
|
|
201
|
+
if (neighboringDistrict != districtID) {
|
|
202
|
+
neighboringDistricts.add(neighboringDistrict);
|
|
203
|
+
}
|
|
204
|
+
if (neighboringDistricts.size > 1) {
|
|
205
|
+
bEmbedded = false;
|
|
206
|
+
// No need to check any more neighbors
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
// If a district is not embedded, there's no need to check anymore
|
|
213
|
+
// border geos.
|
|
214
|
+
if (!bEmbedded) {
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return bEmbedded;
|
|
220
|
+
}
|
|
221
|
+
exports.isEmbedded = isEmbedded;
|
|
222
|
+
// TODO - MIXED MAPS
|
|
223
|
+
// - When we generalize to mixed maps, the determination of "neighbors in
|
|
224
|
+
// the map" -- which is the core function in determining connectedness -- becomes
|
|
225
|
+
// *much* more complicated and dynamic.
|
|
226
|
+
// - I can write up (if not implement) the logic for this. It is a bit tricky
|
|
227
|
+
// and requires special preprocessing of the summary level hierarchy (which I
|
|
228
|
+
// also have a script for that we can repurpose) to distinguish between 'interior'
|
|
229
|
+
// and 'edge' children.
|
|
230
|
+
//# sourceMappingURL=valid.js.map
|
package/lib/valid.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"valid.js","sourceRoot":"","sources":["../src/valid.ts"],"names":[],"mappings":";AAAA,EAAE;AACF,uBAAuB;AACvB,EAAE;;AAGF,6BAA6B;AAC7B,gCAAgC;AAEhC,6BAA4B;AAG5B,EAAE;AACF,6EAA6E;AAC7E,uCAAuC;AACvC,EAAE;AAEF,SAAgB,YAAY,CAAC,CAAmB;IAC9C,IAAI,IAAI,GAAG,CAAC,CAAC,OAAO,kBAAgC,CAAC;IAErD,IAAI,YAAY,GAAG,IAAI,CAAC;IACxB,IAAI,UAAU,GAAG,IAAI,CAAC;IAEtB,IAAI,kBAAkB,GAAa,EAAE,CAAC;IACtC,IAAI,cAAc,GAAa,EAAE,CAAC;IAElC,wEAAwE;IACxE,wCAAwC;IACxC,IAAI,mBAAmB,GAAG,CAAC,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC;IAC5E,mBAAmB,GAAG,mBAAmB,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IAEvD,0CAA0C;IAC1C,+DAA+D;IAC/D,YAAY,GAAG,CAAC,CAAC,mBAAmB,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC;IACtD,IAAI,CAAC,YAAY,EAAE;QACjB,IAAI,kBAAkB,GAAG,CAAC,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC;QACpE,kBAAkB,GAAG,KAAK,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAC;QACpD,kBAAkB,GAAG,kBAAkB,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,yBAAyB,C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
|
|
1
1
|
{
|
|
2
2
|
"name": "@dra2020/district-analytics",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "District analytics TypeScript package",
|
|
5
5
|
"main": "dist/district-analytics.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"tslint": "^5.18.0",
|
|
41
41
|
"tslint-config-prettier": "^1.18.0",
|
|
42
42
|
"typescript": "^3.5.3",
|
|
43
|
-
"webpack": "^4.39.
|
|
43
|
+
"webpack": "^4.39.3",
|
|
44
44
|
"webpack-cli": "^3.3.7",
|
|
45
45
|
"yargs": "^12.0.5"
|
|
46
46
|
},
|
package/src/_api.ts
CHANGED
package/src/_data.ts
CHANGED
|
@@ -195,13 +195,31 @@ export class Districts {
|
|
|
195
195
|
repVotes += outerThis._session.features.fieldForFeature(f, Dataset.ELECTION, FeatureField.RepVotes);
|
|
196
196
|
|
|
197
197
|
// Voting-age demographic breakdowns (or citizen voting-age)
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
198
|
+
// Guard againt null/NaN values
|
|
199
|
+
let _totalVAP = outerThis._session.features.fieldForFeature(f, Dataset.VAP, FeatureField.TotalPop);
|
|
200
|
+
let _whitePop = outerThis._session.features.fieldForFeature(f, Dataset.VAP, FeatureField.WhitePop);
|
|
201
|
+
let _blackPop = outerThis._session.features.fieldForFeature(f, Dataset.VAP, FeatureField.BlackPop);
|
|
202
|
+
let _hispanicPop = outerThis._session.features.fieldForFeature(f, Dataset.VAP, FeatureField.HispanicPop);
|
|
203
|
+
let _pacificPop = outerThis._session.features.fieldForFeature(f, Dataset.VAP, FeatureField.PacificPop);
|
|
204
|
+
let _asianPop = outerThis._session.features.fieldForFeature(f, Dataset.VAP, FeatureField.AsianPop);
|
|
205
|
+
let _nativePop = outerThis._session.features.fieldForFeature(f, Dataset.VAP, FeatureField.NativePop);
|
|
206
|
+
|
|
207
|
+
if (_totalVAP) totalVAP += _totalVAP;
|
|
208
|
+
if (_whitePop) whitePop += _whitePop;
|
|
209
|
+
if (_blackPop) blackPop += _blackPop;
|
|
210
|
+
if (_hispanicPop) hispanicPop += _hispanicPop;
|
|
211
|
+
if (_pacificPop) pacificPop += _pacificPop;
|
|
212
|
+
if (_asianPop) asianPop += _asianPop;
|
|
213
|
+
if (_nativePop) nativePop += _nativePop;
|
|
214
|
+
|
|
215
|
+
// TODO - DELETE
|
|
216
|
+
// totalVAP += outerThis._session.features.fieldForFeature(f, Dataset.VAP, FeatureField.TotalPop);
|
|
217
|
+
// whitePop += outerThis._session.features.fieldForFeature(f, Dataset.VAP, FeatureField.WhitePop);
|
|
218
|
+
// blackPop += outerThis._session.features.fieldForFeature(f, Dataset.VAP, FeatureField.BlackPop);
|
|
219
|
+
// hispanicPop += outerThis._session.features.fieldForFeature(f, Dataset.VAP, FeatureField.HispanicPop);
|
|
220
|
+
// pacificPop += outerThis._session.features.fieldForFeature(f, Dataset.VAP, FeatureField.PacificPop);
|
|
221
|
+
// asianPop += outerThis._session.features.fieldForFeature(f, Dataset.VAP, FeatureField.AsianPop);
|
|
222
|
+
// nativePop += outerThis._session.features.fieldForFeature(f, Dataset.VAP, FeatureField.NativePop);
|
|
205
223
|
|
|
206
224
|
// 4 - MORE ...
|
|
207
225
|
});
|
|
@@ -220,9 +238,9 @@ export class Districts {
|
|
|
220
238
|
// Total two-party (not total total!) votes, Democratic and Republican vote
|
|
221
239
|
// shares, and Democratic first-past-the-post win (= 1) or loss (= 0).
|
|
222
240
|
let totVotes: number;
|
|
223
|
-
let demPct: number
|
|
224
|
-
let repPct: number
|
|
225
|
-
let DemSeat: number
|
|
241
|
+
let demPct: number = 0;
|
|
242
|
+
let repPct: number = 0;
|
|
243
|
+
let DemSeat: number = 0;
|
|
226
244
|
|
|
227
245
|
totVotes = demVotes + repVotes;
|
|
228
246
|
if (totVotes > 0) {
|
|
@@ -235,13 +253,13 @@ export class Districts {
|
|
|
235
253
|
let minorityPop = totalVAP - whitePop
|
|
236
254
|
|
|
237
255
|
// Voting-age demographic proportions (or citizen voting-age)
|
|
238
|
-
let whitePct: number
|
|
239
|
-
let minorityPct: number
|
|
240
|
-
let blackPct: number
|
|
241
|
-
let hispanicPct: number
|
|
242
|
-
let pacificPct: number
|
|
243
|
-
let asianPct: number
|
|
244
|
-
let nativePct: number
|
|
256
|
+
let whitePct: number = 0;
|
|
257
|
+
let minorityPct: number = 0;
|
|
258
|
+
let blackPct: number = 0;
|
|
259
|
+
let hispanicPct: number = 0;
|
|
260
|
+
let pacificPct: number = 0;
|
|
261
|
+
let asianPct: number = 0;
|
|
262
|
+
let nativePct: number = 0;
|
|
245
263
|
|
|
246
264
|
if (totalVAP > 0) {
|
|
247
265
|
whitePct = whitePop / totalVAP;
|
|
@@ -380,9 +398,13 @@ export type DatasetKeys = {
|
|
|
380
398
|
export const DatasetDescriptions: any = {
|
|
381
399
|
D16F: "2016 ACS Total Population",
|
|
382
400
|
D16T: "2016 ACS Voting Age Population",
|
|
383
|
-
E16GPR: "2016 Presidential Election"
|
|
401
|
+
E16GPR: "2016 Presidential Election",
|
|
402
|
+
D10F: "2010 Census Total Population",
|
|
403
|
+
D10T: "2010 Voting Age Population",
|
|
404
|
+
C16GCO: "2016 Presidential, US Senate, Governor, and AG election results"
|
|
384
405
|
|
|
385
406
|
// TODO - What other potential datasets?
|
|
407
|
+
// MORE ...
|
|
386
408
|
}
|
|
387
409
|
|
|
388
410
|
// Identifiers of fields for each feature in the datasets
|
|
@@ -416,7 +438,8 @@ export class Features {
|
|
|
416
438
|
}
|
|
417
439
|
nFeatures(): number { return this._data.features.length; }
|
|
418
440
|
featureByIndex(i: number): T.GeoFeature { return this._data.features[i]; }
|
|
419
|
-
|
|
441
|
+
// TODO - Generalize this
|
|
442
|
+
geoIDForFeature(f: any): string { return f.properties['GEOID10']; }
|
|
420
443
|
fieldForFeature(f: any, dt: Dataset, fk: string): any {
|
|
421
444
|
let dk: string = this._keys[dt];
|
|
422
445
|
|