UNPKG

1.06 kBJavaScriptView Raw
1const Constants = require('../constants');
2
3module.exports = function(parent, startVertex, endVertex, map) {
4 const startCoord = startVertex.geometry.coordinates;
5 const endCoord = endVertex.geometry.coordinates;
6
7 // If a coordinate exceeds the projection, we can't calculate a midpoint,
8 // so run away
9 if (startCoord[1] > Constants.LAT_RENDERED_MAX ||
10 startCoord[1] < Constants.LAT_RENDERED_MIN ||
11 endCoord[1] > Constants.LAT_RENDERED_MAX ||
12 endCoord[1] < Constants.LAT_RENDERED_MIN) {
13 return null;
14 }
15
16 const ptA = map.project([ startCoord[0], startCoord[1] ]);
17 const ptB = map.project([ endCoord[0], endCoord[1] ]);
18 const mid = map.unproject([ (ptA.x + ptB.x) / 2, (ptA.y + ptB.y) / 2 ]);
19
20 return {
21 type: Constants.geojsonTypes.FEATURE,
22 properties: {
23 meta: Constants.meta.MIDPOINT,
24 parent: parent,
25 lng: mid.lng,
26 lat: mid.lat,
27 coord_path: endVertex.properties.coord_path
28 },
29 geometry: {
30 type: Constants.geojsonTypes.POINT,
31 coordinates: [mid.lng, mid.lat]
32 }
33 };
34};