Refactor ol.geom.LineString

This commit is contained in:
Tom Payne
2013-11-09 15:43:09 +01:00
parent a415b66e88
commit ec748f254e
2 changed files with 166 additions and 41 deletions

View File

@@ -1,7 +1,5 @@
goog.provide('ol.geom.LineString');
goog.require('goog.asserts');
goog.require('ol.extent');
goog.require('ol.geom.Geometry');
@@ -10,17 +8,11 @@ goog.require('ol.geom.Geometry');
* @constructor
* @extends {ol.geom.Geometry}
* @param {ol.geom.RawLineString} coordinates Coordinates.
* @param {ol.geom.Layout=} opt_layout Layout.
*/
ol.geom.LineString = function(coordinates) {
ol.geom.LineString = function(coordinates, opt_layout) {
goog.base(this);
/**
* @private
* @type {ol.geom.RawLineString}
*/
this.coordinates_ = coordinates;
this.setCoordinates(coordinates, opt_layout);
};
goog.inherits(ol.geom.LineString, ol.geom.Geometry);
@@ -29,21 +21,8 @@ goog.inherits(ol.geom.LineString, ol.geom.Geometry);
* @return {ol.geom.RawLineString} Coordinates.
*/
ol.geom.LineString.prototype.getCoordinates = function() {
return this.coordinates_;
};
/**
* @inheritDoc
*/
ol.geom.LineString.prototype.getExtent = function(opt_extent) {
if (this.extentRevision != this.revision) {
this.extent = ol.extent.createOrUpdateFromCoordinates(
this.coordinates_, this.extent);
this.extentRevision = this.revision;
}
goog.asserts.assert(goog.isDef(this.extent));
return ol.extent.returnOrUpdate(this.extent, opt_extent);
return ol.geom.inflateCoordinates(
this.flatCoordinates, 0, this.flatCoordinates.length, this.stride);
};
@@ -57,21 +36,11 @@ ol.geom.LineString.prototype.getType = function() {
/**
* @param {ol.geom.RawLineString} coordinates Coordinates.
* @param {ol.geom.Layout=} opt_layout Layout.
*/
ol.geom.LineString.prototype.setCoordinates = function(coordinates) {
this.coordinates_ = coordinates;
ol.geom.LineString.prototype.setCoordinates =
function(coordinates, opt_layout) {
this.setLayout(opt_layout, coordinates, 1);
ol.geom.deflateCoordinates(this.flatCoordinates, 0, coordinates, this.stride);
this.dispatchChangeEvent();
};
/**
* @inheritDoc
*/
ol.geom.LineString.prototype.transform = function(transformFn) {
var coordinates = this.coordinates_;
var i, ii;
for (i = 0, ii = coordinates.length; i < ii; ++i) {
var coordinate = coordinates[i];
transformFn(coordinate, coordinate, 2);
}
};

View File

@@ -0,0 +1,156 @@
goog.provide('ol.test.geom.LineString');
describe('ol.geom.LineString', function() {
describe('construct empty', function() {
var lineString;
beforeEach(function() {
lineString = new ol.geom.LineString([]);
});
it('defaults to layout XY', function() {
expect(lineString.getLayout()).to.be(ol.geom.Layout.XY);
});
it('has empty coordinates', function() {
expect(lineString.getCoordinates()).to.be.empty();
});
it('has an empty extent', function() {
expect(ol.extent.isEmpty(lineString.getExtent())).to.be(true);
});
it('has empty flat coordinates', function() {
expect(lineString.getFlatCoordinates()).to.be.empty();
});
it('has stride the expected stride', function() {
expect(lineString.getStride()).to.be(2);
});
});
describe('construct with 2D coordinates', function() {
var lineString;
beforeEach(function() {
lineString = new ol.geom.LineString([[1, 2], [3, 4]]);
});
it('has the expected layout', function() {
expect(lineString.getLayout()).to.be(ol.geom.Layout.XY);
});
it('has the expected coordinates', function() {
expect(lineString.getCoordinates()).to.eql([[1, 2], [3, 4]]);
});
it('has the expected extent', function() {
expect(lineString.getExtent()).to.eql([1, 2, 3, 4]);
});
it('has the expected flat coordinates', function() {
expect(lineString.getFlatCoordinates()).to.eql([1, 2, 3, 4]);
});
it('has stride the expected stride', function() {
expect(lineString.getStride()).to.be(2);
});
});
describe('construct with 3D coordinates', function() {
var lineString;
beforeEach(function() {
lineString = new ol.geom.LineString([[1, 2, 3], [4, 5, 6]]);
});
it('has the expected layout', function() {
expect(lineString.getLayout()).to.be(ol.geom.Layout.XYZ);
});
it('has the expected coordinates', function() {
expect(lineString.getCoordinates()).to.eql([[1, 2, 3], [4, 5, 6]]);
});
it('has the expected extent', function() {
expect(lineString.getExtent()).to.eql([1, 2, 4, 5]);
});
it('has the expected flat coordinates', function() {
expect(lineString.getFlatCoordinates()).to.eql([1, 2, 3, 4, 5, 6]);
});
it('has the expected stride', function() {
expect(lineString.getStride()).to.be(3);
});
});
describe('construct with 3D coordinates and layout XYM', function() {
var lineString;
beforeEach(function() {
lineString = new ol.geom.LineString(
[[1, 2, 3], [4, 5, 6]], ol.geom.Layout.XYM);
});
it('has the expected layout', function() {
expect(lineString.getLayout()).to.be(ol.geom.Layout.XYM);
});
it('has the expected coordinates', function() {
expect(lineString.getCoordinates()).to.eql([[1, 2, 3], [4, 5, 6]]);
});
it('has the expected extent', function() {
expect(lineString.getExtent()).to.eql([1, 2, 4, 5]);
});
it('has the expected flat coordinates', function() {
expect(lineString.getFlatCoordinates()).to.eql([1, 2, 3, 4, 5, 6]);
});
it('has the expected stride', function() {
expect(lineString.getStride()).to.be(3);
});
});
describe('construct with 4D coordinates', function() {
var lineString;
beforeEach(function() {
lineString = new ol.geom.LineString([[1, 2, 3, 4], [5, 6, 7, 8]]);
});
it('has the expected layout', function() {
expect(lineString.getLayout()).to.be(ol.geom.Layout.XYZM);
});
it('has the expected coordinates', function() {
expect(lineString.getCoordinates()).to.eql([[1, 2, 3, 4], [5, 6, 7, 8]]);
});
it('has the expected extent', function() {
expect(lineString.getExtent()).to.eql([1, 2, 5, 6]);
});
it('has the expected flat coordinates', function() {
expect(lineString.getFlatCoordinates()).to.eql([1, 2, 3, 4, 5, 6, 7, 8]);
});
it('has the expected stride', function() {
expect(lineString.getStride()).to.be(4);
});
});
});
goog.require('ol.extent');
goog.require('ol.geom.LineString');