-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathprojection.js
312 lines (290 loc) · 9.96 KB
/
projection.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import {
geoAlbers,
geoAlbersUsa,
geoAzimuthalEqualArea,
geoAzimuthalEquidistant,
geoClipRectangle,
geoConicConformal,
geoConicEqualArea,
geoConicEquidistant,
geoEqualEarth,
geoEquirectangular,
geoGnomonic,
geoMercator,
geoOrthographic,
geoPath,
geoStereographic,
geoStream,
geoTransform,
geoTransverseMercator
} from "d3";
import {valueObject} from "./channel.js";
import {coerceNumbers, constant, isObject} from "./options.js";
import {warn} from "./warnings.js";
const pi = Math.PI;
const tau = 2 * pi;
const defaultAspectRatio = 0.618;
export function createProjection(
{
projection,
inset: globalInset = 0,
insetTop = globalInset,
insetRight = globalInset,
insetBottom = globalInset,
insetLeft = globalInset
} = {},
dimensions
) {
if (projection == null) return;
if (typeof projection.stream === "function") return projection; // d3 projection
let options;
let domain;
let clip = "frame";
// If the projection was specified as an object with additional options,
// extract those. The order of precedence for insetTop (and other insets) is:
// projection.insetTop, projection.inset, (global) insetTop, (global) inset.
// Any other options on this object will be passed through to the initializer.
if (isObject(projection)) {
let inset;
({
type: projection,
domain,
inset,
insetTop = inset !== undefined ? inset : insetTop,
insetRight = inset !== undefined ? inset : insetRight,
insetBottom = inset !== undefined ? inset : insetBottom,
insetLeft = inset !== undefined ? inset : insetLeft,
clip = clip,
...options
} = projection);
if (projection == null) return;
}
// For named projections, retrieve the corresponding projection initializer.
if (typeof projection !== "function") ({type: projection} = namedProjection(projection));
// Compute the frame dimensions and invoke the projection initializer.
const {width, height, marginLeft, marginRight, marginTop, marginBottom} = dimensions;
const dx = width - marginLeft - marginRight - insetLeft - insetRight;
const dy = height - marginTop - marginBottom - insetTop - insetBottom;
projection = projection?.({width: dx, height: dy, clip, ...options});
// The projection initializer might decide to not use a projection.
if (projection == null) return;
clip = maybePostClip(clip, marginLeft, marginTop, width - marginRight, height - marginBottom);
// Translate the origin to the top-left corner, respecting margins and insets.
let tx = marginLeft + insetLeft;
let ty = marginTop + insetTop;
let transform;
// If a domain is specified, fit the projection to the frame.
if (domain != null) {
const [[x0, y0], [x1, y1]] = geoPath(projection).bounds(domain);
const k = Math.min(dx / (x1 - x0), dy / (y1 - y0));
if (k > 0) {
tx -= (k * (x0 + x1) - dx) / 2;
ty -= (k * (y0 + y1) - dy) / 2;
transform = geoTransform({
point(x, y) {
this.stream.point(x * k + tx, y * k + ty);
}
});
} else {
warn(`Warning: the projection could not be fit to the specified domain; using the default scale.`);
}
}
transform ??=
tx === 0 && ty === 0
? identity()
: geoTransform({
point(x, y) {
this.stream.point(x + tx, y + ty);
}
});
return {stream: (s) => projection.stream(transform.stream(clip(s)))};
}
function namedProjection(projection) {
switch (`${projection}`.toLowerCase()) {
case "albers-usa":
return scaleProjection(geoAlbersUsa, 0.7463, 0.4673);
case "albers":
return conicProjection(geoAlbers, 0.7463, 0.4673);
case "azimuthal-equal-area":
return scaleProjection(geoAzimuthalEqualArea, 4, 4);
case "azimuthal-equidistant":
return scaleProjection(geoAzimuthalEquidistant, tau, tau);
case "conic-conformal":
return conicProjection(geoConicConformal, tau, tau);
case "conic-equal-area":
return conicProjection(geoConicEqualArea, 6.1702, 2.9781);
case "conic-equidistant":
return conicProjection(geoConicEquidistant, 7.312, 3.6282);
case "equal-earth":
return scaleProjection(geoEqualEarth, 5.4133, 2.6347);
case "equirectangular":
return scaleProjection(geoEquirectangular, tau, pi);
case "gnomonic":
return scaleProjection(geoGnomonic, 3.4641, 3.4641);
case "identity":
return {type: identity};
case "reflect-y":
return {type: reflectY};
case "mercator":
return scaleProjection(geoMercator, tau, tau);
case "orthographic":
return scaleProjection(geoOrthographic, 2, 2);
case "stereographic":
return scaleProjection(geoStereographic, 2, 2);
case "transverse-mercator":
return scaleProjection(geoTransverseMercator, tau, tau);
default:
throw new Error(`unknown projection type: ${projection}`);
}
}
function maybePostClip(clip, x1, y1, x2, y2) {
if (clip === false || clip == null || typeof clip === "number") return (s) => s;
if (clip === true) clip = "frame";
switch (`${clip}`.toLowerCase()) {
case "frame":
return geoClipRectangle(x1, y1, x2, y2);
default:
throw new Error(`unknown projection clip type: ${clip}`);
}
}
function scaleProjection(createProjection, kx, ky) {
return {
type: ({width, height, rotate, precision = 0.15, clip}) => {
const projection = createProjection();
if (precision != null) projection.precision?.(precision);
if (rotate != null) projection.rotate?.(rotate);
if (typeof clip === "number") projection.clipAngle?.(clip);
if (width != null) {
projection.scale(Math.min(width / kx, height / ky));
projection.translate([width / 2, height / 2]);
}
return projection;
},
aspectRatio: ky / kx
};
}
function conicProjection(createProjection, kx, ky) {
const {type, aspectRatio} = scaleProjection(createProjection, kx, ky);
return {
type: (options) => {
const {parallels, domain, width, height} = options;
const projection = type(options);
if (parallels != null) {
projection.parallels(parallels);
if (domain === undefined && width != null) {
projection.fitSize([width, height], {type: "Sphere"});
}
}
return projection;
},
aspectRatio
};
}
const identity = constant({stream: (stream) => stream});
const reflectY = constant(
geoTransform({
point(x, y) {
this.stream.point(x, -y);
}
})
);
// Applies a point-wise projection to the given paired x and y channels.
// Note: mutates values!
export function project(cx, cy, values, projection) {
const x = values[cx];
const y = values[cy];
const n = x.length;
const X = (values[cx] = new Float64Array(n).fill(NaN));
const Y = (values[cy] = new Float64Array(n).fill(NaN));
let i;
const stream = projection.stream({
point(x, y) {
X[i] = x;
Y[i] = y;
}
});
for (i = 0; i < n; ++i) {
stream.point(x[i], y[i]);
}
}
// Returns true if a projection was specified. This should match the logic of
// createProjection above, and is called before we construct the projection.
// (Though note that we ignore the edge case where the projection initializer
// may return null.)
export function hasProjection({projection} = {}) {
if (projection == null) return false;
if (typeof projection.stream === "function") return true;
if (isObject(projection)) projection = projection.type;
return projection != null;
}
// When a projection is specified, we can use its aspect ratio to determine a
// good value for the projection’s height based on the desired width. When we
// don’t have a way to know, the golden ratio is our best guess. Due to a
// circular dependency (we need to know the height before we can construct the
// projection), we have to test the raw projection option rather than the
// materialized projection; therefore we must be extremely careful that the
// logic of this function exactly matches createProjection above!
export function projectionAspectRatio(projection) {
if (typeof projection?.stream === "function") return defaultAspectRatio;
if (isObject(projection)) {
let domain, options;
({domain, type: projection, ...options} = projection);
if (domain != null && projection != null) {
const type = typeof projection === "string" ? namedProjection(projection).type : projection;
const [[x0, y0], [x1, y1]] = geoPath(type({...options, width: 100, height: 100})).bounds(domain);
const r = (y1 - y0) / (x1 - x0);
return r && isFinite(r) ? (r < 0.2 ? 0.2 : r > 5 ? 5 : r) : defaultAspectRatio;
}
}
if (projection == null) return;
if (typeof projection !== "function") {
const {aspectRatio} = namedProjection(projection);
if (aspectRatio) return aspectRatio;
}
return defaultAspectRatio;
}
// Extract the (possibly) scaled values for the x and y channels, and apply the
// projection if any.
export function applyPosition(channels, scales, {projection}) {
const {x, y} = channels;
let position = {};
if (x) position.x = x;
if (y) position.y = y;
position = valueObject(position, scales);
if (projection && x?.scale === "x" && y?.scale === "y") project("x", "y", position, projection);
if (x) position.x = coerceNumbers(position.x);
if (y) position.y = coerceNumbers(position.y);
return position;
}
export function getGeometryChannels(channel) {
const X = [];
const Y = [];
const x = {scale: "x", value: X};
const y = {scale: "y", value: Y};
const sink = {
point(x, y) {
X.push(x);
Y.push(y);
},
lineStart() {},
lineEnd() {},
polygonStart() {},
polygonEnd() {},
sphere() {}
};
for (const object of channel.value) geoStream(object, sink);
return [x, y];
}
// If no projection is specified, default to a projection that passes points
// through the x and y scales, if any.
export function xyProjection({x: X, y: Y}) {
if (X || Y) {
X ??= (x) => x;
Y ??= (y) => y;
return geoTransform({
point(x, y) {
this.stream.point(X(x), Y(y));
}
});
}
}