diff --git a/src/ol/render/feature.js b/src/ol/render/feature.js index d45e085111..a3d187fc1a 100644 --- a/src/ol/render/feature.js +++ b/src/ol/render/feature.js @@ -1,8 +1,12 @@ goog.provide('ol.render.Feature'); goog.require('ol'); +goog.require('ol.array'); goog.require('ol.extent'); goog.require('ol.geom.GeometryType'); +goog.require('ol.geom.flat.center'); +goog.require('ol.geom.flat.interiorpoint'); +goog.require('ol.geom.flat.interpolate'); goog.require('ol.geom.flat.transform'); goog.require('ol.transform'); @@ -45,6 +49,18 @@ ol.render.Feature = function(type, flatCoordinates, ends, properties, id) { */ this.flatCoordinates_ = flatCoordinates; + /** + * @private + * @type {Array.} + */ + this.flatInteriorPoints_ = null; + + /** + * @private + * @type {Array.} + */ + this.flatMidpoints_ = null; + /** * @private * @type {Array.|Array.>} @@ -102,6 +118,66 @@ ol.render.Feature.prototype.getExtent = function() { return this.extent_; }; + +/** + * @return {Array.} Flat interior points. + */ +ol.render.Feature.prototype.getFlatInteriorPoint = function() { + if (!this.flatInteriorPoints_) { + var flatCenter = ol.extent.getCenter(this.getExtent()); + this.flatInteriorPoints_ = ol.geom.flat.interiorpoint.linearRings( + this.flatCoordinates_, 0, this.ends_, 2, flatCenter, 0); + } + return this.flatInteriorPoints_; +}; + + +/** + * @return {Array.} Flat interior points. + */ +ol.render.Feature.prototype.getFlatInteriorPoints = function() { + if (!this.flatInteriorPoints_) { + var flatCenters = ol.geom.flat.center.linearRingss( + this.flatCoordinates_, 0, this.ends_, 2); + this.flatInteriorPoints_ = ol.geom.flat.interiorpoint.linearRingss( + this.flatCoordinates_, 0, this.ends_, 2, flatCenters); + } + return this.flatInteriorPoints_; +}; + + +/** + * @return {Array.} Flat midpoint. + */ +ol.render.Feature.prototype.getFlatMidpoint = function() { + if (!this.flatMidpoints_) { + this.flatMidpoints_ = ol.geom.flat.interpolate.lineString( + this.flatCoordinates_, 0, this.flatCoordinates_.length, 2, 0.5); + } + return this.flatMidpoints_; +}; + + +/** + * @return {Array.} Flat midpoints. + */ +ol.render.Feature.prototype.getFlatMidpoints = function() { + if (!this.flatMidpoints_) { + this.flatMidpoints_ = []; + var flatCoordinates = this.flatCoordinates_; + var offset = 0; + var ends = this.ends_; + for (var i = 0, ii = ends.length; i < ii; ++i) { + var end = ends[i]; + var midpoint = ol.geom.flat.interpolate.lineString( + flatCoordinates, offset, end, 2, 0.5); + ol.array.extend(this.flatMidpoints_, midpoint); + offset = end; + } + } + return this.flatMidpoints_; +}; + /** * Get the feature identifier. This is a stable identifier for the feature and * is set when reading data from a remote source. diff --git a/test/spec/ol/render/feature.test.js b/test/spec/ol/render/feature.test.js index c73dea5171..2618c54234 100644 --- a/test/spec/ol/render/feature.test.js +++ b/test/spec/ol/render/feature.test.js @@ -1,5 +1,7 @@ - - +goog.require('ol.geom.LineString'); +goog.require('ol.geom.MultiLineString'); +goog.require('ol.geom.MultiPolygon'); +goog.require('ol.geom.Polygon'); goog.require('ol.render.Feature'); @@ -51,6 +53,51 @@ describe('ol.render.Feature', function() { }); }); + describe('#getFlatInteriorPoint()', function() { + it('returns correct point and caches it', function() { + var polygon = new ol.geom.Polygon([[[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]]]); + var feature = new ol.render.Feature('Polygon', polygon.getOrientedFlatCoordinates(), + polygon.getEnds()); + expect(feature.getFlatInteriorPoint()).to.eql([5, 5, 10]); + expect(feature.getFlatInteriorPoint()).to.be(feature.flatInteriorPoints_); + }); + }); + + describe('#getFlatInteriorPoints()', function() { + it('returns correct points and caches them', function() { + var polygon = new ol.geom.MultiPolygon([ + [[[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]]], + [[[10, 0], [10, 10], [20, 10], [20, 0], [10, 0]]] + ]); + var feature = new ol.render.Feature('MultiPolygon', polygon.getOrientedFlatCoordinates(), + polygon.getEndss()); + expect(feature.getFlatInteriorPoints()).to.eql([5, 5, 10, 15, 5, 10]); + expect(feature.getFlatInteriorPoints()).to.be(feature.flatInteriorPoints_); + }); + }); + + describe('#getFlatMidpoint()', function() { + it('returns correct point', function() { + var line = new ol.geom.LineString([[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]]); + var feature = new ol.render.Feature('LineString', line.getFlatCoordinates()); + expect(feature.getFlatMidpoint()).to.eql([10, 10]); + expect(feature.getFlatMidpoint()).to.eql(feature.flatMidpoints_); + }); + }); + + describe('#getFlatMidpoints()', function() { + it('returns correct points and caches them', function() { + var line = new ol.geom.MultiLineString([ + [[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]], + [[10, 0], [10, 10], [20, 10], [20, 0], [10, 0]] + ]); + var feature = new ol.render.Feature('MultiLineString', line.getFlatCoordinates(), + line.getEnds()); + expect(feature.getFlatMidpoints()).to.eql([10, 10, 20, 10]); + expect(feature.getFlatMidpoints()).to.be(feature.flatMidpoints_); + }); + }); + describe('#getGeometry()', function() { it('returns itself as geometry', function() { expect(renderFeature.getGeometry()).to.equal(renderFeature);