Add geom.Collection & geom.collection.
Also make MultiPoint and multipoint inherit from Collection/collection respectively.
This commit is contained in:
133
src/api/geom/collection.js
Normal file
133
src/api/geom/collection.js
Normal 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;
|
||||
};
|
||||
@@ -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
61
src/ol/geom/Collection.js
Normal 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);
|
||||
};
|
||||
@@ -50,6 +50,8 @@
|
||||
<script type="text/javascript" src="spec/api/geom/geom.test.js"></script>
|
||||
<script type="text/javascript" src="spec/api/geom/point.test.js"></script>
|
||||
<script type="text/javascript" src="spec/api/geom/multipoint.test.js"></script>
|
||||
<script type="text/javascript" src="spec/api/geom/linestring.test.js"></script>
|
||||
<script type="text/javascript" src="spec/api/geom/collection.test.js"></script>
|
||||
<script type="text/javascript" src="spec/api/loc.test.js"></script>
|
||||
<script type="text/javascript" src="spec/api/map.test.js"></script>
|
||||
<script type="text/javascript" src="spec/api/projection.test.js"></script>
|
||||
@@ -63,6 +65,8 @@
|
||||
<script type="text/javascript" src="spec/ol/TileSet.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/geom/Point.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/geom/MultiPoint.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/geom/LineString.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/geom/Collection.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/layer/TileLayer.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/layer/XYZ.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/Feature.test.js"></script>
|
||||
|
||||
185
test/spec/api/geom/collection.test.js
Normal file
185
test/spec/api/geom/collection.test.js
Normal file
@@ -0,0 +1,185 @@
|
||||
describe("ol.geom.collection", function() {
|
||||
var c;
|
||||
beforeEach(function() {
|
||||
c = ol.geom.collection([
|
||||
ol.geom.point([0, 1]),
|
||||
ol.geom.linestring([
|
||||
ol.geom.point([2, 3]),
|
||||
ol.geom.point([4, 5])
|
||||
])
|
||||
]);
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
c = null;
|
||||
});
|
||||
describe("can construct instances with some components", function() {
|
||||
it("works for instances of ol.geom.Geometry", function(){
|
||||
expect( c ).toBeA( ol.geom.Collection );
|
||||
});
|
||||
});
|
||||
|
||||
describe("can construct instances without any components", function() {
|
||||
|
||||
it("works with an empty array", function(){
|
||||
c = ol.geom.collection([]);
|
||||
|
||||
expect( c ).toBeA( ol.geom.Collection );
|
||||
});
|
||||
|
||||
it("works without arguments", function(){
|
||||
c = ol.geom.collection();
|
||||
|
||||
expect( c ).toBeA( ol.geom.Collection );
|
||||
});
|
||||
});
|
||||
|
||||
describe("the method 'add'", function() {
|
||||
it("exists", function(){
|
||||
expect( c.add ).toBeA( Function );
|
||||
});
|
||||
|
||||
describe("can be used as setter", function(){
|
||||
it("works with a single point specification and an index", function(){
|
||||
var p = ol.geom.point([24,7]);
|
||||
c.add(p, 0);
|
||||
|
||||
expect(c.components().length).toBe(3);
|
||||
|
||||
var firstComponent = c.components()[0];
|
||||
|
||||
expect( firstComponent.x() + ',' + firstComponent.y() ).toBe( '24,7' );
|
||||
});
|
||||
|
||||
it("the index is functional", function(){
|
||||
var p = ol.geom.point([24,7]);
|
||||
c.add(p, 1);
|
||||
|
||||
expect(c.components().length).toBe(3);
|
||||
|
||||
var firstComponent = c.components()[0], // untouched
|
||||
secondComponent = c.components()[1], // this should be ours
|
||||
thirdComponent = c.components()[2]; // shifted here
|
||||
|
||||
expect( firstComponent.x() + ',' + firstComponent.y() ).toBe( '0,1' );
|
||||
expect( secondComponent.x() + ',' + secondComponent.y() ).toBe( '24,7' );
|
||||
expect( thirdComponent ).toBeA( ol.geom.LineString );
|
||||
});
|
||||
|
||||
it("the index is optional", function(){
|
||||
var p = ol.geom.point([24,7]);
|
||||
c.add(p);
|
||||
|
||||
expect(c.components().length).toBe(3);
|
||||
|
||||
var thirdComponent = c.components()[2];
|
||||
expect( thirdComponent.x() + ',' + thirdComponent.y() ).toBe( '24,7' );
|
||||
});
|
||||
|
||||
it("returns the collection instance", function(){
|
||||
var p = ol.geom.point([24,7]);
|
||||
var returned = c.add(p);
|
||||
|
||||
expect(returned).toBe(c);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("the method 'addAll'", function(){
|
||||
it("exists", function(){
|
||||
expect( c.addAll ).toBeA( Function );
|
||||
});
|
||||
|
||||
describe("can be used as setter", function(){
|
||||
|
||||
it("works with an array of points and an index", function(){
|
||||
var ps = [
|
||||
ol.geom.point([24,7]),
|
||||
ol.geom.point([7,11])
|
||||
];
|
||||
c.addAll(ps, 0);
|
||||
|
||||
expect(c.components().length).toBe(4);
|
||||
|
||||
var firstComponent = c.components()[0],
|
||||
secondComponent = c.components()[1];
|
||||
|
||||
expect( firstComponent.x() + ',' + firstComponent.y() ).toBe( '24,7' );
|
||||
expect( secondComponent.x() + ',' + secondComponent.y() ).toBe( '7,11' );
|
||||
});
|
||||
|
||||
it("the index is functional", function(){
|
||||
var ps = [
|
||||
ol.geom.point([24,7]),
|
||||
ol.geom.point({x:7, y:11})
|
||||
];
|
||||
c.addAll(ps, 1);
|
||||
|
||||
expect(c.components().length).toBe(4);
|
||||
|
||||
var firstComponent = c.components()[0], // untouched
|
||||
secondComponent = c.components()[1], // this should be ours
|
||||
thirdComponent = c.components()[2], // this should be ours
|
||||
fourthComponent = c.components()[3]; // shifted here
|
||||
|
||||
expect( firstComponent.x() + ',' + firstComponent.y() ).toBe( '0,1' );
|
||||
expect( secondComponent.x() + ',' + secondComponent.y() ).toBe( '24,7' );
|
||||
expect( thirdComponent.x() + ',' + thirdComponent.y() ).toBe( '7,11' );
|
||||
expect( fourthComponent ).toBeA( ol.geom.LineString );
|
||||
});
|
||||
|
||||
it("the index is optional", function(){
|
||||
var ps = [
|
||||
ol.geom.point([24,7]),
|
||||
ol.geom.point({x:7, y:11})
|
||||
];
|
||||
c.addAll(ps);
|
||||
|
||||
expect(c.components().length).toBe(4);
|
||||
|
||||
var thirdComponent = c.components()[2],
|
||||
fourthComponent = c.components()[3];
|
||||
expect( thirdComponent.x() + ',' + thirdComponent.y() ).toBe( '24,7' );
|
||||
expect( fourthComponent.x() + ',' + fourthComponent.y() ).toBe( '7,11' );
|
||||
});
|
||||
|
||||
it("returns the collection instance", function(){
|
||||
var ps = [
|
||||
ol.geom.point([24,7]),
|
||||
ol.geom.point({x:7, y:11})
|
||||
];
|
||||
var returned = c.addAll(ps);
|
||||
|
||||
expect(returned).toBe(c);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe("the method 'remove'", function() {
|
||||
it("exists", function(){
|
||||
expect( c.add ).toBeA( Function );
|
||||
});
|
||||
|
||||
it("works with a single point", function(){
|
||||
var p = c.components()[0];
|
||||
c.remove(p);
|
||||
expect(c.components().length).toBe(1);
|
||||
var firstComponent = c.components()[0];
|
||||
expect( firstComponent ).toBeA( ol.geom.LineString );
|
||||
});
|
||||
|
||||
it("works with an array of point specifications", function(){
|
||||
var ps = [
|
||||
c.components()[1],
|
||||
c.components()[0]
|
||||
];
|
||||
c.remove(ps);
|
||||
expect(c.components().length).toBe(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
114
test/spec/ol/geom/Collection.test.js
Normal file
114
test/spec/ol/geom/Collection.test.js
Normal file
@@ -0,0 +1,114 @@
|
||||
describe("ol.geom.Collection", function() {
|
||||
var c;
|
||||
|
||||
beforeEach(function(){
|
||||
c = new ol.geom.Collection([
|
||||
new ol.geom.Point(10,20),
|
||||
new ol.geom.LineString([
|
||||
new ol.geom.Point(47,11),
|
||||
new ol.geom.Point(3.14,2.8)
|
||||
])
|
||||
]);
|
||||
});
|
||||
|
||||
afterEach(function(){
|
||||
c = null;
|
||||
});
|
||||
|
||||
it("constructs instances", function() {
|
||||
expect( c ).toBeA( ol.geom.Collection );
|
||||
});
|
||||
|
||||
it("can construct instances without any components", function() {
|
||||
// empty array
|
||||
c = new ol.geom.Collection([]);
|
||||
expect( c ).toBeA( ol.geom.Collection );
|
||||
|
||||
// no argument at all
|
||||
c = new ol.geom.Collection();
|
||||
expect( c ).toBeA( ol.geom.Collection );
|
||||
});
|
||||
|
||||
it("inherits from ol.geom.Geometry", function() {
|
||||
expect( c ).toBeA( ol.geom.Geometry );
|
||||
});
|
||||
|
||||
it("has a working getter for components", function() {
|
||||
|
||||
var components = c.getComponents();
|
||||
|
||||
expect( components ).toBeA( Array );
|
||||
expect( components.length ).toBe( 2 );
|
||||
expect( components[0] ).toBeA( ol.geom.Point );
|
||||
expect( components[1] ).toBeA( ol.geom.LineString );
|
||||
|
||||
expect( components[0].getX() + ',' + components[0].getY()).toBe( '10,20' );
|
||||
expect( components[1].getVertices()[0].getX() + ',' + components[1].getVertices()[0].getY()).toBe( '47,11' );
|
||||
|
||||
});
|
||||
|
||||
it("has a working setter for components", function() {
|
||||
|
||||
c.setComponents([
|
||||
new ol.geom.Point(30,40),
|
||||
new ol.geom.LineString([
|
||||
new ol.geom.Point(3,9),
|
||||
new ol.geom.Point(4,16)
|
||||
])
|
||||
]);
|
||||
|
||||
var components = c.getComponents();
|
||||
|
||||
expect( components.length ).toBe( 2 );
|
||||
expect( components[0] ).toBeA( ol.geom.Point );
|
||||
expect( components[1] ).toBeA( ol.geom.LineString );
|
||||
|
||||
expect( components[0].getX() + ',' + components[0].getY()).toBe( '30,40' );
|
||||
expect( components[1].getVertices()[0].getX() + ',' + components[1].getVertices()[0].getY()).toBe( '3,9' );
|
||||
|
||||
});
|
||||
|
||||
it("has a method to add components", function() {
|
||||
|
||||
c.addComponent(
|
||||
new ol.geom.Point(30,40),
|
||||
1
|
||||
);
|
||||
c.addComponent(
|
||||
new ol.geom.LineString([
|
||||
new ol.geom.Point(5,25),
|
||||
new ol.geom.Point(6,36)
|
||||
]),
|
||||
0
|
||||
);
|
||||
|
||||
var components = c.getComponents();
|
||||
|
||||
expect( components.length ).toBe( 4 );
|
||||
expect( components[0].getVertices()[0].getX() + ',' + components[0].getVertices()[0].getY()).toBe( '5,25' );
|
||||
expect( components[1].getX() + ',' + components[1].getY()).toBe( '10,20' );
|
||||
expect( components[2].getX() + ',' + components[2].getY()).toBe( '30,40' );
|
||||
expect( components[3].getVertices()[0].getX() + ',' + components[3].getVertices()[0].getY()).toBe( '47,11' );
|
||||
|
||||
});
|
||||
|
||||
it("has a method to remove components", function() {
|
||||
c.setComponents([
|
||||
new ol.geom.Point(0,10),
|
||||
new ol.geom.Point(10,20),
|
||||
new ol.geom.Point(20,30),
|
||||
new ol.geom.Point(30,40)
|
||||
]);
|
||||
|
||||
var p = c.getComponents()[2]; // 20,30;
|
||||
|
||||
c.removeComponent( p );
|
||||
|
||||
var components = c.getComponents();
|
||||
|
||||
expect( components.length ).toBe( 3 );
|
||||
expect( components[0].getX() + ',' + components[0].getY()).toBe( '0,10' );
|
||||
expect( components[1].getX() + ',' + components[1].getY()).toBe( '10,20' );
|
||||
expect( components[2].getX() + ',' + components[2].getY()).toBe( '30,40' );
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user