From 3287ef11ff49a1af32a8f5f35a83c22ce70b0300 Mon Sep 17 00:00:00 2001 From: Sandro Mani Date: Tue, 15 Nov 2016 17:13:38 +0100 Subject: [PATCH] Add GET_PIXEL_FROM_COORDINATES_HOOK and GET_COORDINATES_FROM_PIXEL_HOOK map hooks --- web/client/components/map/leaflet/Map.jsx | 9 +++++++++ .../map/leaflet/__tests__/Map-test.jsx | 18 ++++++++++++++++++ web/client/components/map/openlayers/Map.jsx | 6 ++++++ .../map/openlayers/__tests__/Map-test.jsx | 19 +++++++++++++++++++ web/client/utils/MapUtils.js | 4 ++++ 5 files changed, 56 insertions(+) diff --git a/web/client/components/map/leaflet/Map.jsx b/web/client/components/map/leaflet/Map.jsx index 44528debe2..1a13773acb 100644 --- a/web/client/components/map/leaflet/Map.jsx +++ b/web/client/components/map/leaflet/Map.jsx @@ -299,6 +299,15 @@ let LeafletMap = React.createClass({ rotation: 0 }; }); + mapUtils.registerHook(mapUtils.GET_PIXEL_FROM_COORDINATES_HOOK, (pos) => { + let latLng = CoordinatesUtils.reproject(pos, this.props.projection, 'EPSG:4326'); + let pixel = this.map.latLngToContainerPoint([latLng.x, latLng.y]); + return [pixel.x, pixel.y]; + }); + mapUtils.registerHook(mapUtils.GET_COORDINATES_FROM_PIXEL_HOOK, (pixel) => { + let pos = CoordinatesUtils.reproject(this.map.containerPointToLatLng(pixel), 'EPSG:4326', this.props.projection); + return [pos.x, pos.y]; + }); } }); diff --git a/web/client/components/map/leaflet/__tests__/Map-test.jsx b/web/client/components/map/leaflet/__tests__/Map-test.jsx index 2518b1e54a..fa7a746bf1 100644 --- a/web/client/components/map/leaflet/__tests__/Map-test.jsx +++ b/web/client/components/map/leaflet/__tests__/Map-test.jsx @@ -255,4 +255,22 @@ describe('LeafletMap', () => { expect(map.map.getCenter().lat).toBe(40.0); }); + it('test GET_PIXEL_FROM_COORDINATES_HOOK/GET_COORDINATES_FROM_PIXEL_HOOK hook registration', () => { + mapUtils.registerHook(mapUtils.GET_PIXEL_FROM_COORDINATES_HOOK, undefined); + mapUtils.registerHook(mapUtils.GET_COORDINATES_FROM_PIXEL_HOOK, undefined); + let getPixelFromCoordinates = mapUtils.getHook(mapUtils.GET_PIXEL_FROM_COORDINATES_HOOK); + let getCoordinatesFromPixel = mapUtils.getHook(mapUtils.GET_COORDINATES_FROM_PIXEL_HOOK); + expect(getPixelFromCoordinates).toNotExist(); + expect(getCoordinatesFromPixel).toNotExist(); + + const map = ReactDOM.render(, + document.getElementById("container")); + expect(map).toExist(); + + getPixelFromCoordinates = mapUtils.getHook(mapUtils.GET_PIXEL_FROM_COORDINATES_HOOK); + getCoordinatesFromPixel = mapUtils.getHook(mapUtils.GET_COORDINATES_FROM_PIXEL_HOOK); + expect(getPixelFromCoordinates).toExist(); + expect(getCoordinatesFromPixel).toExist(); + }); + }); diff --git a/web/client/components/map/openlayers/Map.jsx b/web/client/components/map/openlayers/Map.jsx index 73e7c13801..74215f327c 100644 --- a/web/client/components/map/openlayers/Map.jsx +++ b/web/client/components/map/openlayers/Map.jsx @@ -345,6 +345,12 @@ var OpenlayersMap = React.createClass({ rotation: this.map.getView().getRotation() }; }); + mapUtils.registerHook(mapUtils.GET_PIXEL_FROM_COORDINATES_HOOK, (pos) => { + return this.map.getPixelFromCoordinate(pos); + }); + mapUtils.registerHook(mapUtils.GET_COORDINATES_FROM_PIXEL_HOOK, (pixel) => { + return this.map.getCoordinateFromPixel(pixel); + }); } }); // add overrides for css diff --git a/web/client/components/map/openlayers/__tests__/Map-test.jsx b/web/client/components/map/openlayers/__tests__/Map-test.jsx index 4f7ade469f..4e68beb4e7 100644 --- a/web/client/components/map/openlayers/__tests__/Map-test.jsx +++ b/web/client/components/map/openlayers/__tests__/Map-test.jsx @@ -223,4 +223,23 @@ describe('OpenlayersMap', () => { expect(bbox.crs).toBe("EPSG:3857"); expect(bbox.rotation).toBe(0); }); + + it('test GET_PIXEL_FROM_COORDINATES_HOOK/GET_COORDINATES_FROM_PIXEL_HOOK hook registration', () => { + mapUtils.registerHook(mapUtils.GET_PIXEL_FROM_COORDINATES_HOOK, undefined); + mapUtils.registerHook(mapUtils.GET_COORDINATES_FROM_PIXEL_HOOK, undefined); + let getPixelFromCoordinates = mapUtils.getHook(mapUtils.GET_PIXEL_FROM_COORDINATES_HOOK); + let getCoordinatesFromPixel = mapUtils.getHook(mapUtils.GET_COORDINATES_FROM_PIXEL_HOOK); + expect(getPixelFromCoordinates).toNotExist(); + expect(getCoordinatesFromPixel).toNotExist(); + + const map = ReactDOM.render(, + document.getElementById("map")); + expect(map).toExist(); + + getPixelFromCoordinates = mapUtils.getHook(mapUtils.GET_PIXEL_FROM_COORDINATES_HOOK); + getCoordinatesFromPixel = mapUtils.getHook(mapUtils.GET_COORDINATES_FROM_PIXEL_HOOK); + expect(getPixelFromCoordinates).toExist(); + expect(getCoordinatesFromPixel).toExist(); + }); + }); diff --git a/web/client/utils/MapUtils.js b/web/client/utils/MapUtils.js index 01b23033f5..c949e0cd4c 100644 --- a/web/client/utils/MapUtils.js +++ b/web/client/utils/MapUtils.js @@ -18,6 +18,8 @@ const EXTENT_TO_ZOOM_HOOK = 'EXTENT_TO_ZOOM_HOOK'; const RESOLUTIONS_HOOK = 'RESOLUTIONS_HOOK'; const RESOLUTION_HOOK = 'RESOLUTION_HOOK'; const COMPUTE_BBOX_HOOK = 'COMPUTE_BBOX_HOOK'; +const GET_PIXEL_FROM_COORDINATES_HOOK = 'GET_PIXEL_FROM_COORDINATES_HOOK'; +const GET_COORDINATES_FROM_PIXEL_HOOK = 'GET_COORDINATES_FROM_PIXEL_HOOK'; var hooks = {}; var CoordinatesUtils = require('./CoordinatesUtils'); @@ -245,6 +247,8 @@ module.exports = { RESOLUTIONS_HOOK, RESOLUTION_HOOK, COMPUTE_BBOX_HOOK, + GET_PIXEL_FROM_COORDINATES_HOOK, + GET_COORDINATES_FROM_PIXEL_HOOK, DEFAULT_SCREEN_DPI, registerHook, getHook,