1 | const constrainFeatureMovement = require('./constrain_feature_movement');
|
2 | const Constants = require('../constants');
|
3 |
|
4 | module.exports = function(features, delta) {
|
5 | const constrainedDelta = constrainFeatureMovement(features.map(feature => feature.toGeoJSON()), delta);
|
6 |
|
7 | features.forEach(feature => {
|
8 | const currentCoordinates = feature.getCoordinates();
|
9 |
|
10 | const moveCoordinate = (coord) => {
|
11 | const point = {
|
12 | lng: coord[0] + constrainedDelta.lng,
|
13 | lat: coord[1] + constrainedDelta.lat
|
14 | };
|
15 | return [point.lng, point.lat];
|
16 | };
|
17 | const moveRing = (ring) => ring.map(coord => moveCoordinate(coord));
|
18 | const moveMultiPolygon = (multi) => multi.map(ring => moveRing(ring));
|
19 |
|
20 | let nextCoordinates;
|
21 | if (feature.type === Constants.geojsonTypes.POINT) {
|
22 | nextCoordinates = moveCoordinate(currentCoordinates);
|
23 | } else if (feature.type === Constants.geojsonTypes.LINE_STRING || feature.type === Constants.geojsonTypes.MULTI_POINT) {
|
24 | nextCoordinates = currentCoordinates.map(moveCoordinate);
|
25 | } else if (feature.type === Constants.geojsonTypes.POLYGON || feature.type === Constants.geojsonTypes.MULTI_LINE_STRING) {
|
26 | nextCoordinates = currentCoordinates.map(moveRing);
|
27 | } else if (feature.type === Constants.geojsonTypes.MULTI_POLYGON) {
|
28 | nextCoordinates = currentCoordinates.map(moveMultiPolygon);
|
29 | }
|
30 |
|
31 | feature.incomingCoords(nextCoordinates);
|
32 | });
|
33 | };
|