-
Notifications
You must be signed in to change notification settings - Fork 165
/
split-polygon-mode.ts
201 lines (176 loc) · 6.76 KB
/
split-polygon-mode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import booleanPointInPolygon from '@turf/boolean-point-in-polygon';
import turfDifference from '@turf/difference';
import turfBuffer from '@turf/buffer';
import lineIntersect from '@turf/line-intersect';
import { lineString, Point } from '@turf/helpers';
import turfBearing from '@turf/bearing';
import turfDistance from '@turf/distance';
import turfDestination from '@turf/destination';
import turfPolygonToLine from '@turf/polygon-to-line';
import nearestPointOnLine from '@turf/nearest-point-on-line';
import { generatePointsParallelToLinePoints } from '../utils';
import { FeatureCollection } from '../geojson-types';
import {
ClickEvent,
PointerMoveEvent,
ModeProps,
GuideFeatureCollection,
TentativeFeature,
} from '../types';
import { GeoJsonEditMode, GeoJsonEditAction } from './geojson-edit-mode';
import { ImmutableFeatureCollection } from './immutable-feature-collection';
export class SplitPolygonMode extends GeoJsonEditMode {
calculateMapCoords(clickSequence: any, mapCoords: any, props: ModeProps<FeatureCollection>) {
const modeConfig = props.modeConfig;
if (!modeConfig || !modeConfig.lock90Degree || !clickSequence.length) {
return mapCoords;
}
if (clickSequence.length === 1) {
// if first point is clicked, then find closest polygon point and build ~90deg vector
const firstPoint = clickSequence[0];
const selectedGeometry = this.getSelectedGeometry(props);
// @ts-expect-error turf types diff
const feature = turfPolygonToLine(selectedGeometry);
const lines = feature.type === 'FeatureCollection' ? feature.features : [feature];
let minDistance = Number.MAX_SAFE_INTEGER;
let closestPoint = null;
// If Multipolygon, then we should find nearest polygon line and stick split to it.
lines.forEach((line) => {
const snapPoint = nearestPointOnLine(line, firstPoint);
const distanceFromOrigin = turfDistance(snapPoint, firstPoint);
if (minDistance > distanceFromOrigin) {
minDistance = distanceFromOrigin;
closestPoint = snapPoint;
}
});
if (closestPoint) {
// closest point is used as 90degree entry to the polygon
const lastBearing = turfBearing(firstPoint, closestPoint);
const currentDistance = turfDistance(firstPoint, mapCoords, { units: 'meters' });
return turfDestination(firstPoint, currentDistance, lastBearing, {
units: 'meters',
}).geometry.coordinates;
}
return mapCoords;
}
// Allow only 90 degree turns
const lastPoint = clickSequence[clickSequence.length - 1];
const [approximatePoint] = generatePointsParallelToLinePoints(
clickSequence[clickSequence.length - 2],
lastPoint,
mapCoords
);
// align point with current ground
const nearestPt = nearestPointOnLine(lineString([lastPoint, approximatePoint]), mapCoords)
.geometry.coordinates;
return nearestPt;
}
getGuides(props: ModeProps<FeatureCollection>): GuideFeatureCollection {
const clickSequence = this.getClickSequence();
const guides: GuideFeatureCollection = {
type: 'FeatureCollection',
features: [],
};
if (clickSequence.length === 0 || !props.lastPointerMoveEvent) {
// nothing to do yet
return guides;
}
const { mapCoords } = props.lastPointerMoveEvent;
guides.features.push({
type: 'Feature',
properties: {
guideType: 'tentative',
},
geometry: {
type: 'LineString',
coordinates: [...clickSequence, this.calculateMapCoords(clickSequence, mapCoords, props)],
},
});
return guides;
}
handleClick(event: ClickEvent, props: ModeProps<FeatureCollection>) {
const tentativeFeature = this.getTentativeGuide(props);
const selectedGeometry = this.getSelectedGeometry(props);
if (!selectedGeometry) {
// eslint-disable-next-line no-console,no-undef
console.warn('A polygon must be selected for splitting');
return;
}
const clickSequence = this.getClickSequence();
if (tentativeFeature && tentativeFeature.geometry.type === 'LineString') {
clickSequence.push(
tentativeFeature.geometry.coordinates[tentativeFeature.geometry.coordinates.length - 1]
);
} else {
this.addClickSequence(event);
}
const pt: Point = {
type: 'Point',
coordinates: clickSequence[clickSequence.length - 1],
};
// @ts-expect-error turf types diff
const isPointInPolygon = booleanPointInPolygon(pt, selectedGeometry);
if (clickSequence.length > 1 && tentativeFeature && !isPointInPolygon) {
this.resetClickSequence();
// @ts-expect-error narrow type
const isLineInterectingWithPolygon = lineIntersect(tentativeFeature, selectedGeometry);
if (isLineInterectingWithPolygon.features.length === 0) {
return;
}
const editAction = this.splitPolygon(tentativeFeature, props);
if (editAction) {
props.onEdit(editAction);
}
}
}
handlePointerMove(event: PointerMoveEvent, props: ModeProps<FeatureCollection>) {
props.onUpdateCursor('cell');
}
splitPolygon(tentativeFeature: TentativeFeature, props: ModeProps<FeatureCollection>) {
const selectedGeometry = this.getSelectedGeometry(props);
const featureIndex = props.selectedIndexes[0];
const modeConfig = props.modeConfig || {};
// Default gap in between the polygon
let { gap = 0.1, units = 'centimeters' } = modeConfig;
if (gap === 0) {
gap = 0.1;
units = 'centimeters';
}
const buffer = turfBuffer(tentativeFeature, gap, { units });
// @ts-expect-error turf types diff
const updatedGeometry = turfDifference(selectedGeometry, buffer);
if (!updatedGeometry) {
// eslint-disable-next-line no-console,no-undef
console.warn('Canceling edit. Split Polygon erased');
return null;
}
const { type, coordinates } = updatedGeometry.geometry;
let updatedCoordinates = [];
if (type === 'Polygon') {
// Update the coordinates as per Multipolygon
updatedCoordinates = coordinates.map((c) => [c]);
} else {
// Handle Case when Multipolygon has holes
updatedCoordinates = coordinates.reduce((agg, prev) => {
prev.forEach((p) => {
// @ts-expect-error revisit coordinates type here
agg.push([p]);
});
return agg;
}, []);
}
// Update the type to Mulitpolygon
const updatedData = new ImmutableFeatureCollection(props.data).replaceGeometry(featureIndex, {
type: 'MultiPolygon',
coordinates: updatedCoordinates,
});
const editAction: GeoJsonEditAction = {
updatedData: updatedData.getObject(),
editType: 'split',
editContext: {
featureIndexes: [featureIndex],
},
};
return editAction;
}
}