Merge pull request #1830 from twpayne/geom-fixes

Extra geometry functions
This commit is contained in:
Tom Payne
2014-03-12 13:24:45 +01:00
15 changed files with 345 additions and 14 deletions

View File

@@ -1,4 +1,5 @@
@exportSymbol ol.geom.LineString
@exportProperty ol.geom.LineString.prototype.appendCoordinate
@exportProperty ol.geom.LineString.prototype.clone
@exportProperty ol.geom.LineString.prototype.getCoordinateAtM
@exportProperty ol.geom.LineString.prototype.getCoordinates

View File

@@ -1,5 +1,7 @@
goog.provide('ol.geom.LineString');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('ol.extent');
goog.require('ol.geom.GeometryType');
goog.require('ol.geom.SimpleGeometry');
@@ -50,6 +52,20 @@ ol.geom.LineString = function(coordinates, opt_layout) {
goog.inherits(ol.geom.LineString, ol.geom.SimpleGeometry);
/**
* @param {ol.Coordinate} coordinate Coordinate.
*/
ol.geom.LineString.prototype.appendCoordinate = function(coordinate) {
goog.asserts.assert(coordinate.length == this.stride);
if (goog.isNull(this.flatCoordinates)) {
this.flatCoordinates = coordinate.slice();
} else {
goog.array.extend(this.flatCoordinates, coordinate);
}
this.dispatchChangeEvent();
};
/**
* @inheritDoc
*/

View File

@@ -1,7 +1,9 @@
@exportSymbol ol.geom.MultiLineString
@exportProperty ol.geom.MultiLineString.prototype.appendLineString
@exportProperty ol.geom.MultiLineString.prototype.clone
@exportProperty ol.geom.MultiLineString.prototype.getCoordinateAtM
@exportProperty ol.geom.MultiLineString.prototype.getCoordinates
@exportProperty ol.geom.MultiLineString.prototype.getLineString
@exportProperty ol.geom.MultiLineString.prototype.getLineStrings
@exportProperty ol.geom.MultiLineString.prototype.getType
@exportProperty ol.geom.MultiLineString.prototype.setCoordinates

View File

@@ -47,6 +47,22 @@ ol.geom.MultiLineString = function(coordinates, opt_layout) {
goog.inherits(ol.geom.MultiLineString, ol.geom.SimpleGeometry);
/**
* @param {ol.geom.LineString} lineString LineString.
*/
ol.geom.MultiLineString.prototype.appendLineString = function(lineString) {
goog.asserts.assert(lineString.getLayout() == this.layout);
if (goog.isNull(this.flatCoordinates)) {
this.flatCoordinates = lineString.getFlatCoordinates().slice();
} else {
goog.array.extend(
this.flatCoordinates, lineString.getFlatCoordinates().slice());
}
this.ends_.push(this.flatCoordinates.length);
this.dispatchChangeEvent();
};
/**
* @inheritDoc
*/
@@ -131,17 +147,40 @@ ol.geom.MultiLineString.prototype.getEnds = function() {
};
/**
* @param {number} index Index.
* @return {ol.geom.LineString} LineString.
*/
ol.geom.MultiLineString.prototype.getLineString = function(index) {
goog.asserts.assert(0 <= index && index < this.ends_.length);
if (index < 0 || this.ends_.length <= index) {
return null;
}
var lineString = new ol.geom.LineString(null);
lineString.setFlatCoordinates(this.layout, this.flatCoordinates.slice(
index === 0 ? 0 : this.ends_[index - 1], this.ends_[index]));
return lineString;
};
/**
* @return {Array.<ol.geom.LineString>} LineStrings.
* @todo stability experimental
*/
ol.geom.MultiLineString.prototype.getLineStrings = function() {
// FIXME we should construct the line strings from the flat coordinates
var coordinates = this.getCoordinates();
var flatCoordinates = this.flatCoordinates;
var ends = this.ends_;
var layout = this.layout;
/** @type {Array.<ol.geom.LineString>} */
var lineStrings = [];
var offset = 0;
var i, ii;
for (i = 0, ii = coordinates.length; i < ii; ++i) {
lineStrings.push(new ol.geom.LineString(coordinates[i]));
for (i = 0, ii = ends.length; i < ii; ++i) {
var end = ends[i];
var lineString = new ol.geom.LineString(null);
lineString.setFlatCoordinates(layout, flatCoordinates.slice(offset, end));
lineStrings.push(lineString);
offset = end;
}
return lineStrings;
};
@@ -222,6 +261,13 @@ ol.geom.MultiLineString.prototype.setCoordinates =
*/
ol.geom.MultiLineString.prototype.setFlatCoordinates =
function(layout, flatCoordinates, ends) {
if (goog.isNull(flatCoordinates)) {
goog.asserts.assert(!goog.isNull(ends) && ends.length === 0);
} else if (ends.length === 0) {
goog.asserts.assert(flatCoordinates.length === 0);
} else {
goog.asserts.assert(flatCoordinates.length == ends[ends.length - 1]);
}
this.setFlatCoordinatesInternal(layout, flatCoordinates);
this.ends_ = ends;
this.dispatchChangeEvent();

View File

@@ -1,6 +1,8 @@
@exportSymbol ol.geom.MultiPoint
@exportProperty ol.geom.MultiPoint.prototype.appendPoint
@exportProperty ol.geom.MultiPoint.prototype.clone
@exportProperty ol.geom.MultiPoint.prototype.getCoordinates
@exportProperty ol.geom.MultiPoint.prototype.getPoint
@exportProperty ol.geom.MultiPoint.prototype.getPoints
@exportProperty ol.geom.MultiPoint.prototype.getType
@exportProperty ol.geom.MultiPoint.prototype.setCoordinates

View File

@@ -1,5 +1,7 @@
goog.provide('ol.geom.MultiPoint');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('ol.extent');
goog.require('ol.geom.GeometryType');
goog.require('ol.geom.Point');
@@ -22,6 +24,20 @@ ol.geom.MultiPoint = function(coordinates, opt_layout) {
goog.inherits(ol.geom.MultiPoint, ol.geom.SimpleGeometry);
/**
* @param {ol.geom.Point} point Point.
*/
ol.geom.MultiPoint.prototype.appendPoint = function(point) {
goog.asserts.assert(point.getLayout() == this.layout);
if (goog.isNull(this.flatCoordinates)) {
this.flatCoordinates = point.getFlatCoordinates().slice();
} else {
goog.array.extend(this.flatCoordinates, point.getFlatCoordinates());
}
this.dispatchChangeEvent();
};
/**
* @inheritDoc
*/
@@ -69,17 +85,39 @@ ol.geom.MultiPoint.prototype.getCoordinates = function() {
};
/**
* @param {number} index Index.
* @return {ol.geom.Point} Point.
*/
ol.geom.MultiPoint.prototype.getPoint = function(index) {
var n = goog.isNull(this.flatCoordinates) ?
0 : this.flatCoordinates.length / this.stride;
goog.asserts.assert(0 <= index && index < n);
if (index < 0 || n <= index) {
return null;
}
var point = new ol.geom.Point(null);
point.setFlatCoordinates(this.layout, this.flatCoordinates.slice(
index * this.stride, (index + 1) * this.stride));
return point;
};
/**
* @return {Array.<ol.geom.Point>} Points.
* @todo stability experimental
*/
ol.geom.MultiPoint.prototype.getPoints = function() {
// FIXME we should construct the points from the flat coordinates
var coordinates = this.getCoordinates();
var flatCoordinates = this.flatCoordinates;
var layout = this.layout;
var stride = this.stride;
/** @type {Array.<ol.geom.Point>} */
var points = [];
var i, ii;
for (i = 0, ii = coordinates.length; i < ii; ++i) {
points.push(new ol.geom.Point(coordinates[i]));
for (i = 0, ii = flatCoordinates.length; i < ii; i += stride) {
var point = new ol.geom.Point(null);
point.setFlatCoordinates(layout, flatCoordinates.slice(i, i + stride));
points.push(point);
}
return points;
};

View File

@@ -1,8 +1,10 @@
@exportSymbol ol.geom.MultiPolygon
@exportProperty ol.geom.MultiPolygon.prototype.appendPolygon
@exportProperty ol.geom.MultiPolygon.prototype.clone
@exportProperty ol.geom.MultiPolygon.prototype.getArea
@exportProperty ol.geom.MultiPolygon.prototype.getCoordinates
@exportProperty ol.geom.MultiPolygon.prototype.getInteriorPoints
@exportProperty ol.geom.MultiPolygon.prototype.getPolygon
@exportProperty ol.geom.MultiPolygon.prototype.getPolygons
@exportProperty ol.geom.MultiPolygon.prototype.getType
@exportProperty ol.geom.MultiPolygon.prototype.setCoordinates

View File

@@ -72,6 +72,31 @@ ol.geom.MultiPolygon = function(coordinates, opt_layout) {
goog.inherits(ol.geom.MultiPolygon, ol.geom.SimpleGeometry);
/**
* @param {ol.geom.Polygon} polygon Polygon.
*/
ol.geom.MultiPolygon.prototype.appendPolygon = function(polygon) {
goog.asserts.assert(polygon.getLayout() == this.layout);
/** @type {Array.<number>} */
var ends;
if (goog.isNull(this.flatCoordinates)) {
this.flatCoordinates = polygon.getFlatCoordinates().slice();
ends = polygon.getEnds().slice();
this.endss_.push();
} else {
var offset = this.flatCoordinates.length;
goog.array.extend(this.flatCoordinates, polygon.getFlatCoordinates());
ends = polygon.getEnds().slice();
var i, ii;
for (i = 0, ii = ends.length; i < ii; ++i) {
ends[i] += offset;
}
}
this.endss_.push(ends);
this.dispatchChangeEvent();
};
/**
* @inheritDoc
*/
@@ -205,6 +230,37 @@ ol.geom.MultiPolygon.prototype.getSimplifiedGeometryInternal =
};
/**
* @param {number} index Index.
* @return {ol.geom.Polygon} Polygon.
*/
ol.geom.MultiPolygon.prototype.getPolygon = function(index) {
goog.asserts.assert(0 <= index && index < this.endss_.length);
if (index < 0 || this.endss_.length <= index) {
return null;
}
var offset;
if (index === 0) {
offset = 0;
} else {
var prevEnds = this.endss_[index - 1];
offset = prevEnds[prevEnds.length - 1];
}
var ends = this.endss_[index].slice();
var end = ends[ends.length - 1];
if (offset !== 0) {
var i, ii;
for (i = 0, ii = ends.length; i < ii; ++i) {
ends[i] -= offset;
}
}
var polygon = new ol.geom.Polygon(null);
polygon.setFlatCoordinates(
this.layout, this.flatCoordinates.slice(offset, end), ends);
return polygon;
};
/**
* @return {Array.<ol.geom.Polygon>} Polygons.
* @todo stability experimental
@@ -217,17 +273,16 @@ ol.geom.MultiPolygon.prototype.getPolygons = function() {
var offset = 0;
var i, ii, j, jj;
for (i = 0, ii = endss.length; i < ii; ++i) {
var ends = endss[i];
var ends = endss[i].slice();
var end = ends[ends.length - 1];
var polygon = new ol.geom.Polygon(null);
var polygonEnds = ends.slice();
if (offset !== 0) {
for (j = 0, jj = polygonEnds.length; j < jj; ++j) {
polygonEnds[j] = polygonEnds[j] - offset;
for (j = 0, jj = ends.length; j < jj; ++j) {
ends[j] -= offset;
}
}
var polygon = new ol.geom.Polygon(null);
polygon.setFlatCoordinates(
layout, flatCoordinates.slice(offset, end), polygonEnds);
layout, flatCoordinates.slice(offset, end), ends);
polygons.push(polygon);
offset = end;
}
@@ -274,6 +329,14 @@ ol.geom.MultiPolygon.prototype.setCoordinates =
*/
ol.geom.MultiPolygon.prototype.setFlatCoordinates =
function(layout, flatCoordinates, endss) {
goog.asserts.assert(!goog.isNull(endss));
if (goog.isNull(flatCoordinates) || flatCoordinates.length === 0) {
goog.asserts.assert(endss.length === 0);
} else {
goog.asserts.assert(endss.length > 0);
var ends = endss[endss.length - 1];
goog.asserts.assert(flatCoordinates.length == ends[ends.length - 1]);
}
this.setFlatCoordinatesInternal(layout, flatCoordinates);
this.endss_ = endss;
this.dispatchChangeEvent();

View File

@@ -1,8 +1,10 @@
@exportSymbol ol.geom.Polygon
@exportProperty ol.geom.Polygon.prototype.appendLinearRing
@exportProperty ol.geom.Polygon.prototype.clone
@exportProperty ol.geom.Polygon.prototype.getArea
@exportProperty ol.geom.Polygon.prototype.getCoordinates
@exportProperty ol.geom.Polygon.prototype.getInteriorPoint
@exportProperty ol.geom.Polygon.prototype.getLinearRing
@exportProperty ol.geom.Polygon.prototype.getLinearRings
@exportProperty ol.geom.Polygon.prototype.getType
@exportProperty ol.geom.Polygon.prototype.setCoordinates

View File

@@ -1,5 +1,7 @@
goog.provide('ol.geom.Polygon');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('ol.extent');
goog.require('ol.geom.GeometryType');
goog.require('ol.geom.LinearRing');
@@ -70,6 +72,21 @@ ol.geom.Polygon = function(coordinates, opt_layout) {
goog.inherits(ol.geom.Polygon, ol.geom.SimpleGeometry);
/**
* @param {ol.geom.LinearRing} linearRing Linear ring.
*/
ol.geom.Polygon.prototype.appendLinearRing = function(linearRing) {
goog.asserts.assert(linearRing.getLayout() == this.layout);
if (goog.isNull(this.flatCoordinates)) {
this.flatCoordinates = linearRing.getFlatCoordinates().slice();
} else {
goog.array.extend(this.flatCoordinates, linearRing.getFlatCoordinates());
}
this.ends_.push(this.flatCoordinates.length);
this.dispatchChangeEvent();
};
/**
* @inheritDoc
*/
@@ -161,6 +178,22 @@ ol.geom.Polygon.prototype.getInteriorPoint = function() {
};
/**
* @param {number} index Index.
* @return {ol.geom.LinearRing} Linear ring.
*/
ol.geom.Polygon.prototype.getLinearRing = function(index) {
goog.asserts.assert(0 <= index && index < this.ends_.length);
if (index < 0 || this.ends_.length <= index) {
return null;
}
var linearRing = new ol.geom.LinearRing(null);
linearRing.setFlatCoordinates(this.layout, this.flatCoordinates.slice(
index === 0 ? 0 : this.ends_[index - 1], this.ends_[index]));
return linearRing;
};
/**
* @return {Array.<ol.geom.LinearRing>} Linear rings.
* @todo stability experimental
@@ -257,6 +290,13 @@ ol.geom.Polygon.prototype.setCoordinates = function(coordinates, opt_layout) {
*/
ol.geom.Polygon.prototype.setFlatCoordinates =
function(layout, flatCoordinates, ends) {
if (goog.isNull(flatCoordinates)) {
goog.asserts.assert(!goog.isNull(ends) && ends.length === 0);
} else if (ends.length === 0) {
goog.asserts.assert(flatCoordinates.length === 0);
} else {
goog.asserts.assert(flatCoordinates.length == ends[ends.length - 1]);
}
this.setFlatCoordinatesInternal(layout, flatCoordinates);
this.ends_ = ends;
this.dispatchChangeEvent();

View File

@@ -37,6 +37,13 @@ describe('ol.geom.LineString', function() {
expect(lineString.getStride()).to.be(2);
});
it('can append coordinates', function() {
lineString.appendCoordinate([1, 2]);
expect(lineString.getCoordinates()).to.eql([[1, 2]]);
lineString.appendCoordinate([3, 4]);
expect(lineString.getCoordinates()).to.eql([[1, 2], [3, 4]]);
});
});
describe('construct with 2D coordinates', function() {

View File

@@ -37,6 +37,17 @@ describe('ol.geom.MultiLineString', function() {
expect(multiLineString.getStride()).to.be(2);
});
it('can append line strings', function() {
multiLineString.appendLineString(
new ol.geom.LineString([[1, 2], [3, 4]]));
expect(multiLineString.getCoordinates()).to.eql(
[[[1, 2], [3, 4]]]);
multiLineString.appendLineString(
new ol.geom.LineString([[5, 6], [7, 8]]));
expect(multiLineString.getCoordinates()).to.eql(
[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
});
});
describe('construct with 2D coordinates', function() {
@@ -142,6 +153,17 @@ describe('ol.geom.MultiLineString', function() {
expect(multiLineString.getStride()).to.be(3);
});
it('can return individual line strings', function() {
var lineString0 = multiLineString.getLineString(0);
expect(lineString0).to.be.an(ol.geom.LineString);
expect(lineString0.getLayout()).to.be(ol.geom.GeometryLayout.XYM);
expect(lineString0.getCoordinates()).to.eql([[1, 2, 3], [4, 5, 6]]);
var lineString1 = multiLineString.getLineString(1);
expect(lineString1).to.be.an(ol.geom.LineString);
expect(lineString1.getLayout()).to.be(ol.geom.GeometryLayout.XYM);
expect(lineString1.getCoordinates()).to.eql([[7, 8, 9], [10, 11, 12]]);
});
describe('#getCoordinateAtM', function() {
describe('with extrapolation and interpolation', function() {

View File

@@ -37,6 +37,13 @@ describe('ol.geom.MultiPoint', function() {
expect(multiPoint.getStride()).to.be(2);
});
it('can append points', function() {
multiPoint.appendPoint(new ol.geom.Point([1, 2]));
expect(multiPoint.getCoordinates()).to.eql([[1, 2]]);
multiPoint.appendPoint(new ol.geom.Point([3, 4]));
expect(multiPoint.getCoordinates()).to.eql([[1, 2], [3, 4]]);
});
});
describe('construct with 2D coordinates', function() {
@@ -125,6 +132,26 @@ describe('ol.geom.MultiPoint', function() {
expect(multiPoint.getStride()).to.be(3);
});
it('can return individual points', function() {
var point0 = multiPoint.getPoint(0);
expect(point0.getLayout()).to.be(ol.geom.GeometryLayout.XYM);
expect(point0.getCoordinates()).to.eql([1, 2, 3]);
var point1 = multiPoint.getPoint(1);
expect(point1.getLayout()).to.be(ol.geom.GeometryLayout.XYM);
expect(point1.getCoordinates()).to.eql([4, 5, 6]);
});
it('can return all points', function() {
var points = multiPoint.getPoints();
expect(points).to.have.length(2);
expect(points[0]).to.be.an(ol.geom.Point);
expect(points[0].getLayout()).to.be(ol.geom.GeometryLayout.XYM);
expect(points[0].getCoordinates()).to.eql([1, 2, 3]);
expect(points[1]).to.be.an(ol.geom.Point);
expect(points[1].getLayout()).to.be(ol.geom.GeometryLayout.XYM);
expect(points[1].getCoordinates()).to.eql([4, 5, 6]);
});
});
describe('construct with 4D coordinates', function() {
@@ -170,3 +197,4 @@ describe('ol.geom.MultiPoint', function() {
goog.require('ol.extent');
goog.require('ol.geom.MultiPoint');
goog.require('ol.geom.Point');

View File

@@ -10,6 +10,28 @@ describe('ol.geom.MultiPolygon', function() {
}).not.to.throwException();
});
describe('with an empty MultiPolygon', function() {
var multiPolygon;
beforeEach(function() {
multiPolygon = new ol.geom.MultiPolygon(null);
});
it('can append polygons', function() {
multiPolygon.appendPolygon(
new ol.geom.Polygon([[[0, 0], [0, 2], [1, 1], [2, 0]]]));
expect(multiPolygon.getCoordinates()).to.eql(
[[[[0, 0], [0, 2], [1, 1], [2, 0]]]]);
multiPolygon.appendPolygon(
new ol.geom.Polygon([[[3, 0], [4, 1], [5, 2], [5, 0]]]));
expect(multiPolygon.getCoordinates()).to.eql([
[[[0, 0], [0, 2], [1, 1], [2, 0]]],
[[[3, 0], [4, 1], [5, 2], [5, 0]]]
]);
});
});
describe('with a simple MultiPolygon', function() {
var multiPolygon;
@@ -20,6 +42,29 @@ describe('ol.geom.MultiPolygon', function() {
]);
});
it('can return individual polygons', function() {
var polygon0 = multiPolygon.getPolygon(0);
expect(polygon0).to.be.an(ol.geom.Polygon);
expect(polygon0.getCoordinates()).to.eql(
[[[0, 0], [0, 2], [1, 1], [2, 0]]]);
var polygon1 = multiPolygon.getPolygon(1);
expect(polygon1).to.be.an(ol.geom.Polygon);
expect(polygon1.getCoordinates()).to.eql(
[[[3, 0], [4, 1], [5, 2], [5, 0]]]);
});
it('can return all polygons', function() {
var polygons = multiPolygon.getPolygons();
expect(polygons).to.be.an(Array);
expect(polygons).to.have.length(2);
expect(polygons[0]).to.be.an(ol.geom.Polygon);
expect(polygons[0].getCoordinates()).to.eql(
[[[0, 0], [0, 2], [1, 1], [2, 0]]]);
expect(polygons[1]).to.be.an(ol.geom.Polygon);
expect(polygons[1].getCoordinates()).to.eql(
[[[3, 0], [4, 1], [5, 2], [5, 0]]]);
});
describe('#getSimplifiedGeometry', function() {
it('returns the expected result', function() {
@@ -38,3 +83,4 @@ describe('ol.geom.MultiPolygon', function() {
goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Polygon');

View File

@@ -37,6 +37,17 @@ describe('ol.geom.Polygon', function() {
expect(polygon.getStride()).to.be(2);
});
it('can append linear rings', function() {
polygon.appendLinearRing(
new ol.geom.LinearRing([[1, 2], [3, 4], [5, 6]]));
expect(polygon.getCoordinates()).to.eql(
[[[1, 2], [3, 4], [5, 6]]]);
polygon.appendLinearRing(
new ol.geom.LinearRing([[7, 8], [9, 10], [11, 12]]));
expect(polygon.getCoordinates()).to.eql(
[[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10], [11, 12]]]);
});
});
describe('construct with 2D coordinates', function() {
@@ -73,6 +84,11 @@ describe('ol.geom.Polygon', function() {
expect(polygon.getStride()).to.be(2);
});
it('can return individual rings', function() {
expect(polygon.getLinearRing(0).getCoordinates()).to.eql(outerRing);
expect(polygon.getLinearRing(1).getCoordinates()).to.eql(innerRing);
});
it('has the expected rings', function() {
var linearRings = polygon.getLinearRings();
expect(linearRings).to.be.an(Array);