Merge pull request #1097 from ahocevar/multi-geometry-events
Fire change events in multi-part geometries
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
goog.provide('ol.geom.AbstractCollection');
|
||||
|
||||
goog.require('goog.events.EventType');
|
||||
goog.require('ol.extent');
|
||||
goog.require('ol.geom.Geometry');
|
||||
goog.require('ol.geom.GeometryEvent');
|
||||
|
||||
|
||||
|
||||
@@ -16,6 +18,7 @@ ol.geom.AbstractCollection = function() {
|
||||
|
||||
/**
|
||||
* @type {Array.<ol.geom.Geometry>}
|
||||
* @protected
|
||||
*/
|
||||
this.components = null;
|
||||
|
||||
@@ -45,6 +48,14 @@ ol.geom.AbstractCollection.prototype.getBounds = function() {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {Array.<ol.geom.Geometry>} Components.
|
||||
*/
|
||||
ol.geom.AbstractCollection.prototype.getComponents = function() {
|
||||
return this.components;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
@@ -64,6 +75,26 @@ ol.geom.AbstractCollection.prototype.getCoordinates = function() {
|
||||
ol.geom.AbstractCollection.prototype.getType = goog.abstractMethod;
|
||||
|
||||
|
||||
/**
|
||||
* Listener for component change events.
|
||||
* @param {ol.geom.GeometryEvent} evt Geometry event.
|
||||
* @protected
|
||||
*/
|
||||
ol.geom.AbstractCollection.prototype.handleComponentChange = function(evt) {
|
||||
this.bounds = null;
|
||||
var oldExtent = ol.extent.createEmpty();
|
||||
var components = this.components;
|
||||
for (var i = components.length - 1; i >= 0; --i) {
|
||||
var component = components[i];
|
||||
ol.extent.extend(oldExtent,
|
||||
component === evt.target && !goog.isNull(evt.oldExtent) ?
|
||||
evt.oldExtent : component.getBounds());
|
||||
}
|
||||
this.dispatchEvent(new ol.geom.GeometryEvent(goog.events.EventType.CHANGE,
|
||||
this, oldExtent));
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
goog.provide('ol.geom.GeometryCollection');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('goog.events');
|
||||
goog.require('goog.events.EventType');
|
||||
goog.require('ol.geom.AbstractCollection');
|
||||
goog.require('ol.geom.Geometry');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
@@ -18,8 +20,14 @@ goog.require('ol.geom.GeometryType');
|
||||
ol.geom.GeometryCollection = function(geometries) {
|
||||
goog.base(this);
|
||||
|
||||
for (var i = geometries.length - 1; i >= 0; --i) {
|
||||
goog.events.listen(geometries[i], goog.events.EventType.CHANGE,
|
||||
this.handleComponentChange, false, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {Array.<ol.geom.Geometry>}
|
||||
* @protected
|
||||
*/
|
||||
this.components = geometries;
|
||||
|
||||
@@ -27,6 +35,19 @@ ol.geom.GeometryCollection = function(geometries) {
|
||||
goog.inherits(ol.geom.GeometryCollection, ol.geom.AbstractCollection);
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.geom.GeometryCollection.prototype.clone = function() {
|
||||
var numComponents = this.components.length;
|
||||
var components = new Array(numComponents);
|
||||
for (var i = 0; i < numComponents; ++i) {
|
||||
components[i] = this.components[i].clone();
|
||||
}
|
||||
return new ol.geom.GeometryCollection(components);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
goog.provide('ol.geom.MultiLineString');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('goog.events');
|
||||
goog.require('goog.events.EventType');
|
||||
goog.require('ol.CoordinateArray');
|
||||
goog.require('ol.geom.AbstractCollection');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
@@ -21,10 +23,14 @@ ol.geom.MultiLineString = function(coordinates) {
|
||||
|
||||
/**
|
||||
* @type {Array.<ol.geom.LineString>}
|
||||
* @protected
|
||||
*/
|
||||
this.components = new Array(numParts);
|
||||
for (var i = 0; i < numParts; ++i) {
|
||||
this.components[i] = new ol.geom.LineString(coordinates[i]);
|
||||
var component = new ol.geom.LineString(coordinates[i]);
|
||||
this.components[i] = component;
|
||||
goog.events.listen(component, goog.events.EventType.CHANGE,
|
||||
this.handleComponentChange, false, this);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
goog.provide('ol.geom.MultiPoint');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('goog.events');
|
||||
goog.require('goog.events.EventType');
|
||||
goog.require('ol.CoordinateArray');
|
||||
goog.require('ol.geom.AbstractCollection');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
@@ -21,10 +23,14 @@ ol.geom.MultiPoint = function(coordinates) {
|
||||
|
||||
/**
|
||||
* @type {Array.<ol.geom.Point>}
|
||||
* @protected
|
||||
*/
|
||||
this.components = new Array(numParts);
|
||||
for (var i = 0; i < numParts; ++i) {
|
||||
this.components[i] = new ol.geom.Point(coordinates[i]);
|
||||
var component = new ol.geom.Point(coordinates[i]);
|
||||
this.components[i] = component;
|
||||
goog.events.listen(component, goog.events.EventType.CHANGE,
|
||||
this.handleComponentChange, false, this);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
goog.provide('ol.geom.MultiPolygon');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('goog.events');
|
||||
goog.require('goog.events.EventType');
|
||||
goog.require('ol.CoordinateArray');
|
||||
goog.require('ol.geom.AbstractCollection');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
@@ -22,10 +24,14 @@ ol.geom.MultiPolygon = function(coordinates) {
|
||||
|
||||
/**
|
||||
* @type {Array.<ol.geom.Polygon>}
|
||||
* @protected
|
||||
*/
|
||||
this.components = new Array(numParts);
|
||||
for (var i = 0; i < numParts; ++i) {
|
||||
this.components[i] = new ol.geom.Polygon(coordinates[i]);
|
||||
var component = new ol.geom.Polygon(coordinates[i]);
|
||||
this.components[i] = component;
|
||||
goog.events.listen(component, goog.events.EventType.CHANGE,
|
||||
this.handleComponentChange, false, this);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -315,8 +315,9 @@ ol.parser.GeoJSON.prototype.encodeGeometry_ = function(geometry) {
|
||||
*/
|
||||
ol.parser.GeoJSON.prototype.encodeGeometryCollection_ = function(collection) {
|
||||
var geometries = [];
|
||||
for (var i = 0, ii = collection.components.length; i < ii; ++i) {
|
||||
geometries.push(this.encodeGeometry_(collection.components[i]));
|
||||
var components = collection.getComponents();
|
||||
for (var i = 0, ii = components.length; i < ii; ++i) {
|
||||
geometries.push(this.encodeGeometry_(components[i]));
|
||||
}
|
||||
return /** @type {GeoJSONGeometryCollection} */({
|
||||
type: 'GeometryCollection',
|
||||
|
||||
@@ -159,8 +159,9 @@ ol.parser.GPX = function(opt_options) {
|
||||
if (geom instanceof ol.geom.LineString) {
|
||||
this.writeNode('trkseg', feature.getGeometry(), undefined, node);
|
||||
} else if (geom instanceof ol.geom.MultiLineString) {
|
||||
for (i = 0, ii = geom.components.length; i < ii; ++i) {
|
||||
this.writeNode('trkseg', geom.components[i], undefined, node);
|
||||
var components = geom.getComponents();
|
||||
for (i = 0, ii = components.length; i < ii; ++i) {
|
||||
this.writeNode('trkseg', components[i], undefined, node);
|
||||
}
|
||||
} else if (geom instanceof ol.geom.Polygon) {
|
||||
rings = geom.getRings();
|
||||
|
||||
@@ -823,8 +823,9 @@ ol.parser.KML = function(opt_options) {
|
||||
},
|
||||
'MultiGeometry': function(geometry) {
|
||||
var node = this.createElementNS('MultiGeometry');
|
||||
for (var i = 0, ii = geometry.components.length; i < ii; ++i) {
|
||||
this.writeNode('_geometry', geometry.components[i], null, node);
|
||||
var components = geometry.getComponents();
|
||||
for (var i = 0, ii = components.length; i < ii; ++i) {
|
||||
this.writeNode('_geometry', components[i], null, node);
|
||||
}
|
||||
return node;
|
||||
},
|
||||
|
||||
@@ -340,8 +340,9 @@ ol.parser.ogc.GML = function(opt_options) {
|
||||
},
|
||||
'MultiPoint': function(geometry) {
|
||||
var node = this.createElementNS('gml:MultiPoint');
|
||||
for (var i = 0, ii = geometry.components.length; i < ii; ++i) {
|
||||
this.writeNode('pointMember', geometry.components[i], null, node);
|
||||
var components = geometry.getComponents();
|
||||
for (var i = 0, ii = components.length; i < ii; ++i) {
|
||||
this.writeNode('pointMember', components[i], null, node);
|
||||
}
|
||||
return node;
|
||||
},
|
||||
@@ -352,9 +353,9 @@ ol.parser.ogc.GML = function(opt_options) {
|
||||
},
|
||||
'MultiLineString': function(geometry) {
|
||||
var node = this.createElementNS('gml:MultiLineString');
|
||||
for (var i = 0, ii = geometry.components.length; i < ii; ++i) {
|
||||
this.writeNode('lineStringMember', geometry.components[i], null,
|
||||
node);
|
||||
var components = geometry.getComponents();
|
||||
for (var i = 0, ii = components.length; i < ii; ++i) {
|
||||
this.writeNode('lineStringMember', components[i], null, node);
|
||||
}
|
||||
return node;
|
||||
},
|
||||
@@ -365,8 +366,9 @@ ol.parser.ogc.GML = function(opt_options) {
|
||||
},
|
||||
'MultiPolygon': function(geometry) {
|
||||
var node = this.createElementNS('gml:MultiPolygon');
|
||||
for (var i = 0, ii = geometry.components.length; i < ii; ++i) {
|
||||
this.writeNode('polygonMember', geometry.components[i], null, node);
|
||||
var components = geometry.getComponents();
|
||||
for (var i = 0, ii = components.length; i < ii; ++i) {
|
||||
this.writeNode('polygonMember', components[i], null, node);
|
||||
}
|
||||
return node;
|
||||
},
|
||||
@@ -377,8 +379,9 @@ ol.parser.ogc.GML = function(opt_options) {
|
||||
},
|
||||
'GeometryCollection': function(geometry) {
|
||||
var node = this.createElementNS('gml:GeometryCollection');
|
||||
for (var i = 0, ii = geometry.components.length; i < ii; ++i) {
|
||||
this.writeNode('geometryMember', geometry.components[i], null, node);
|
||||
var components = geometry.getComponents();
|
||||
for (var i = 0, ii = components.length; i < ii; ++i) {
|
||||
this.writeNode('geometryMember', components[i], null, node);
|
||||
}
|
||||
return node;
|
||||
},
|
||||
|
||||
@@ -349,8 +349,9 @@ ol.parser.ogc.GML_v3 = function(opt_options) {
|
||||
},
|
||||
'MultiCurve': function(geometry) {
|
||||
var node = this.createElementNS('gml:MultiCurve');
|
||||
for (var i = 0, len = geometry.components.length; i < len; ++i) {
|
||||
this.writeNode('curveMember', geometry.components[i], null, node);
|
||||
var components = geometry.getComponents();
|
||||
for (var i = 0, len = components.length; i < len; ++i) {
|
||||
this.writeNode('curveMember', components[i], null, node);
|
||||
}
|
||||
return node;
|
||||
},
|
||||
@@ -365,8 +366,9 @@ ol.parser.ogc.GML_v3 = function(opt_options) {
|
||||
},
|
||||
'MultiSurface': function(geometry) {
|
||||
var node = this.createElementNS('gml:MultiSurface');
|
||||
for (var i = 0, len = geometry.components.length; i < len; ++i) {
|
||||
this.writeNode('surfaceMember', geometry.components[i], null, node);
|
||||
var components = geometry.getComponents();
|
||||
for (var i = 0, len = components.length; i < len; ++i) {
|
||||
this.writeNode('surfaceMember', components[i], null, node);
|
||||
}
|
||||
return node;
|
||||
},
|
||||
|
||||
@@ -171,8 +171,9 @@ ol.parser.WKT.prototype.encodePoint_ = function(geom) {
|
||||
*/
|
||||
ol.parser.WKT.prototype.encodeMultiPoint_ = function(geom) {
|
||||
var array = [];
|
||||
for (var i = 0, ii = geom.components.length; i < ii; ++i) {
|
||||
array.push('(' + this.encodePoint_.apply(this, [geom.components[i]]) + ')');
|
||||
var components = geom.getComponents();
|
||||
for (var i = 0, ii = components.length; i < ii; ++i) {
|
||||
array.push('(' + this.encodePoint_.apply(this, [components[i]]) + ')');
|
||||
}
|
||||
return array.join(',');
|
||||
};
|
||||
@@ -185,8 +186,9 @@ ol.parser.WKT.prototype.encodeMultiPoint_ = function(geom) {
|
||||
*/
|
||||
ol.parser.WKT.prototype.encodeGeometryCollection_ = function(geom) {
|
||||
var array = [];
|
||||
for (var i = 0, ii = geom.components.length; i < ii; ++i) {
|
||||
array.push(this.encode_.apply(this, [geom.components[i]]));
|
||||
var components = geom.getComponents();
|
||||
for (var i = 0, ii = components.length; i < ii; ++i) {
|
||||
array.push(this.encode_.apply(this, [components[i]]));
|
||||
}
|
||||
return array.join(',');
|
||||
};
|
||||
@@ -214,9 +216,10 @@ ol.parser.WKT.prototype.encodeLineString_ = function(geom) {
|
||||
*/
|
||||
ol.parser.WKT.prototype.encodeMultiLineString_ = function(geom) {
|
||||
var array = [];
|
||||
for (var i = 0, ii = geom.components.length; i < ii; ++i) {
|
||||
var components = geom.getComponents();
|
||||
for (var i = 0, ii = components.length; i < ii; ++i) {
|
||||
array.push('(' + this.encodeLineString_.apply(this,
|
||||
[geom.components[i]]) + ')');
|
||||
[components[i]]) + ')');
|
||||
}
|
||||
return array.join(',');
|
||||
};
|
||||
@@ -245,9 +248,9 @@ ol.parser.WKT.prototype.encodePolygon_ = function(geom) {
|
||||
*/
|
||||
ol.parser.WKT.prototype.encodeMultiPolygon_ = function(geom) {
|
||||
var array = [];
|
||||
for (var i = 0, ii = geom.components.length; i < ii; ++i) {
|
||||
array.push('(' + this.encodePolygon_.apply(this,
|
||||
[geom.components[i]]) + ')');
|
||||
var components = geom.getComponents();
|
||||
for (var i = 0, ii = components.length; i < ii; ++i) {
|
||||
array.push('(' + this.encodePolygon_.apply(this, [components[i]]) + ')');
|
||||
}
|
||||
return array.join(',');
|
||||
};
|
||||
|
||||
@@ -172,7 +172,7 @@ ol.renderer.canvas.Vector.prototype.renderLineStringFeatures_ =
|
||||
} else {
|
||||
goog.asserts.assert(geometry instanceof ol.geom.MultiLineString,
|
||||
'Expected MultiLineString');
|
||||
components = geometry.components;
|
||||
components = geometry.getComponents();
|
||||
}
|
||||
for (j = 0, jj = components.length; j < jj; ++j) {
|
||||
line = components[j];
|
||||
@@ -256,7 +256,7 @@ ol.renderer.canvas.Vector.prototype.renderPointFeatures_ =
|
||||
} else {
|
||||
goog.asserts.assert(geometry instanceof ol.geom.MultiPoint,
|
||||
'Expected MultiPoint');
|
||||
components = geometry.components;
|
||||
components = geometry.getComponents();
|
||||
}
|
||||
for (j = 0, jj = components.length; j < jj; ++j) {
|
||||
point = components[j];
|
||||
@@ -358,7 +358,7 @@ ol.renderer.canvas.Vector.prototype.renderPolygonFeatures_ =
|
||||
} else {
|
||||
goog.asserts.assert(geometry instanceof ol.geom.MultiPolygon,
|
||||
'Expected MultiPolygon');
|
||||
components = geometry.components;
|
||||
components = geometry.getComponents();
|
||||
}
|
||||
for (j = 0, jj = components.length; j < jj; ++j) {
|
||||
poly = components[j];
|
||||
@@ -472,7 +472,7 @@ ol.renderer.canvas.Vector.renderCircle_ = function(circle) {
|
||||
*/
|
||||
ol.renderer.canvas.Vector.getLabelVectors = function(geometry) {
|
||||
if (geometry instanceof ol.geom.AbstractCollection) {
|
||||
var components = geometry.components;
|
||||
var components = geometry.getComponents();
|
||||
var numComponents = components.length;
|
||||
var result = [];
|
||||
for (var i = 0; i < numComponents; ++i) {
|
||||
|
||||
@@ -28,10 +28,11 @@ describe('ol.geom.GeometryCollection', function() {
|
||||
var poly = new ol.geom.Polygon([outer, inner1, inner2]);
|
||||
var multi = new ol.geom.GeometryCollection([point, line, poly]);
|
||||
|
||||
expect(multi.components.length).to.be(3);
|
||||
expect(multi.components[0]).to.be.a(ol.geom.Point);
|
||||
expect(multi.components[1]).to.be.a(ol.geom.LineString);
|
||||
expect(multi.components[2]).to.be.a(ol.geom.Polygon);
|
||||
var components = multi.getComponents();
|
||||
expect(components.length).to.be(3);
|
||||
expect(components[0]).to.be.a(ol.geom.Point);
|
||||
expect(components[1]).to.be.a(ol.geom.LineString);
|
||||
expect(components[2]).to.be.a(ol.geom.Polygon);
|
||||
});
|
||||
|
||||
});
|
||||
@@ -45,10 +46,10 @@ describe('ol.geom.GeometryCollection', function() {
|
||||
var multi = new ol.geom.GeometryCollection([point, line, poly]);
|
||||
var clone = multi.clone();
|
||||
expect(clone).to.not.be(multi);
|
||||
var components = clone.components;
|
||||
expect(components[0]).to.eql([10, 20]);
|
||||
expect(components[1]).to.eql([[10, 20], [30, 40]]);
|
||||
expect(components[2]).to.eql([outer, inner1, inner2]);
|
||||
var components = clone.getComponents();
|
||||
expect(components[0].getCoordinates()).to.eql([10, 20]);
|
||||
expect(components[1].getCoordinates()).to.eql([[10, 20], [30, 40]]);
|
||||
expect(components[2].getCoordinates()).to.eql([outer, inner1, inner2]);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -21,9 +21,10 @@ describe('ol.geom.MultiLineString', function() {
|
||||
[[10, 20], [30, 40]],
|
||||
[[20, 30], [40, 50]]]);
|
||||
|
||||
expect(multi.components.length).to.be(2);
|
||||
expect(multi.components[0]).to.be.a(ol.geom.LineString);
|
||||
expect(multi.components[1]).to.be.a(ol.geom.LineString);
|
||||
var components = multi.getComponents();
|
||||
expect(components.length).to.be(2);
|
||||
expect(components[0]).to.be.a(ol.geom.LineString);
|
||||
expect(components[1]).to.be.a(ol.geom.LineString);
|
||||
|
||||
});
|
||||
|
||||
|
||||
@@ -17,9 +17,10 @@ describe('ol.geom.MultiPoint', function() {
|
||||
it('is an array of points', function() {
|
||||
var multi = new ol.geom.MultiPoint([[10, 20], [30, 40]]);
|
||||
|
||||
expect(multi.components.length).to.be(2);
|
||||
expect(multi.components[0]).to.be.a(ol.geom.Point);
|
||||
expect(multi.components[1]).to.be.a(ol.geom.Point);
|
||||
var components = multi.getComponents();
|
||||
expect(components.length).to.be(2);
|
||||
expect(components[0]).to.be.a(ol.geom.Point);
|
||||
expect(components[1]).to.be.a(ol.geom.Point);
|
||||
|
||||
});
|
||||
|
||||
@@ -56,10 +57,11 @@ describe('ol.geom.MultiPoint', function() {
|
||||
var multi = new ol.geom.MultiPoint([[10, 20], [30, 40]]);
|
||||
multi.transform(forward);
|
||||
|
||||
expect(multi.components[0].get(0)).to.roughlyEqual(1113195, 1);
|
||||
expect(multi.components[0].get(1)).to.roughlyEqual(2273031, 1);
|
||||
expect(multi.components[1].get(0)).to.roughlyEqual(3339584, 1);
|
||||
expect(multi.components[1].get(1)).to.roughlyEqual(4865942, 1);
|
||||
var components = multi.getComponents();
|
||||
expect(components[0].get(0)).to.roughlyEqual(1113195, 1);
|
||||
expect(components[0].get(1)).to.roughlyEqual(2273031, 1);
|
||||
expect(components[1].get(0)).to.roughlyEqual(3339584, 1);
|
||||
expect(components[1].get(1)).to.roughlyEqual(4865942, 1);
|
||||
});
|
||||
|
||||
it('inverse transforms a multi-point', function() {
|
||||
@@ -67,10 +69,11 @@ describe('ol.geom.MultiPoint', function() {
|
||||
[[1113195, 2273031], [3339584, 4865942]]);
|
||||
multi.transform(inverse);
|
||||
|
||||
expect(multi.components[0].get(0)).to.roughlyEqual(10, 0.001);
|
||||
expect(multi.components[0].get(1)).to.roughlyEqual(20, 0.001);
|
||||
expect(multi.components[1].get(0)).to.roughlyEqual(30, 0.001);
|
||||
expect(multi.components[1].get(1)).to.roughlyEqual(40, 0.001);
|
||||
var components = multi.getComponents();
|
||||
expect(components[0].get(0)).to.roughlyEqual(10, 0.001);
|
||||
expect(components[0].get(1)).to.roughlyEqual(20, 0.001);
|
||||
expect(components[1].get(0)).to.roughlyEqual(30, 0.001);
|
||||
expect(components[1].get(1)).to.roughlyEqual(40, 0.001);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -33,9 +33,10 @@ describe('ol.geom.MultiPolygon', function() {
|
||||
[outer1, inner1a, inner1b],
|
||||
[outer2]]);
|
||||
|
||||
expect(multi.components.length).to.be(2);
|
||||
expect(multi.components[0]).to.be.a(ol.geom.Polygon);
|
||||
expect(multi.components[1]).to.be.a(ol.geom.Polygon);
|
||||
var components = multi.getComponents();
|
||||
expect(components.length).to.be(2);
|
||||
expect(components[0]).to.be.a(ol.geom.Polygon);
|
||||
expect(components[1]).to.be.a(ol.geom.Polygon);
|
||||
|
||||
});
|
||||
|
||||
@@ -69,9 +70,53 @@ describe('ol.geom.MultiPolygon', function() {
|
||||
|
||||
});
|
||||
|
||||
describe('change event', function() {
|
||||
|
||||
var outer, inner;
|
||||
beforeEach(function() {
|
||||
outer = [[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]];
|
||||
inner = [[2, 2], [2, 8], [8, 8], [8, 2], [2, 2]];
|
||||
});
|
||||
|
||||
it('is fired when outer ring is modified', function(done) {
|
||||
var multi = new ol.geom.MultiPolygon([[outer, inner], [outer, inner]]);
|
||||
var components = multi.getComponents();
|
||||
var bounds = multi.getBounds();
|
||||
goog.events.listen(multi, 'change', function(evt) {
|
||||
expect(evt.target).to.be(multi);
|
||||
expect(evt.oldExtent).to.eql(bounds);
|
||||
expect(evt.target.getBounds()).to.eql([0, 0, 11, 10]);
|
||||
done();
|
||||
});
|
||||
|
||||
var outerOne = components[0].getRings()[0];
|
||||
var outerCoords = outerOne.getCoordinates();
|
||||
outerCoords[1][0] = 11;
|
||||
outerOne.setCoordinates(outerCoords);
|
||||
});
|
||||
|
||||
it('is fired when inner ring is modified', function(done) {
|
||||
var multi = new ol.geom.MultiPolygon([[outer, inner], [outer, inner]]);
|
||||
var components = multi.getComponents();
|
||||
var bounds = multi.getBounds();
|
||||
goog.events.listen(multi, 'change', function(evt) {
|
||||
expect(evt.target).to.be(multi);
|
||||
expect(evt.oldExtent).to.eql(bounds);
|
||||
expect(evt.target.getBounds()).to.eql([0, 0, 10, 10]);
|
||||
done();
|
||||
});
|
||||
|
||||
var innerTwo = components[1].getRings()[1];
|
||||
var innerCoords = innerTwo.getCoordinates();
|
||||
innerCoords[1][0] = 3;
|
||||
innerTwo.setCoordinates(innerCoords);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
goog.require('goog.events');
|
||||
goog.require('ol.geom.Geometry');
|
||||
goog.require('ol.geom.MultiPolygon');
|
||||
goog.require('ol.geom.Polygon');
|
||||
|
||||
@@ -101,10 +101,10 @@ describe('ol.parser.GeoJSON', function() {
|
||||
]);
|
||||
var geojson = parser.write(collection);
|
||||
var got = parser.read(geojson);
|
||||
expect(collection.components.length).to.equal(got.length);
|
||||
for (var i = 0, ii = collection.components.length; i < ii; ++i) {
|
||||
expect(collection.components[i].getCoordinates()).to.eql(
|
||||
got[i].getCoordinates());
|
||||
var components = collection.getComponents();
|
||||
expect(components.length).to.equal(got.length);
|
||||
for (var i = 0, ii = components.length; i < ii; ++i) {
|
||||
expect(components[i].getCoordinates()).to.eql(got[i].getCoordinates());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -118,10 +118,11 @@ describe('ol.parser.KML', function() {
|
||||
afterLoadXml(url, function(xml) {
|
||||
var obj = parser.read(xml);
|
||||
var geom = obj.features[0].getGeometry();
|
||||
var components = geom.getComponents();
|
||||
expect(geom instanceof ol.geom.GeometryCollection).to.be.ok();
|
||||
expect(geom.components.length).to.eql(2);
|
||||
expect(geom.components[0] instanceof ol.geom.LineString).to.be.ok();
|
||||
expect(geom.components[1] instanceof ol.geom.Point).to.be.ok();
|
||||
expect(components.length).to.eql(2);
|
||||
expect(components[0] instanceof ol.geom.LineString).to.be.ok();
|
||||
expect(components[1] instanceof ol.geom.Point).to.be.ok();
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -346,9 +347,10 @@ describe('ol.parser.KML', function() {
|
||||
expect(alaska).to.be.a(ol.Feature);
|
||||
var geometry = alaska.getGeometry();
|
||||
expect(geometry).to.be.a(ol.geom.GeometryCollection);
|
||||
expect(geometry.components).to.have.length(2);
|
||||
expect(geometry.components[0]).to.be.a(ol.geom.Point);
|
||||
expect(geometry.components[1]).to.be.a(ol.geom.MultiPolygon);
|
||||
var components = geometry.getComponents();
|
||||
expect(components).to.have.length(2);
|
||||
expect(components[0]).to.be.a(ol.geom.Point);
|
||||
expect(components[1]).to.be.a(ol.geom.MultiPolygon);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -19,20 +19,22 @@ describe('ol.parser.WKT', function() {
|
||||
// there are two forms to test
|
||||
var wkt = 'MULTIPOINT((10 40),(40 30),(20 20),(30 10))';
|
||||
var geom = parser.read(wkt);
|
||||
expect(geom.components.length).to.eql(4);
|
||||
expect(geom.components[0].getCoordinates()).to.eql([10, 40]);
|
||||
expect(geom.components[1].getCoordinates()).to.eql([40, 30]);
|
||||
expect(geom.components[2].getCoordinates()).to.eql([20, 20]);
|
||||
expect(geom.components[3].getCoordinates()).to.eql([30, 10]);
|
||||
var components = geom.getComponents();
|
||||
expect(components.length).to.eql(4);
|
||||
expect(components[0].getCoordinates()).to.eql([10, 40]);
|
||||
expect(components[1].getCoordinates()).to.eql([40, 30]);
|
||||
expect(components[2].getCoordinates()).to.eql([20, 20]);
|
||||
expect(components[3].getCoordinates()).to.eql([30, 10]);
|
||||
expect(parser.write(geom)).to.eql(wkt);
|
||||
// this has whitespace
|
||||
wkt = 'MULTIPOINT (10 40, 40 30, 20 20, 30 10)';
|
||||
geom = parser.read(wkt);
|
||||
expect(geom.components.length).to.eql(4);
|
||||
expect(geom.components[0].getCoordinates()).to.eql([10, 40]);
|
||||
expect(geom.components[1].getCoordinates()).to.eql([40, 30]);
|
||||
expect(geom.components[2].getCoordinates()).to.eql([20, 20]);
|
||||
expect(geom.components[3].getCoordinates()).to.eql([30, 10]);
|
||||
components = geom.getComponents();
|
||||
expect(components.length).to.eql(4);
|
||||
expect(components[0].getCoordinates()).to.eql([10, 40]);
|
||||
expect(components[1].getCoordinates()).to.eql([40, 30]);
|
||||
expect(components[2].getCoordinates()).to.eql([20, 20]);
|
||||
expect(components[3].getCoordinates()).to.eql([30, 10]);
|
||||
});
|
||||
|
||||
it('LineString read / written correctly', function() {
|
||||
@@ -53,10 +55,10 @@ describe('ol.parser.WKT', function() {
|
||||
'(40 40,30 30,40 20,30 10))';
|
||||
var geom = parser.read(wkt);
|
||||
expect(geom.getType()).to.eql(ol.geom.GeometryType.MULTILINESTRING);
|
||||
expect(geom.components.length).to.eql(2);
|
||||
expect(geom.components[0].getType()).to.eql(
|
||||
ol.geom.GeometryType.LINESTRING);
|
||||
expect(geom.components[0].getCoordinates()).to.eql(
|
||||
var components = geom.getComponents();
|
||||
expect(components.length).to.eql(2);
|
||||
expect(components[0].getType()).to.eql(ol.geom.GeometryType.LINESTRING);
|
||||
expect(components[0].getCoordinates()).to.eql(
|
||||
[[10, 10], [20, 20], [10, 40]]);
|
||||
expect(parser.write(geom)).to.eql(wkt);
|
||||
// test whitespace when reading
|
||||
@@ -64,10 +66,11 @@ describe('ol.parser.WKT', function() {
|
||||
'(40 40, 30 30, 40 20, 30 10) )';
|
||||
geom = parser.read(wkt);
|
||||
expect(geom.getType()).to.eql(ol.geom.GeometryType.MULTILINESTRING);
|
||||
expect(geom.components.length).to.eql(2);
|
||||
expect(geom.components[0].getType()).to.eql(
|
||||
components = geom.getComponents();
|
||||
expect(components.length).to.eql(2);
|
||||
expect(components[0].getType()).to.eql(
|
||||
ol.geom.GeometryType.LINESTRING);
|
||||
expect(geom.components[0].getCoordinates()).to.eql(
|
||||
expect(components[0].getCoordinates()).to.eql(
|
||||
[[10, 10], [20, 20], [10, 40]]);
|
||||
});
|
||||
|
||||
@@ -113,16 +116,17 @@ describe('ol.parser.WKT', function() {
|
||||
'((20 35,45 20,30 5,10 10,10 30,20 35),(30 20,20 25,20 15,30 20)))';
|
||||
var geom = parser.read(wkt);
|
||||
expect(geom.getType()).to.eql(ol.geom.GeometryType.MULTIPOLYGON);
|
||||
expect(geom.components.length).to.eql(2);
|
||||
expect(geom.components[0].getType()).to.eql(ol.geom.GeometryType.POLYGON);
|
||||
expect(geom.components[1].getType()).to.eql(ol.geom.GeometryType.POLYGON);
|
||||
expect(geom.components[0].getRings().length).to.eql(1);
|
||||
expect(geom.components[1].getRings().length).to.eql(2);
|
||||
expect(geom.components[0].getRings()[0].getCoordinates()).to.eql(
|
||||
var components = geom.getComponents();
|
||||
expect(components.length).to.eql(2);
|
||||
expect(components[0].getType()).to.eql(ol.geom.GeometryType.POLYGON);
|
||||
expect(components[1].getType()).to.eql(ol.geom.GeometryType.POLYGON);
|
||||
expect(components[0].getRings().length).to.eql(1);
|
||||
expect(components[1].getRings().length).to.eql(2);
|
||||
expect(components[0].getRings()[0].getCoordinates()).to.eql(
|
||||
[[40, 40], [45, 30], [20, 45], [40, 40]]);
|
||||
expect(geom.components[1].getRings()[0].getCoordinates()).to.eql(
|
||||
expect(components[1].getRings()[0].getCoordinates()).to.eql(
|
||||
[[20, 35], [45, 20], [30, 5], [10, 10], [10, 30], [20, 35]]);
|
||||
expect(geom.components[1].getRings()[1].getCoordinates()).to.eql(
|
||||
expect(components[1].getRings()[1].getCoordinates()).to.eql(
|
||||
[[30, 20], [20, 25], [20, 15], [30, 20]]);
|
||||
expect(parser.write(geom)).to.eql(wkt);
|
||||
|
||||
@@ -132,40 +136,42 @@ describe('ol.parser.WKT', function() {
|
||||
'( 30 20, 20 25,20 15 ,30 20 ) ))';
|
||||
geom = parser.read(wkt);
|
||||
expect(geom.getType()).to.eql(ol.geom.GeometryType.MULTIPOLYGON);
|
||||
expect(geom.components.length).to.eql(2);
|
||||
expect(geom.components[0].getType()).to.eql(ol.geom.GeometryType.POLYGON);
|
||||
expect(geom.components[1].getType()).to.eql(ol.geom.GeometryType.POLYGON);
|
||||
expect(geom.components[0].getRings().length).to.eql(1);
|
||||
expect(geom.components[1].getRings().length).to.eql(2);
|
||||
expect(geom.components[0].getRings()[0].getCoordinates()).to.eql(
|
||||
var components = geom.getComponents();
|
||||
expect(components.length).to.eql(2);
|
||||
expect(components[0].getType()).to.eql(ol.geom.GeometryType.POLYGON);
|
||||
expect(components[1].getType()).to.eql(ol.geom.GeometryType.POLYGON);
|
||||
expect(components[0].getRings().length).to.eql(1);
|
||||
expect(components[1].getRings().length).to.eql(2);
|
||||
expect(components[0].getRings()[0].getCoordinates()).to.eql(
|
||||
[[40, 40], [45, 30], [20, 45], [40, 40]]);
|
||||
expect(geom.components[1].getRings()[0].getCoordinates()).to.eql(
|
||||
expect(components[1].getRings()[0].getCoordinates()).to.eql(
|
||||
[[20, 35], [45, 20], [30, 5], [10, 10], [10, 30], [20, 35]]);
|
||||
expect(geom.components[1].getRings()[1].getCoordinates()).to.eql(
|
||||
expect(components[1].getRings()[1].getCoordinates()).to.eql(
|
||||
[[30, 20], [20, 25], [20, 15], [30, 20]]);
|
||||
});
|
||||
|
||||
it('GeometryCollection read / written correctly', function() {
|
||||
var wkt = 'GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))';
|
||||
var geom = parser.read(wkt);
|
||||
expect(geom.components.length).to.eql(2);
|
||||
var components = geom.getComponents();
|
||||
expect(components.length).to.eql(2);
|
||||
expect(geom.getType()).to.eql(ol.geom.GeometryType.GEOMETRYCOLLECTION);
|
||||
expect(geom.components[0].getType()).to.eql(ol.geom.GeometryType.POINT);
|
||||
expect(geom.components[1].getType()).to.eql(
|
||||
ol.geom.GeometryType.LINESTRING);
|
||||
expect(geom.components[0].getCoordinates()).to.eql([4, 6]);
|
||||
expect(geom.components[1].getCoordinates()).to.eql([[4, 6], [7, 10]]);
|
||||
expect(components[0].getType()).to.eql(ol.geom.GeometryType.POINT);
|
||||
expect(components[1].getType()).to.eql(ol.geom.GeometryType.LINESTRING);
|
||||
expect(components[0].getCoordinates()).to.eql([4, 6]);
|
||||
expect(components[1].getCoordinates()).to.eql([[4, 6], [7, 10]]);
|
||||
expect(parser.write(geom)).to.eql(wkt);
|
||||
// test whitespace when reading
|
||||
wkt = 'GEOMETRYCOLLECTION ( POINT (4 6), LINESTRING (4 6, 7 10) )';
|
||||
geom = parser.read(wkt);
|
||||
expect(geom.components.length).to.eql(2);
|
||||
components = geom.getComponents();
|
||||
expect(components.length).to.eql(2);
|
||||
expect(geom.getType()).to.eql(ol.geom.GeometryType.GEOMETRYCOLLECTION);
|
||||
expect(geom.components[0].getType()).to.eql(ol.geom.GeometryType.POINT);
|
||||
expect(geom.components[1].getType()).to.eql(
|
||||
expect(components[0].getType()).to.eql(ol.geom.GeometryType.POINT);
|
||||
expect(components[1].getType()).to.eql(
|
||||
ol.geom.GeometryType.LINESTRING);
|
||||
expect(geom.components[0].getCoordinates()).to.eql([4, 6]);
|
||||
expect(geom.components[1].getCoordinates()).to.eql([[4, 6], [7, 10]]);
|
||||
expect(components[0].getCoordinates()).to.eql([4, 6]);
|
||||
expect(components[1].getCoordinates()).to.eql([[4, 6], [7, 10]]);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user