Add geom/MultiLineString classes and tests.
This commit is contained in:
61
src/ol/geom/MultiLineString.js
Normal file
61
src/ol/geom/MultiLineString.js
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
goog.provide('ol.geom.MultiLineString');
|
||||||
|
|
||||||
|
goog.require('goog.array');
|
||||||
|
goog.require('ol.geom.Collection');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates ol.geom.MultiLineString objects.
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @extends {ol.geom.Collection}
|
||||||
|
* @param {Array.<ol.geom.LineString>} linestrings An array of linestrings.
|
||||||
|
*
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
ol.geom.MultiLineString = function(linestrings) {
|
||||||
|
this.setTypeWhitelist([ol.geom.LineString]);
|
||||||
|
this.setTypeBlacklist([ol.geom.Geometry]);
|
||||||
|
if (arguments.length === 1 && goog.isDef(linestrings)) {
|
||||||
|
this.setLineStrings(linestrings);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
goog.inherits(ol.geom.MultiLineString, ol.geom.Collection);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the MultiLineString's linestrings.
|
||||||
|
*
|
||||||
|
* @return {Array.<ol.geom.LineString>} An array of linestrings.
|
||||||
|
*/
|
||||||
|
ol.geom.MultiLineString.prototype.getLineStrings = function() {
|
||||||
|
return this.getComponents();
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the MultiLineString's linestrings.
|
||||||
|
*
|
||||||
|
* @param {Array.<ol.geom.LineString>} linestrings An array of linestrings.
|
||||||
|
*/
|
||||||
|
ol.geom.MultiLineString.prototype.setLineStrings = function(linestrings) {
|
||||||
|
this.setComponents(linestrings);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds the given linestring to the list of linestrings at the specified index.
|
||||||
|
*
|
||||||
|
* @param {ol.geom.LineString} linestring A linestring to be added.
|
||||||
|
* @param {number} index The index where to add.
|
||||||
|
*/
|
||||||
|
ol.geom.MultiLineString.prototype.addLineString = function(linestring, index) {
|
||||||
|
this.addComponent(linestring, index);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the given linestring from the list of linestrings.
|
||||||
|
*
|
||||||
|
* @param {ol.geom.LineString} linestring A linestring to be removed.
|
||||||
|
*/
|
||||||
|
ol.geom.MultiLineString.prototype.removeLineString = function(linestring) {
|
||||||
|
this.removeComponent(linestring);
|
||||||
|
};
|
||||||
@@ -85,6 +85,7 @@
|
|||||||
<script type="text/javascript" src="spec/ol/geom/Point.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/MultiPoint.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/MultiLineString.test.js"></script>
|
||||||
<script type="text/javascript" src="spec/ol/geom/Collection.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/TileLayer.test.js"></script>
|
||||||
<script type="text/javascript" src="spec/ol/layer/XYZ.test.js"></script>
|
<script type="text/javascript" src="spec/ol/layer/XYZ.test.js"></script>
|
||||||
|
|||||||
229
test/spec/ol/geom/MultiLineString.test.js
Normal file
229
test/spec/ol/geom/MultiLineString.test.js
Normal file
@@ -0,0 +1,229 @@
|
|||||||
|
describe("ol.geom.MultiLineString", function() {
|
||||||
|
|
||||||
|
var mls,
|
||||||
|
formatPoint = function(p){
|
||||||
|
return p.getX() + ',' + p.getY();
|
||||||
|
},
|
||||||
|
linestringNPointN = function(mls, i, j) {
|
||||||
|
return formatPoint(mls.getLineStrings()[i].getVertices()[j]);
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeEach(function(){
|
||||||
|
mls = new ol.geom.MultiLineString([
|
||||||
|
new ol.geom.LineString([
|
||||||
|
new ol.geom.Point(10,10),
|
||||||
|
new ol.geom.Point(10,0),
|
||||||
|
new ol.geom.Point(20,20)
|
||||||
|
]),
|
||||||
|
new ol.geom.LineString([
|
||||||
|
new ol.geom.Point(30,10),
|
||||||
|
new ol.geom.Point(30,0),
|
||||||
|
new ol.geom.Point(40,20)
|
||||||
|
])
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function(){
|
||||||
|
mls = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
it("constructs instances", function() {
|
||||||
|
expect( mls ).toBeA( ol.geom.MultiLineString );
|
||||||
|
});
|
||||||
|
|
||||||
|
it("can construct instances without any linestrings", function() {
|
||||||
|
// empty array
|
||||||
|
mls = new ol.geom.MultiLineString([]);
|
||||||
|
expect( mls ).toBeA( ol.geom.MultiLineString );
|
||||||
|
|
||||||
|
// no argument at all
|
||||||
|
mls = new ol.geom.MultiLineString();
|
||||||
|
expect( mls ).toBeA( ol.geom.MultiLineString );
|
||||||
|
});
|
||||||
|
|
||||||
|
it("cannot be constructed with component-types other than 'ol.geom.Point'", function() {
|
||||||
|
expect(function(){
|
||||||
|
mls = new ol.geom.MultiLineString([
|
||||||
|
new ol.geom.Point()
|
||||||
|
]);
|
||||||
|
}).toThrow();
|
||||||
|
|
||||||
|
expect(function(){
|
||||||
|
mls = new ol.geom.MultiLineString([
|
||||||
|
new ol.geom.MultiPoint([
|
||||||
|
new ol.geom.Point()
|
||||||
|
])
|
||||||
|
]);
|
||||||
|
}).toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("inherits from ol.geom.Geometry", function() {
|
||||||
|
expect( mls ).toBeA( ol.geom.Geometry );
|
||||||
|
});
|
||||||
|
|
||||||
|
it("has a working getter for linestrings", function() {
|
||||||
|
|
||||||
|
var linestrings = mls.getLineStrings();
|
||||||
|
|
||||||
|
expect( linestrings ).toBeA( Array );
|
||||||
|
expect( linestrings.length ).toBe( 2 );
|
||||||
|
expect( linestrings[0] ).toBeA( ol.geom.LineString );
|
||||||
|
|
||||||
|
expect( linestringNPointN(mls, 0, 0) ).toBe( '10,10' );
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
it("has a working setter for linestrings", function() {
|
||||||
|
|
||||||
|
mls.setLineStrings([
|
||||||
|
new ol.geom.LineString([
|
||||||
|
new ol.geom.Point(-10,10),
|
||||||
|
new ol.geom.Point(-10,0),
|
||||||
|
new ol.geom.Point(-20,20)
|
||||||
|
])
|
||||||
|
]);
|
||||||
|
|
||||||
|
var linestrings = mls.getLineStrings();
|
||||||
|
|
||||||
|
expect( linestrings.length ).toBe( 1 );
|
||||||
|
expect( linestrings[0] ).toBeA( ol.geom.LineString );
|
||||||
|
|
||||||
|
expect( linestringNPointN(mls, 0, 0) ).toBe( '-10,10' );
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
it("has a method to add linestrings", function() {
|
||||||
|
|
||||||
|
mls.addLineString(
|
||||||
|
new ol.geom.LineString([
|
||||||
|
new ol.geom.Point(11,11),
|
||||||
|
new ol.geom.Point(11,1),
|
||||||
|
new ol.geom.Point(21,21)
|
||||||
|
]),
|
||||||
|
1
|
||||||
|
);
|
||||||
|
mls.addLineString(
|
||||||
|
new ol.geom.LineString([
|
||||||
|
new ol.geom.Point(9,9),
|
||||||
|
new ol.geom.Point(9,-1),
|
||||||
|
new ol.geom.Point(19,19)
|
||||||
|
]),
|
||||||
|
0
|
||||||
|
);
|
||||||
|
mls.addLineString(
|
||||||
|
new ol.geom.LineString([
|
||||||
|
new ol.geom.Point(31,11),
|
||||||
|
new ol.geom.Point(31,1),
|
||||||
|
new ol.geom.Point(41,21)
|
||||||
|
]),
|
||||||
|
4
|
||||||
|
);
|
||||||
|
|
||||||
|
var linestrings = mls.getLineStrings();
|
||||||
|
|
||||||
|
expect( linestrings.length ).toBe( 5 );
|
||||||
|
expect( linestringNPointN(mls, 0, 0) ).toBe( '9,9' );
|
||||||
|
expect( linestringNPointN(mls, 1, 0) ).toBe( '10,10' );
|
||||||
|
expect( linestringNPointN(mls, 2, 0) ).toBe( '11,11' );
|
||||||
|
expect( linestringNPointN(mls, 3, 0) ).toBe( '30,10' );
|
||||||
|
expect( linestringNPointN(mls, 4, 0) ).toBe( '31,11' );
|
||||||
|
});
|
||||||
|
|
||||||
|
it("has a method to remove linestrings", function() {
|
||||||
|
mls.setLineStrings([
|
||||||
|
new ol.geom.LineString([
|
||||||
|
new ol.geom.Point(9,9),
|
||||||
|
new ol.geom.Point(9,-1),
|
||||||
|
new ol.geom.Point(19,19)
|
||||||
|
]),
|
||||||
|
new ol.geom.LineString([
|
||||||
|
new ol.geom.Point(10,10),
|
||||||
|
new ol.geom.Point(10,0),
|
||||||
|
new ol.geom.Point(20,20)
|
||||||
|
]),
|
||||||
|
new ol.geom.LineString([
|
||||||
|
new ol.geom.Point(11,11),
|
||||||
|
new ol.geom.Point(11,1),
|
||||||
|
new ol.geom.Point(21,21)
|
||||||
|
])
|
||||||
|
]);
|
||||||
|
|
||||||
|
var ls = mls.getLineStrings()[1]; // p1: 10,10;
|
||||||
|
|
||||||
|
mls.removeLineString( ls );
|
||||||
|
|
||||||
|
var linestrings = mls.getLineStrings();
|
||||||
|
|
||||||
|
expect( linestrings.length ).toBe( 2 );
|
||||||
|
|
||||||
|
expect( linestringNPointN(mls, 0, 0) ).toBe( '9,9' );
|
||||||
|
expect( linestringNPointN(mls, 1, 0) ).toBe( '11,11' );
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('The setters ensure only linestrings can be added', function(){
|
||||||
|
|
||||||
|
it('addLineString cannot be tricked', function(){
|
||||||
|
expect(function(){
|
||||||
|
mls.addLineString(
|
||||||
|
new ol.geom.Point(30,40)
|
||||||
|
);
|
||||||
|
}).toThrow();
|
||||||
|
|
||||||
|
expect(function(){
|
||||||
|
mls.addLineString(
|
||||||
|
new ol.geom.MultiPoint()
|
||||||
|
);
|
||||||
|
}).toThrow();
|
||||||
|
|
||||||
|
expect(function(){
|
||||||
|
mls.addLineString(
|
||||||
|
new ol.geom.Collection()
|
||||||
|
);
|
||||||
|
}).toThrow();
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
it('addComponent cannot be tricked', function(){
|
||||||
|
expect(function(){
|
||||||
|
mls.addComponent(
|
||||||
|
new ol.geom.Point(30,40)
|
||||||
|
);
|
||||||
|
}).toThrow();
|
||||||
|
|
||||||
|
expect(function(){
|
||||||
|
mls.addComponent(
|
||||||
|
new ol.geom.MultiPoint()
|
||||||
|
);
|
||||||
|
}).toThrow();
|
||||||
|
|
||||||
|
expect(function(){
|
||||||
|
mls.addComponent(
|
||||||
|
new ol.geom.Collection()
|
||||||
|
);
|
||||||
|
}).toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("the getCentroid method is functional", function(){
|
||||||
|
it("returns an instance of ol.geom.Point", function(){
|
||||||
|
expect(mls.getCentroid()).toBeA(ol.geom.Point);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("has the expected coordinates", function(){
|
||||||
|
mls.setLineStrings([
|
||||||
|
new ol.geom.LineString([
|
||||||
|
new ol.geom.Point(0,40),
|
||||||
|
new ol.geom.Point(40,40)
|
||||||
|
]),
|
||||||
|
new ol.geom.LineString([
|
||||||
|
new ol.geom.Point(0,0),
|
||||||
|
new ol.geom.Point(40,40)
|
||||||
|
])
|
||||||
|
]);
|
||||||
|
var c = mls.getCentroid();
|
||||||
|
expect( formatPoint(c) ).toBe('20,20');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user