-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
161 lines (141 loc) · 3.77 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
const from = require("from2");
module.exports = function() {
throw new Error("call .point(), .lineString(), or .polygon() instead");
};
function position(bbox) {
if (bbox) return coordInBBBOX(bbox);
else return [lon(), lat()];
}
module.exports.position = position;
module.exports.point = function(count, bbox) {
const features = [];
for (let i = 0; i < count; i++) {
features.push(feature(bbox ? point(position(bbox)) : point()));
}
return collection(features);
};
module.exports.pointStream = function(count, bbox) {
return from.obj(function(size, next) {
if (--count) {
next(null, feature(bbox ? point(position(bbox)) : point()));
} else {
next(null, null);
}
});
};
module.exports.polygon = function(
count,
num_vertices,
max_radial_length,
bbox
) {
if (typeof num_vertices !== "number") num_vertices = 10;
if (typeof max_radial_length !== "number") max_radial_length = 10;
const features = [];
for (let i = 0; i < count; i++) {
let vertices = [];
const circle_offsets = Array.apply(null, new Array(num_vertices + 1)).map(
Math.random
);
circle_offsets.forEach(function sumOffsets(cur, index, arr) {
arr[index] = index > 0 ? cur + arr[index - 1] : cur;
});
circle_offsets.forEach(function scaleOffsets(cur) {
cur = (cur * 2 * Math.PI) / circle_offsets[circle_offsets.length - 1];
const radial_scaler = Math.random();
vertices.push([
radial_scaler * max_radial_length * Math.sin(cur),
radial_scaler * max_radial_length * Math.cos(cur)
]);
});
vertices[vertices.length - 1] = vertices[0]; // close the ring
// center the polygon around something
vertices = vertices.map(vertexToCoordinate(position(bbox)));
features.push(feature(polygon([vertices])));
}
return collection(features);
};
module.exports.lineString = function(
count,
num_vertices,
max_length,
max_rotation,
bbox
) {
if (typeof num_vertices !== "number" || num_vertices < 2) num_vertices = 10;
if (typeof max_length !== "number") max_length = 0.0001;
if (typeof max_rotation !== "number") max_rotation = Math.PI / 8;
const features = [];
for (let i = 0; i < count; i++) {
const startingPoint = position(bbox);
const vertices = [startingPoint];
for (let j = 0; j < num_vertices - 1; j++) {
const priorAngle =
j === 0
? Math.random() * 2 * Math.PI
: Math.tan(
(vertices[j][1] - vertices[j - 1][1]) /
(vertices[j][0] - vertices[j - 1][0])
);
const angle = priorAngle + (Math.random() - 0.5) * max_rotation * 2;
const distance = Math.random() * max_length;
vertices.push([
vertices[j][0] + distance * Math.cos(angle),
vertices[j][1] + distance * Math.sin(angle)
]);
}
features.push(feature(lineString(vertices)));
}
return collection(features);
};
function vertexToCoordinate(hub) {
return function(cur) {
return [cur[0] + hub[0], cur[1] + hub[1]];
};
}
function rnd() {
return Math.random() - 0.5;
}
function lon() {
return rnd() * 360;
}
function lat() {
return rnd() * 180;
}
function point(coordinates) {
return {
type: "Point",
coordinates: coordinates || [lon(), lat()]
};
}
function coordInBBBOX(bbox) {
return [
Math.random() * (bbox[2] - bbox[0]) + bbox[0],
Math.random() * (bbox[3] - bbox[1]) + bbox[1]
];
}
function polygon(coordinates) {
return {
type: "Polygon",
coordinates: coordinates
};
}
function feature(geom) {
return {
type: "Feature",
geometry: geom,
properties: {}
};
}
function collection(f) {
return {
type: "FeatureCollection",
features: f
};
}
function lineString(coordinates) {
return {
type: "LineString",
coordinates: coordinates
};
}