Merge pull request #3237 from Morgul/ccase-circle-drawing
Add circles to Draw interaction.
This commit is contained in:
@@ -40,6 +40,7 @@
|
|||||||
<option value="Point">Point</option>
|
<option value="Point">Point</option>
|
||||||
<option value="LineString">LineString</option>
|
<option value="LineString">LineString</option>
|
||||||
<option value="Polygon">Polygon</option>
|
<option value="Polygon">Polygon</option>
|
||||||
|
<option value="Circle">Circle</option>
|
||||||
</select>
|
</select>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ goog.require('ol.MapBrowserEvent');
|
|||||||
goog.require('ol.MapBrowserEvent.EventType');
|
goog.require('ol.MapBrowserEvent.EventType');
|
||||||
goog.require('ol.Object');
|
goog.require('ol.Object');
|
||||||
goog.require('ol.events.condition');
|
goog.require('ol.events.condition');
|
||||||
|
goog.require('ol.geom.Circle');
|
||||||
goog.require('ol.geom.GeometryType');
|
goog.require('ol.geom.GeometryType');
|
||||||
goog.require('ol.geom.LineString');
|
goog.require('ol.geom.LineString');
|
||||||
goog.require('ol.geom.MultiLineString');
|
goog.require('ol.geom.MultiLineString');
|
||||||
@@ -290,6 +291,8 @@ ol.interaction.Draw.handleUpEvent_ = function(event) {
|
|||||||
if (goog.isNull(this.finishCoordinate_)) {
|
if (goog.isNull(this.finishCoordinate_)) {
|
||||||
this.startDrawing_(event);
|
this.startDrawing_(event);
|
||||||
} else if (this.mode_ === ol.interaction.DrawMode.POINT ||
|
} else if (this.mode_ === ol.interaction.DrawMode.POINT ||
|
||||||
|
this.mode_ === ol.interaction.DrawMode.CIRCLE &&
|
||||||
|
!goog.isNull(this.finishCoordinate_) ||
|
||||||
this.atFinish_(event)) {
|
this.atFinish_(event)) {
|
||||||
this.finishDrawing();
|
this.finishDrawing();
|
||||||
} else {
|
} else {
|
||||||
@@ -398,6 +401,10 @@ ol.interaction.Draw.prototype.startDrawing_ = function(event) {
|
|||||||
start.slice()]));
|
start.slice()]));
|
||||||
this.sketchPolygonCoords_ = [[start.slice(), start.slice()]];
|
this.sketchPolygonCoords_ = [[start.slice(), start.slice()]];
|
||||||
geometry = new ol.geom.Polygon(this.sketchPolygonCoords_);
|
geometry = new ol.geom.Polygon(this.sketchPolygonCoords_);
|
||||||
|
} else if (this.mode_ === ol.interaction.DrawMode.CIRCLE) {
|
||||||
|
geometry = new ol.geom.Circle(start.slice(), 0);
|
||||||
|
this.sketchLine_ = new ol.Feature(new ol.geom.LineString([start.slice(),
|
||||||
|
start.slice()]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
goog.asserts.assert(goog.isDef(geometry));
|
goog.asserts.assert(goog.isDef(geometry));
|
||||||
@@ -420,7 +427,7 @@ ol.interaction.Draw.prototype.startDrawing_ = function(event) {
|
|||||||
ol.interaction.Draw.prototype.modifyDrawing_ = function(event) {
|
ol.interaction.Draw.prototype.modifyDrawing_ = function(event) {
|
||||||
var coordinate = event.coordinate;
|
var coordinate = event.coordinate;
|
||||||
var geometry = this.sketchFeature_.getGeometry();
|
var geometry = this.sketchFeature_.getGeometry();
|
||||||
var coordinates, last;
|
var coordinates, last, sketchLineGeom;
|
||||||
if (this.mode_ === ol.interaction.DrawMode.POINT) {
|
if (this.mode_ === ol.interaction.DrawMode.POINT) {
|
||||||
goog.asserts.assertInstanceof(geometry, ol.geom.Point);
|
goog.asserts.assertInstanceof(geometry, ol.geom.Point);
|
||||||
last = geometry.getCoordinates();
|
last = geometry.getCoordinates();
|
||||||
@@ -434,6 +441,9 @@ ol.interaction.Draw.prototype.modifyDrawing_ = function(event) {
|
|||||||
} else if (this.mode_ === ol.interaction.DrawMode.POLYGON) {
|
} else if (this.mode_ === ol.interaction.DrawMode.POLYGON) {
|
||||||
goog.asserts.assertInstanceof(geometry, ol.geom.Polygon);
|
goog.asserts.assertInstanceof(geometry, ol.geom.Polygon);
|
||||||
coordinates = this.sketchPolygonCoords_[0];
|
coordinates = this.sketchPolygonCoords_[0];
|
||||||
|
} else if (this.mode_ === ol.interaction.DrawMode.CIRCLE) {
|
||||||
|
goog.asserts.assertInstanceof(geometry, ol.geom.Circle);
|
||||||
|
coordinates = geometry.getCenter();
|
||||||
}
|
}
|
||||||
if (this.atFinish_(event)) {
|
if (this.atFinish_(event)) {
|
||||||
// snap to finish
|
// snap to finish
|
||||||
@@ -449,11 +459,17 @@ ol.interaction.Draw.prototype.modifyDrawing_ = function(event) {
|
|||||||
goog.asserts.assertInstanceof(geometry, ol.geom.LineString);
|
goog.asserts.assertInstanceof(geometry, ol.geom.LineString);
|
||||||
geometry.setCoordinates(coordinates);
|
geometry.setCoordinates(coordinates);
|
||||||
} else if (this.mode_ === ol.interaction.DrawMode.POLYGON) {
|
} else if (this.mode_ === ol.interaction.DrawMode.POLYGON) {
|
||||||
var sketchLineGeom = this.sketchLine_.getGeometry();
|
sketchLineGeom = this.sketchLine_.getGeometry();
|
||||||
goog.asserts.assertInstanceof(sketchLineGeom, ol.geom.LineString);
|
goog.asserts.assertInstanceof(sketchLineGeom, ol.geom.LineString);
|
||||||
sketchLineGeom.setCoordinates(coordinates);
|
sketchLineGeom.setCoordinates(coordinates);
|
||||||
goog.asserts.assertInstanceof(geometry, ol.geom.Polygon);
|
goog.asserts.assertInstanceof(geometry, ol.geom.Polygon);
|
||||||
geometry.setCoordinates(this.sketchPolygonCoords_);
|
geometry.setCoordinates(this.sketchPolygonCoords_);
|
||||||
|
} else if (this.mode_ === ol.interaction.DrawMode.CIRCLE) {
|
||||||
|
goog.asserts.assertInstanceof(geometry, ol.geom.Circle);
|
||||||
|
sketchLineGeom = this.sketchLine_.getGeometry();
|
||||||
|
goog.asserts.assertInstanceof(sketchLineGeom, ol.geom.LineString);
|
||||||
|
sketchLineGeom.setCoordinates([geometry.getCenter(), coordinate]);
|
||||||
|
geometry.setRadius(sketchLineGeom.getLength());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.updateSketchFeatures_();
|
this.updateSketchFeatures_();
|
||||||
@@ -557,7 +573,7 @@ ol.interaction.Draw.prototype.shouldStopEvent = goog.functions.FALSE;
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Redraw the skecth features.
|
* Redraw the sketch features.
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
ol.interaction.Draw.prototype.updateSketchFeatures_ = function() {
|
ol.interaction.Draw.prototype.updateSketchFeatures_ = function() {
|
||||||
@@ -606,6 +622,8 @@ ol.interaction.Draw.getMode_ = function(type) {
|
|||||||
} else if (type === ol.geom.GeometryType.POLYGON ||
|
} else if (type === ol.geom.GeometryType.POLYGON ||
|
||||||
type === ol.geom.GeometryType.MULTI_POLYGON) {
|
type === ol.geom.GeometryType.MULTI_POLYGON) {
|
||||||
mode = ol.interaction.DrawMode.POLYGON;
|
mode = ol.interaction.DrawMode.POLYGON;
|
||||||
|
} else if (type === ol.geom.GeometryType.CIRCLE) {
|
||||||
|
mode = ol.interaction.DrawMode.CIRCLE;
|
||||||
}
|
}
|
||||||
goog.asserts.assert(goog.isDef(mode));
|
goog.asserts.assert(goog.isDef(mode));
|
||||||
return mode;
|
return mode;
|
||||||
@@ -620,5 +638,6 @@ ol.interaction.Draw.getMode_ = function(type) {
|
|||||||
ol.interaction.DrawMode = {
|
ol.interaction.DrawMode = {
|
||||||
POINT: 'Point',
|
POINT: 'Point',
|
||||||
LINE_STRING: 'LineString',
|
LINE_STRING: 'LineString',
|
||||||
POLYGON: 'Polygon'
|
POLYGON: 'Polygon',
|
||||||
|
CIRCLE: 'Circle'
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -309,6 +309,12 @@ ol.style.createDefaultEditingStyles = function() {
|
|||||||
styles[ol.geom.GeometryType.MULTI_LINE_STRING] =
|
styles[ol.geom.GeometryType.MULTI_LINE_STRING] =
|
||||||
styles[ol.geom.GeometryType.LINE_STRING];
|
styles[ol.geom.GeometryType.LINE_STRING];
|
||||||
|
|
||||||
|
styles[ol.geom.GeometryType.CIRCLE] =
|
||||||
|
styles[ol.geom.GeometryType.POLYGON].concat(
|
||||||
|
styles[ol.geom.GeometryType.LINE_STRING]
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
styles[ol.geom.GeometryType.POINT] = [
|
styles[ol.geom.GeometryType.POINT] = [
|
||||||
new ol.style.Style({
|
new ol.style.Style({
|
||||||
image: new ol.style.Circle({
|
image: new ol.style.Circle({
|
||||||
|
|||||||
@@ -470,6 +470,58 @@ describe('ol.interaction.Draw', function() {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('drawing circles', function() {
|
||||||
|
var draw;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
draw = new ol.interaction.Draw({
|
||||||
|
source: source,
|
||||||
|
type: ol.geom.GeometryType.CIRCLE
|
||||||
|
});
|
||||||
|
map.addInteraction(draw);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('draws circle with clicks, finishing on second point', function() {
|
||||||
|
// first point
|
||||||
|
simulateEvent('pointermove', 10, 20);
|
||||||
|
simulateEvent('pointerdown', 10, 20);
|
||||||
|
simulateEvent('pointerup', 10, 20);
|
||||||
|
|
||||||
|
// finish on second point
|
||||||
|
simulateEvent('pointermove', 30, 20);
|
||||||
|
simulateEvent('pointerdown', 30, 20);
|
||||||
|
simulateEvent('pointerup', 30, 20);
|
||||||
|
|
||||||
|
var features = source.getFeatures();
|
||||||
|
expect(features).to.have.length(1);
|
||||||
|
var geometry = features[0].getGeometry();
|
||||||
|
expect(geometry).to.be.a(ol.geom.Circle);
|
||||||
|
expect(geometry.getCenter()).to.eql([10, -20]);
|
||||||
|
expect(geometry.getRadius()).to.eql(20);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('triggers draw events', function() {
|
||||||
|
var ds = sinon.spy();
|
||||||
|
var de = sinon.spy();
|
||||||
|
goog.events.listen(draw, ol.DrawEventType.DRAWSTART, ds);
|
||||||
|
goog.events.listen(draw, ol.DrawEventType.DRAWEND, de);
|
||||||
|
|
||||||
|
// first point
|
||||||
|
simulateEvent('pointermove', 10, 20);
|
||||||
|
simulateEvent('pointerdown', 10, 20);
|
||||||
|
simulateEvent('pointerup', 10, 20);
|
||||||
|
|
||||||
|
// finish on second point
|
||||||
|
simulateEvent('pointermove', 30, 20);
|
||||||
|
simulateEvent('pointerdown', 30, 20);
|
||||||
|
simulateEvent('pointerup', 30, 20);
|
||||||
|
|
||||||
|
expect(ds).to.be.called(1);
|
||||||
|
expect(de).to.be.called(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
describe('#setActive()', function() {
|
describe('#setActive()', function() {
|
||||||
var interaction;
|
var interaction;
|
||||||
|
|
||||||
@@ -596,6 +648,7 @@ goog.require('ol.DrawEventType');
|
|||||||
goog.require('ol.Map');
|
goog.require('ol.Map');
|
||||||
goog.require('ol.MapBrowserPointerEvent');
|
goog.require('ol.MapBrowserPointerEvent');
|
||||||
goog.require('ol.View');
|
goog.require('ol.View');
|
||||||
|
goog.require('ol.geom.Circle');
|
||||||
goog.require('ol.geom.GeometryType');
|
goog.require('ol.geom.GeometryType');
|
||||||
goog.require('ol.geom.LineString');
|
goog.require('ol.geom.LineString');
|
||||||
goog.require('ol.geom.MultiLineString');
|
goog.require('ol.geom.MultiLineString');
|
||||||
|
|||||||
Reference in New Issue
Block a user