/
helpers.js
154 lines (124 loc) · 4.17 KB
/
helpers.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
const _ = require("lodash")
// MAP TEXTURE
export var projection = d3.geo.equirectangular()
.translate([1024, 512])
.scale(325);
// GEOHELPERS
export function getPoint(event) {
// Get the vertices
let a = this.geometry.vertices[event.face.a];
let b = this.geometry.vertices[event.face.b];
let c = this.geometry.vertices[event.face.c];
// Averge them together
let point = {
x: (a.x + b.x + c.x) / 3,
y: (a.y + b.y + c.y) / 3,
z: (a.z + b.z + c.z) / 3
};
return point;
}
export function getEventCenter(event, radius) {
radius = radius || 200;
var point = getPoint.call(this, event);
var latRads = Math.acos(point.y / radius);
var lngRads = Math.atan2(point.z, point.x);
var lat = (Math.PI / 2 - latRads) * (180 / Math.PI);
var lng = (Math.PI - lngRads) * (180 / Math.PI);
return [lat, lng - 180];
}
export function convertToXYZ(point, radius) {
radius = radius || 200;
var latRads = (90 - point[0]) * Math.PI / 180;
var lngRads = (180 - point[1]) * Math.PI / 180;
var x = radius * Math.sin(latRads) * Math.cos(lngRads);
var y = radius * Math.cos(latRads);
var z = radius * Math.sin(latRads) * Math.sin(lngRads);
return {
x: x,
y: y,
z: z
};
}
// Get geographic information from country features
export var geodecoder = function(features) {
let store = {};
for (let i = 0; i < features.length; i++) {
store[features[i].id] = features[i];
}
return {
find: function(id) {
return store[id];
},
search: function(lat, lng) {
let match = false;
let country, coords;
for (let i = 0; i < features.length; i++) {
country = features[i];
if (country.geometry.type === 'Polygon') {
match = pointInPolygon(country.geometry.coordinates[0], [lng, lat]);
if (match) {
// Attach values or undefined to country
return {
code: features[i].id,
name: features[i].properties.name,
aid: features[i]["aid-given"],
recieved: features[i]["aid-received"]
};
}
} else if (country.geometry.type === 'MultiPolygon') {
coords = country.geometry.coordinates;
for (let j = 0; j < coords.length; j++) {
match = pointInPolygon(coords[j][0], [lng, lat]);
if (match) {
// Attach values or undefined to country
return {
code: features[i].id,
name: features[i].properties.name,
aid: features[i]["aid-given"],
recieved: features[i]["aid-received"]
};
}
}
}
}
return null;
}
};
};
export var pointInPolygon = function(poly, point) {
let x = point[0];
let y = point[1];
let inside = false,
xi, xj, yi, yj, xk;
for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
xi = poly[i][0];
yi = poly[i][1];
xj = poly[j][0];
yj = poly[j][1];
xk = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (xk) {
inside = !inside;
}
}
return inside;
};
function curHelp(num) {
num = _.chunk(num.split(""), 3);
return _.map(num, (d) => d.join("")).join(",");
}
// Currency helper
export function currency(number) {
var stuff;
number = number.toString();
if (number.length % 3 === 0) {
return curHelp(number)
}
if ((number.length - 1) % 3 === 0) {
stuff = number.split("").splice(0,1);
number = number.split("").splice(1);
} else {
stuff = number.split("").splice(0,2).join("");
number = number.split("").splice(2);
}
return `${stuff},${curHelp(number.join(""))}`;
}