1 | const createVertex = require('./create_vertex');
|
2 | const createMidpoint = require('./create_midpoint');
|
3 | const Constants = require('../constants');
|
4 |
|
5 | function 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 |
|
13 | supplementaryPoints.push(createVertex(featureId, coordinates, basePath, isSelectedPath(basePath)));
|
14 | } else if (type === Constants.geojsonTypes.POLYGON) {
|
15 |
|
16 |
|
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 |
|
34 |
|
35 |
|
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 |
|
45 |
|
46 |
|
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 |
|
63 |
|
64 |
|
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 |
|
83 | module.exports = createSupplementaryPoints;
|