Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GET_PIXEL_FROM_COORDINATES_HOOK and GET_COORDINATES_FROM_PIXEL_HOOK map hooks #1278

Merged
merged 1 commit into from
Nov 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions web/client/components/map/leaflet/Map.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
}
});

Expand Down
18 changes: 18 additions & 0 deletions web/client/components/map/leaflet/__tests__/Map-test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<LeafletMap id="mymap" center={{y: 0, x: 0}} zoom={11} registerHooks={true}/>,
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();
});

});
6 changes: 6 additions & 0 deletions web/client/components/map/openlayers/Map.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions web/client/components/map/openlayers/__tests__/Map-test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<OpenlayersMap id="mymap" center={{y: 0, x: 0}} zoom={11} registerHooks={true}/>,
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();
});

});
4 changes: 4 additions & 0 deletions web/client/utils/MapUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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,
Expand Down