Skip to content

Commit

Permalink
Merge pull request #49 from henrythasler/reimplement-layer-functions
Browse files Browse the repository at this point in the history
implement getBounds(), fixes #48
  • Loading branch information
henrythasler committed Nov 15, 2019
2 parents cc4ac4d + 32a0f44 commit 759dfbc
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const bundle = (format, filename, options = {}) => ({

export default [
bundle('cjs', pkg.main),
bundle('es', pkg.module),
bundle('esm', pkg.module),
bundle('umd', pkg.browser.replace('.min', ''), { resolve: true, stats: true }),
bundle('umd', pkg.browser, { resolve: true, minimize: true }),
];
45 changes: 45 additions & 0 deletions spec/geodesic-circle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ const defaultOptions: GeodesicOptions = { wrap: true, steps: 24, fill: true, noC

const eps = 0.000001;

function checkFixture(specimen: L.LatLngLiteral[][], fixture: L.LatLngLiteral[][]): void {
expect(specimen).to.be.an("array");
expect(specimen).to.be.length(fixture.length);
specimen.forEach((line, k) => {
expect(line).to.be.length(fixture[k].length);
line.forEach((point, l) => {
expect(point).to.be.an("object");
expect(point).to.include.all.keys("lat", "lng");
expect(point.lat).to.be.closeTo(fixture[k][l].lat, eps);
expect(point.lng).to.be.closeTo(fixture[k][l].lng, eps);
});
});
}

describe("Main functionality", function () {
let map: L.Map;
const radius = 1000 * 1000;
Expand Down Expand Up @@ -100,4 +114,35 @@ describe("Main functionality", function () {
expect(circle.statistics.vertices).to.be.equal(25);
});

});

describe("Bugs", function () {
let map: L.Map;

beforeEach(function () {
map = L.map(document.createElement('div'));
});

afterEach(function () {
map.remove();
});

it("Calling getBounds on a GeodesicCircle throws an error (#48)", async function () {
const circle = new GeodesicCircleClass(Seattle, { radius: 10});
const group = new L.FeatureGroup([circle]).addTo(map);

expect(circle.options).to.be.deep.equal({ ...defaultOptions, ...{ radius: 10 } });
expect(circle.polyline).to.be.an("object");
expect(circle.center.lat).to.be.closeTo(Seattle.lat, eps);
expect(circle.center.lng).to.be.closeTo(Seattle.lng, eps);

expect(map.hasLayer(group)).to.be.true;
map.eachLayer(function (layer) {
expect(layer).to.be.instanceOf(L.FeatureGroup);
});

const bounds = group.getBounds();
expect(bounds).to.be.instanceOf(L.LatLngBounds);
checkFixture([[bounds.getCenter()]], [[Seattle]]);
});
});
29 changes: 29 additions & 0 deletions spec/geodesic-line.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,32 @@ describe("GeoJSON-Support", function () {
expect(mockLog.mock.calls[0][0]).to.match(/Type "GeometryCollection" not supported/);
});
});

describe("Re-Implementing Layer-Functions", function () {
let map: L.Map;

beforeEach(function () {
map = L.map(document.createElement('div'));
});

afterEach(function () {
map.remove();
});

it("getBounds()", async function () {
const line = new GeodesicLine([FlindersPeak, Buninyong]);
const group = new L.FeatureGroup([line]).addTo(map);

expect(line.options).to.be.deep.equal(defaultOptions);
expect(line.polyline).to.be.an("object");

expect(map.hasLayer(group)).to.be.true;
map.eachLayer(function (layer) {
expect(layer).to.be.instanceOf(L.FeatureGroup);
});

const bounds = group.getBounds();
expect(bounds).to.be.instanceOf(L.LatLngBounds);
checkFixture([[bounds.getCenter()]], [[{lat: (FlindersPeak.lat+Buninyong.lat)/2, lng: (FlindersPeak.lng+Buninyong.lng)/2}]]);
});
});
4 changes: 4 additions & 0 deletions src/geodesic-circle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export class GeodesicCircleClass extends L.Layer {
return this;
}

getBounds(): L.LatLngBounds {
return this.polyline.getBounds();
}

private update(): void {
const latlngs = this.geom.circle(this.center, this.radius);

Expand Down
4 changes: 4 additions & 0 deletions src/geodesic-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export class GeodesicLine extends L.Layer {
return this;
}

getBounds(): L.LatLngBounds {
return this.polyline.getBounds();
}

private updateLatLngs(latlngs: L.LatLngExpression[] | L.LatLngExpression[][]): void {
const latLngLiteral = latlngExpressionArraytoLiteralArray(latlngs);
const geodesic = this.geom.multiLineString(latLngLiteral);
Expand Down

0 comments on commit 759dfbc

Please sign in to comment.