Permalink
Browse files

Add getBounds to geometry

  • Loading branch information...
1 parent 216d30d commit b4d44f815f7489f85efe635b0c7347624c2be8d6 @tschaub tschaub committed Jan 21, 2013
@@ -1,3 +1,4 @@
+goog.require('ol.Extent');
goog.provide('ol.geom.Coordinate');
goog.provide('ol.geom.CoordinateArray');
goog.provide('ol.geom.Geometry');
@@ -11,6 +12,20 @@ ol.geom.Geometry = function() {};
/**
+ * The dimension of this geometry (2 or 3).
+ * @type {number}
+ */
+ol.geom.Geometry.prototype.dimension;
+
+
+/**
+ * Get the rectangular 2D evelope for this geoemtry.
+ * @return {ol.Extent} The bounding rectangular envelope.
+ */
+ol.geom.Geometry.prototype.getBounds = goog.abstractMethod;
+
+
+/**
* @typedef {Array.<number>}
*/
ol.geom.Coordinate;
@@ -0,0 +1,70 @@
+goog.provide('ol.geom.GeometryCollection');
+
+goog.require('goog.asserts');
+goog.require('ol.Extent');
+goog.require('ol.geom.Geometry');
+
+
+
+/**
+ * A mixed collection of geometries. This constructor is typically not called
+ * directly. Instead call @see ol.geom.GeometryCollection#fromGeometries.
+ * @constructor
+ * @implements {ol.geom.Geometry}
+ */
+ol.geom.GeometryCollection = function() {
+
+ /**
+ * @type {Array.<ol.geom.Geometry>}
+ */
+ this.components = null;
+
+ /**
+ * @type {number}
+ */
+ this.dimension;
+
+ /**
+ * @type {ol.Extent}
+ * @protected
+ */
+ this.bounds = null;
+
+};
+
+
+/**
+ * @inheritDoc
+ */
+ol.geom.GeometryCollection.prototype.getBounds = function() {
+ if (goog.isNull(this.bounds)) {
+ var minX,
+ minY = minX = Number.POSITIVE_INFINITY,
+ maxX,
+ maxY = maxX = Number.NEGATIVE_INFINITY,
+ components = this.components,
+ len = components.length,
+ bounds, i;
+
+ for (i = 0; i < len; ++i) {
+ bounds = components[i].getBounds();
+ minX = Math.min(bounds.minX, minX);
+ minY = Math.min(bounds.minY, minY);
+ maxX = Math.max(bounds.maxX, maxX);
+ maxY = Math.max(bounds.maxY, maxY);
+ }
+ this.bounds = new ol.Extent(minX, minY, maxX, maxY);
+ }
+ return this.bounds;
+};
+
+
+/**
+ * @param {Array.<ol.geom.Geometry>} components Array of geometries.
+ * @return {ol.geom.GeometryCollection} A mixed geometry collection.
+ */
+ol.geom.GeometryCollection.fromGeometries = function(components) {
+ var collection = new ol.geom.GeometryCollection();
+ collection.components = components;
+ return collection;
+};
@@ -2,6 +2,7 @@ goog.provide('ol.geom.LineString');
goog.require('goog.asserts');
goog.require('goog.vec.Float64Array');
+goog.require('ol.Extent');
goog.require('ol.geom.CoordinateArray');
goog.require('ol.geom.Geometry');
@@ -35,4 +36,38 @@ ol.geom.LineString = function(coordinates) {
this.dimension = dimension;
goog.asserts.assert(this.dimension >= 2);
+ /**
+ * @type {ol.Extent}
+ * @private
+ */
+ this.bounds_ = null;
+
+};
+
+
+/**
+ * @inheritDoc
+ */
+ol.geom.LineString.prototype.getBounds = function() {
+ if (goog.isNull(this.bounds_)) {
+ var minX,
+ minY = minX = Number.POSITIVE_INFINITY,
+ maxX,
+ maxY = maxX = Number.NEGATIVE_INFINITY,
+ coordinates = this.coordinates,
+ len = coordinates.length,
+ dim = this.dimension,
+ x, y, i;
+
+ for (i = 0; i < len; i += dim) {
+ x = coordinates[i];
+ y = coordinates[i + 1];
+ minX = Math.min(minX, x);
+ minY = Math.min(minY, y);
+ maxX = Math.max(maxX, x);
+ maxY = Math.max(maxY, y);
+ }
+ this.bounds_ = new ol.Extent(minX, minY, maxX, maxY);
+ }
+ return this.bounds_;
};
@@ -2,17 +2,18 @@ goog.provide('ol.geom.MultiLineString');
goog.require('goog.asserts');
goog.require('ol.geom.CoordinateArray');
-goog.require('ol.geom.Geometry');
+goog.require('ol.geom.GeometryCollection');
goog.require('ol.geom.LineString');
/**
* @constructor
- * @implements {ol.geom.Geometry}
+ * @extends {ol.geom.GeometryCollection}
* @param {Array.<ol.geom.CoordinateArray>} coordinates Coordinates array.
*/
ol.geom.MultiLineString = function(coordinates) {
+ goog.base(this);
var numParts = coordinates.length,
dimension;
@@ -24,7 +25,7 @@ ol.geom.MultiLineString = function(coordinates) {
for (var i = 0; i < numParts; ++i) {
this.components[i] = new ol.geom.LineString(coordinates[i]);
if (!goog.isDef(dimension)) {
- dimension = this.components[i];
+ dimension = this.components[i].dimension;
} else {
goog.asserts.assert(this.components[i].dimension === dimension);
}
@@ -37,3 +38,4 @@ ol.geom.MultiLineString = function(coordinates) {
goog.asserts.assert(this.dimension >= 2);
};
+goog.inherits(ol.geom.MultiLineString, ol.geom.GeometryCollection);
@@ -2,17 +2,18 @@ goog.provide('ol.geom.MultiPoint');
goog.require('goog.asserts');
goog.require('ol.geom.CoordinateArray');
-goog.require('ol.geom.Geometry');
+goog.require('ol.geom.GeometryCollection');
goog.require('ol.geom.Point');
/**
* @constructor
- * @implements {ol.geom.Geometry}
+ * @extends {ol.geom.GeometryCollection}
* @param {ol.geom.CoordinateArray} coordinates Coordinates array.
*/
ol.geom.MultiPoint = function(coordinates) {
+ goog.base(this);
var numParts = coordinates.length,
dimension;
@@ -37,3 +38,4 @@ ol.geom.MultiPoint = function(coordinates) {
goog.asserts.assert(this.dimension >= 2);
};
+goog.inherits(ol.geom.MultiPoint, ol.geom.GeometryCollection);
@@ -2,18 +2,19 @@ goog.provide('ol.geom.MultiPolygon');
goog.require('goog.asserts');
goog.require('ol.geom.CoordinateArray');
-goog.require('ol.geom.Geometry');
+goog.require('ol.geom.GeometryCollection');
goog.require('ol.geom.Polygon');
/**
* @constructor
- * @implements {ol.geom.Geometry}
+ * @extends {ol.geom.GeometryCollection}
* @param {Array.<Array.<ol.geom.CoordinateArray>>} coordinates Coordinates
* array.
*/
ol.geom.MultiPolygon = function(coordinates) {
+ goog.base(this);
var numParts = coordinates.length,
dimension;
@@ -25,7 +26,7 @@ ol.geom.MultiPolygon = function(coordinates) {
for (var i = 0; i < numParts; ++i) {
this.components[i] = new ol.geom.Polygon(coordinates[i]);
if (!goog.isDef(dimension)) {
- dimension = this.components[i];
+ dimension = this.components[i].dimension;
} else {
goog.asserts.assert(this.components[i].dimension === dimension);
}
@@ -38,3 +39,4 @@ ol.geom.MultiPolygon = function(coordinates) {
goog.asserts.assert(this.dimension >= 2);
};
+goog.inherits(ol.geom.MultiPolygon, ol.geom.GeometryCollection);
View
@@ -2,6 +2,7 @@ goog.provide('ol.geom.Point');
goog.require('goog.asserts');
goog.require('goog.vec.Float64Array');
+goog.require('ol.Extent');
goog.require('ol.geom.Coordinate');
goog.require('ol.geom.Geometry');
@@ -25,4 +26,23 @@ ol.geom.Point = function(coordinates) {
this.dimension = coordinates.length;
goog.asserts.assert(this.dimension >= 2);
+ /**
+ * @type {ol.Extent}
+ * @private
+ */
+ this.bounds_ = null;
+
+};
+
+
+/**
+ * @inheritDoc
+ */
+ol.geom.Point.prototype.getBounds = function() {
+ if (goog.isNull(this.bounds_)) {
+ var x = this.coordinates[0],
+ y = this.coordinates[1];
+ this.bounds_ = new ol.Extent(x, y, x, y);
+ }
+ return this.bounds_;
};
@@ -2,6 +2,7 @@ goog.provide('ol.geom.Polygon');
goog.require('goog.asserts');
goog.require('goog.vec.Float64Array');
+goog.require('ol.Extent');
goog.require('ol.geom.CoordinateArray');
goog.require('ol.geom.Geometry');
goog.require('ol.geom.LinearRing');
@@ -38,4 +39,18 @@ ol.geom.Polygon = function(coordinates) {
this.dimension = dimension;
goog.asserts.assert(this.dimension >= 2);
+ /**
+ * @type {ol.Extent}
+ * @private
+ */
+ this.bounds_ = null;
+
+};
+
+
+/**
+ * @inheritDoc
+ */
+ol.geom.Polygon.prototype.getBounds = function() {
+ return this.rings[0].getBounds();
};
View
@@ -81,6 +81,9 @@
<script type="text/javascript" src="spec/ol/resolutionconstraint.test.js"></script>
<script type="text/javascript" src="spec/ol/view2d.test.js"></script>
<script type="text/javascript" src="spec/ol/io/geojson.test.js"></script>
+ <script type="text/javascript" src="spec/ol/geom/multipoint.test.js"></script>
+ <script type="text/javascript" src="spec/ol/geom/multilinestring.test.js"></script>
+ <script type="text/javascript" src="spec/ol/geom/multipolygon.test.js"></script>
<script type="text/javascript" src="spec/ol/geom/linearring.test.js"></script>
<script type="text/javascript" src="spec/ol/geom/linestring.test.js"></script>
<script type="text/javascript" src="spec/ol/geom/point.test.js"></script>
@@ -15,7 +15,7 @@ describe('ol.geom.LinearRing', function() {
});
- describe('coordinates', function() {
+ describe('#coordinates', function() {
it('is a Float64Array', function() {
var ring = new ol.geom.LinearRing([[10, 20], [30, 40]]);
@@ -30,7 +30,7 @@ describe('ol.geom.LinearRing', function() {
});
- describe('dimension', function() {
+ describe('#dimension', function() {
it('can be 2', function() {
var ring = new ol.geom.LinearRing([[10, 20], [30, 40]]);
@@ -15,7 +15,7 @@ describe('ol.geom.LineString', function() {
});
- describe('coordinates', function() {
+ describe('#coordinates', function() {
it('is a Float64Array', function() {
var line = new ol.geom.LineString([[10, 20], [30, 40]]);
@@ -30,7 +30,7 @@ describe('ol.geom.LineString', function() {
});
- describe('dimension', function() {
+ describe('#dimension', function() {
it('can be 2', function() {
var line = new ol.geom.LineString([[10, 20], [30, 40]]);
@@ -44,6 +44,19 @@ describe('ol.geom.LineString', function() {
});
+ describe('#getBounds()', function() {
+
+ it('returns the bounding extent', function() {
+ var line = new ol.geom.LineString([[10, 20], [20, 30], [30, 40]]);
+ var bounds = line.getBounds();
+ expect(bounds.minX).toBe(10);
+ expect(bounds.minY).toBe(20);
+ expect(bounds.maxX).toBe(30);
+ expect(bounds.maxY).toBe(40);
+ });
+
+ });
+
});
Oops, something went wrong.

0 comments on commit b4d44f8

Please sign in to comment.