Skip to content

Commit

Permalink
Add getBounds to geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaub committed Jan 21, 2013
1 parent 216d30d commit b4d44f8
Show file tree
Hide file tree
Showing 16 changed files with 418 additions and 16 deletions.
15 changes: 15 additions & 0 deletions src/ol/geom/geometry.js
@@ -1,3 +1,4 @@
goog.require('ol.Extent');
goog.provide('ol.geom.Coordinate'); goog.provide('ol.geom.Coordinate');
goog.provide('ol.geom.CoordinateArray'); goog.provide('ol.geom.CoordinateArray');
goog.provide('ol.geom.Geometry'); goog.provide('ol.geom.Geometry');
Expand All @@ -10,6 +11,20 @@ goog.provide('ol.geom.Geometry');
ol.geom.Geometry = function() {}; 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>} * @typedef {Array.<number>}
*/ */
Expand Down
70 changes: 70 additions & 0 deletions src/ol/geom/geometrycollection.js
@@ -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;
};
35 changes: 35 additions & 0 deletions src/ol/geom/linestring.js
Expand Up @@ -2,6 +2,7 @@ goog.provide('ol.geom.LineString');


goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('goog.vec.Float64Array'); goog.require('goog.vec.Float64Array');
goog.require('ol.Extent');
goog.require('ol.geom.CoordinateArray'); goog.require('ol.geom.CoordinateArray');
goog.require('ol.geom.Geometry'); goog.require('ol.geom.Geometry');


Expand Down Expand Up @@ -35,4 +36,38 @@ ol.geom.LineString = function(coordinates) {
this.dimension = dimension; this.dimension = dimension;
goog.asserts.assert(this.dimension >= 2); 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_;
}; };
8 changes: 5 additions & 3 deletions src/ol/geom/multilinestring.js
Expand Up @@ -2,17 +2,18 @@ goog.provide('ol.geom.MultiLineString');


goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('ol.geom.CoordinateArray'); goog.require('ol.geom.CoordinateArray');
goog.require('ol.geom.Geometry'); goog.require('ol.geom.GeometryCollection');
goog.require('ol.geom.LineString'); goog.require('ol.geom.LineString');






/** /**
* @constructor * @constructor
* @implements {ol.geom.Geometry} * @extends {ol.geom.GeometryCollection}
* @param {Array.<ol.geom.CoordinateArray>} coordinates Coordinates array. * @param {Array.<ol.geom.CoordinateArray>} coordinates Coordinates array.
*/ */
ol.geom.MultiLineString = function(coordinates) { ol.geom.MultiLineString = function(coordinates) {
goog.base(this);


var numParts = coordinates.length, var numParts = coordinates.length,
dimension; dimension;
Expand All @@ -24,7 +25,7 @@ ol.geom.MultiLineString = function(coordinates) {
for (var i = 0; i < numParts; ++i) { for (var i = 0; i < numParts; ++i) {
this.components[i] = new ol.geom.LineString(coordinates[i]); this.components[i] = new ol.geom.LineString(coordinates[i]);
if (!goog.isDef(dimension)) { if (!goog.isDef(dimension)) {
dimension = this.components[i]; dimension = this.components[i].dimension;
} else { } else {
goog.asserts.assert(this.components[i].dimension === dimension); goog.asserts.assert(this.components[i].dimension === dimension);
} }
Expand All @@ -37,3 +38,4 @@ ol.geom.MultiLineString = function(coordinates) {
goog.asserts.assert(this.dimension >= 2); goog.asserts.assert(this.dimension >= 2);


}; };
goog.inherits(ol.geom.MultiLineString, ol.geom.GeometryCollection);
6 changes: 4 additions & 2 deletions src/ol/geom/multipoint.js
Expand Up @@ -2,17 +2,18 @@ goog.provide('ol.geom.MultiPoint');


goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('ol.geom.CoordinateArray'); goog.require('ol.geom.CoordinateArray');
goog.require('ol.geom.Geometry'); goog.require('ol.geom.GeometryCollection');
goog.require('ol.geom.Point'); goog.require('ol.geom.Point');






/** /**
* @constructor * @constructor
* @implements {ol.geom.Geometry} * @extends {ol.geom.GeometryCollection}
* @param {ol.geom.CoordinateArray} coordinates Coordinates array. * @param {ol.geom.CoordinateArray} coordinates Coordinates array.
*/ */
ol.geom.MultiPoint = function(coordinates) { ol.geom.MultiPoint = function(coordinates) {
goog.base(this);


var numParts = coordinates.length, var numParts = coordinates.length,
dimension; dimension;
Expand All @@ -37,3 +38,4 @@ ol.geom.MultiPoint = function(coordinates) {
goog.asserts.assert(this.dimension >= 2); goog.asserts.assert(this.dimension >= 2);


}; };
goog.inherits(ol.geom.MultiPoint, ol.geom.GeometryCollection);
8 changes: 5 additions & 3 deletions src/ol/geom/multipolygon.js
Expand Up @@ -2,18 +2,19 @@ goog.provide('ol.geom.MultiPolygon');


goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('ol.geom.CoordinateArray'); goog.require('ol.geom.CoordinateArray');
goog.require('ol.geom.Geometry'); goog.require('ol.geom.GeometryCollection');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');






/** /**
* @constructor * @constructor
* @implements {ol.geom.Geometry} * @extends {ol.geom.GeometryCollection}
* @param {Array.<Array.<ol.geom.CoordinateArray>>} coordinates Coordinates * @param {Array.<Array.<ol.geom.CoordinateArray>>} coordinates Coordinates
* array. * array.
*/ */
ol.geom.MultiPolygon = function(coordinates) { ol.geom.MultiPolygon = function(coordinates) {
goog.base(this);


var numParts = coordinates.length, var numParts = coordinates.length,
dimension; dimension;
Expand All @@ -25,7 +26,7 @@ ol.geom.MultiPolygon = function(coordinates) {
for (var i = 0; i < numParts; ++i) { for (var i = 0; i < numParts; ++i) {
this.components[i] = new ol.geom.Polygon(coordinates[i]); this.components[i] = new ol.geom.Polygon(coordinates[i]);
if (!goog.isDef(dimension)) { if (!goog.isDef(dimension)) {
dimension = this.components[i]; dimension = this.components[i].dimension;
} else { } else {
goog.asserts.assert(this.components[i].dimension === dimension); goog.asserts.assert(this.components[i].dimension === dimension);
} }
Expand All @@ -38,3 +39,4 @@ ol.geom.MultiPolygon = function(coordinates) {
goog.asserts.assert(this.dimension >= 2); goog.asserts.assert(this.dimension >= 2);


}; };
goog.inherits(ol.geom.MultiPolygon, ol.geom.GeometryCollection);
20 changes: 20 additions & 0 deletions src/ol/geom/point.js
Expand Up @@ -2,6 +2,7 @@ goog.provide('ol.geom.Point');


goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('goog.vec.Float64Array'); goog.require('goog.vec.Float64Array');
goog.require('ol.Extent');
goog.require('ol.geom.Coordinate'); goog.require('ol.geom.Coordinate');
goog.require('ol.geom.Geometry'); goog.require('ol.geom.Geometry');


Expand All @@ -25,4 +26,23 @@ ol.geom.Point = function(coordinates) {
this.dimension = coordinates.length; this.dimension = coordinates.length;
goog.asserts.assert(this.dimension >= 2); 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_;
}; };
15 changes: 15 additions & 0 deletions src/ol/geom/polygon.js
Expand Up @@ -2,6 +2,7 @@ goog.provide('ol.geom.Polygon');


goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('goog.vec.Float64Array'); goog.require('goog.vec.Float64Array');
goog.require('ol.Extent');
goog.require('ol.geom.CoordinateArray'); goog.require('ol.geom.CoordinateArray');
goog.require('ol.geom.Geometry'); goog.require('ol.geom.Geometry');
goog.require('ol.geom.LinearRing'); goog.require('ol.geom.LinearRing');
Expand Down Expand Up @@ -38,4 +39,18 @@ ol.geom.Polygon = function(coordinates) {
this.dimension = dimension; this.dimension = dimension;
goog.asserts.assert(this.dimension >= 2); 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();
}; };
3 changes: 3 additions & 0 deletions test/ol.html
Expand Up @@ -81,6 +81,9 @@
<script type="text/javascript" src="spec/ol/resolutionconstraint.test.js"></script> <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/view2d.test.js"></script>
<script type="text/javascript" src="spec/ol/io/geojson.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/linearring.test.js"></script>
<script type="text/javascript" src="spec/ol/geom/linestring.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> <script type="text/javascript" src="spec/ol/geom/point.test.js"></script>
Expand Down
4 changes: 2 additions & 2 deletions test/spec/ol/geom/linearring.test.js
Expand Up @@ -15,7 +15,7 @@ describe('ol.geom.LinearRing', function() {


}); });


describe('coordinates', function() { describe('#coordinates', function() {


it('is a Float64Array', function() { it('is a Float64Array', function() {
var ring = new ol.geom.LinearRing([[10, 20], [30, 40]]); var ring = new ol.geom.LinearRing([[10, 20], [30, 40]]);
Expand All @@ -30,7 +30,7 @@ describe('ol.geom.LinearRing', function() {


}); });


describe('dimension', function() { describe('#dimension', function() {


it('can be 2', function() { it('can be 2', function() {
var ring = new ol.geom.LinearRing([[10, 20], [30, 40]]); var ring = new ol.geom.LinearRing([[10, 20], [30, 40]]);
Expand Down
17 changes: 15 additions & 2 deletions test/spec/ol/geom/linestring.test.js
Expand Up @@ -15,7 +15,7 @@ describe('ol.geom.LineString', function() {


}); });


describe('coordinates', function() { describe('#coordinates', function() {


it('is a Float64Array', function() { it('is a Float64Array', function() {
var line = new ol.geom.LineString([[10, 20], [30, 40]]); var line = new ol.geom.LineString([[10, 20], [30, 40]]);
Expand All @@ -30,7 +30,7 @@ describe('ol.geom.LineString', function() {


}); });


describe('dimension', function() { describe('#dimension', function() {


it('can be 2', function() { it('can be 2', function() {
var line = new ol.geom.LineString([[10, 20], [30, 40]]); var line = new ol.geom.LineString([[10, 20], [30, 40]]);
Expand All @@ -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);
});

});



}); });


0 comments on commit b4d44f8

Please sign in to comment.