From d4c1589a01d41bee55c9faf8714ed2e1cf4dff8c Mon Sep 17 00:00:00 2001 From: Tobias Kohr Date: Mon, 23 Sep 2019 17:23:32 +0200 Subject: [PATCH 1/7] Seperate internal and API methods for the map --- examples/region-growing.js | 2 +- src/ol/MapBrowserEvent.js | 2 +- src/ol/Overlay.js | 6 ++-- src/ol/PluggableMap.js | 38 +++++++++++++++++++------ src/ol/control/MousePosition.js | 2 +- src/ol/control/OverviewMap.js | 4 +-- src/ol/interaction/DragPan.js | 4 +-- src/ol/interaction/DragZoom.js | 4 +-- src/ol/interaction/Draw.js | 2 +- src/ol/interaction/Extent.js | 31 +++++++++++++------- src/ol/interaction/Modify.js | 10 +++---- src/ol/interaction/PinchRotate.js | 2 +- src/ol/interaction/PinchZoom.js | 2 +- src/ol/interaction/Snap.js | 18 ++++++------ src/ol/render/Box.js | 2 +- test/spec/ol/interaction/extent.test.js | 10 +++---- 16 files changed, 86 insertions(+), 53 deletions(-) diff --git a/examples/region-growing.js b/examples/region-growing.js index 0503ff7127..c09678144a 100644 --- a/examples/region-growing.js +++ b/examples/region-growing.js @@ -117,7 +117,7 @@ raster.on('beforeoperations', function(event) { const data = event.data; data.delta = thresholdControl.value; if (coordinate) { - data.pixel = map.getPixelFromCoordinate(coordinate); + data.pixel = map.getPixelFromCoordinateExternal(coordinate); } }); diff --git a/src/ol/MapBrowserEvent.js b/src/ol/MapBrowserEvent.js index 9919d08516..ce60448307 100644 --- a/src/ol/MapBrowserEvent.js +++ b/src/ol/MapBrowserEvent.js @@ -74,7 +74,7 @@ class MapBrowserEvent extends MapEvent { */ get coordinate() { if (!this.coordinate_) { - this.coordinate_ = this.map.getCoordinateFromPixel(this.pixel); + this.coordinate_ = this.map.getCoordinateFromPixelInternal(this.pixel); } return this.coordinate_; } diff --git a/src/ol/Overlay.js b/src/ol/Overlay.js index 3fb91c38ac..0e1fbdae6b 100644 --- a/src/ol/Overlay.js +++ b/src/ol/Overlay.js @@ -419,14 +419,14 @@ class Overlay extends BaseObject { if (delta[0] !== 0 || delta[1] !== 0) { const center = /** @type {import("./coordinate.js").Coordinate} */ (map.getView().getCenterInternal()); - const centerPx = map.getPixelFromCoordinate(center); + const centerPx = map.getPixelFromCoordinateInternal(center); const newCenterPx = [ centerPx[0] + delta[0], centerPx[1] + delta[1] ]; map.getView().animateInternal({ - center: map.getCoordinateFromPixel(newCenterPx), + center: map.getCoordinateFromPixelInternal(newCenterPx), duration: this.autoPanAnimation.duration, easing: this.autoPanAnimation.easing }); @@ -488,7 +488,7 @@ class Overlay extends BaseObject { return; } - const pixel = map.getPixelFromCoordinate(position); + const pixel = map.getPixelFromCoordinateInternal(position); const mapSize = map.getSize(); this.updateRenderedPosition(pixel, mapSize); } diff --git a/src/ol/PluggableMap.js b/src/ol/PluggableMap.js index a520636521..f4e49f0750 100644 --- a/src/ol/PluggableMap.js +++ b/src/ol/PluggableMap.js @@ -27,6 +27,7 @@ import LayerGroup from './layer/Group.js'; import {hasArea} from './size.js'; import {DROP} from './structs/PriorityQueue.js'; import {create as createTransform, apply as applyTransform} from './transform.js'; +import { toUserCoordinate } from './proj.js'; /** @@ -545,7 +546,7 @@ class PluggableMap extends BaseObject { if (!this.frameState_) { return; } - const coordinate = this.getCoordinateFromPixel(pixel); + const coordinate = this.getCoordinateFromPixelExternal(pixel); opt_options = opt_options !== undefined ? opt_options : /** @type {AtPixelOptions} */ ({}); const hitTolerance = opt_options.hitTolerance !== undefined ? @@ -617,7 +618,7 @@ class PluggableMap extends BaseObject { if (!this.frameState_) { return false; } - const coordinate = this.getCoordinateFromPixel(pixel); + const coordinate = this.getCoordinateFromPixelExternal(pixel); opt_options = opt_options !== undefined ? opt_options : /** @type {AtPixelOptions} */ ({}); const layerFilter = opt_options.layerFilter !== undefined ? opt_options.layerFilter : TRUE; @@ -634,7 +635,7 @@ class PluggableMap extends BaseObject { * @api */ getEventCoordinate(event) { - return this.getCoordinateFromPixel(this.getEventPixel(event)); + return this.getCoordinateFromPixelExternal(this.getEventPixel(event)); } /** @@ -686,12 +687,22 @@ class PluggableMap extends BaseObject { /** * Get the coordinate for a given pixel. This returns a coordinate in the - * map view projection. + * user projection. * @param {import("./pixel.js").Pixel} pixel Pixel position in the map viewport. * @return {import("./coordinate.js").Coordinate} The coordinate for the pixel position. * @api */ - getCoordinateFromPixel(pixel) { + getCoordinateFromPixelExternal(pixel) { + return toUserCoordinate(this.getCoordinateFromPixelInternal(pixel), this.getView().getProjection()) + } + + /** + * Get the coordinate for a given pixel. This returns a coordinate in the + * map view projection. + * @param {import("./pixel.js").Pixel} pixel Pixel position in the map viewport. + * @return {import("./coordinate.js").Coordinate} The coordinate for the pixel position. + */ + getCoordinateFromPixelInternal(pixel) { const frameState = this.frameState_; if (!frameState) { return null; @@ -699,7 +710,7 @@ class PluggableMap extends BaseObject { return applyTransform(frameState.pixelToCoordinateTransform, pixel.slice()); } } - + /** * Get the map controls. Modifying this collection changes the controls * associated with the map. @@ -783,13 +794,24 @@ class PluggableMap extends BaseObject { } /** - * Get the pixel for a coordinate. This takes a coordinate in the map view + * Get the pixel for a coordinate. This takes a coordinate in the user * projection and returns the corresponding pixel. * @param {import("./coordinate.js").Coordinate} coordinate A map coordinate. * @return {import("./pixel.js").Pixel} A pixel position in the map viewport. * @api */ - getPixelFromCoordinate(coordinate) { + getPixelFromCoordinateExternal(coordinate) { + const userCoordinate = toUserCoordinate(coordinate, this.getView().getProjection()); + return this.getPixelFromCoordinateInternal(userCoordinate); + } + + /** + * Get the pixel for a coordinate. This takes a coordinate in the map view + * projection and returns the corresponding pixel. + * @param {import("./coordinate.js").Coordinate} coordinate A map coordinate. + * @return {import("./pixel.js").Pixel} A pixel position in the map viewport. + */ + getPixelFromCoordinateInternal(coordinate) { const frameState = this.frameState_; if (!frameState) { return null; diff --git a/src/ol/control/MousePosition.js b/src/ol/control/MousePosition.js index de59a82d2b..0ae9b41896 100644 --- a/src/ol/control/MousePosition.js +++ b/src/ol/control/MousePosition.js @@ -218,7 +218,7 @@ class MousePosition extends Control { } } const map = this.getMap(); - const coordinate = map.getCoordinateFromPixel(pixel); + const coordinate = map.getCoordinateFromPixelExternal(pixel); if (coordinate) { this.transform_(coordinate, coordinate); const coordinateFormat = this.getCoordinateFormat(); diff --git a/src/ol/control/OverviewMap.js b/src/ol/control/OverviewMap.js index 6a3de53aae..0d7a87ad98 100644 --- a/src/ol/control/OverviewMap.js +++ b/src/ol/control/OverviewMap.js @@ -361,9 +361,9 @@ class OverviewMap extends Control { const ovextent = ovview.calculateExtentInternal(ovmapSize); const topLeftPixel = - ovmap.getPixelFromCoordinate(getTopLeft(extent)); + ovmap.getPixelFromCoordinateInternal(getTopLeft(extent)); const bottomRightPixel = - ovmap.getPixelFromCoordinate(getBottomRight(extent)); + ovmap.getPixelFromCoordinateInternal(getBottomRight(extent)); const boxWidth = Math.abs(topLeftPixel[0] - bottomRightPixel[0]); const boxHeight = Math.abs(topLeftPixel[1] - bottomRightPixel[1]); diff --git a/src/ol/interaction/DragPan.js b/src/ol/interaction/DragPan.js index 941d628d5a..fda3d7276d 100644 --- a/src/ol/interaction/DragPan.js +++ b/src/ol/interaction/DragPan.js @@ -114,8 +114,8 @@ class DragPan extends PointerInteraction { const distance = this.kinetic_.getDistance(); const angle = this.kinetic_.getAngle(); const center = view.getCenterInternal(); - const centerpx = map.getPixelFromCoordinate(center); - const dest = map.getCoordinateFromPixel([ + const centerpx = map.getPixelFromCoordinateInternal(center); + const dest = map.getCoordinateFromPixelInternal([ centerpx[0] - distance * Math.cos(angle), centerpx[1] - distance * Math.sin(angle) ]); diff --git a/src/ol/interaction/DragZoom.js b/src/ol/interaction/DragZoom.js index 37cabeb78b..bd3ac19724 100644 --- a/src/ol/interaction/DragZoom.js +++ b/src/ol/interaction/DragZoom.js @@ -75,8 +75,8 @@ function onBoxEnd() { if (this.out_) { const mapExtent = view.calculateExtentInternal(size); const boxPixelExtent = createOrUpdateFromCoordinates([ - map.getPixelFromCoordinate(getBottomLeft(extent)), - map.getPixelFromCoordinate(getTopRight(extent))]); + map.getPixelFromCoordinateInternal(getBottomLeft(extent)), + map.getPixelFromCoordinateInternal(getTopRight(extent))]); const factor = view.getResolutionForExtentInternal(boxPixelExtent, size); scaleFromCenter(mapExtent, 1 / factor); diff --git a/src/ol/interaction/Draw.js b/src/ol/interaction/Draw.js index 80560e2f0c..629a99dda7 100644 --- a/src/ol/interaction/Draw.js +++ b/src/ol/interaction/Draw.js @@ -639,7 +639,7 @@ class Draw extends PointerInteraction { const map = event.map; for (let i = 0, ii = potentiallyFinishCoordinates.length; i < ii; i++) { const finishCoordinate = potentiallyFinishCoordinates[i]; - const finishPixel = map.getPixelFromCoordinate(finishCoordinate); + const finishPixel = map.getPixelFromCoordinateInternal(finishCoordinate); const pixel = event.pixel; const dx = pixel[0] - finishPixel[0]; const dy = pixel[1] - finishPixel[1]; diff --git a/src/ol/interaction/Extent.js b/src/ol/interaction/Extent.js index 02ffdf7777..eb45d1aae8 100644 --- a/src/ol/interaction/Extent.js +++ b/src/ol/interaction/Extent.js @@ -13,6 +13,7 @@ import PointerInteraction from './Pointer.js'; import VectorLayer from '../layer/Vector.js'; import VectorSource from '../source/Vector.js'; import {createEditingStyle} from '../style/Style.js'; +import { toUserExtent } from '../proj.js'; /** @@ -177,12 +178,12 @@ class Extent extends PointerInteraction { * @private */ snapToVertex_(pixel, map) { - const pixelCoordinate = map.getCoordinateFromPixel(pixel); + const pixelCoordinate = map.getCoordinateFromPixelInternal(pixel); const sortByDistance = function(a, b) { return squaredDistanceToSegment(pixelCoordinate, a) - squaredDistanceToSegment(pixelCoordinate, b); }; - const extent = this.getExtent(); + const extent = this.getExtentInternal(); if (extent) { //convert extents to line segments and find the segment closest to pixelCoordinate const segments = getSegments(extent); @@ -191,13 +192,13 @@ class Extent extends PointerInteraction { let vertex = (closestOnSegment(pixelCoordinate, closestSegment)); - const vertexPixel = map.getPixelFromCoordinate(vertex); + const vertexPixel = map.getPixelFromCoordinateInternal(vertex); //if the distance is within tolerance, snap to the segment if (coordinateDistance(pixel, vertexPixel) <= this.pixelTolerance_) { //test if we should further snap to a vertex - const pixel1 = map.getPixelFromCoordinate(closestSegment[0]); - const pixel2 = map.getPixelFromCoordinate(closestSegment[1]); + const pixel1 = map.getPixelFromCoordinateInternal(closestSegment[0]); + const pixel2 = map.getPixelFromCoordinateInternal(closestSegment[1]); const squaredDist1 = squaredCoordinateDistance(vertexPixel, pixel1); const squaredDist2 = squaredCoordinateDistance(vertexPixel, pixel2); const dist = Math.sqrt(Math.min(squaredDist1, squaredDist2)); @@ -222,7 +223,7 @@ class Extent extends PointerInteraction { let vertex = this.snapToVertex_(pixel, map); if (!vertex) { - vertex = map.getCoordinateFromPixel(pixel); + vertex = map.getCoordinateFromPixelInternal(pixel); } this.createOrUpdatePointerFeature_(vertex); } @@ -295,7 +296,7 @@ class Extent extends PointerInteraction { const pixel = mapBrowserEvent.pixel; const map = mapBrowserEvent.map; - const extent = this.getExtent(); + const extent = this.getExtentInternal(); let vertex = this.snapToVertex_(pixel, map); //find the extent corner opposite the passed corner @@ -338,7 +339,7 @@ class Extent extends PointerInteraction { } //no snap - new bbox } else { - vertex = map.getCoordinateFromPixel(pixel); + vertex = map.getCoordinateFromPixelInternal(pixel); this.setExtent([vertex[0], vertex[1], vertex[0], vertex[1]]); this.pointerHandler_ = getPointHandler(vertex); } @@ -363,7 +364,7 @@ class Extent extends PointerInteraction { handleUpEvent(mapBrowserEvent) { this.pointerHandler_ = null; //If bbox is zero area, set to null; - const extent = this.getExtent(); + const extent = this.getExtentInternal(); if (!extent || getArea(extent) === 0) { this.setExtent(null); } @@ -379,13 +380,23 @@ class Extent extends PointerInteraction { super.setMap(map); } + /** + * Returns the current drawn extent in the view projection (or user projection if set) + * + * @return {import("../extent.js").Extent} Drawn extent in the view projection. + * @api + */ + getExtentExternal() { + return toUserExtent(this.getExtentInternal(), this.getMap().getView().getProjection()); + } + /** * Returns the current drawn extent in the view projection * * @return {import("../extent.js").Extent} Drawn extent in the view projection. * @api */ - getExtent() { + getExtentInternal() { return this.extent_; } diff --git a/src/ol/interaction/Modify.js b/src/ol/interaction/Modify.js index 8e8e4e1abb..69648a655d 100644 --- a/src/ol/interaction/Modify.js +++ b/src/ol/interaction/Modify.js @@ -775,7 +775,7 @@ class Modify extends PointerInteraction { return false; } this.handlePointerAtPixel_(evt.pixel, evt.map); - const pixelCoordinate = evt.map.getCoordinateFromPixel(evt.pixel); + const pixelCoordinate = evt.map.getCoordinateFromPixelInternal(evt.pixel); this.dragSegments_.length = 0; this.modified_ = false; const vertexFeature = this.vertexFeature_; @@ -883,7 +883,7 @@ class Modify extends PointerInteraction { * @private */ handlePointerAtPixel_(pixel, map) { - const pixelCoordinate = map.getCoordinateFromPixel(pixel); + const pixelCoordinate = map.getCoordinateFromPixelInternal(pixel); const sortByDistance = function(a, b) { return pointDistanceToSegmentDataSquared(pixelCoordinate, a) - pointDistanceToSegmentDataSquared(pixelCoordinate, b); @@ -899,7 +899,7 @@ class Modify extends PointerInteraction { const node = nodes[0]; const closestSegment = node.segment; let vertex = closestOnSegmentData(pixelCoordinate, node); - const vertexPixel = map.getPixelFromCoordinate(vertex); + const vertexPixel = map.getPixelFromCoordinateInternal(vertex); let dist = coordinateDistance(pixel, vertexPixel); if (dist <= this.pixelTolerance_) { /** @type {Object} */ @@ -911,8 +911,8 @@ class Modify extends PointerInteraction { this.snappedToVertex_ = true; this.createOrUpdateVertexFeature_(vertex); } else { - const pixel1 = map.getPixelFromCoordinate(closestSegment[0]); - const pixel2 = map.getPixelFromCoordinate(closestSegment[1]); + const pixel1 = map.getPixelFromCoordinateInternal(closestSegment[0]); + const pixel2 = map.getPixelFromCoordinateInternal(closestSegment[1]); const squaredDist1 = squaredCoordinateDistance(vertexPixel, pixel1); const squaredDist2 = squaredCoordinateDistance(vertexPixel, pixel2); dist = Math.sqrt(Math.min(squaredDist1, squaredDist2)); diff --git a/src/ol/interaction/PinchRotate.js b/src/ol/interaction/PinchRotate.js index 3977d82357..f3ad9b23e0 100644 --- a/src/ol/interaction/PinchRotate.js +++ b/src/ol/interaction/PinchRotate.js @@ -112,7 +112,7 @@ class PinchRotate extends PointerInteraction { const centroid = centroidFromPointers(this.targetPointers); centroid[0] -= viewportPosition.left; centroid[1] -= viewportPosition.top; - this.anchor_ = map.getCoordinateFromPixel(centroid); + this.anchor_ = map.getCoordinateFromPixelInternal(centroid); // rotate if (this.rotating_) { diff --git a/src/ol/interaction/PinchZoom.js b/src/ol/interaction/PinchZoom.js index 9d352bafe6..eca8c8ab9f 100644 --- a/src/ol/interaction/PinchZoom.js +++ b/src/ol/interaction/PinchZoom.js @@ -91,7 +91,7 @@ class PinchZoom extends PointerInteraction { const centroid = centroidFromPointers(this.targetPointers); centroid[0] -= viewportPosition.left; centroid[1] -= viewportPosition.top; - this.anchor_ = map.getCoordinateFromPixel(centroid); + this.anchor_ = map.getCoordinateFromPixelInternal(centroid); // scale, bypass the resolution constraint map.render(); diff --git a/src/ol/interaction/Snap.js b/src/ol/interaction/Snap.js index a90475b016..9f1510d15c 100644 --- a/src/ol/interaction/Snap.js +++ b/src/ol/interaction/Snap.js @@ -384,9 +384,9 @@ class Snap extends PointerInteraction { */ snapTo(pixel, pixelCoordinate, map) { - const lowerLeft = map.getCoordinateFromPixel( + const lowerLeft = map.getCoordinateFromPixelInternal( [pixel[0] - this.pixelTolerance_, pixel[1] + this.pixelTolerance_]); - const upperRight = map.getCoordinateFromPixel( + const upperRight = map.getCoordinateFromPixelInternal( [pixel[0] + this.pixelTolerance_, pixel[1] - this.pixelTolerance_]); const box = boundingExtent([lowerLeft, upperRight]); @@ -412,8 +412,8 @@ class Snap extends PointerInteraction { const isCircle = segments[0].feature.getGeometry().getType() === GeometryType.CIRCLE; if (this.vertex_ && !this.edge_) { - pixel1 = map.getPixelFromCoordinate(closestSegment[0]); - pixel2 = map.getPixelFromCoordinate(closestSegment[1]); + pixel1 = map.getPixelFromCoordinateInternal(closestSegment[0]); + pixel2 = map.getPixelFromCoordinateInternal(closestSegment[1]); squaredDist1 = squaredCoordinateDistance(pixel, pixel1); squaredDist2 = squaredCoordinateDistance(pixel, pixel2); dist = Math.sqrt(Math.min(squaredDist1, squaredDist2)); @@ -421,7 +421,7 @@ class Snap extends PointerInteraction { if (snappedToVertex) { snapped = true; vertex = squaredDist1 > squaredDist2 ? closestSegment[1] : closestSegment[0]; - vertexPixel = map.getPixelFromCoordinate(vertex); + vertexPixel = map.getPixelFromCoordinateInternal(vertex); } } else if (this.edge_) { if (isCircle) { @@ -430,19 +430,19 @@ class Snap extends PointerInteraction { } else { vertex = closestOnSegment(pixelCoordinate, closestSegment); } - vertexPixel = map.getPixelFromCoordinate(vertex); + vertexPixel = map.getPixelFromCoordinateInternal(vertex); if (coordinateDistance(pixel, vertexPixel) <= this.pixelTolerance_) { snapped = true; if (this.vertex_ && !isCircle) { - pixel1 = map.getPixelFromCoordinate(closestSegment[0]); - pixel2 = map.getPixelFromCoordinate(closestSegment[1]); + pixel1 = map.getPixelFromCoordinateInternal(closestSegment[0]); + pixel2 = map.getPixelFromCoordinateInternal(closestSegment[1]); squaredDist1 = squaredCoordinateDistance(vertexPixel, pixel1); squaredDist2 = squaredCoordinateDistance(vertexPixel, pixel2); dist = Math.sqrt(Math.min(squaredDist1, squaredDist2)); snappedToVertex = dist <= this.pixelTolerance_; if (snappedToVertex) { vertex = squaredDist1 > squaredDist2 ? closestSegment[1] : closestSegment[0]; - vertexPixel = map.getPixelFromCoordinate(vertex); + vertexPixel = map.getPixelFromCoordinateInternal(vertex); } } } diff --git a/src/ol/render/Box.js b/src/ol/render/Box.js index 7303c56e4d..5d949f4e03 100644 --- a/src/ol/render/Box.js +++ b/src/ol/render/Box.js @@ -105,7 +105,7 @@ class RenderBox extends Disposable { endPixel, [endPixel[0], startPixel[1]] ]; - const coordinates = pixels.map(this.map_.getCoordinateFromPixel, this.map_); + const coordinates = pixels.map(this.map_.getCoordinateFromPixelInternal, this.map_); // close the polygon coordinates[4] = coordinates[0].slice(); if (!this.geometry_) { diff --git a/test/spec/ol/interaction/extent.test.js b/test/spec/ol/interaction/extent.test.js index 56fd15765e..c36e8a8350 100644 --- a/test/spec/ol/interaction/extent.test.js +++ b/test/spec/ol/interaction/extent.test.js @@ -99,7 +99,7 @@ describe('ol.interaction.Extent', function() { simulateEvent('pointerdrag', 50, 50, false, 0); simulateEvent('pointerup', 50, 50, false, 0); - expect(interaction.getExtent()).to.eql([-50, -50, 50, 50]); + expect(interaction.getExtentExternal()).to.eql([-50, -50, 50, 50]); }); it('clicking off extent nulls extent', function() { @@ -108,7 +108,7 @@ describe('ol.interaction.Extent', function() { simulateEvent('pointerdown', -10, -10, false, 0); simulateEvent('pointerup', -10, -10, false, 0); - expect(interaction.getExtent()).to.equal(null); + expect(interaction.getExtentExternal()).to.equal(null); }); it('clicking on extent does not null extent', function() { @@ -117,7 +117,7 @@ describe('ol.interaction.Extent', function() { simulateEvent('pointerdown', 50, 50, false, 0); simulateEvent('pointerup', 50, 50, false, 0); - expect(interaction.getExtent()).to.eql([-50, -50, 50, 50]); + expect(interaction.getExtentExternal()).to.eql([-50, -50, 50, 50]); }); it('snap and drag vertex works', function() { @@ -127,7 +127,7 @@ describe('ol.interaction.Extent', function() { simulateEvent('pointerdrag', -70, -40, false, 0); simulateEvent('pointerup', -70, -40, false, 0); - expect(interaction.getExtent()).to.eql([-70, -50, -50, -40]); + expect(interaction.getExtentExternal()).to.eql([-70, -50, -50, -40]); }); it('snap and drag edge works', function() { @@ -137,7 +137,7 @@ describe('ol.interaction.Extent', function() { simulateEvent('pointerdrag', 20, -30, false, 0); simulateEvent('pointerup', 20, -30, false, 0); - expect(interaction.getExtent()).to.eql([-50, -50, 20, 50]); + expect(interaction.getExtentExternal()).to.eql([-50, -50, 20, 50]); }); }); }); From 686c185de3d77e8d62951ac0d39c3211341177a5 Mon Sep 17 00:00:00 2001 From: Tobias Kohr Date: Mon, 23 Sep 2019 19:20:51 +0200 Subject: [PATCH 2/7] Call internal method in MousePosition --- src/ol/control/MousePosition.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ol/control/MousePosition.js b/src/ol/control/MousePosition.js index 0ae9b41896..c81c52bd13 100644 --- a/src/ol/control/MousePosition.js +++ b/src/ol/control/MousePosition.js @@ -5,7 +5,7 @@ import {listen} from '../events.js'; import EventType from '../pointer/EventType.js'; import {getChangeEventType} from '../Object.js'; import Control from './Control.js'; -import {getTransformFromProjections, identityTransform, get as getProjection} from '../proj.js'; +import {getTransformFromProjections, identityTransform, get as getProjection, getUserProjection} from '../proj.js'; import '@openlayers/pepjs'; @@ -218,8 +218,13 @@ class MousePosition extends Control { } } const map = this.getMap(); - const coordinate = map.getCoordinateFromPixelExternal(pixel); + const coordinate = map.getCoordinateFromPixelInternal(pixel); if (coordinate) { + const userProjection = getUserProjection() + if (userProjection) { + this.transform_ = getTransformFromProjections( + this.mapProjection_, userProjection); + } this.transform_(coordinate, coordinate); const coordinateFormat = this.getCoordinateFormat(); if (coordinateFormat) { From 5711cd30d04cfb66ec0b2a930eb8c96f84310b18 Mon Sep 17 00:00:00 2001 From: Tobias Kohr Date: Mon, 23 Sep 2019 19:22:36 +0200 Subject: [PATCH 3/7] Seperate getEventCoordinate into internal/external method --- examples/igc.js | 2 +- examples/synthetic-points.js | 2 +- examples/utfgrid.js | 2 +- src/ol/PluggableMap.js | 13 +++++++++++-- src/ol/control/OverviewMap.js | 4 ++-- 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/examples/igc.js b/examples/igc.js index 261108f59a..be2d2f984d 100644 --- a/examples/igc.js +++ b/examples/igc.js @@ -135,7 +135,7 @@ map.on('pointermove', function(evt) { if (evt.dragging) { return; } - const coordinate = map.getEventCoordinate(evt.originalEvent); + const coordinate = map.getEventCoordinateExternal(evt.originalEvent); displaySnap(coordinate); }); diff --git a/examples/synthetic-points.js b/examples/synthetic-points.js index 1eceb86fdf..8ccfbc685c 100644 --- a/examples/synthetic-points.js +++ b/examples/synthetic-points.js @@ -85,7 +85,7 @@ map.on('pointermove', function(evt) { if (evt.dragging) { return; } - const coordinate = map.getEventCoordinate(evt.originalEvent); + const coordinate = map.getEventCoordinateExternal(evt.originalEvent); displaySnap(coordinate); }); diff --git a/examples/utfgrid.js b/examples/utfgrid.js index 92b117c100..b2007e921d 100644 --- a/examples/utfgrid.js +++ b/examples/utfgrid.js @@ -63,7 +63,7 @@ map.on('pointermove', function(evt) { if (evt.dragging) { return; } - const coordinate = map.getEventCoordinate(evt.originalEvent); + const coordinate = map.getEventCoordinateExternal(evt.originalEvent); displayCountryInfo(coordinate); }); diff --git a/src/ol/PluggableMap.js b/src/ol/PluggableMap.js index f4e49f0750..27597fae3c 100644 --- a/src/ol/PluggableMap.js +++ b/src/ol/PluggableMap.js @@ -629,15 +629,24 @@ class PluggableMap extends BaseObject { } /** - * Returns the coordinate in view projection for a browser event. + * Returns the coordinate in user projection for a browser event. * @param {Event} event Event. * @return {import("./coordinate.js").Coordinate} Coordinate. * @api */ - getEventCoordinate(event) { + getEventCoordinateExternal(event) { return this.getCoordinateFromPixelExternal(this.getEventPixel(event)); } + /** + * Returns the coordinate in view projection for a browser event. + * @param {Event} event Event. + * @return {import("./coordinate.js").Coordinate} Coordinate. + */ + getEventCoordinateInternal(event) { + return this.getCoordinateFromPixelInternal(this.getEventPixel(event)); + } + /** * Returns the map pixel position for a browser event relative to the viewport. * @param {Event|TouchEvent} event Event. diff --git a/src/ol/control/OverviewMap.js b/src/ol/control/OverviewMap.js index 0d7a87ad98..ff051e56ea 100644 --- a/src/ol/control/OverviewMap.js +++ b/src/ol/control/OverviewMap.js @@ -223,13 +223,13 @@ class OverviewMap extends Control { const move = function(event) { const position = /** @type {?} */ (computeDesiredMousePosition(event)); - const coordinates = ovmap.getEventCoordinate(/** @type {Event} */ (position)); + const coordinates = ovmap.getEventCoordinateInternal(/** @type {Event} */ (position)); overlay.setPosition(coordinates); }; const endMoving = function(event) { - const coordinates = ovmap.getEventCoordinate(event); + const coordinates = ovmap.getEventCoordinateInternal(event); scope.getMap().getView().setCenterInternal(coordinates); From b1196e7601265813649b6ccfe8d98d67067129a5 Mon Sep 17 00:00:00 2001 From: Tobias Kohr Date: Tue, 24 Sep 2019 10:13:21 +0200 Subject: [PATCH 4/7] fix linting --- src/ol/PluggableMap.js | 6 +++--- src/ol/control/MousePosition.js | 2 +- src/ol/interaction/Extent.js | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ol/PluggableMap.js b/src/ol/PluggableMap.js index 27597fae3c..55dd1fa509 100644 --- a/src/ol/PluggableMap.js +++ b/src/ol/PluggableMap.js @@ -27,7 +27,7 @@ import LayerGroup from './layer/Group.js'; import {hasArea} from './size.js'; import {DROP} from './structs/PriorityQueue.js'; import {create as createTransform, apply as applyTransform} from './transform.js'; -import { toUserCoordinate } from './proj.js'; +import {toUserCoordinate} from './proj.js'; /** @@ -702,7 +702,7 @@ class PluggableMap extends BaseObject { * @api */ getCoordinateFromPixelExternal(pixel) { - return toUserCoordinate(this.getCoordinateFromPixelInternal(pixel), this.getView().getProjection()) + return toUserCoordinate(this.getCoordinateFromPixelInternal(pixel), this.getView().getProjection()); } /** @@ -719,7 +719,7 @@ class PluggableMap extends BaseObject { return applyTransform(frameState.pixelToCoordinateTransform, pixel.slice()); } } - + /** * Get the map controls. Modifying this collection changes the controls * associated with the map. diff --git a/src/ol/control/MousePosition.js b/src/ol/control/MousePosition.js index c81c52bd13..3d97fe04d1 100644 --- a/src/ol/control/MousePosition.js +++ b/src/ol/control/MousePosition.js @@ -220,7 +220,7 @@ class MousePosition extends Control { const map = this.getMap(); const coordinate = map.getCoordinateFromPixelInternal(pixel); if (coordinate) { - const userProjection = getUserProjection() + const userProjection = getUserProjection(); if (userProjection) { this.transform_ = getTransformFromProjections( this.mapProjection_, userProjection); diff --git a/src/ol/interaction/Extent.js b/src/ol/interaction/Extent.js index eb45d1aae8..863a08fa67 100644 --- a/src/ol/interaction/Extent.js +++ b/src/ol/interaction/Extent.js @@ -13,7 +13,7 @@ import PointerInteraction from './Pointer.js'; import VectorLayer from '../layer/Vector.js'; import VectorSource from '../source/Vector.js'; import {createEditingStyle} from '../style/Style.js'; -import { toUserExtent } from '../proj.js'; +import {toUserExtent} from '../proj.js'; /** From a0df485736d0dc99855945509f3fa6aff4054a78 Mon Sep 17 00:00:00 2001 From: Tobias Kohr Date: Tue, 24 Sep 2019 17:29:47 +0200 Subject: [PATCH 5/7] add tests for modified methods --- src/ol/PluggableMap.js | 4 +-- test/spec/ol/map.test.js | 75 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/src/ol/PluggableMap.js b/src/ol/PluggableMap.js index 55dd1fa509..53bf87cc22 100644 --- a/src/ol/PluggableMap.js +++ b/src/ol/PluggableMap.js @@ -27,7 +27,7 @@ import LayerGroup from './layer/Group.js'; import {hasArea} from './size.js'; import {DROP} from './structs/PriorityQueue.js'; import {create as createTransform, apply as applyTransform} from './transform.js'; -import {toUserCoordinate} from './proj.js'; +import {toUserCoordinate, fromUserCoordinate} from './proj.js'; /** @@ -810,7 +810,7 @@ class PluggableMap extends BaseObject { * @api */ getPixelFromCoordinateExternal(coordinate) { - const userCoordinate = toUserCoordinate(coordinate, this.getView().getProjection()); + const userCoordinate = fromUserCoordinate(coordinate, this.getView().getProjection()); return this.getPixelFromCoordinateInternal(userCoordinate); } diff --git a/test/spec/ol/map.test.js b/test/spec/ol/map.test.js index 3ad872aa87..979d8056a2 100644 --- a/test/spec/ol/map.test.js +++ b/test/spec/ol/map.test.js @@ -7,7 +7,7 @@ import View from '../../../src/ol/View.js'; import {LineString, Point} from '../../../src/ol/geom.js'; import {focus} from '../../../src/ol/events/condition.js'; import {defaults as defaultInteractions} from '../../../src/ol/interaction.js'; -import {get as getProjection} from '../../../src/ol/proj.js'; +import {get as getProjection, useGeographic, transform} from '../../../src/ol/proj.js'; import GeoJSON from '../../../src/ol/format/GeoJSON.js'; import DragPan from '../../../src/ol/interaction/DragPan.js'; import DoubleClickZoom from '../../../src/ol/interaction/DoubleClickZoom.js'; @@ -734,6 +734,77 @@ describe('ol.Map', function() { }); - }); + describe('getCoordinateFromPixel() and getPixelFromCoordinate()', function() { + let target, view, map; + const centerGeographic = [2.460938, 48.850258]; + const centerMercator = transform(centerGeographic, getProjection('EPSG:4326'), getProjection('EPSG:3857')); + const screenCenter = [500, 500]; + + beforeEach(function() { + target = document.createElement('div'); + + const style = target.style; + style.position = 'absolute'; + style.left = '-1000px'; + style.top = '-1000px'; + style.width = `${screenCenter[0] * 2}px`; + style.height = `${screenCenter[1] * 2}px`; + document.body.appendChild(target); + + useGeographic(); + + view = new View({ + center: centerGeographic, + zoom: 3 + }); + map = new Map({ + target: target, + view: view, + layers: [ + new TileLayer({ + source: new XYZ({ + url: '#{x}/{y}/{z}' + }) + }) + ] + }); + }); + + afterEach(function() { + map.dispose(); + document.body.removeChild(target); + }); + + it('gets coordinates in user projection', function(done) { + map.renderSync(); + const coordinateGeographic = map.getCoordinateFromPixelExternal(screenCenter); + expect(coordinateGeographic[0]).to.roughlyEqual(centerGeographic[0], 1e-5); + expect(coordinateGeographic[1]).to.roughlyEqual(centerGeographic[1], 1e-5); + done(); + }); + + it('gets coordinates in view projection', function(done) { + map.renderSync(); + const coordinateMercator = map.getCoordinateFromPixelInternal(screenCenter); + expect(coordinateMercator[0]).to.roughlyEqual(centerMercator[0], 1e-5); + expect(coordinateMercator[1]).to.roughlyEqual(centerMercator[1], 1e-5); + done(); + }); + + it('gets pixel from coordinates in user projection', function(done) { + map.renderSync(); + const pixel = map.getPixelFromCoordinateExternal(centerGeographic); + expect(pixel).to.eql(screenCenter); + done(); + }); + + it('gets pixel from coordinates in view projection', function(done) { + map.renderSync(); + const pixel = map.getPixelFromCoordinateInternal(centerMercator); + expect(pixel).to.eql(screenCenter); + done(); + }); + }); + }); }); From 182ed012e2fe2afbbdb8ab6fb1934528bdd4704e Mon Sep 17 00:00:00 2001 From: Tobias Kohr Date: Tue, 24 Sep 2019 17:54:22 +0200 Subject: [PATCH 6/7] review corrections --- src/ol/PluggableMap.js | 4 ++-- test/spec/ol/map.test.js | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ol/PluggableMap.js b/src/ol/PluggableMap.js index 53bf87cc22..c1e5afba06 100644 --- a/src/ol/PluggableMap.js +++ b/src/ol/PluggableMap.js @@ -810,8 +810,8 @@ class PluggableMap extends BaseObject { * @api */ getPixelFromCoordinateExternal(coordinate) { - const userCoordinate = fromUserCoordinate(coordinate, this.getView().getProjection()); - return this.getPixelFromCoordinateInternal(userCoordinate); + const viewCoordinate = fromUserCoordinate(coordinate, this.getView().getProjection()); + return this.getPixelFromCoordinateInternal(viewCoordinate); } /** diff --git a/test/spec/ol/map.test.js b/test/spec/ol/map.test.js index 979d8056a2..80a9dbaba6 100644 --- a/test/spec/ol/map.test.js +++ b/test/spec/ol/map.test.js @@ -7,7 +7,7 @@ import View from '../../../src/ol/View.js'; import {LineString, Point} from '../../../src/ol/geom.js'; import {focus} from '../../../src/ol/events/condition.js'; import {defaults as defaultInteractions} from '../../../src/ol/interaction.js'; -import {get as getProjection, useGeographic, transform} from '../../../src/ol/proj.js'; +import {get as getProjection, useGeographic, transform, clearUserProjection} from '../../../src/ol/proj.js'; import GeoJSON from '../../../src/ol/format/GeoJSON.js'; import DragPan from '../../../src/ol/interaction/DragPan.js'; import DoubleClickZoom from '../../../src/ol/interaction/DoubleClickZoom.js'; @@ -774,6 +774,7 @@ describe('ol.Map', function() { afterEach(function() { map.dispose(); document.body.removeChild(target); + clearUserProjection(); }); it('gets coordinates in user projection', function(done) { From 8c4937170b6cb112a66ebae17a636eaf4716291d Mon Sep 17 00:00:00 2001 From: Tobias Kohr Date: Tue, 24 Sep 2019 18:13:57 +0200 Subject: [PATCH 7/7] rename external methods to API names --- examples/igc.js | 2 +- examples/region-growing.js | 2 +- examples/synthetic-points.js | 2 +- examples/utfgrid.js | 2 +- src/ol/PluggableMap.js | 12 ++++++------ src/ol/interaction/Extent.js | 2 +- test/spec/ol/interaction/extent.test.js | 10 +++++----- test/spec/ol/map.test.js | 4 ++-- 8 files changed, 18 insertions(+), 18 deletions(-) diff --git a/examples/igc.js b/examples/igc.js index be2d2f984d..261108f59a 100644 --- a/examples/igc.js +++ b/examples/igc.js @@ -135,7 +135,7 @@ map.on('pointermove', function(evt) { if (evt.dragging) { return; } - const coordinate = map.getEventCoordinateExternal(evt.originalEvent); + const coordinate = map.getEventCoordinate(evt.originalEvent); displaySnap(coordinate); }); diff --git a/examples/region-growing.js b/examples/region-growing.js index c09678144a..0503ff7127 100644 --- a/examples/region-growing.js +++ b/examples/region-growing.js @@ -117,7 +117,7 @@ raster.on('beforeoperations', function(event) { const data = event.data; data.delta = thresholdControl.value; if (coordinate) { - data.pixel = map.getPixelFromCoordinateExternal(coordinate); + data.pixel = map.getPixelFromCoordinate(coordinate); } }); diff --git a/examples/synthetic-points.js b/examples/synthetic-points.js index 8ccfbc685c..1eceb86fdf 100644 --- a/examples/synthetic-points.js +++ b/examples/synthetic-points.js @@ -85,7 +85,7 @@ map.on('pointermove', function(evt) { if (evt.dragging) { return; } - const coordinate = map.getEventCoordinateExternal(evt.originalEvent); + const coordinate = map.getEventCoordinate(evt.originalEvent); displaySnap(coordinate); }); diff --git a/examples/utfgrid.js b/examples/utfgrid.js index b2007e921d..92b117c100 100644 --- a/examples/utfgrid.js +++ b/examples/utfgrid.js @@ -63,7 +63,7 @@ map.on('pointermove', function(evt) { if (evt.dragging) { return; } - const coordinate = map.getEventCoordinateExternal(evt.originalEvent); + const coordinate = map.getEventCoordinate(evt.originalEvent); displayCountryInfo(coordinate); }); diff --git a/src/ol/PluggableMap.js b/src/ol/PluggableMap.js index c1e5afba06..b239dd20d6 100644 --- a/src/ol/PluggableMap.js +++ b/src/ol/PluggableMap.js @@ -546,7 +546,7 @@ class PluggableMap extends BaseObject { if (!this.frameState_) { return; } - const coordinate = this.getCoordinateFromPixelExternal(pixel); + const coordinate = this.getCoordinateFromPixel(pixel); opt_options = opt_options !== undefined ? opt_options : /** @type {AtPixelOptions} */ ({}); const hitTolerance = opt_options.hitTolerance !== undefined ? @@ -618,7 +618,7 @@ class PluggableMap extends BaseObject { if (!this.frameState_) { return false; } - const coordinate = this.getCoordinateFromPixelExternal(pixel); + const coordinate = this.getCoordinateFromPixel(pixel); opt_options = opt_options !== undefined ? opt_options : /** @type {AtPixelOptions} */ ({}); const layerFilter = opt_options.layerFilter !== undefined ? opt_options.layerFilter : TRUE; @@ -634,8 +634,8 @@ class PluggableMap extends BaseObject { * @return {import("./coordinate.js").Coordinate} Coordinate. * @api */ - getEventCoordinateExternal(event) { - return this.getCoordinateFromPixelExternal(this.getEventPixel(event)); + getEventCoordinate(event) { + return this.getCoordinateFromPixel(this.getEventPixel(event)); } /** @@ -701,7 +701,7 @@ class PluggableMap extends BaseObject { * @return {import("./coordinate.js").Coordinate} The coordinate for the pixel position. * @api */ - getCoordinateFromPixelExternal(pixel) { + getCoordinateFromPixel(pixel) { return toUserCoordinate(this.getCoordinateFromPixelInternal(pixel), this.getView().getProjection()); } @@ -809,7 +809,7 @@ class PluggableMap extends BaseObject { * @return {import("./pixel.js").Pixel} A pixel position in the map viewport. * @api */ - getPixelFromCoordinateExternal(coordinate) { + getPixelFromCoordinate(coordinate) { const viewCoordinate = fromUserCoordinate(coordinate, this.getView().getProjection()); return this.getPixelFromCoordinateInternal(viewCoordinate); } diff --git a/src/ol/interaction/Extent.js b/src/ol/interaction/Extent.js index 863a08fa67..5ca737e2bb 100644 --- a/src/ol/interaction/Extent.js +++ b/src/ol/interaction/Extent.js @@ -386,7 +386,7 @@ class Extent extends PointerInteraction { * @return {import("../extent.js").Extent} Drawn extent in the view projection. * @api */ - getExtentExternal() { + getExtent() { return toUserExtent(this.getExtentInternal(), this.getMap().getView().getProjection()); } diff --git a/test/spec/ol/interaction/extent.test.js b/test/spec/ol/interaction/extent.test.js index c36e8a8350..56fd15765e 100644 --- a/test/spec/ol/interaction/extent.test.js +++ b/test/spec/ol/interaction/extent.test.js @@ -99,7 +99,7 @@ describe('ol.interaction.Extent', function() { simulateEvent('pointerdrag', 50, 50, false, 0); simulateEvent('pointerup', 50, 50, false, 0); - expect(interaction.getExtentExternal()).to.eql([-50, -50, 50, 50]); + expect(interaction.getExtent()).to.eql([-50, -50, 50, 50]); }); it('clicking off extent nulls extent', function() { @@ -108,7 +108,7 @@ describe('ol.interaction.Extent', function() { simulateEvent('pointerdown', -10, -10, false, 0); simulateEvent('pointerup', -10, -10, false, 0); - expect(interaction.getExtentExternal()).to.equal(null); + expect(interaction.getExtent()).to.equal(null); }); it('clicking on extent does not null extent', function() { @@ -117,7 +117,7 @@ describe('ol.interaction.Extent', function() { simulateEvent('pointerdown', 50, 50, false, 0); simulateEvent('pointerup', 50, 50, false, 0); - expect(interaction.getExtentExternal()).to.eql([-50, -50, 50, 50]); + expect(interaction.getExtent()).to.eql([-50, -50, 50, 50]); }); it('snap and drag vertex works', function() { @@ -127,7 +127,7 @@ describe('ol.interaction.Extent', function() { simulateEvent('pointerdrag', -70, -40, false, 0); simulateEvent('pointerup', -70, -40, false, 0); - expect(interaction.getExtentExternal()).to.eql([-70, -50, -50, -40]); + expect(interaction.getExtent()).to.eql([-70, -50, -50, -40]); }); it('snap and drag edge works', function() { @@ -137,7 +137,7 @@ describe('ol.interaction.Extent', function() { simulateEvent('pointerdrag', 20, -30, false, 0); simulateEvent('pointerup', 20, -30, false, 0); - expect(interaction.getExtentExternal()).to.eql([-50, -50, 20, 50]); + expect(interaction.getExtent()).to.eql([-50, -50, 20, 50]); }); }); }); diff --git a/test/spec/ol/map.test.js b/test/spec/ol/map.test.js index 80a9dbaba6..f72a7ccc28 100644 --- a/test/spec/ol/map.test.js +++ b/test/spec/ol/map.test.js @@ -779,7 +779,7 @@ describe('ol.Map', function() { it('gets coordinates in user projection', function(done) { map.renderSync(); - const coordinateGeographic = map.getCoordinateFromPixelExternal(screenCenter); + const coordinateGeographic = map.getCoordinateFromPixel(screenCenter); expect(coordinateGeographic[0]).to.roughlyEqual(centerGeographic[0], 1e-5); expect(coordinateGeographic[1]).to.roughlyEqual(centerGeographic[1], 1e-5); done(); @@ -795,7 +795,7 @@ describe('ol.Map', function() { it('gets pixel from coordinates in user projection', function(done) { map.renderSync(); - const pixel = map.getPixelFromCoordinateExternal(centerGeographic); + const pixel = map.getPixelFromCoordinate(centerGeographic); expect(pixel).to.eql(screenCenter); done(); });