Skip to content
This repository

getCentroid should just return the first point if a linear ring only has... #348

Merged
merged 1 commit into from over 2 years ago

3 participants

Bart van den Eijnden Tim Schaub Frédéric Junod
Bart van den Eijnden
Owner

... less than three components

Bart van den Eijnden getCentroid should just return the first point if a linear ring only …
…has less than three components
955c83b
Bart van den Eijnden
Owner

Geometry/Polygon tests pass in Safari and FF, TIA for any review.

Frédéric Junod fredj commented on the diff
lib/OpenLayers/Geometry/LinearRing.js
@@ -191,21 +191,26 @@ OpenLayers.Geometry.LinearRing = OpenLayers.Class(
191 191 * {<OpenLayers.Geometry.Point>} The centroid of the collection
192 192 */
193 193 getCentroid: function() {
194   - if (this.components && (this.components.length > 2)) {
195   - var sumX = 0.0;
196   - var sumY = 0.0;
197   - for (var i = 0; i < this.components.length - 1; i++) {
198   - var b = this.components[i];
199   - var c = this.components[i+1];
200   - sumX += (b.x + c.x) * (b.x * c.y - c.x * b.y);
201   - sumY += (b.y + c.y) * (b.x * c.y - c.x * b.y);
  194 + if (this.components) {
  195 + var len = this.components.length;
  196 + if (len > 0 && len <= 2) {
3
Frédéric Junod Owner
fredj added a note

could it be simply if (len == 1) { ?

Bart van den Eijnden Owner
bartvde added a note

no we need len == 2 as well since the other code path only accounts for len > 2

Frédéric Junod Owner
fredj added a note

oups, I should have seen it. sorry for the noise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Tim Schaub
Owner

Looks good. Please merge.

Bart van den Eijnden bartvde merged commit 0dce0b9 into from
Bart van den Eijnden bartvde closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Mar 22, 2012
Bart van den Eijnden getCentroid should just return the first point if a linear ring only …
…has less than three components
955c83b
This page is out of date. Refresh to see the latest.
33 lib/OpenLayers/Geometry/LinearRing.js
@@ -191,21 +191,26 @@ OpenLayers.Geometry.LinearRing = OpenLayers.Class(
191 191 * {<OpenLayers.Geometry.Point>} The centroid of the collection
192 192 */
193 193 getCentroid: function() {
194   - if (this.components && (this.components.length > 2)) {
195   - var sumX = 0.0;
196   - var sumY = 0.0;
197   - for (var i = 0; i < this.components.length - 1; i++) {
198   - var b = this.components[i];
199   - var c = this.components[i+1];
200   - sumX += (b.x + c.x) * (b.x * c.y - c.x * b.y);
201   - sumY += (b.y + c.y) * (b.x * c.y - c.x * b.y);
  194 + if (this.components) {
  195 + var len = this.components.length;
  196 + if (len > 0 && len <= 2) {
  197 + return this.components[0].clone();
  198 + } else if (len > 2) {
  199 + var sumX = 0.0;
  200 + var sumY = 0.0;
  201 + for (var i = 0; i < this.components.length - 1; i++) {
  202 + var b = this.components[i];
  203 + var c = this.components[i+1];
  204 + sumX += (b.x + c.x) * (b.x * c.y - c.x * b.y);
  205 + sumY += (b.y + c.y) * (b.x * c.y - c.x * b.y);
  206 + }
  207 + var area = -1 * this.getArea();
  208 + var x = sumX / (6 * area);
  209 + var y = sumY / (6 * area);
  210 + return new OpenLayers.Geometry.Point(x, y);
  211 + } else {
  212 + return null;
202 213 }
203   - var area = -1 * this.getArea();
204   - var x = sumX / (6 * area);
205   - var y = sumY / (6 * area);
206   - return new OpenLayers.Geometry.Point(x, y);
207   - } else {
208   - return null;
209 214 }
210 215 },
211 216
14 tests/Geometry/Polygon.html
@@ -385,6 +385,20 @@
385 385 t.ok(geodesicErr < planarErr, "geodesic measure is better (" + geodesicErr.toFixed(3) + " vs. " + planarErr.toFixed(3) + ")");
386 386
387 387 }
  388 +
  389 + function testGetCentroid(t) {
  390 + t.plan(4);
  391 + var bounds = new OpenLayers.Bounds(5, 10, 5, 10);
  392 + var geometry = bounds.toGeometry();
  393 + var centroid = geometry.getCentroid();
  394 + t.eq(geometry.components[0].components.length, 2, "only two vertices since the box has left=right and bottom=top");
  395 + t.ok(centroid && centroid.x === 5 && centroid.y === 10, "getCentroid returns a point geometry even if the ring of the polygon has only 2 vertices");
  396 + bounds = new OpenLayers.Bounds(0, 0, 10, 10);
  397 + geometry = bounds.toGeometry();
  398 + centroid = geometry.getCentroid();
  399 + t.eq(geometry.components[0].components.length, 5, "five vertices expected");
  400 + t.ok(centroid && centroid.x === 5 && centroid.y === 5, "getCentroid returns the correct point geometry");
  401 + }
388 402
389 403
390 404 </script>

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.