Skip to content

Commit

Permalink
Add action to change map scales
Browse files Browse the repository at this point in the history
  • Loading branch information
mwa authored and manisandro committed Dec 12, 2016
1 parent b639dee commit d00eecf
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 2 deletions.
11 changes: 11 additions & 0 deletions web/client/actions/__tests__/map-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ var {
CHANGE_MOUSE_POINTER,
CHANGE_ZOOM_LVL,
CHANGE_MAP_CRS,
CHANGE_MAP_SCALES,
CHANGE_MAP_STYLE,
CHANGE_ROTATION,
changeMapView,
clickOnMap,
changeMousePointer,
changeZoomLevel,
changeMapCrs,
changeMapScales,
changeMapStyle,
changeRotation
} = require('../map');
Expand Down Expand Up @@ -79,6 +81,15 @@ describe('Test correctness of the map actions', () => {
expect(retval.crs).toBe(testVal);
});

it('changeMapScales', () => {
const testScales = [100000, 50000, 25000, 10000, 5000];
const retval = changeMapScales(testScales);

expect(retval).toExist();
expect(retval.type).toBe(CHANGE_MAP_SCALES);
expect(retval.scales).toEqual(testScales);
});

it('changeMapStyle', () => {
let style = {width: 100};
let mapStateSource = "test";
Expand Down
9 changes: 9 additions & 0 deletions web/client/actions/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const CHANGE_ZOOM_LVL = 'CHANGE_ZOOM_LVL';
const PAN_TO = 'PAN_TO';
const ZOOM_TO_EXTENT = 'ZOOM_TO_EXTENT';
const CHANGE_MAP_CRS = 'CHANGE_MAP_CRS';
const CHANGE_MAP_SCALES = 'CHANGE_MAP_SCALES';
const CHANGE_MAP_STYLE = 'CHANGE_MAP_STYLE';
const CHANGE_ROTATION = 'CHANGE_ROTATION';

Expand All @@ -36,6 +37,12 @@ function changeMapCrs(crs) {
};
}

function changeMapScales(scales) {
return {
type: CHANGE_MAP_SCALES,
scales: scales
};
}

function clickOnMap(point) {
return {
Expand Down Expand Up @@ -97,13 +104,15 @@ module.exports = {
PAN_TO,
ZOOM_TO_EXTENT,
CHANGE_MAP_CRS,
CHANGE_MAP_SCALES,
CHANGE_MAP_STYLE,
CHANGE_ROTATION,
changeMapView,
clickOnMap,
changeMousePointer,
changeZoomLevel,
changeMapCrs,
changeMapScales,
zoomToExtent,
panTo,
changeMapStyle,
Expand Down
70 changes: 70 additions & 0 deletions web/client/reducers/__tests__/map-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,76 @@ describe('Test the map reducer', () => {
expect(state.projection).toBe('EPSG:4326');
});

it('sets new map scales', () => {
// set map scales
const action = {
type: 'CHANGE_MAP_SCALES',
scales: [9600, 960]
};
const resolutions = [2.54, 0.254];
const action2 = {
type: 'CHANGE_MAP_SCALES',
scales: [38400, 19200, 9600, 4800]
};
const resolutions2 = [10.16, 5.08, 2.54, 1.27];
// reset map scales
const actionReset = {
type: 'CHANGE_MAP_SCALES'
};

// add map scales
var state = mapConfig({projection: "EPSG:3857"}, action);
expect(state.mapOptions).toExist();
expect(state.mapOptions.view).toExist();
expect(state.mapOptions.view.resolutions).toEqual(resolutions);
expect(state.projection).toBe("EPSG:3857");

// update map scales
state = mapConfig(state, action2);
expect(state.mapOptions).toExist();
expect(state.mapOptions.view).toExist();
expect(state.mapOptions.view.resolutions).toEqual(resolutions2);

// remove state.mapOptions on map scales reset
state = mapConfig({
mapOptions: {
view: {
resolutions: [8, 4, 2]
}
},
prop: 'prop'
}, actionReset);
expect(state.mapOptions).toNotExist();
expect(state.prop).toBe('prop');

// remove only state.mapOptions.view on map scales reset
state = mapConfig({
mapOptions: {
view: {
resolutions: [8, 4, 2]
},
prop: 'prop'
}
}, actionReset);
expect(state.mapOptions).toExist();
expect(state.mapOptions.view).toNotExist();
expect(state.mapOptions.prop).toBe('prop');

// remove only state.mapOptions.view.resolutions on map scales reset
state = mapConfig({
mapOptions: {
view: {
resolutions: [8, 4, 2],
prop: 'prop'
}
}
}, actionReset);
expect(state.mapOptions).toExist();
expect(state.mapOptions.view).toExist();
expect(state.mapOptions.view.resolutions).toNotExist();
expect(state.mapOptions.view.prop).toBe('prop');
});

it('zoom to extent', () => {
const action = {
type: 'ZOOM_TO_EXTENT',
Expand Down
33 changes: 31 additions & 2 deletions web/client/reducers/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
*/

var {CHANGE_MAP_VIEW, CHANGE_MOUSE_POINTER,
CHANGE_ZOOM_LVL, CHANGE_MAP_CRS, ZOOM_TO_EXTENT, PAN_TO, CHANGE_MAP_STYLE,
CHANGE_ROTATION} = require('../actions/map');
CHANGE_ZOOM_LVL, CHANGE_MAP_CRS, CHANGE_MAP_SCALES, ZOOM_TO_EXTENT, PAN_TO,
CHANGE_MAP_STYLE, CHANGE_ROTATION} = require('../actions/map');


var assign = require('object-assign');
Expand All @@ -33,6 +33,35 @@ function mapConfig(state = null, action) {
return assign({}, state, {
projection: action.crs
});
case CHANGE_MAP_SCALES:
if (action.scales) {
const dpi = state.mapOptions && state.mapOptions.view && state.mapOptions.view.DPI || null;
const resolutions = MapUtils.getResolutionsForScales(action.scales, state.projection || "EPSG:4326", dpi);
// add or update mapOptions.view.resolutions
let mapOptions = assign({}, state.mapOptions);
mapOptions.view = assign({}, mapOptions.view, {
resolutions: resolutions
});
return assign({}, state, {
mapOptions: mapOptions
});
} else if (state.mapOptions && state.mapOptions.view && state.mapOptions.view && state.mapOptions.view.resolutions) {
// deeper clone
let newState = assign({}, state);
newState.mapOptions = assign({}, newState.mapOptions);
newState.mapOptions.view = assign({}, newState.mapOptions.view);
// remove resolutions
delete newState.mapOptions.view.resolutions;
// cleanup state
if (Object.keys(newState.mapOptions.view).length === 0) {
delete newState.mapOptions.view;
}
if (Object.keys(newState.mapOptions).length === 0) {
delete newState.mapOptions;
}
return newState;
}
return state;
case ZOOM_TO_EXTENT: {
let zoom = 0;
let bounds = CoordinatesUtils.reprojectBbox(action.extent, action.crs, state.bbox && state.bbox.crs || "EPSG:4326");
Expand Down

0 comments on commit d00eecf

Please sign in to comment.