2013-11-08 05:54:33 +08:00
|
|
|
describe('Polygon', function () {
|
2013-03-02 05:49:20 +08:00
|
|
|
|
2013-02-20 06:03:37 +08:00
|
|
|
var c = document.createElement('div');
|
|
|
|
c.style.width = '400px';
|
|
|
|
c.style.height = '400px';
|
|
|
|
var map = new L.Map(c);
|
|
|
|
map.setView(new L.LatLng(55.8, 37.6), 6);
|
2013-03-02 05:49:20 +08:00
|
|
|
|
2013-11-08 05:54:33 +08:00
|
|
|
describe("#initialize", function () {
|
2015-03-05 01:14:51 +08:00
|
|
|
it("should never be flat", function () {
|
|
|
|
var latLngs = [[1, 2], [3, 4]];
|
|
|
|
|
|
|
|
var polygon = new L.Polygon(latLngs);
|
|
|
|
|
2015-05-08 19:11:58 +08:00
|
|
|
expect(L.Polyline._flat(polygon._latlngs)).to.be(false);
|
2015-03-06 16:14:00 +08:00
|
|
|
expect(polygon.getLatLngs()).to.eql(polygon._latlngs);
|
2015-03-05 01:14:51 +08:00
|
|
|
});
|
|
|
|
|
2013-02-20 06:03:37 +08:00
|
|
|
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);
|
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(sourceLatLngs).to.eql(originalLatLngs);
|
|
|
|
expect(polygon._latlngs).to.not.eql(sourceLatLngs);
|
2013-02-20 06:03:37 +08:00
|
|
|
});
|
2013-05-03 07:17:04 +08:00
|
|
|
|
|
|
|
it("can be called with an empty array", function () {
|
|
|
|
var polygon = new L.Polygon([]);
|
2015-03-06 16:14:00 +08:00
|
|
|
expect(polygon._latlngs).to.eql([[]]);
|
|
|
|
expect(polygon.getLatLngs()).to.eql(polygon._latlngs);
|
2013-05-03 07:17:04 +08:00
|
|
|
});
|
2013-11-08 05:54:33 +08:00
|
|
|
|
2013-10-14 00:17:12 +08:00
|
|
|
it("can be initialized with holes", function () {
|
|
|
|
var originalLatLngs = [
|
2015-03-06 16:14:00 +08:00
|
|
|
[ //external ring
|
2013-10-14 00:17:12 +08:00
|
|
|
[0, 10], [10, 10], [10, 0]
|
|
|
|
], [ //hole
|
|
|
|
[2, 3], [2, 4], [3, 4]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
var polygon = new L.Polygon(originalLatLngs);
|
|
|
|
|
2015-03-06 16:14:00 +08:00
|
|
|
expect(polygon._latlngs).to.eql([
|
2013-12-14 01:41:55 +08:00
|
|
|
[L.latLng([0, 10]), L.latLng([10, 10]), L.latLng([10, 0])],
|
|
|
|
[L.latLng([2, 3]), L.latLng([2, 4]), L.latLng([3, 4])]
|
|
|
|
]);
|
2015-03-06 16:14:00 +08:00
|
|
|
expect(polygon.getLatLngs()).to.eql(polygon._latlngs);
|
2013-11-08 05:54:33 +08:00
|
|
|
});
|
2015-03-05 01:14:51 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2015-03-06 16:14:00 +08:00
|
|
|
expect(polygon._latlngs).to.eql([
|
2015-03-05 01:14:51 +08:00
|
|
|
[[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])]]
|
|
|
|
]);
|
2015-03-06 16:14:00 +08:00
|
|
|
expect(polygon.getLatLngs()).to.eql(polygon._latlngs);
|
2015-03-05 01:14:51 +08:00
|
|
|
});
|
2013-02-20 06:03:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(sourceLatLngs).to.eql(originalLatLngs);
|
2013-02-20 06:03:37 +08:00
|
|
|
});
|
2013-11-08 05:54:33 +08:00
|
|
|
|
|
|
|
it("can be set external ring and holes", function () {
|
2013-10-14 00:17:12 +08:00
|
|
|
var latLngs = [
|
2015-03-06 16:14:00 +08:00
|
|
|
[ //external ring
|
2013-10-14 00:17:12 +08:00
|
|
|
[0, 10], [10, 10], [10, 0]
|
|
|
|
], [ //hole
|
|
|
|
[2, 3], [2, 4], [3, 4]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
var polygon = new L.Polygon([]);
|
|
|
|
polygon.setLatLngs(latLngs);
|
|
|
|
|
2013-12-14 01:41:55 +08:00
|
|
|
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])]
|
|
|
|
]);
|
2013-11-08 05:54:33 +08:00
|
|
|
});
|
2015-03-06 16:14:00 +08:00
|
|
|
|
|
|
|
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])]]
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2013-02-20 06:03:37 +08:00
|
|
|
});
|
|
|
|
|
2015-04-13 00:33:56 +08:00
|
|
|
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]));
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-02-20 06:03:37 +08:00
|
|
|
});
|