From a89bf0c3295439cf95b25241bdcb24485ccf6048 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Sat, 9 Nov 2013 16:05:30 +0100 Subject: [PATCH] Refactor ol.geom.Point --- src/ol/geom/point.js | 30 +++---- test/spec/ol/format/geojsonformat.test.js | 2 +- test/spec/ol/geom/point.test.js | 97 +++++++++++++++++++++++ test/spec/ol/source/vectorsource.test.js | 6 +- 4 files changed, 114 insertions(+), 21 deletions(-) create mode 100644 test/spec/ol/geom/point.test.js diff --git a/src/ol/geom/point.js b/src/ol/geom/point.js index 90ff74b821..38e7f4eb81 100644 --- a/src/ol/geom/point.js +++ b/src/ol/geom/point.js @@ -9,27 +9,21 @@ goog.require('ol.geom.Geometry'); /** * @constructor * @extends {ol.geom.Geometry} - * @param {ol.geom.RawPoint} coordinate Coordinate. + * @param {ol.geom.RawPoint} coordinates Coordinates. + * @param {ol.geom.Layout=} opt_layout Layout. */ -ol.geom.Point = function(coordinate) { - +ol.geom.Point = function(coordinates, opt_layout) { goog.base(this); - - /** - * @private - * @type {ol.geom.RawPoint} - */ - this.coordinate_ = coordinate; - + this.setCoordinates(coordinates, opt_layout); }; goog.inherits(ol.geom.Point, ol.geom.Geometry); /** - * @return {ol.geom.RawPoint} Coordinate. + * @return {ol.geom.RawPoint} Coordinates. */ -ol.geom.Point.prototype.getCoordinate = function() { - return this.coordinate_; +ol.geom.Point.prototype.getCoordinates = function() { + return this.flatCoordinates.slice(); }; @@ -39,7 +33,7 @@ ol.geom.Point.prototype.getCoordinate = function() { ol.geom.Point.prototype.getExtent = function(opt_extent) { if (this.extentRevision != this.revision) { this.extent = ol.extent.createOrUpdateFromCoordinate( - this.coordinate_, this.extent); + this.flatCoordinates, this.extent); this.extentRevision = this.revision; } goog.asserts.assert(goog.isDef(this.extent)); @@ -56,9 +50,11 @@ ol.geom.Point.prototype.getType = function() { /** - * @param {ol.geom.RawPoint} coordinate Coordinate. + * @param {ol.geom.RawPoint} coordinates Coordinates. + * @param {ol.geom.Layout=} opt_layout Layout. */ -ol.geom.Point.prototype.setCoordinate = function(coordinate) { - this.coordinate_ = coordinate; +ol.geom.Point.prototype.setCoordinates = function(coordinates, opt_layout) { + this.setLayout(opt_layout, coordinates, 0); + this.flatCoordinates = coordinates; this.dispatchChangeEvent(); }; diff --git a/test/spec/ol/format/geojsonformat.test.js b/test/spec/ol/format/geojsonformat.test.js index d088fd3591..155bcb02f4 100644 --- a/test/spec/ol/format/geojsonformat.test.js +++ b/test/spec/ol/format/geojsonformat.test.js @@ -58,7 +58,7 @@ describe('ol.format.GeoJSON', function() { expect(feature).to.be.an(ol.Feature); var geometry = feature.getGeometry(); expect(geometry).to.be.an(ol.geom.Point); - expect(geometry.getCoordinate()).to.eql([102.0, 0.5]); + expect(geometry.getCoordinates()).to.eql([102.0, 0.5]); expect(feature.get('prop0')).to.be('value0'); }); diff --git a/test/spec/ol/geom/point.test.js b/test/spec/ol/geom/point.test.js new file mode 100644 index 0000000000..cea7644fe7 --- /dev/null +++ b/test/spec/ol/geom/point.test.js @@ -0,0 +1,97 @@ +goog.provide('ol.test.geom.Point'); + + +describe('ol.geom.Point', function() { + + describe('construct with 2D coordinates', function() { + + var point; + beforeEach(function() { + point = new ol.geom.Point([1, 2]); + }); + + it('has the expected layout', function() { + expect(point.getLayout()).to.be(ol.geom.Layout.XY); + }); + + it('has the expected coordinates', function() { + expect(point.getCoordinates()).to.eql([1, 2]); + }); + + it('has the expected extent', function() { + expect(point.getExtent()).to.eql([1, 2, 1, 2]); + }); + + it('has the expected flat coordinates', function() { + expect(point.getFlatCoordinates()).to.eql([1, 2]); + }); + + it('has stride the expected stride', function() { + expect(point.getStride()).to.be(2); + }); + + }); + + describe('construct with 3D coordinates and layout XYM', function() { + + var point; + beforeEach(function() { + point = new ol.geom.Point([1, 2, 3], ol.geom.Layout.XYM); + }); + + it('has the expected layout', function() { + expect(point.getLayout()).to.be(ol.geom.Layout.XYM); + }); + + it('has the expected coordinates', function() { + expect(point.getCoordinates()).to.eql([1, 2, 3]); + }); + + it('has the expected extent', function() { + expect(point.getExtent()).to.eql([1, 2, 1, 2]); + }); + + it('has the expected flat coordinates', function() { + expect(point.getFlatCoordinates()).to.eql([1, 2, 3]); + }); + + it('has the expected stride', function() { + expect(point.getStride()).to.be(3); + }); + + }); + + describe('construct with 4D coordinates', function() { + + var point; + beforeEach(function() { + point = new ol.geom.Point([1, 2, 3, 4]); + }); + + it('has the expected layout', function() { + expect(point.getLayout()).to.be(ol.geom.Layout.XYZM); + }); + + it('has the expected coordinates', function() { + expect(point.getCoordinates()).to.eql([1, 2, 3, 4]); + }); + + it('has the expected extent', function() { + expect(point.getExtent()).to.eql([1, 2, 1, 2]); + }); + + it('has the expected flat coordinates', function() { + expect(point.getFlatCoordinates()).to.eql([1, 2, 3, 4]); + }); + + it('has the expected stride', function() { + expect(point.getStride()).to.be(4); + }); + + }); + +}); + + +goog.require('ol.extent'); +goog.require('ol.geom.Point'); diff --git a/test/spec/ol/source/vectorsource.test.js b/test/spec/ol/source/vectorsource.test.js index 9f227eef24..8ade73ae36 100644 --- a/test/spec/ol/source/vectorsource.test.js +++ b/test/spec/ol/source/vectorsource.test.js @@ -128,7 +128,7 @@ describe('ol.source.Vector', function() { it('fires a change event', function() { var listener = sinon.spy(); goog.events.listen(vectorSource, 'change', listener); - features[0].getGeometry().setCoordinate([100, 100]); + features[0].getGeometry().setCoordinates([100, 100]); expect(listener).to.be.called(); }); @@ -136,10 +136,10 @@ describe('ol.source.Vector', function() { it('keeps the R-Tree index up to date', function() { expect(vectorSource.getAllFeaturesInExtent([0, 0, 1, 1])). to.have.length(10); - features[0].getGeometry().setCoordinate([100, 100]); + features[0].getGeometry().setCoordinates([100, 100]); expect(vectorSource.getAllFeaturesInExtent([0, 0, 1, 1])). to.have.length(9); - features[0].getGeometry().setCoordinate([0.5, 0.5]); + features[0].getGeometry().setCoordinates([0.5, 0.5]); expect(vectorSource.getAllFeaturesInExtent([0, 0, 1, 1])). to.have.length(10); });