Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the average perimeter as the center of a polygon or line. #761

Merged
merged 7 commits into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions src/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,7 @@ var annotation = function (type, args) {
* or `undefined` if no such position exists.
*/
this._labelPosition = function () {
var coor = this._coordinates(), position = {x: 0, y: 0}, i;
if (!coor || !coor.length) {
return undefined;
}
if (coor.length === 1) {
return coor[0];
}
for (i = 0; i < coor.length; i += 1) {
position.x += coor[i].x;
position.y += coor[i].y;
}
position.x /= coor.length;
position.y /= coor.length;
return position;
return util.centerFromPerimeter(this._coordinates());
};

/**
Expand Down
36 changes: 36 additions & 0 deletions src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,42 @@ var util = module.exports = {
return {map: mapParams, layer: layerParams};
},

/**
* Return the coordinate associated with the center of the perimeter formed
* from a list of points. This averages all of the vertices in the perimeter
* weighted by the line length on either side of each point. Functionally,
* this is the same as the average of all the points of the lines of the
* perimeter.
*
* @param {geo.geoPosition[]} coor An array of coordinates.
* @returns {geo.geoPosition|undefined} The position for the center, or
* `undefined` if no such position exists.
*/
centerFromPerimeter: function (coor) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manthey it would be good to describe the math in words to explain what is done in this snippet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aashish24 I added a bit more to the function comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, I found it useful.

var position, p0, p1, w, sumw, i;
if (!coor || !coor.length) {
return;
}
if (coor.length === 1) {
return {x: coor[0].x, y: coor[0].y};
}
position = {x: 0, y: 0};
sumw = 0;
p0 = coor[coor.length - 1];
for (i = 0; i < coor.length; i += 1) {
p1 = p0;
p0 = coor[i];
w = Math.sqrt(Math.pow(p1.x - p0.x, 2) + Math.pow(p1.y - p0.y, 2));
position.x += (p0.x + p1.x) * w;
position.y += (p0.y + p1.y) * w;
sumw += 2 * w;
}
position.x /= sumw;
position.y /= sumw;
// return a copy of p0 if all points are the same
return sumw ? position : {x: p0.x, y: p0.y};
},

/**
* Escape any character in a string that has a code point >= 127.
*
Expand Down
4 changes: 3 additions & 1 deletion tests/cases/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ describe('geo.annotation', function () {
ann._coordinates = function () {
return [{x: 1, y: 2}, {x: 3, y: 5}, {x: 8, y: 11}];
};
expect(ann._labelPosition()).toEqual({x: 4, y: 6});
var pos = ann._labelPosition();
expect(pos.x).toBeCloseTo(4.447);
expect(pos.y).toBeCloseTo(6.539);
});
it('labelRecord', function () {
var ann = geo.annotation.annotation('test', {
Expand Down
13 changes: 13 additions & 0 deletions tests/cases/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,17 @@ describe('geo.util', function () {
expect(({}) instanceof iframeWindow.Object).toBe(false);
iframe.remove();
});

it('centerFromPerimter', function () {
expect(util.centerFromPerimeter()).toBe(undefined);
expect(util.centerFromPerimeter([])).toBe(undefined);
expect(util.centerFromPerimeter([{x: 1, y: 1}])).toEqual({x: 1, y: 1});
expect(util.centerFromPerimeter([{x: 1, y: 1}, {x: 1, y: 1}])).toEqual({x: 1, y: 1});
expect(util.centerFromPerimeter([
{x: 1, y: 1}, {x: 3, y: 1}, {x: 3, y: 3}, {x: 1, y: 3}
])).toEqual({x: 2, y: 2});
expect(util.centerFromPerimeter([
{x: 1, y: 1}, {x: 3, y: 1}, {x: 5, y: 1}, {x: 5, y: 3}, {x: 1, y: 3}
])).toEqual({x: 3, y: 2});
});
});