Add geom.Collection & geom.collection.

Also make MultiPoint and multipoint inherit from Collection/collection
respectively.
This commit is contained in:
Marc Jansen
2012-06-21 15:53:46 +02:00
parent 6ca9112af2
commit 0374d1abc7
6 changed files with 499 additions and 0 deletions

133
src/api/geom/collection.js Normal file
View File

@@ -0,0 +1,133 @@
goog.provide('ol.geom.collection');
goog.require('ol.geom.Collection');
goog.require('ol.geom.point');
goog.require('ol.projection');
/**
* @export
* @param {Array.<ol.geom.Geometry>} opt_arg Components.
* @return {ol.geom.Collection} Collection.
*/
ol.geom.collection = function(opt_arg){
if (opt_arg instanceof ol.geom.Collection) {
return opt_arg;
}
var components = [];
if (arguments.length == 1 && goog.isDef(opt_arg)) {
if (goog.isArray(opt_arg)) {
var allValid = goog.array.every(opt_arg, function(geom){
if (geom instanceof ol.geom.Geometry) {
components.push(geom);
return true;
} else {
return false;
}
});
if (!allValid) {
var msg = 'ol.geom.collection: at least one component '
+ 'definition was no geometry.';
throw new Error(msg);
}
} else {
throw new Error('ol.geom.collection');
}
}
var c = new ol.geom.Collection(components);
return c;
};
goog.inherits(ol.geom.collection, ol.geom.geometry);
/**
* @export
* @param {Array.<ol.geom.Geometry>=} opt_arg An array of point specifications.
* @return {Array.<ol.geom.Geometry>|ol.geom.Collection|undefined} Result.
*/
ol.geom.Collection.prototype.components = function(opt_arg){
if (arguments.length == 1 && goog.isDef(opt_arg)) {
var components = [],
allValid = false;
allValid = goog.array.every(opt_arg, function(geom){
if (geom instanceof ol.geom.Geometry) {
components.push(geom);
return true;
} else {
return false;
}
});
if (!allValid) {
components = [];
}
this.setComponents(components);
return this;
}
else {
return this.getComponents();
}
};
/**
* @export
* @param {ol.geom.Geometry} geom A geometry.
* @param {number=} opt_index An optional index to add the point(s) at. If not
* provided, the point(s) will be added to the end of the list of components.
* @return {ol.geom.Collection} The Collection instance.
*/
ol.geom.Collection.prototype.add = function(geom, opt_index){
var index = this.components_.length;
if (arguments.length == 2 && goog.isDef(opt_index)) {
index = opt_index;
}
this.addComponent(geom, index);
return this;
};
/**
* @export
* @param {Array.<ol.geom.Geometry>} components Some point specifications.
* @param {number=} opt_index An optional index to add the components at. If not
* provided, the components will be added to the end of the list of
* components.
* @return {ol.geom.Collection} The Collection instance.
*/
ol.geom.Collection.prototype.addAll = function(components, opt_index){
var index = this.components_.length;
if (arguments.length == 2 && goog.isDef(opt_index)) {
index = opt_index;
}
goog.array.every(components, function(c){
this.addComponent(c, index);
index++;
return true;
}, this);
return this;
};
/**
* @export
* @param {(ol.geom.Geometry|Array.<ol.geom.Geometry>)} components A point specification or
* an array of point specifications.
* @return {ol.geom.Collection} The Collection instance.
*/
ol.geom.Collection.prototype.remove = function(components){
var compArr = [];
if (!goog.isArray(components)) {
compArr.push(components);
} else {
compArr = components;
}
goog.array.every(compArr, function(c){
this.removeComponent(c);
return true;
}, this);
return this;
};

View File

@@ -16,6 +16,8 @@ goog.require("ol.TileSet");
goog.require("ol.geom.geometry");
goog.require("ol.geom.point");
goog.require("ol.geom.multipoint");
goog.require("ol.geom.linestring");
goog.require("ol.geom.collection");
goog.require('ol.layer.XYZ');
goog.require('ol.layer.OSM');
goog.require('ol.renderer.TileLayerRenderer');

61
src/ol/geom/Collection.js Normal file
View File

@@ -0,0 +1,61 @@
goog.provide('ol.geom.Collection');
goog.require('goog.array');
goog.require('ol.geom.Geometry');
goog.require('ol.Projection');
/**
* Creates ol.geom.Collection objects.
*
* @extends {ol.geom.Geometry}
* @param {Array.<ol.geom.Geometry>} components An array of components.
*
* @constructor
*/
ol.geom.Collection = function(components) {
/**
* @private
* @type {Array.<ol.geom.Geometry>}
*/
this.components_ = components;
};
goog.inherits(ol.geom.Collection, ol.geom.Geometry);
/**
* Sets the Collection's components.
*
* @return {Array.<ol.geom.Geometry>} An array of components.
*/
ol.geom.Collection.prototype.getComponents = function() {
return this.components_;
};
/**
* Gets the Collection's components.
*
* @param {Array.<ol.geom.Geometry>} components An array of components.
*/
ol.geom.Collection.prototype.setComponents = function(components) {
this.components_ = components;
};
/**
* Adds the given component to the list of components at the specified index.
*
* @param {ol.geom.Geometry} component A component to be added.
* @param {number} index The index where to add.
*/
ol.geom.Collection.prototype.addComponent = function(component, index) {
goog.array.insertAt(this.components_,component,index);
};
/**
* Removes the given component from the list of components.
*
* @param {ol.geom.Geometry} component A component to be removed.
*/
ol.geom.Collection.prototype.removeComponent = function(component) {
goog.array.remove(this.components_, component);
};