UNPKG

3.16 kBJavaScriptView Raw
1const createVertex = require('./create_vertex');
2const createMidpoint = require('./create_midpoint');
3const Constants = require('../constants');
4
5function createSupplementaryPoints(geojson, options = {}, basePath = null) {
6 const { type, coordinates } = geojson.geometry;
7 const featureId = geojson.properties && geojson.properties.id;
8
9 let supplementaryPoints = [];
10
11 if (type === Constants.geojsonTypes.POINT) {
12 // For points, just create a vertex
13 supplementaryPoints.push(createVertex(featureId, coordinates, basePath, isSelectedPath(basePath)));
14 } else if (type === Constants.geojsonTypes.POLYGON) {
15 // Cycle through a Polygon's rings and
16 // process each line
17 coordinates.forEach((line, lineIndex) => {
18 processLine(line, (basePath !== null) ? `${basePath}.${lineIndex}` : String(lineIndex));
19 });
20 } else if (type === Constants.geojsonTypes.LINE_STRING) {
21 processLine(coordinates, basePath);
22 } else if (type.indexOf(Constants.geojsonTypes.MULTI_PREFIX) === 0) {
23 processMultiGeometry();
24 }
25
26 function processLine(line, lineBasePath) {
27 let firstPointString = '';
28 let lastVertex = null;
29 line.forEach((point, pointIndex) => {
30 const pointPath = (lineBasePath !== undefined && lineBasePath !== null) ? `${lineBasePath}.${pointIndex}` : String(pointIndex);
31 const vertex = createVertex(featureId, point, pointPath, isSelectedPath(pointPath));
32
33 // If we're creating midpoints, check if there was a
34 // vertex before this one. If so, add a midpoint
35 // between that vertex and this one.
36 if (options.midpoints && lastVertex) {
37 const midpoint = createMidpoint(featureId, lastVertex, vertex, options.map);
38 if (midpoint) {
39 supplementaryPoints.push(midpoint);
40 }
41 }
42 lastVertex = vertex;
43
44 // A Polygon line's last point is the same as the first point. If we're on the last
45 // point, we want to draw a midpoint before it but not another vertex on it
46 // (since we already a vertex there, from the first point).
47 const stringifiedPoint = JSON.stringify(point);
48 if (firstPointString !== stringifiedPoint) {
49 supplementaryPoints.push(vertex);
50 }
51 if (pointIndex === 0) {
52 firstPointString = stringifiedPoint;
53 }
54 });
55 }
56
57 function isSelectedPath(path) {
58 if (!options.selectedPaths) return false;
59 return options.selectedPaths.indexOf(path) !== -1;
60 }
61
62 // Split a multi-geometry into constituent
63 // geometries, and accumulate the supplementary points
64 // for each of those constituents
65 function processMultiGeometry() {
66 const subType = type.replace(Constants.geojsonTypes.MULTI_PREFIX, '');
67 coordinates.forEach((subCoordinates, index) => {
68 const subFeature = {
69 type: Constants.geojsonTypes.FEATURE,
70 properties: geojson.properties,
71 geometry: {
72 type: subType,
73 coordinates: subCoordinates
74 }
75 };
76 supplementaryPoints = supplementaryPoints.concat(createSupplementaryPoints(subFeature, options, index));
77 });
78 }
79
80 return supplementaryPoints;
81}
82
83module.exports = createSupplementaryPoints;