forked from developmentseed/dirty-reprojectors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
albers-usa-territories.js
120 lines (101 loc) · 4.73 KB
/
albers-usa-territories.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
var d3 = require('d3-geo')
var R = 6378137.0 // radius of Earth in meters
function multiplex(streams) {
const n = streams.length;
return {
point(x, y) { for (const s of streams) s.point(x, y); },
sphere() { for (const s of streams) s.sphere(); },
lineStart() { for (const s of streams) s.lineStart(); },
lineEnd() { for (const s of streams) s.lineEnd(); },
polygonStart() { for (const s of streams) s.polygonStart(); },
polygonEnd() { for (const s of streams) s.polygonEnd(); }
};
}
function geoAlbersUsaTerritories() {
let epsilon = 0.000001
var cache,
cacheStream,
lower48 = d3.geoAlbers(), lower48Point,
alaska = d3.geoConicEqualArea().rotate([154, 0]).center([-2, 58.5]).parallels([55, 65]),
alaskaPoint,
hawaii = d3.geoConicEqualArea().rotate([157, 0]).center([-3, 19.9]).parallels([8, 18]),
hawaiiPoint,
puertoRico = d3.geoConicEqualArea().rotate([66, 0]).center([0, 18]).parallels([8, 18]),
puertoRicoPoint,
guamMariana = d3.geoConicEqualArea().rotate([-145, 0]).center([0, 16]).parallels([10, 20]),
guamMarianaPoint,
americanSamoa = d3.geoConicEqualArea().rotate([170, 0]).center([0, -14]).parallels([-14, 0]),
americanSamoaPoint,
point,
pointStream = {point: function(x, y) { point = [x, y]; }};
function albersUsaTerritories(coordinates) {
var x = coordinates[0], y = coordinates[1];
return point = null,
(lower48Point.point(x, y), point)
|| (alaskaPoint.point(x, y), point)
|| (hawaiiPoint.point(x, y), point)
|| (puertoRicoPoint.point(x, y), point)
|| (guamMarianaPoint.point(x, y), point)
|| (americanSamoaPoint.point(x, y), point);
}
albersUsaTerritories.invert = function(coordinates) {
var k = lower48.scale(),
t = lower48.translate(),
x = (coordinates[0] - t[0]) / k,
y = (coordinates[1] - t[1]) / k;
return (y >= 0.120 && y < 0.234 && x >= -0.390 && x < -0.185 ? alaska
: y >= 0.166 && y < 0.234 && x >= -0.185 && x < -0.080 ? hawaii
: y >= 0.204 && y < 0.234 && x >= 0.300 && x < 0.380 ? puertoRico
: y >= 0.050 && y < 0.210 && x >= -0.450 && x < - 0.390 ? guamMariana
: y >= 0.210 && y < 0.234 && x >= -0.450 && x < -0.390 ? americanSamoa
: lower48).invert(coordinates);
};
albersUsaTerritories.stream = function(stream) {
return cache && cacheStream === stream ? cache : cache = multiplex([lower48.stream(cacheStream = stream), alaska.stream(stream), hawaii.stream(stream), puertoRico.stream(stream), guamMariana.stream(stream),americanSamoa.stream(stream)]);
};
albersUsaTerritories.precision = function(_) {
if (!arguments.length) return lower48.precision();
lower48.precision(_), alaska.precision(_), hawaii.precision(_), puertoRico.precision(_), guamMariana.precision(_), americanSamoa.precision(_);
return reset();
};
albersUsaTerritories.scale = function(_) {
if (!arguments.length) return lower48.scale();
lower48.scale(_), alaska.scale(_ * 0.35), hawaii.scale(_), puertoRico.scale(_), guamMariana.scale(_), americanSamoa.scale(_);
return albersUsaTerritories.translate(lower48.translate());
};
albersUsaTerritories.translate = function(_) {
if (!arguments.length) return lower48.translate();
var k = lower48.scale(), x = +_[0], y = +_[1];
lower48Point = lower48
.translate(_)
.clipExtent([[x - 0.455 * k, y - 0.238 * k], [x + 0.455 * k, y + 0.238 * k]])
.stream(pointStream);
alaskaPoint = alaska
.translate([x - 0.275 * k, y + 0.201 * k])
.clipExtent([[x - 0.390 * k + epsilon, y + 0.120 * k + epsilon], [x - 0.185 * k - epsilon, y + 0.234 * k - epsilon]])
.stream(pointStream);
hawaiiPoint = hawaii
.translate([x - 0.180 * k, y + 0.212 * k])
.clipExtent([[x - 0.185 * k + epsilon, y + 0.166 * k + epsilon], [x - 0.080 * k - epsilon, y + 0.234 * k - epsilon]])
.stream(pointStream);
puertoRicoPoint = puertoRico
.translate([x + 0.335 * k, y + 0.224 * k])
.clipExtent([[x + 0.300 * k, y + 0.204 * k], [x + 0.380 * k, y + 0.234 * k]])
.stream(pointStream).point;
guamMarianaPoint = guamMariana
.translate([x - 0.415 * k, y + 0.140 * k])
.clipExtent([[x - 0.450 * k, y + 0.050 * k], [x - 0.390 * k, y + 0.210 * k]])
.stream(pointStream).point;
americanSamoaPoint = americanSamoa
.translate([x - 0.415 * k, y + 0.215 * k])
.clipExtent([[x - 0.450 * k, y + 0.210 * k], [x - 0.390 * k, y + 0.234 * k]])
.stream(pointStream).point;
return reset();
};
function reset() {
cache = cacheStream = null;
return albersUsaTerritories;
}
return albersUsaTerritories.scale(1070);
}
module.exports = geoAlbersUsaTerritories