-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
193 lines (159 loc) · 4.96 KB
/
index.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import mapboxgl from "mapbox-gl";
// import { feature } from "topojson";
// import { json } from "d3-request";
// import { schemeCategory10 as colors } from "d3-scale";
// import { range } from "d3-array";
// import { select } from "d3-selection";
import * as d3 from "d3";
var digitProperties = ["digit_one", "digit_two", "digit_three", "digit_four"];
var testPostcodes = [
[null, null, null, null], // 0
[3 , null, null, null], // 1
[3 , 1 , null, null], // 2
[3 , 1 , 9 , null], // 3
[3 , 1 , 9 , 6] , // 4
];
d3.json("bounds.json", function(error, bounds) {
var map = initMap();
window.map = map;
map.style.on("load", function() {
var postcodeIndex = 2;
var addedLayers = false;
d3.select(".postcode").on("input", function() {
if (addedLayers) clearMapLayers(map);
var value = d3.select(this).property("value").split("");
var postcode = [ null, null, null, null ];
for (var valueIndex = 0; valueIndex < value.length && valueIndex < 4; valueIndex++) {
postcode[valueIndex] = +value[valueIndex];
}
console.log(postcode);
// debugger
var layers = buildLayers(buildFilters(postcode));
// debugger
setMapLayers(map, layers);
var bound = getBounds(bounds, postcode);
console.log(layers);
if (bound.infos.length) {
map.fitBounds(bound.bounds);
addedLayers = true;
}
});
// var interval = setInterval(function() {
// if (addedLayers) clearMapLayers(map);
// if (postcodeIndex === testPostcodes.length - 1) {
// clearInterval(interval);
// }
// var postcode = testPostcodes[postcodeIndex];
// var layers = buildLayers(buildFilters(postcode));
// setMapLayers(map, layers);
// var bound = getBounds(bounds, postcode);
// map.fitBounds(bound.bounds);
// addedLayers = true;
// postcodeIndex++;
// }, 5000);
});
});
function clearMapLayers(map) {
d3.range(10).map(function(id) {
try {
map.removeLayer(id.toString());
} catch (e) {
}
});
}
function setMapLayers(map, layers) {
layers.forEach(function(layer) {
map.addLayer(layer, "Roads");
});
}
function buildLayers(filters) {
return filters.map(function(filter, filterIndex) {
return {
"id": filterIndex.toString(),
"source": "mapbox://arrayjam.88b94e49",
"source-layer": "POA_2011_AUST_DIGITS",
"type": "fill",
"paint": {
"fill-color": d3.schemeCategory10[filterIndex],
"fill-opacity": 0.6,
},
"filter": filter,
};
});
}
function buildFilters(postcode) {
var baseFilter = ["all"];
// Build filter for existing digits
var digitIndex;
for (digitIndex = 0; digitIndex < postcode.join("").length; digitIndex++) {
var digitKey = digitProperties[digitIndex];
var digit = postcode[digitIndex];
baseFilter.push([ "==", digitKey, digit ]);
}
if (digitIndex === postcode.length) {
return [ baseFilter ];
} else {
// Return 10 filters with existing filter + each digit
return d3.range(10).map(function(digit) {
var digitFilter = baseFilter.slice();
var digitKey = digitProperties[digitIndex];
if (digitKey) {
digitFilter.push([ "==", digitKey, digit ]);
}
return digitFilter;
});
}
}
function getBounds(bounds, postcode) {
var filteredBoundsInfos = bounds.filter(function(bound) {
var result = true;
for (var postcodeIndex = 0; postcodeIndex < 4; postcodeIndex++) {
var digit = postcode[postcodeIndex];
if (digit !== null) {
if (bound[digitProperties[postcodeIndex]] !== digit) {
result = false;
break;
}
}
}
return result;
});
var result = {};
var extent = mergeBounds(filteredBoundsInfos);
extent = makeEnvelope(extent);
result.infos = filteredBoundsInfos;
result.bounds = extent;
return result;
}
function makeEnvelope(bounds) {
var width = bounds[1][0] - bounds[0][0];
var height = bounds[1][1] - bounds[0][1];
var side = Math.max(width, height);
var envelopeRatio = 0.3;
var envelope = Math.max(side * envelopeRatio, 0.1);
return [
[ bounds[0][0] - envelope, bounds[0][1] - envelope ],
[ bounds[1][0] + envelope, bounds[1][1] + envelope ],
];
}
function mergeBounds(bounds) {
var b = [[Infinity, Infinity], [-Infinity, -Infinity]];
bounds.forEach(function(boundRaw) {
var bound = boundRaw.bounds;
if (bound[0][0] < b[0][0]) b[0][0] = bound[0][0];
if (bound[0][1] < b[0][1]) b[0][1] = bound[0][1];
if (bound[1][0] > b[1][0]) b[1][0] = bound[1][0];
if (bound[1][1] > b[1][1]) b[1][1] = bound[1][1];
});
return b;
}
function initMap() {
mapboxgl.accessToken = "pk.eyJ1IjoiYXJyYXlqYW0iLCJhIjoiY2lwbW9ubXNiMDA0dHRibWNwcXVpYXVjbiJ9.zdSKQ2cnswTFRHFsg1ZnNg";
var map = new mapboxgl.Map({
container: "map",
style: "mapbox://styles/arrayjam/cinjxlkne002zb7mbxe807imk",
zoom: 13,
center: [145.129434, -38.051802],
});
return map;
}