From b5c2013eb9ea3ecaef4e2e13837a9f4a8c0d1b5f Mon Sep 17 00:00:00 2001 From: David Manthey Date: Fri, 19 Jan 2018 16:50:43 -0500 Subject: [PATCH] Don't create zero-area rectangles. --- src/annotation.js | 10 ++++++++++ tests/cases/annotation.js | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/annotation.js b/src/annotation.js index f1585696c0..04018f253c 100644 --- a/src/annotation.js +++ b/src/annotation.js @@ -697,6 +697,11 @@ var rectangleAnnotation = function (args) { map.displayToGcs({x: evt.upperRight.x, y: evt.upperRight.y}, null), map.displayToGcs({x: evt.upperRight.x, y: evt.lowerLeft.y}, null) ]; + /* Don't keep rectangles that have nearly zero area in display pixels */ + if (layer.displayDistance(corners[0], null, corners[1], null) * + layer.displayDistance(corners[0], null, corners[3], null) < 0.01) { + return 'remove'; + } m_this.options('corners', corners); m_this.state(annotationState.done); return 'done'; @@ -854,6 +859,11 @@ var rectangleAnnotation = function (args) { evt.handled = true; if (corners.length) { m_this._setCornersFromMouse(corners, evt); + /* Don't keep rectangles that have nearly zero area in display pixels */ + if (layer.displayDistance(corners[0], null, corners[1], null) * + layer.displayDistance(corners[0], null, corners[3], null) < 0.01) { + return 'remove'; + } m_this.state(annotationState.done); return 'done'; } diff --git a/tests/cases/annotation.js b/tests/cases/annotation.js index f4f983debe..edb86ddcea 100644 --- a/tests/cases/annotation.js +++ b/tests/cases/annotation.js @@ -347,6 +347,16 @@ describe('geo.annotation', function () { expect(coor[0].y).toBeCloseTo(10.055); expect(coor[2].x).toBeCloseTo(-20.215); expect(coor[2].y).toBeCloseTo(15.199); + // a zero-area rectangle should be asked to be removed. + ann = geo.annotation.rectangleAnnotation({layer: layer, corners: corners}); + ann.state(geo.annotation.state.create); + evt = { + event: geo.event.actionselection, + state: {action: geo.geo_action.annotation_rectangle}, + lowerLeft: {x: 10, y: 65}, + upperRight: {x: 90, y: 65} + }; + expect(ann.processAction(evt)).toBe('remove'); }); it('_coordinates', function () { var ann = geo.annotation.rectangleAnnotation({corners: corners}); @@ -428,6 +438,22 @@ describe('geo.annotation', function () { }); expect(ann.options('corners').length).toBe(4); expect(ann.state()).toBe(geo.annotation.state.done); + // a zero-area rectangle should be asked to be removed. + ann = geo.annotation.rectangleAnnotation({layer: layer}); + ann.state(geo.annotation.state.create); + expect(ann.mouseClick({ + buttonsDown: {left: true}, + time: time, + map: corners[0], + mapgcs: map.displayToGcs(corners[0], null) + })).toBe(true); + expect(ann.options('corners').length).toBe(4); + expect(ann.mouseClick({ + buttonsDown: {left: true}, + time: time, + map: corners[1], + mapgcs: map.displayToGcs(corners[1], null) + })).toBe('remove'); }); });