Merge pull request #1096 from tschaub/editable-features

Editable geometries and features.
This commit is contained in:
Tim Schaub
2013-10-03 19:22:30 -07:00
17 changed files with 480 additions and 66 deletions

View File

@@ -155,6 +155,28 @@ describe('ol.Feature', function() {
});
it('triggers a featurechange event', function(done) {
var feature = new ol.Feature();
goog.events.listen(feature, 'featurechange', function(evt) {
expect(evt.target).to.be(feature);
expect(evt.oldExtent).to.be(null);
done();
});
feature.set('foo', 'bar');
});
it('triggers a featurechange event with oldExtent', function(done) {
var feature = new ol.Feature({
geom: new ol.geom.Point([15, 30])
});
goog.events.listen(feature, 'featurechange', function(evt) {
expect(evt.target).to.be(feature);
expect(evt.oldExtent).to.eql([15, 30, 15, 30]);
done();
});
feature.setGeometry(new ol.geom.Point([1, 2]));
});
});
describe('#setGeometry()', function() {
@@ -197,11 +219,34 @@ describe('ol.Feature', function() {
expect(feature.getGeometry()).to.be(point);
});
it('triggers a featurechange event', function(done) {
var feature = new ol.Feature();
goog.events.listen(feature, 'featurechange', function(evt) {
expect(evt.target).to.be(feature);
done();
});
feature.setGeometry('foo', point);
});
it('triggers a featurechange event with old extent', function(done) {
var first = new ol.geom.Point([10, 20]);
var feature = new ol.Feature({geom: first});
var second = new ol.geom.Point([20, 30]);
goog.events.listen(feature, 'featurechange', function(evt) {
expect(evt.target).to.be(feature);
expect(evt.target.getGeometry()).to.be(second);
expect(evt.oldExtent).to.eql(first.getBounds());
done();
});
feature.setGeometry(second);
});
});
});
goog.require('goog.events');
goog.require('goog.object');
goog.require('ol.Feature');
goog.require('ol.geom.Point');

View File

@@ -0,0 +1,37 @@
goog.provide('ol.test.geom.Geometry');
describe('ol.geom.Geometry', function() {
describe('constructor', function() {
it('creates a new geometry', function() {
var geom = new ol.geom.Geometry();
expect(geom).to.be.a(ol.geom.Geometry);
expect(geom).to.be.a(goog.events.EventTarget);
});
});
});
describe('ol.geom.GeometryEvent', function() {
describe('constructor', function() {
it('creates a new event', function() {
var point = new ol.geom.Point([1, 2]);
var bounds = point.getBounds();
var evt = new ol.geom.GeometryEvent('change', point, bounds);
expect(evt).to.be.a(ol.geom.GeometryEvent);
expect(evt).to.be.a(goog.events.Event);
expect(evt.target).to.be(point);
expect(evt.oldExtent).to.be(bounds);
});
});
});
goog.require('goog.events.Event');
goog.require('goog.events.EventTarget');
goog.require('ol.geom.Geometry');
goog.require('ol.geom.GeometryEvent');
goog.require('ol.geom.Point');

View File

@@ -34,6 +34,35 @@ describe('ol.geom.LineString', function() {
});
describe('#setCoordinates()', function() {
it('updates the coordinates', function() {
var line = new ol.geom.LineString([[10, 20], [30, 40]]);
line.setCoordinates([[30, 40], [50, 60]]);
expect(line.getCoordinates()).to.eql([[30, 40], [50, 60]]);
});
it('invalidates bounds', function() {
var line = new ol.geom.LineString([[10, 20], [30, 40]]);
line.setCoordinates([[30, 40], [50, 60]]);
expect(line.getBounds()).to.eql([30, 40, 50, 60]);
});
it('triggers a change event', function(done) {
var line = new ol.geom.LineString([[10, 20], [30, 40]]);
expect(line.getBounds()).to.eql([10, 20, 30, 40]);
goog.events.listen(line, 'change', function(evt) {
expect(evt.target).to.equal(line);
expect(evt.oldExtent).to.eql([10, 20, 30, 40]);
expect(evt.target.getBounds()).to.eql([30, 40, 50, 60]);
expect(evt.target.getCoordinates()).to.eql([[30, 40], [50, 60]]);
done();
});
line.setCoordinates([[30, 40], [50, 60]]);
});
});
describe('#transform()', function() {
var forward = ol.proj.getTransform('EPSG:4326', 'EPSG:3857');
@@ -67,6 +96,7 @@ describe('ol.geom.LineString', function() {
});
goog.require('goog.events');
goog.require('ol.geom.Geometry');
goog.require('ol.geom.LineString');
goog.require('ol.proj');

View File

@@ -34,6 +34,35 @@ describe('ol.geom.Point', function() {
});
describe('#setCoordinates()', function() {
it('updates the coordinates', function() {
var point = new ol.geom.Point([10, 20]);
point.setCoordinates([30, 40]);
expect(point.getCoordinates()).to.eql([30, 40]);
});
it('invalidates bounds', function() {
var point = new ol.geom.Point([10, 20]);
point.setCoordinates([30, 40]);
expect(point.getBounds()).to.eql([30, 40, 30, 40]);
});
it('triggers a change event', function(done) {
var point = new ol.geom.Point([10, 20]);
expect(point.getBounds()).to.eql([10, 20, 10, 20]);
goog.events.listen(point, 'change', function(evt) {
expect(evt.target).to.equal(point);
expect(evt.oldExtent).to.eql([10, 20, 10, 20]);
expect(evt.target.getBounds()).to.eql([30, 40, 30, 40]);
expect(evt.target.getCoordinates()).to.eql([30, 40]);
done();
});
point.setCoordinates([30, 40]);
});
});
describe('#transform()', function() {
var forward = ol.proj.getTransform('EPSG:4326', 'EPSG:3857');
@@ -57,6 +86,7 @@ describe('ol.geom.Point', function() {
});
goog.require('goog.events');
goog.require('ol.geom.Geometry');
goog.require('ol.geom.Point');
goog.require('ol.proj');

View File

@@ -16,15 +16,15 @@ describe('ol.geom.Polygon', function() {
});
describe('#rings', function() {
describe('#getRings()', function() {
it('is an array of LinearRing', function() {
it('returns an array of LinearRing', function() {
var poly = new ol.geom.Polygon([outer, inner1, inner2]);
expect(poly.rings.length).to.be(3);
expect(poly.rings[0]).to.be.a(ol.geom.LinearRing);
expect(poly.rings[1]).to.be.a(ol.geom.LinearRing);
expect(poly.rings[2]).to.be.a(ol.geom.LinearRing);
var rings = poly.getRings();
expect(rings.length).to.be(3);
expect(rings[0]).to.be.a(ol.geom.LinearRing);
expect(rings[1]).to.be.a(ol.geom.LinearRing);
expect(rings[2]).to.be.a(ol.geom.LinearRing);
});
var isClockwise = ol.geom.LinearRing.isClockwise;
@@ -34,7 +34,7 @@ describe('ol.geom.Polygon', function() {
expect(isClockwise(outer)).to.be(false);
var poly = new ol.geom.Polygon([outer]);
var ring = poly.rings[0];
var ring = poly.getRings()[0];
expect(isClockwise(ring.getCoordinates())).to.be(true);
});
@@ -44,7 +44,7 @@ describe('ol.geom.Polygon', function() {
expect(isClockwise(inner)).to.be(true);
var poly = new ol.geom.Polygon([outer, inner]);
var ring = poly.rings[1];
var ring = poly.getRings()[1];
expect(isClockwise(ring.getCoordinates())).to.be(false);
});
@@ -132,8 +132,51 @@ describe('ol.geom.Polygon', 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 poly = new ol.geom.Polygon([outer, inner]);
var rings = poly.getRings();
var bounds = poly.getBounds();
goog.events.listen(poly, 'change', function(evt) {
expect(evt.target).to.be(poly);
expect(evt.oldExtent).to.eql(bounds);
expect(evt.target.getBounds()).to.eql([0, 0, 11, 10]);
done();
});
var outerCoords = rings[0].getCoordinates();
outerCoords[1][0] = 11;
rings[0].setCoordinates(outerCoords);
});
it('is fired when inner ring is modified', function(done) {
var poly = new ol.geom.Polygon([outer, inner]);
var rings = poly.getRings();
var bounds = poly.getBounds();
goog.events.listen(poly, 'change', function(evt) {
expect(evt.target).to.be(poly);
expect(evt.oldExtent).to.eql(bounds);
expect(evt.target.getBounds()).to.eql([0, 0, 10, 10]);
done();
});
var innerCoords = rings[1].getCoordinates();
innerCoords[1][0] = 3;
rings[1].setCoordinates(innerCoords);
});
});
});
goog.require('goog.events');
goog.require('ol.geom.Geometry');
goog.require('ol.geom.LinearRing');
goog.require('ol.geom.Polygon');

View File

@@ -147,6 +147,37 @@ describe('ol.layer.Vector', function() {
});
describe('ol.layer.VectorEvent', function() {
var layer, features;
beforeEach(function() {
features = [
new ol.Feature({
g: new ol.geom.Point([16.0, 48.0])
}),
new ol.Feature({
g: new ol.geom.LineString([[17.0, 49.0], [17.1, 49.1]])
})
];
layer = new ol.layer.Vector({
source: new ol.source.Vector({})
});
layer.addFeatures(features);
});
it('dispatches events on feature change', function(done) {
layer.on('featurechange', function(evt) {
expect(evt.features[0]).to.be(features[0]);
expect(evt.extents[0]).to.eql(features[0].getGeometry().getBounds());
done();
});
features[0].set('foo', 'bar');
});
});
});
goog.require('goog.dispose');

View File

@@ -73,13 +73,15 @@ describe('ol.parser.GeoJSON', function() {
it('encodes point', function() {
var point = new ol.geom.Point([10, 20]);
var geojson = parser.write(point);
expect(point).to.eql(parser.read(geojson));
expect(point.getCoordinates()).to.eql(
parser.read(geojson).getCoordinates());
});
it('encodes linestring', function() {
var linestring = new ol.geom.LineString([[10, 20], [30, 40]]);
var geojson = parser.write(linestring);
expect(linestring).to.eql(parser.read(geojson));
expect(linestring.getCoordinates()).to.eql(
parser.read(geojson).getCoordinates());
});
it('encodes polygon', function() {
@@ -88,7 +90,8 @@ describe('ol.parser.GeoJSON', function() {
inner2 = [[8, 8], [9, 8], [9, 9], [8, 9], [8, 8]];
var polygon = new ol.geom.Polygon([outer, inner1, inner2]);
var geojson = parser.write(polygon);
expect(polygon).to.eql(parser.read(geojson));
expect(polygon.getCoordinates()).to.eql(
parser.read(geojson).getCoordinates());
});
it('encodes geometry collection', function() {
@@ -97,9 +100,12 @@ describe('ol.parser.GeoJSON', function() {
new ol.geom.LineString([[30, 40], [50, 60]])
]);
var geojson = parser.write(collection);
// surprised to see read return an Array of geometries instead of
// a true ol.geom.GeometryCollection, so compare collection.components
expect(collection.components).to.eql(parser.read(geojson));
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());
}
});
it('encodes feature collection', function() {
@@ -107,9 +113,18 @@ describe('ol.parser.GeoJSON', function() {
array = parser.read(str);
var geojson = parser.write(array);
var result = parser.read(geojson);
expect(array.length).to.equal(result.length);
var got, exp, gotAttr, expAttr;
for (var i = 0, ii = array.length; i < ii; ++i) {
expect(array[i].getGeometry()).to.eql(result[i].getGeometry());
expect(array[i].getAttributes()).to.eql(result[i].getAttributes());
got = array[i];
exp = result[i];
expect(got.getGeometry().getCoordinates()).to.eql(
exp.getGeometry().getCoordinates());
gotAttr = got.getAttributes();
delete gotAttr.geometry;
expAttr = exp.getAttributes();
delete expAttr.geometry;
expect(gotAttr).to.eql(expAttr);
}
});
@@ -150,10 +165,11 @@ describe('ol.parser.GeoJSON', function() {
var obj = parser.read(str);
expect(obj).to.be.a(ol.geom.Polygon);
expect(obj.rings.length).to.be(3);
expect(obj.rings[0]).to.be.a(ol.geom.LinearRing);
expect(obj.rings[1]).to.be.a(ol.geom.LinearRing);
expect(obj.rings[2]).to.be.a(ol.geom.LinearRing);
var rings = obj.getRings();
expect(rings.length).to.be(3);
expect(rings[0]).to.be.a(ol.geom.LinearRing);
expect(rings[1]).to.be.a(ol.geom.LinearRing);
expect(rings[2]).to.be.a(ol.geom.LinearRing);
});
it('parses geometry collection', function() {

View File

@@ -75,9 +75,10 @@ describe('ol.parser.WKT', function() {
var wkt = 'POLYGON((30 10,10 20,20 40,40 40,30 10))';
var geom = parser.read(wkt);
expect(geom.getType()).to.eql(ol.geom.GeometryType.POLYGON);
expect(geom.rings.length).to.eql(1);
expect(geom.rings[0].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
expect(geom.rings[0].getCoordinates()).to.eql(
var rings = geom.getRings();
expect(rings.length).to.eql(1);
expect(rings[0].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
expect(rings[0].getCoordinates()).to.eql(
[[30, 10], [10, 20], [20, 40], [40, 40], [30, 10]]);
expect(parser.write(geom)).to.eql(wkt);
@@ -85,12 +86,13 @@ describe('ol.parser.WKT', function() {
wkt = 'POLYGON((35 10,10 20,15 40,45 45,35 10),(20 30,30 20,35 35,20 30))';
geom = parser.read(wkt);
expect(geom.getType()).to.eql(ol.geom.GeometryType.POLYGON);
expect(geom.rings.length).to.eql(2);
expect(geom.rings[0].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
expect(geom.rings[1].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
expect(geom.rings[0].getCoordinates()).to.eql(
var rings = geom.getRings();
expect(rings.length).to.eql(2);
expect(rings[0].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
expect(rings[1].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
expect(rings[0].getCoordinates()).to.eql(
[[35, 10], [10, 20], [15, 40], [45, 45], [35, 10]]);
expect(geom.rings[1].getCoordinates()).to.eql(
expect(rings[1].getCoordinates()).to.eql(
[[20, 30], [30, 20], [35, 35], [20, 30]]);
expect(parser.write(geom)).to.eql(wkt);
@@ -98,9 +100,10 @@ describe('ol.parser.WKT', function() {
wkt = 'POLYGON ( (30 10, 10 20, 20 40, 40 40, 30 10) )';
geom = parser.read(wkt);
expect(geom.getType()).to.eql(ol.geom.GeometryType.POLYGON);
expect(geom.rings.length).to.eql(1);
expect(geom.rings[0].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
expect(geom.rings[0].getCoordinates()).to.eql(
var rings = geom.getRings();
expect(rings.length).to.eql(1);
expect(rings[0].getType()).to.eql(ol.geom.GeometryType.LINEARRING);
expect(rings[0].getCoordinates()).to.eql(
[[30, 10], [10, 20], [20, 40], [40, 40], [30, 10]]);
});
@@ -113,13 +116,13 @@ describe('ol.parser.WKT', function() {
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].rings.length).to.eql(1);
expect(geom.components[1].rings.length).to.eql(2);
expect(geom.components[0].rings[0].getCoordinates()).to.eql(
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(
[[40, 40], [45, 30], [20, 45], [40, 40]]);
expect(geom.components[1].rings[0].getCoordinates()).to.eql(
expect(geom.components[1].getRings()[0].getCoordinates()).to.eql(
[[20, 35], [45, 20], [30, 5], [10, 10], [10, 30], [20, 35]]);
expect(geom.components[1].rings[1].getCoordinates()).to.eql(
expect(geom.components[1].getRings()[1].getCoordinates()).to.eql(
[[30, 20], [20, 25], [20, 15], [30, 20]]);
expect(parser.write(geom)).to.eql(wkt);
@@ -132,13 +135,13 @@ describe('ol.parser.WKT', function() {
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].rings.length).to.eql(1);
expect(geom.components[1].rings.length).to.eql(2);
expect(geom.components[0].rings[0].getCoordinates()).to.eql(
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(
[[40, 40], [45, 30], [20, 45], [40, 40]]);
expect(geom.components[1].rings[0].getCoordinates()).to.eql(
expect(geom.components[1].getRings()[0].getCoordinates()).to.eql(
[[20, 35], [45, 20], [30, 5], [10, 10], [10, 30], [20, 35]]);
expect(geom.components[1].rings[1].getCoordinates()).to.eql(
expect(geom.components[1].getRings()[1].getCoordinates()).to.eql(
[[30, 20], [20, 25], [20, 15], [30, 20]]);
});