-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
PolygonSpec.js
343 lines (263 loc) 路 9.93 KB
/
PolygonSpec.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
describe('Polygon', function () {
var map;
before(function () {
map = new L.Map(document.createElement('div'), {center: [55.8, 37.6], zoom: 6});
});
after(function () {
map.remove();
});
describe("#initialize", function () {
it("should never be flat", function () {
var latLngs = [[1, 2], [3, 4]];
var polygon = new L.Polygon(latLngs);
expect(L.LineUtil.isFlat(polygon._latlngs)).to.be(false);
expect(polygon.getLatLngs()).to.eql(polygon._latlngs);
});
it("doesn't overwrite the given latlng array", function () {
var originalLatLngs = [
[1, 2],
[3, 4]
];
var sourceLatLngs = originalLatLngs.slice();
var polygon = new L.Polygon(sourceLatLngs);
expect(sourceLatLngs).to.eql(originalLatLngs);
expect(polygon._latlngs).to.not.eql(sourceLatLngs);
});
it("can be called with an empty array", function () {
var polygon = new L.Polygon([]);
expect(polygon._latlngs).to.eql([[]]);
expect(polygon.getLatLngs()).to.eql(polygon._latlngs);
});
it("can be initialized with holes", function () {
var originalLatLngs = [
[[0, 10], [10, 10], [10, 0]], // external ring
[[2, 3], [2, 4], [3, 4]] // hole
];
var polygon = new L.Polygon(originalLatLngs);
expect(polygon._latlngs).to.eql([
[L.latLng([0, 10]), L.latLng([10, 10]), L.latLng([10, 0])],
[L.latLng([2, 3]), L.latLng([2, 4]), L.latLng([3, 4])]
]);
expect(polygon.getLatLngs()).to.eql(polygon._latlngs);
});
it("can be initialized with multi including hole", function () {
var latLngs = [
[[[10, 20], [30, 40], [50, 60]]],
[[[0, 10], [10, 10], [10, 0]], [[2, 3], [2, 4], [3, 4]]]
];
var polygon = new L.Polygon(latLngs);
expect(polygon._latlngs).to.eql([
[[L.latLng([10, 20]), L.latLng([30, 40]), L.latLng([50, 60])]],
[[L.latLng([0, 10]), L.latLng([10, 10]), L.latLng([10, 0])], [L.latLng([2, 3]), L.latLng([2, 4]), L.latLng([3, 4])]]
]);
expect(polygon.getLatLngs()).to.eql(polygon._latlngs);
});
it("can be added to the map when empty", function () {
var polygon = new L.Polygon([]).addTo(map);
var isAdded = map.hasLayer(polygon);
expect(isAdded).to.be(true);
});
});
describe("#isEmpty", function () {
it('should return true for a polygon with no latlngs', function () {
var layer = new L.Polygon([]);
expect(layer.isEmpty()).to.be(true);
});
it('should return false for simple polygon', function () {
var latLngs = [[1, 2], [3, 4], [5, 6]];
var layer = new L.Polygon(latLngs);
expect(layer.isEmpty()).to.be(false);
});
it('should return false for a multi-polygon', function () {
var latLngs = [
[[[10, 20], [30, 40], [50, 60]]],
[[[0, 10], [10, 10], [10, 0]], [[2, 3], [2, 4], [3, 4]]]
];
var layer = new L.Polygon(latLngs);
expect(layer.isEmpty()).to.be(false);
});
});
describe("#setLatLngs", function () {
it("doesn't overwrite the given latlng array", function () {
var originalLatLngs = [
[1, 2],
[3, 4]
];
var sourceLatLngs = originalLatLngs.slice();
var polygon = new L.Polygon(sourceLatLngs);
polygon.setLatLngs(sourceLatLngs);
expect(sourceLatLngs).to.eql(originalLatLngs);
});
it("can be set external ring and holes", function () {
var latLngs = [
[[0, 10], [10, 10], [10, 0]], // external ring
[[2, 3], [2, 4], [3, 4]] // hole
];
var polygon = new L.Polygon([]);
polygon.setLatLngs(latLngs);
expect(polygon.getLatLngs()).to.eql([
[L.latLng([0, 10]), L.latLng([10, 10]), L.latLng([10, 0])],
[L.latLng([2, 3]), L.latLng([2, 4]), L.latLng([3, 4])]
]);
});
it("can be set multi including hole", function () {
var latLngs = [
[[[10, 20], [30, 40], [50, 60]]],
[[[0, 10], [10, 10], [10, 0]], [[2, 3], [2, 4], [3, 4]]]
];
var polygon = new L.Polygon([]);
polygon.setLatLngs(latLngs);
expect(polygon.getLatLngs()).to.eql([
[[L.latLng([10, 20]), L.latLng([30, 40]), L.latLng([50, 60])]],
[[L.latLng([0, 10]), L.latLng([10, 10]), L.latLng([10, 0])], [L.latLng([2, 3]), L.latLng([2, 4]), L.latLng([3, 4])]]
]);
});
});
describe('#getCenter', function () {
it('should compute center of a big simple polygon around equator', function () {
var latlngs = [
[[0, 0], [10, 0], [10, 10], [0, 10]]
];
var layer = new L.Polygon(latlngs).addTo(map);
expect(layer.getCenter()).to.be.nearLatLng(L.latLng([5, 5]), 1e-1);
});
it('should compute center of a small simple polygon', function () {
var latlngs = [
[[0, 0], [0.010, 0], [0.010, 0.010], [0, 0.010]]
];
var layer = new L.Polygon(latlngs).addTo(map);
map.setZoom(0); // Make the polygon disappear in screen.
expect(layer.getCenter()).to.be.nearLatLng(L.latLng([0, 0]));
});
it('throws error if not yet added to map', function () {
expect(function () {
var latlngs = [
[[0, 0], [10, 0], [10, 10], [0, 10]]
];
var layer = new L.Polygon(latlngs);
layer.getCenter();
}).to.throwException('Must add layer to map before using getCenter()');
});
});
describe("#_defaultShape", function () {
it("should return latlngs on a simple polygon", function () {
var latlngs = [
L.latLng([1, 2]),
L.latLng([3, 4])
];
var polygon = new L.Polygon(latlngs);
expect(polygon._defaultShape()).to.eql(latlngs);
});
it("should return first latlngs on a polygon with hole", function () {
var latlngs = [
[L.latLng([0, 12]), L.latLng([13, 14]), L.latLng([15, 16])],
[L.latLng([1, 2]), L.latLng([3, 4]), L.latLng([5, 6])]
];
var polygon = new L.Polygon(latlngs);
expect(polygon._defaultShape()).to.eql(latlngs[0]);
});
it("should return first latlngs on a multipolygon", function () {
var latlngs = [
[[L.latLng([1, 2]), L.latLng([3, 4]), L.latLng([5, 6])]],
[[L.latLng([11, 12]), L.latLng([13, 14]), L.latLng([15, 16])]]
];
var polygon = new L.Polygon(latlngs);
expect(polygon._defaultShape()).to.eql(latlngs[0][0]);
});
it("should return first latlngs on a multipolygon with hole", function () {
var latlngs = [
[[L.latLng([0, 10]), L.latLng([10, 10]), L.latLng([10, 0])],
[L.latLng([2, 3]), L.latLng([2, 4]), L.latLng([3, 4])]],
[[L.latLng([10, 20]), L.latLng([30, 40]), L.latLng([50, 60])]]
];
var polygon = new L.Polygon(latlngs);
expect(polygon._defaultShape()).to.eql(latlngs[0][0]);
});
});
describe("#addLatLng", function () {
it("should add latlng to latlngs", function () {
var latLngs = [
[1, 2],
[3, 4]
];
var polygon = new L.Polygon(latLngs);
polygon.addLatLng([5, 6]);
expect(polygon._latlngs).to.eql([[L.latLng([1, 2]), L.latLng([3, 4]), L.latLng([5, 6])]]);
});
it("should add latlng to first latlngs on a polygon with hole", function () {
var latLngs = [
[[0, 12], [13, 14], [15, 16]],
[[1, 2], [3, 4], [5, 6]]
];
var polygon = new L.Polygon(latLngs);
polygon.addLatLng([17, 0]);
expect(polygon._latlngs[0]).to.eql([L.latLng([0, 12]), L.latLng([13, 14]), L.latLng([15, 16]), L.latLng([17, 0])]);
expect(polygon._latlngs[1]).to.eql([L.latLng([1, 2]), L.latLng([3, 4]), L.latLng([5, 6])]);
});
it("should add latlng by reference on a polygon with hole", function () {
var latLngs = [
[[0, 12], [13, 14], [15, 16]],
[[1, 2], [3, 4], [5, 6]]
];
var polygon = new L.Polygon(latLngs);
polygon.addLatLng([7, 8], polygon._latlngs[1]);
expect(polygon._latlngs[0]).to.eql([L.latLng([0, 12]), L.latLng([13, 14]), L.latLng([15, 16])]);
expect(polygon._latlngs[1]).to.eql([L.latLng([1, 2]), L.latLng([3, 4]), L.latLng([5, 6]), L.latLng([7, 8])]);
});
it("should add latlng to first latlngs on a multi", function () {
var latLngs = [
[[[1, 2], [3, 4]]],
[[[11, 12], [13, 14], [15, 16]]]
];
var polygon = new L.Polygon(latLngs);
polygon.addLatLng([5, 6]);
expect(polygon._latlngs[0]).to.eql([[L.latLng([1, 2]), L.latLng([3, 4]), L.latLng([5, 6])]]);
expect(polygon._latlngs[1]).to.eql([[L.latLng([11, 12]), L.latLng([13, 14]), L.latLng([15, 16])]]);
});
it("should add latlng to latlngs by reference on a multi", function () {
var latLngs = [
[[[11, 12], [13, 14], [15, 16]]],
[[[1, 2], [3, 4]]]
];
var polygon = new L.Polygon(latLngs);
polygon.addLatLng([5, 6], polygon._latlngs[1][0]);
expect(polygon._latlngs[1]).to.eql([[L.latLng([1, 2]), L.latLng([3, 4]), L.latLng([5, 6])]]);
expect(polygon._latlngs[0]).to.eql([[L.latLng([11, 12]), L.latLng([13, 14]), L.latLng([15, 16])]]);
});
it("should add latlng on first latlngs by default on a multipolygon with hole", function () {
var latLngs = [
[[[0, 10], [10, 10], [10, 0]], [[2, 3], [2, 4], [3, 4]]],
[[[10, 20], [30, 40], [50, 60]]]
];
var polygon = new L.Polygon(latLngs);
polygon.addLatLng([-10, -10]);
expect(polygon._latlngs[0][0]).to.eql([L.latLng([0, 10]), L.latLng([10, 10]), L.latLng([10, 0]), L.latLng([-10, -10])]);
expect(polygon._latlngs[0][1]).to.eql([L.latLng([2, 3]), L.latLng([2, 4]), L.latLng([3, 4])]);
expect(polygon._latlngs[1][0]).to.eql([L.latLng([10, 20]), L.latLng([30, 40]), L.latLng([50, 60])]);
});
it("should add latlng by reference on a multipolygon with hole", function () {
var latLngs = [
[[[10, 20], [30, 40], [50, 60]]],
[[[0, 10], [10, 10], [10, 0]], [[2, 3], [2, 4], [3, 4]]]
];
var polygon = new L.Polygon(latLngs);
polygon.addLatLng([2, 2], polygon._latlngs[1][1]);
expect(polygon._latlngs[0][0]).to.eql([L.latLng([10, 20]), L.latLng([30, 40]), L.latLng([50, 60])]);
expect(polygon._latlngs[1][0]).to.eql([L.latLng([0, 10]), L.latLng([10, 10]), L.latLng([10, 0])]);
expect(polygon._latlngs[1][1]).to.eql([L.latLng([2, 3]), L.latLng([2, 4]), L.latLng([3, 4]), L.latLng([2, 2])]);
});
});
describe("#setStyle", function () {
it("succeeds for empty Polygon already added to the map", function () {
var style = {
weight: 3
};
var polygon = L.polygon([]);
polygon.addTo(map);
polygon.setStyle(style);
for (var prop in style) {
expect(polygon.options[prop]).to.be(style[prop]);
}
});
});
});