Geometries inherit from ol.Observable

Because ol.Observable is now a struct, we have stricter type checking (hence the extra assertions).
This commit is contained in:
Tim Schaub
2013-12-13 14:18:10 -07:00
parent f1efb502ca
commit 5caa0eb659
5 changed files with 28 additions and 9 deletions

View File

@@ -10,6 +10,7 @@ goog.require('ol.MapBrowserEvent');
goog.require('ol.MapBrowserEvent.EventType');
goog.require('ol.geom.GeometryType');
goog.require('ol.geom.LineString');
goog.require('ol.geom.LinearRing');
goog.require('ol.geom.MultiLineString');
goog.require('ol.geom.MultiPoint');
goog.require('ol.geom.MultiPolygon');
@@ -209,6 +210,7 @@ ol.interaction.Draw.prototype.atFinish_ = function(event) {
if (this.mode_ === ol.interaction.DrawMode.LINESTRING) {
potentiallyDone = geometry.getCoordinates().length > 2;
} else if (this.mode_ === ol.interaction.DrawMode.POLYGON) {
goog.asserts.assertInstanceof(geometry, ol.geom.Polygon);
potentiallyDone = geometry.getRings()[0].getCoordinates().length > 3;
}
if (potentiallyDone) {
@@ -270,22 +272,29 @@ ol.interaction.Draw.prototype.modifyDrawing_ = function(event) {
var geometry = this.sketchFeature_.getGeometry();
var coordinates, last;
if (this.mode_ === ol.interaction.DrawMode.POINT) {
goog.asserts.assertInstanceof(geometry, ol.geom.Point);
last = geometry.getCoordinates();
last[0] = coordinate[0];
last[1] = coordinate[1];
geometry.setCoordinates(last);
} else {
if (this.mode_ === ol.interaction.DrawMode.LINESTRING) {
goog.asserts.assertInstanceof(geometry, ol.geom.LineString);
coordinates = geometry.getCoordinates();
} else if (this.mode_ === ol.interaction.DrawMode.POLYGON) {
} else {
goog.asserts.assert(this.mode_ === ol.interaction.DrawMode.POLYGON);
goog.asserts.assertInstanceof(geometry, ol.geom.Polygon);
geometry = geometry.getRings()[0];
goog.asserts.assertInstanceof(geometry, ol.geom.LinearRing);
coordinates = geometry.getCoordinates();
}
if (this.atFinish_(event)) {
// snap to finish
coordinate = this.finishCoordinate_.slice();
}
this.sketchPoint_.getGeometry().setCoordinates(coordinate);
var point = this.sketchPoint_.getGeometry();
goog.asserts.assertInstanceof(point, ol.geom.Point);
point.setCoordinates(coordinate);
last = coordinates[coordinates.length - 1];
last[0] = coordinate[0];
last[1] = coordinate[1];
@@ -304,11 +313,13 @@ ol.interaction.Draw.prototype.addToDrawing_ = function(event) {
var geometry = this.sketchFeature_.getGeometry();
var coordinates, last;
if (this.mode_ === ol.interaction.DrawMode.LINESTRING) {
goog.asserts.assertInstanceof(geometry, ol.geom.LineString);
this.finishCoordinate_ = coordinate.slice();
coordinates = geometry.getCoordinates();
coordinates.push(coordinate.slice());
geometry.setCoordinates(coordinates);
} else if (this.mode_ === ol.interaction.DrawMode.POLYGON) {
goog.asserts.assertInstanceof(geometry, ol.geom.Polygon);
var ring = geometry.getRings()[0];
coordinates = ring.getCoordinates();
coordinates.push(coordinate.slice());
@@ -329,10 +340,12 @@ ol.interaction.Draw.prototype.finishDrawing_ = function(event) {
var geometry = sketchFeature.getGeometry();
var coordinates = geometry.getCoordinates();
if (this.mode_ === ol.interaction.DrawMode.LINESTRING) {
goog.asserts.assertInstanceof(geometry, ol.geom.LineString);
// remove the redundant last point
coordinates.pop();
geometry.setCoordinates(coordinates);
} else if (this.mode_ === ol.interaction.DrawMode.POLYGON) {
goog.asserts.assertInstanceof(geometry, ol.geom.Polygon);
// force clockwise order for exterior ring
sketchFeature.setGeometry(new ol.geom.Polygon(coordinates));
}

View File

@@ -339,8 +339,9 @@ ol.interaction.Modify.prototype.createOrUpdateVertexFeature_ =
this.vertexFeature_ = vertexFeature;
this.sketchLayer_.getVectorSource().addFeatures([vertexFeature]);
} else {
var geometry = vertexFeature.getGeometry();
geometry.setCoordinates(coordinates);
var point = vertexFeature.getGeometry();
goog.asserts.assertInstanceof(point, ol.geom.Point);
point.setCoordinates(coordinates);
}
if (this.sketchLayer_.getStyle() !== style) {
this.sketchLayer_.setStyle(style);
@@ -505,6 +506,7 @@ ol.interaction.Modify.prototype.insertVertex_ =
var segment = segmentData.segment;
var feature = segmentData.feature;
var geometry = segmentData.geometry;
goog.asserts.assertInstanceof(geometry, ol.geom.LineString);
var index = segmentData.index;
var coordinates = geometry.getCoordinates();
coordinates.splice(index + 1, 0, vertex);