Add getBounds to geometry

This commit is contained in:
Tim Schaub
2013-01-21 12:59:04 -07:00
parent 216d30ddc1
commit b4d44f815f
16 changed files with 418 additions and 16 deletions

View File

@@ -1,3 +1,4 @@
goog.require('ol.Extent');
goog.provide('ol.geom.Coordinate');
goog.provide('ol.geom.CoordinateArray');
goog.provide('ol.geom.Geometry');
@@ -10,6 +11,20 @@ goog.provide('ol.geom.Geometry');
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>}
*/

View File

@@ -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;
};

View File

@@ -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_;
};

View File

@@ -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);

View File

@@ -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);

View File

@@ -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 File

@@ -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_;
};

View File

@@ -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();
};