dgeoutils 2.4.48 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/dist/cjs/DCircle.d.ts +18 -0
  2. package/dist/cjs/DCircle.js +102 -0
  3. package/dist/cjs/DLine.d.ts +39 -0
  4. package/dist/cjs/DLine.js +343 -0
  5. package/dist/cjs/DNumbers.d.ts +8 -0
  6. package/dist/cjs/DNumbers.js +30 -0
  7. package/dist/cjs/DPlane.d.ts +25 -0
  8. package/dist/cjs/DPlane.js +132 -0
  9. package/dist/cjs/DPoint.d.ts +139 -0
  10. package/dist/cjs/DPoint.js +820 -0
  11. package/dist/cjs/DPolygon.d.ts +139 -0
  12. package/dist/cjs/DPolygon.js +1774 -0
  13. package/dist/cjs/DPolygonLoop.d.ts +60 -0
  14. package/dist/cjs/DPolygonLoop.js +439 -0
  15. package/dist/cjs/FastSearch.d.ts +6 -0
  16. package/dist/cjs/FastSearch.js +53 -0
  17. package/dist/cjs/InterpolationMatrix.d.ts +24 -0
  18. package/dist/cjs/InterpolationMatrix.js +173 -0
  19. package/dist/cjs/TraceMatrix.d.ts +22 -0
  20. package/dist/cjs/TraceMatrix.js +285 -0
  21. package/dist/cjs/index.d.ts +11 -0
  22. package/dist/cjs/index.js +37 -0
  23. package/dist/cjs/utils.d.ts +49 -0
  24. package/dist/cjs/utils.js +281 -0
  25. package/dist/es2015/DCircle.js +87 -0
  26. package/dist/es2015/DLine.js +290 -0
  27. package/dist/es2015/DNumbers.js +22 -0
  28. package/dist/es2015/DPlane.js +105 -0
  29. package/dist/es2015/DPoint.js +693 -0
  30. package/dist/es2015/DPolygon.js +1203 -0
  31. package/dist/es2015/DPolygonLoop.js +430 -0
  32. package/dist/es2015/FastSearch.js +25 -0
  33. package/dist/es2015/InterpolationMatrix.js +128 -0
  34. package/dist/es2015/TraceMatrix.js +229 -0
  35. package/dist/es2015/index.js +11 -0
  36. package/dist/es2015/utils.js +208 -0
  37. package/dist/esm/DCircle.js +99 -0
  38. package/dist/esm/DLine.js +340 -0
  39. package/dist/esm/DNumbers.js +27 -0
  40. package/dist/esm/DPlane.js +129 -0
  41. package/dist/esm/DPoint.js +817 -0
  42. package/dist/esm/DPolygon.js +1771 -0
  43. package/dist/esm/DPolygonLoop.js +436 -0
  44. package/dist/esm/FastSearch.js +50 -0
  45. package/dist/esm/InterpolationMatrix.js +170 -0
  46. package/dist/esm/TraceMatrix.js +282 -0
  47. package/dist/esm/index.js +11 -0
  48. package/dist/esm/utils.js +266 -0
  49. package/dist/umd/dgeoutils.js +4394 -0
  50. package/dist/umd/dgeoutils.min.js +1 -0
  51. package/dist/umd/dgeoutils.min.js.map +1 -0
  52. package/package.json +9 -1
@@ -0,0 +1,1774 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __generator = (this && this.__generator) || function (thisArg, body) {
14
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
15
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
16
+ function verb(n) { return function (v) { return step([n, v]); }; }
17
+ function step(op) {
18
+ if (f) throw new TypeError("Generator is already executing.");
19
+ while (_) try {
20
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
21
+ if (y = 0, t) op = [op[0] & 2, t.value];
22
+ switch (op[0]) {
23
+ case 0: case 1: t = op; break;
24
+ case 4: _.label++; return { value: op[1], done: false };
25
+ case 5: _.label++; y = op[1]; op = [0]; continue;
26
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
27
+ default:
28
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
29
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
30
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
31
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
32
+ if (t[2]) _.ops.pop();
33
+ _.trys.pop(); continue;
34
+ }
35
+ op = body.call(thisArg, _);
36
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
37
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
38
+ }
39
+ };
40
+ var __values = (this && this.__values) || function(o) {
41
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
42
+ if (m) return m.call(o);
43
+ if (o && typeof o.length === "number") return {
44
+ next: function () {
45
+ if (o && i >= o.length) o = void 0;
46
+ return { value: o && o[i++], done: !o };
47
+ }
48
+ };
49
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
50
+ };
51
+ var __read = (this && this.__read) || function (o, n) {
52
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
53
+ if (!m) return o;
54
+ var i = m.call(o), r, ar = [], e;
55
+ try {
56
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
57
+ }
58
+ catch (error) { e = { error: error }; }
59
+ finally {
60
+ try {
61
+ if (r && !r.done && (m = i["return"])) m.call(i);
62
+ }
63
+ finally { if (e) throw e.error; }
64
+ }
65
+ return ar;
66
+ };
67
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
68
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
69
+ if (ar || !(i in from)) {
70
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
71
+ ar[i] = from[i];
72
+ }
73
+ }
74
+ return to.concat(ar || Array.prototype.slice.call(from));
75
+ };
76
+ Object.defineProperty(exports, "__esModule", { value: true });
77
+ exports.DPolygon = exports.MIN_POINTS_IN_VALID_POLYGON = void 0;
78
+ var DPoint_1 = require("./DPoint");
79
+ var DLine_1 = require("./DLine");
80
+ var DCircle_1 = require("./DCircle");
81
+ var DNumbers_1 = require("./DNumbers");
82
+ var jsts_1 = require("jsts");
83
+ var DPolygonLoop_1 = require("./DPolygonLoop");
84
+ var utils_1 = require("./utils");
85
+ var _a = jsts_1.operation.buffer.BufferParameters, CAP_ROUND = _a.CAP_ROUND, CAP_FLAT = _a.CAP_FLAT, CAP_SQUARE = _a.CAP_SQUARE;
86
+ exports.MIN_POINTS_IN_VALID_POLYGON = 3;
87
+ var APPROXIMATION_VALUE = 0.1;
88
+ var MAX_CONVEX_ITERATIONS = 100;
89
+ var CLOSE_TO_INTERSECTION_DISTANCE = 0.001;
90
+ var triangleCenter = function (triangle) {
91
+ var p0 = triangle.at(0);
92
+ var p1 = triangle.at(1);
93
+ var p2 = triangle.at(2);
94
+ return new DPoint_1.DPoint((p0.x + p1.x + p2.x) / 3, (p0.y + p1.y + p2.y) / 3);
95
+ };
96
+ var containCalculator = function (poly, p) {
97
+ var e_1, _a, e_2, _b;
98
+ var hasSamePoint = poly.points.some(function (point) { return point.equal(p); });
99
+ if (hasSamePoint) {
100
+ return true;
101
+ }
102
+ try {
103
+ for (var _c = __values(poly.loopPointsGenerator(true)()), _d = _c.next(); !_d.done; _d = _c.next()) {
104
+ var _e = __read(_d.value, 3), polygonLine = _e[2];
105
+ var onBorder = polygonLine.x(p).equal(p) && polygonLine.inRange(p);
106
+ if (onBorder) {
107
+ return true;
108
+ }
109
+ }
110
+ }
111
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
112
+ finally {
113
+ try {
114
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
115
+ }
116
+ finally { if (e_1) throw e_1.error; }
117
+ }
118
+ var totalFi = 0;
119
+ try {
120
+ for (var _f = __values(poly.loopPointsGenerator()()), _g = _f.next(); !_g.done; _g = _f.next()) {
121
+ var _h = __read(_g.value, 2), _j = _h[0], x = _j.x, y = _j.y, _k = _h[1], a = _k.x, b = _k.y;
122
+ var line1 = new DLine_1.DLine(x - p.x, y - p.y, 0);
123
+ var line2 = new DLine_1.DLine(a - p.x, b - p.y, 0);
124
+ var fiDif = line1.findFi(line2);
125
+ if (line1.vectorProduct(line2).c > 0) {
126
+ totalFi += fiDif;
127
+ }
128
+ else {
129
+ totalFi -= fiDif;
130
+ }
131
+ }
132
+ }
133
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
134
+ finally {
135
+ try {
136
+ if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
137
+ }
138
+ finally { if (e_2) throw e_2.error; }
139
+ }
140
+ var eps = Math.PI / 10000;
141
+ var result = false;
142
+ var absTotalFi = Math.abs(totalFi);
143
+ if (absTotalFi < eps) {
144
+ result = false;
145
+ }
146
+ else if (Math.abs(2 * Math.PI - absTotalFi) < eps) {
147
+ result = true;
148
+ }
149
+ else {
150
+ throw new Error('contains2 faild');
151
+ }
152
+ return result;
153
+ };
154
+ var DPolygon = (function () {
155
+ function DPolygon(pPoints) {
156
+ if (pPoints === void 0) { pPoints = []; }
157
+ this.pPoints = pPoints;
158
+ this.properties = {};
159
+ this.holes = [];
160
+ this.searchStore = {};
161
+ }
162
+ DPolygon.arrayOfTrianglesToVertices = function (triangles, height) {
163
+ return triangles.map(function (v) { return ((0, utils_1.isDefAndNotNull)(height) ? v
164
+ .loop()
165
+ .height(height)
166
+ .run() : v)
167
+ .toArrayOfCoords(); })
168
+ .flat(2);
169
+ };
170
+ DPolygon.minAreaRectangleSize = function (poly) {
171
+ var _a = poly.clone().open(), first = _a.first, second = _a.second, last = _a.last;
172
+ return new DPoint_1.DPoint(first.distance(second), first.distance(last));
173
+ };
174
+ DPolygon.toDash = function (poly) {
175
+ var e_3, _a;
176
+ var p = new DPolygon();
177
+ var result = [p];
178
+ var trigger = true;
179
+ try {
180
+ for (var _b = __values(poly.points), _c = _b.next(); !_c.done; _c = _b.next()) {
181
+ var point = _c.value;
182
+ if (trigger) {
183
+ p.push(point.clone());
184
+ }
185
+ if (point.properties.pieceBorder) {
186
+ trigger = !trigger;
187
+ if (trigger) {
188
+ p = new DPolygon();
189
+ result.push(p);
190
+ p.push(point.clone());
191
+ }
192
+ }
193
+ }
194
+ }
195
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
196
+ finally {
197
+ try {
198
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
199
+ }
200
+ finally { if (e_3) throw e_3.error; }
201
+ }
202
+ return result;
203
+ };
204
+ DPolygon.minAreaRectangleDirection = function (poly) {
205
+ var _a = poly.clone().open(), first = _a.first, second = _a.second, last = _a.last;
206
+ if (!first || !second || !last) {
207
+ return 0;
208
+ }
209
+ if (first.distance(second) > first.distance(last)) {
210
+ return first.findLine(second).getFi();
211
+ }
212
+ return first.findLine(last).getFi();
213
+ };
214
+ DPolygon.parseFromWKT = function (wkt) {
215
+ var data = wkt.trim().toUpperCase();
216
+ var res = new DPolygon();
217
+ if (data.indexOf('POLYGON') === 0) {
218
+ var regexp = /POLYGON \(\((?<data>(?:(?!\)\)$).)*?)\)\)$/miu;
219
+ var reg = regexp.exec(data);
220
+ var _a = __read(reg.groups.data
221
+ .split('), (')
222
+ .map(function (p) { return new DPolygon(p.split(', ')
223
+ .map(function (pares) { return DPoint_1.DPoint.parse(pares.split(' ').map(Number)); })); })), path = _a[0], holes = _a.slice(1);
224
+ if (holes && holes.length) {
225
+ path.holes = holes;
226
+ }
227
+ res = path;
228
+ }
229
+ if (data.indexOf('LINESTRING') === 0) {
230
+ var regexp = /LINESTRING \((?<data>(?:(?!\)$).)*?)\)$/miu;
231
+ var reg = regexp.exec(data);
232
+ res = new DPolygon(reg.groups.data
233
+ .split(', ').map(function (t) { return DPoint_1.DPoint.parse(t.split(' ').map(Number)); }));
234
+ }
235
+ if (data.indexOf('POINT') === 0) {
236
+ res = new DPolygon([DPoint_1.DPoint.parseFromWKT(data)]);
237
+ }
238
+ return res;
239
+ };
240
+ DPolygon.createSquareBySize = function (size) {
241
+ return new DPolygon([DPoint_1.DPoint.zero(), size.clone().setX(0), size.clone(), size.clone().setY(0)]).close();
242
+ };
243
+ DPolygon.prototype.loop = function () {
244
+ return new DPolygonLoop_1.DPolygonLoop(this);
245
+ };
246
+ Object.defineProperty(DPolygon.prototype, "points", {
247
+ get: function () {
248
+ return this.pPoints;
249
+ },
250
+ set: function (p) {
251
+ this.pPoints = p;
252
+ },
253
+ enumerable: false,
254
+ configurable: true
255
+ });
256
+ Object.defineProperty(DPolygon.prototype, "maxX", {
257
+ get: function () {
258
+ return this.reduce(function (a, r) { return Math.max(a, r.x); }, -Infinity);
259
+ },
260
+ enumerable: false,
261
+ configurable: true
262
+ });
263
+ Object.defineProperty(DPolygon.prototype, "minX", {
264
+ get: function () {
265
+ return this.reduce(function (a, r) { return Math.min(a, r.x); }, Infinity);
266
+ },
267
+ enumerable: false,
268
+ configurable: true
269
+ });
270
+ Object.defineProperty(DPolygon.prototype, "maxY", {
271
+ get: function () {
272
+ return this.reduce(function (a, r) { return Math.max(a, r.y); }, -Infinity);
273
+ },
274
+ enumerable: false,
275
+ configurable: true
276
+ });
277
+ Object.defineProperty(DPolygon.prototype, "minY", {
278
+ get: function () {
279
+ return this.reduce(function (a, r) { return Math.min(a, r.y); }, Infinity);
280
+ },
281
+ enumerable: false,
282
+ configurable: true
283
+ });
284
+ Object.defineProperty(DPolygon.prototype, "innerCenter", {
285
+ get: function () {
286
+ var center = this.center;
287
+ return this.toTriangles().map(function (t) {
288
+ var c = triangleCenter(t);
289
+ c.properties.score = Math.min(c.distance(t.at(0)), c.distance(t.at(1)), c.distance(t.at(2))) + c.distance(center);
290
+ return c;
291
+ })
292
+ .sort(function (a, b) { return a.properties.score - b.properties.score; })[0];
293
+ },
294
+ enumerable: false,
295
+ configurable: true
296
+ });
297
+ Object.defineProperty(DPolygon.prototype, "center", {
298
+ get: function () {
299
+ return this.leftTop.move(this.size.divide(2));
300
+ },
301
+ enumerable: false,
302
+ configurable: true
303
+ });
304
+ Object.defineProperty(DPolygon.prototype, "h", {
305
+ get: function () {
306
+ return this.maxY - this.minY;
307
+ },
308
+ enumerable: false,
309
+ configurable: true
310
+ });
311
+ Object.defineProperty(DPolygon.prototype, "w", {
312
+ get: function () {
313
+ return this.maxX - this.minX;
314
+ },
315
+ enumerable: false,
316
+ configurable: true
317
+ });
318
+ Object.defineProperty(DPolygon.prototype, "dY", {
319
+ get: function () {
320
+ return this.h;
321
+ },
322
+ enumerable: false,
323
+ configurable: true
324
+ });
325
+ Object.defineProperty(DPolygon.prototype, "dX", {
326
+ get: function () {
327
+ return this.w;
328
+ },
329
+ enumerable: false,
330
+ configurable: true
331
+ });
332
+ Object.defineProperty(DPolygon.prototype, "extend", {
333
+ get: function () {
334
+ var _a = this, minX = _a.minX, minY = _a.minY, maxX = _a.maxX, maxY = _a.maxY;
335
+ return new DPolygon([
336
+ new DPoint_1.DPoint(minX, minY),
337
+ new DPoint_1.DPoint(maxX, minY),
338
+ new DPoint_1.DPoint(maxX, maxY),
339
+ new DPoint_1.DPoint(minX, maxY),
340
+ new DPoint_1.DPoint(minX, minY)
341
+ ]);
342
+ },
343
+ enumerable: false,
344
+ configurable: true
345
+ });
346
+ Object.defineProperty(DPolygon.prototype, "size", {
347
+ get: function () {
348
+ var _a = this, w = _a.w, h = _a.h;
349
+ return new DPoint_1.DPoint(w, h);
350
+ },
351
+ enumerable: false,
352
+ configurable: true
353
+ });
354
+ Object.defineProperty(DPolygon.prototype, "leftTop", {
355
+ get: function () {
356
+ var _a = this, minX = _a.minX, minY = _a.minY;
357
+ return new DPoint_1.DPoint(minX, minY);
358
+ },
359
+ enumerable: false,
360
+ configurable: true
361
+ });
362
+ Object.defineProperty(DPolygon.prototype, "minPoint", {
363
+ get: function () {
364
+ return this.leftTop;
365
+ },
366
+ enumerable: false,
367
+ configurable: true
368
+ });
369
+ Object.defineProperty(DPolygon.prototype, "rightBottom", {
370
+ get: function () {
371
+ var _a = this, maxX = _a.maxX, maxY = _a.maxY;
372
+ return new DPoint_1.DPoint(maxX, maxY);
373
+ },
374
+ enumerable: false,
375
+ configurable: true
376
+ });
377
+ Object.defineProperty(DPolygon.prototype, "maxPoint", {
378
+ get: function () {
379
+ return this.rightBottom;
380
+ },
381
+ enumerable: false,
382
+ configurable: true
383
+ });
384
+ Object.defineProperty(DPolygon.prototype, "length", {
385
+ get: function () {
386
+ return this.pPoints.length;
387
+ },
388
+ enumerable: false,
389
+ configurable: true
390
+ });
391
+ Object.defineProperty(DPolygon.prototype, "fullLength", {
392
+ get: function () {
393
+ return this.clone().open().perimeter;
394
+ },
395
+ enumerable: false,
396
+ configurable: true
397
+ });
398
+ Object.defineProperty(DPolygon.prototype, "fullLengthLatLon", {
399
+ get: function () {
400
+ return this.clone().open().perimeterLatLon;
401
+ },
402
+ enumerable: false,
403
+ configurable: true
404
+ });
405
+ Object.defineProperty(DPolygon.prototype, "perimeter", {
406
+ get: function () {
407
+ var e_4, _a;
408
+ var p = 0;
409
+ try {
410
+ for (var _b = __values(this.loopPointsGenerator()()), _c = _b.next(); !_c.done; _c = _b.next()) {
411
+ var _d = __read(_c.value, 2), p1 = _d[0], p2 = _d[1];
412
+ p += p1.distance(p2);
413
+ }
414
+ }
415
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
416
+ finally {
417
+ try {
418
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
419
+ }
420
+ finally { if (e_4) throw e_4.error; }
421
+ }
422
+ return p;
423
+ },
424
+ enumerable: false,
425
+ configurable: true
426
+ });
427
+ Object.defineProperty(DPolygon.prototype, "perimeterLatLon", {
428
+ get: function () {
429
+ var e_5, _a;
430
+ var p = 0;
431
+ try {
432
+ for (var _b = __values(this.loopPointsGenerator()()), _c = _b.next(); !_c.done; _c = _b.next()) {
433
+ var _d = __read(_c.value, 2), p1 = _d[0], p2 = _d[1];
434
+ p += p1.distanceLatLon(p2);
435
+ }
436
+ }
437
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
438
+ finally {
439
+ try {
440
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
441
+ }
442
+ finally { if (e_5) throw e_5.error; }
443
+ }
444
+ return p;
445
+ },
446
+ enumerable: false,
447
+ configurable: true
448
+ });
449
+ Object.defineProperty(DPolygon.prototype, "area", {
450
+ get: function () {
451
+ var e_6, _a;
452
+ var sum = 0;
453
+ try {
454
+ for (var _b = __values(this.deintersection.loopPointsGenerator()()), _c = _b.next(); !_c.done; _c = _b.next()) {
455
+ var _d = __read(_c.value, 2), _e = _d[0], x = _e.x, y = _e.y, _f = _d[1], a = _f.x, b = _f.y;
456
+ sum += x * b - y * a;
457
+ }
458
+ }
459
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
460
+ finally {
461
+ try {
462
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
463
+ }
464
+ finally { if (e_6) throw e_6.error; }
465
+ }
466
+ return Math.abs(sum / 2) - this.holes.reduce(function (a, hole) { return a + hole.area; }, 0);
467
+ },
468
+ enumerable: false,
469
+ configurable: true
470
+ });
471
+ Object.defineProperty(DPolygon.prototype, "filledDeintersection", {
472
+ get: function () {
473
+ var p = this.clone().deintersection.removeDuplicates().clockWise.open();
474
+ var findSameCorners = function (startIndex) {
475
+ if (startIndex === void 0) { startIndex = 0; }
476
+ var store = {};
477
+ for (var i = startIndex; i < p.length; i++) {
478
+ var key = p.at(i).toString();
479
+ if (typeof store[key] === 'number') {
480
+ return [store[key], i];
481
+ }
482
+ store[key] = i;
483
+ }
484
+ return undefined;
485
+ };
486
+ var indexes = findSameCorners();
487
+ var holes = [];
488
+ while (indexes) {
489
+ var a = new DPolygon(p.clone().removePart(indexes[0], indexes[1] - indexes[0]));
490
+ if (a.isClockwise) {
491
+ indexes = findSameCorners(indexes[0] + 1);
492
+ }
493
+ else {
494
+ holes.push(a.close());
495
+ p.removePart(indexes[0], indexes[1] - indexes[0]);
496
+ indexes = findSameCorners();
497
+ }
498
+ }
499
+ p.holes = holes;
500
+ return p.close();
501
+ },
502
+ enumerable: false,
503
+ configurable: true
504
+ });
505
+ Object.defineProperty(DPolygon.prototype, "deintersection", {
506
+ get: function () {
507
+ var e_7, _a;
508
+ var p = this.clone().close();
509
+ var store = {};
510
+ for (var i = 0; i < p.length - 1; i++) {
511
+ var k = p.at(i).toString();
512
+ store[k] = store[k] || [];
513
+ store[k].push(i);
514
+ var _loop_1 = function (j) {
515
+ var firstLine = p.at(i).findLine(p.at(i + 1));
516
+ var secondLine = p.at(j).findLine(p.at(j + 1));
517
+ var intersectionPoint = firstLine.intersection(secondLine);
518
+ if (intersectionPoint &&
519
+ !__spreadArray(__spreadArray([], __read(firstLine.points), false), __read(secondLine.points), false).some(function (t) { return t.like(intersectionPoint); })) {
520
+ var part = p.removePart(i, j - i).reverse();
521
+ p.insertAfter.apply(p, __spreadArray([i], __read(part), false));
522
+ p.insertAfter(j, intersectionPoint);
523
+ p.insertAfter(i, intersectionPoint);
524
+ }
525
+ };
526
+ for (var j = i + 2; j < p.length - 1; j++) {
527
+ _loop_1(j);
528
+ }
529
+ }
530
+ try {
531
+ for (var _b = __values(Object.keys(store)), _c = _b.next(); !_c.done; _c = _b.next()) {
532
+ var key = _c.value;
533
+ var record = store[key];
534
+ if (record.length > 1) {
535
+ var _loop_2 = function (j) {
536
+ var origin_1 = p.clone();
537
+ var d = record[j] - record[j - 1];
538
+ if (d > 1) {
539
+ var part = new DPolygon(origin_1.removePart(record[j - 1], d));
540
+ var allInside = part.reduce(function (a, e) { return a && containCalculator(origin_1, e); }, true);
541
+ if (allInside && origin_1.isClockwise === part.isClockwise) {
542
+ origin_1.insertAfter.apply(origin_1, __spreadArray([record[j - 1] - 1], __read(part.reverse().points), false));
543
+ p = origin_1;
544
+ }
545
+ }
546
+ };
547
+ for (var j = record.length - 1; j > 0; j--) {
548
+ _loop_2(j);
549
+ }
550
+ }
551
+ }
552
+ }
553
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
554
+ finally {
555
+ try {
556
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
557
+ }
558
+ finally { if (e_7) throw e_7.error; }
559
+ }
560
+ return p;
561
+ },
562
+ enumerable: false,
563
+ configurable: true
564
+ });
565
+ Object.defineProperty(DPolygon.prototype, "valid", {
566
+ get: function () {
567
+ return this.length > exports.MIN_POINTS_IN_VALID_POLYGON;
568
+ },
569
+ enumerable: false,
570
+ configurable: true
571
+ });
572
+ Object.defineProperty(DPolygon.prototype, "first", {
573
+ get: function () {
574
+ return this.at(0);
575
+ },
576
+ enumerable: false,
577
+ configurable: true
578
+ });
579
+ Object.defineProperty(DPolygon.prototype, "second", {
580
+ get: function () {
581
+ return this.at(1);
582
+ },
583
+ enumerable: false,
584
+ configurable: true
585
+ });
586
+ Object.defineProperty(DPolygon.prototype, "last", {
587
+ get: function () {
588
+ return this.at(this.length - 1);
589
+ },
590
+ enumerable: false,
591
+ configurable: true
592
+ });
593
+ Object.defineProperty(DPolygon.prototype, "minAreaRectangle", {
594
+ get: function () {
595
+ var e_8, _a, e_9, _b;
596
+ var p = this.convex;
597
+ var resultPolygon = new DPolygon();
598
+ var resultArea = Infinity;
599
+ try {
600
+ for (var _c = __values(p.loopPointsGenerator(true)()), _d = _c.next(); !_d.done; _d = _c.next()) {
601
+ var _e = __read(_d.value, 3), l = _e[2];
602
+ var maxWidth = 0;
603
+ var maxWidthPoint1 = null;
604
+ var maxWidthPoint2 = null;
605
+ var maxHeight = 0;
606
+ var maxHeightPoint = null;
607
+ try {
608
+ for (var _f = (e_9 = void 0, __values(p.loopPointsGenerator()())), _g = _f.next(); !_g.done; _g = _f.next()) {
609
+ var _h = __read(_g.value, 4), z = _h[0], i = _h[3];
610
+ var p1 = l.findPoint(l.findPerpendicular(z));
611
+ var h = p1.distance(z);
612
+ if (h >= maxHeight) {
613
+ maxHeight = h;
614
+ maxHeightPoint = z;
615
+ }
616
+ for (var j = i; j < p.length - 1; j++) {
617
+ var p2 = l.findPoint(l.findPerpendicular(p.at(j)));
618
+ var w = p1.distance(p2);
619
+ if (w >= maxWidth) {
620
+ maxWidth = w;
621
+ maxWidthPoint1 = p1;
622
+ maxWidthPoint2 = p2;
623
+ }
624
+ }
625
+ }
626
+ }
627
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
628
+ finally {
629
+ try {
630
+ if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
631
+ }
632
+ finally { if (e_9) throw e_9.error; }
633
+ }
634
+ if (!maxWidthPoint1 || !maxWidthPoint2 || !maxHeightPoint) {
635
+ continue;
636
+ }
637
+ var widthLine = maxWidthPoint1.findLine(maxWidthPoint2);
638
+ var perpendicular1 = widthLine.findPerpendicular(maxWidthPoint1);
639
+ var perpendicular2 = widthLine.findPerpendicular(maxWidthPoint2);
640
+ var tempPolygon = new DPolygon([
641
+ maxWidthPoint1,
642
+ maxWidthPoint2,
643
+ perpendicular2.findPoint(perpendicular2.findPerpendicular(maxHeightPoint)),
644
+ perpendicular1.findPoint(perpendicular1.findPerpendicular(maxHeightPoint))
645
+ ]).close();
646
+ if (tempPolygon.area < resultArea) {
647
+ resultPolygon = tempPolygon;
648
+ resultArea = tempPolygon.area;
649
+ }
650
+ }
651
+ }
652
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
653
+ finally {
654
+ try {
655
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
656
+ }
657
+ finally { if (e_8) throw e_8.error; }
658
+ }
659
+ return resultPolygon;
660
+ },
661
+ enumerable: false,
662
+ configurable: true
663
+ });
664
+ Object.defineProperty(DPolygon.prototype, "convex", {
665
+ get: function () {
666
+ var p = this.clone().open();
667
+ var isClockwise = p.isClockwise;
668
+ if (!isClockwise) {
669
+ p.reverse();
670
+ }
671
+ var l = 0;
672
+ do {
673
+ var p1 = p.last;
674
+ var p2 = p.first;
675
+ var p3 = p.second;
676
+ var d = p2.findInnerAngle(p1, p3);
677
+ if (d > Math.PI || DNumbers_1.DNumbers.likeZero(DNumbers_1.DNumbers.rad2Deg(d)) || DNumbers_1.DNumbers.likePI(d) || DNumbers_1.DNumbers.like2PI(d)) {
678
+ p.removePart(-1, 1);
679
+ }
680
+ else {
681
+ break;
682
+ }
683
+ } while (p.length);
684
+ p.close();
685
+ var iteration = 0;
686
+ do {
687
+ p = p.deintersection;
688
+ l = p.length;
689
+ for (var i = 1; i < p.length - 1; i++) {
690
+ var p1 = p.at(i - 1);
691
+ var p2 = p.at(i);
692
+ var p3 = p.at(i + 1);
693
+ var d = p2.findInnerAngle(p1, p3);
694
+ if (d > Math.PI || DNumbers_1.DNumbers.likeZero(DNumbers_1.DNumbers.rad2Deg(d)) || DNumbers_1.DNumbers.likePI(d) || DNumbers_1.DNumbers.like2PI(d)) {
695
+ p.removePart(--i, 1);
696
+ }
697
+ }
698
+ iteration++;
699
+ } while (p.length !== l && iteration < MAX_CONVEX_ITERATIONS);
700
+ if (!isClockwise) {
701
+ p.reverse();
702
+ }
703
+ return p;
704
+ },
705
+ enumerable: false,
706
+ configurable: true
707
+ });
708
+ Object.defineProperty(DPolygon.prototype, "isClockwise", {
709
+ get: function () {
710
+ var e_10, _a;
711
+ var sum = 0;
712
+ try {
713
+ for (var _b = __values(this.clone().close()
714
+ .loopPointsGenerator()()), _c = _b.next(); !_c.done; _c = _b.next()) {
715
+ var _d = __read(_c.value, 2), _e = _d[0], x = _e.x, y = _e.y, _f = _d[1], a = _f.x, b = _f.y;
716
+ sum += (a - x) * (b + y);
717
+ }
718
+ }
719
+ catch (e_10_1) { e_10 = { error: e_10_1 }; }
720
+ finally {
721
+ try {
722
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
723
+ }
724
+ finally { if (e_10) throw e_10.error; }
725
+ }
726
+ return sum < 0;
727
+ },
728
+ enumerable: false,
729
+ configurable: true
730
+ });
731
+ Object.defineProperty(DPolygon.prototype, "clockWise", {
732
+ get: function () {
733
+ if (this.isClockwise) {
734
+ return this.clone();
735
+ }
736
+ return this.clone().reverse();
737
+ },
738
+ enumerable: false,
739
+ configurable: true
740
+ });
741
+ Object.defineProperty(DPolygon.prototype, "noHoles", {
742
+ get: function () {
743
+ var res = this.clone();
744
+ res.holes = [];
745
+ return res;
746
+ },
747
+ enumerable: false,
748
+ configurable: true
749
+ });
750
+ DPolygon.prototype.reduce = function (f, v) {
751
+ return this.pPoints.reduce(f, v);
752
+ };
753
+ DPolygon.prototype.intersection = function (l, includeOnly) {
754
+ var e_11, _a;
755
+ if (includeOnly === void 0) { includeOnly = false; }
756
+ var res = [];
757
+ try {
758
+ for (var _b = __values(this.loopPointsGenerator(true)()), _c = _b.next(); !_c.done; _c = _b.next()) {
759
+ var _d = __read(_c.value, 3), line = _d[2];
760
+ var intersect = line.intersection(l, 0, includeOnly);
761
+ if (intersect) {
762
+ res.push(intersect);
763
+ }
764
+ }
765
+ }
766
+ catch (e_11_1) { e_11 = { error: e_11_1 }; }
767
+ finally {
768
+ try {
769
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
770
+ }
771
+ finally { if (e_11) throw e_11.error; }
772
+ }
773
+ return res;
774
+ };
775
+ DPolygon.prototype.setCenter = function (newCenter) {
776
+ return this.loop()
777
+ .move(newCenter.clone().move(this.center.minus()))
778
+ .run();
779
+ };
780
+ DPolygon.prototype.toWKT = function (type, withZ) {
781
+ if (type === void 0) { type = DPolygon.WKT_POLYGON; }
782
+ if (withZ === void 0) { withZ = false; }
783
+ if (type === DPolygon.WKT_POLYGON) {
784
+ var h = '';
785
+ if (this.holes && this.holes.length) {
786
+ h = ", ".concat(this.holes.map(function (hole) { return hole.toString(); })
787
+ .join(', '));
788
+ }
789
+ return "POLYGON ((".concat(this.deintersection.mapArray(function (r) { return "".concat(r.x, " ").concat(r.y).concat(withZ ? " ".concat(r.z) : ''); })
790
+ .join(', '), ")").concat(h, ")");
791
+ }
792
+ return "LINESTRING (".concat(this.mapArray(function (r) { return "".concat(r.x, " ").concat(r.y).concat(withZ ? " ".concat(r.z) : ''); })
793
+ .join(', '), ")");
794
+ };
795
+ DPolygon.prototype.filter = function (f) {
796
+ this.pPoints = this.pPoints.filter(f);
797
+ return this;
798
+ };
799
+ DPolygon.prototype.map = function (f) {
800
+ this.pPoints = this.mapArray(f);
801
+ this.holes = this.holes.map(function (h) { return h.map(f); });
802
+ return this;
803
+ };
804
+ DPolygon.prototype.mapArray = function (f) {
805
+ return this.pPoints.map(f);
806
+ };
807
+ DPolygon.prototype.sort = function (f) {
808
+ this.points.sort(f);
809
+ return this;
810
+ };
811
+ DPolygon.prototype.at = function (index) {
812
+ var length = this.length;
813
+ return this.points[(index % length + length) % length];
814
+ };
815
+ DPolygon.prototype.pop = function () {
816
+ return this.pPoints.pop();
817
+ };
818
+ DPolygon.prototype.push = function () {
819
+ var _a;
820
+ var args = [];
821
+ for (var _i = 0; _i < arguments.length; _i++) {
822
+ args[_i] = arguments[_i];
823
+ }
824
+ return (_a = this.pPoints).push.apply(_a, __spreadArray([], __read(args), false));
825
+ };
826
+ DPolygon.prototype.shift = function () {
827
+ return this.pPoints.shift();
828
+ };
829
+ DPolygon.prototype.unshift = function () {
830
+ var _a;
831
+ var args = [];
832
+ for (var _i = 0; _i < arguments.length; _i++) {
833
+ args[_i] = arguments[_i];
834
+ }
835
+ return (_a = this.pPoints).unshift.apply(_a, __spreadArray([], __read(args), false));
836
+ };
837
+ DPolygon.prototype.reverse = function () {
838
+ this.pPoints = this.pPoints.reverse();
839
+ this.holes = this.holes.map(function (h) { return h.reverse(); });
840
+ return this;
841
+ };
842
+ DPolygon.prototype.getValue = function () {
843
+ return (this.pPoints.map(function (r) { return r.getValue(); }) + this.holes
844
+ .reduce(function (a, h) { return a + h.getValue(); }, ''));
845
+ };
846
+ DPolygon.prototype.toString = function () {
847
+ return "(".concat(this.mapArray(function (r) { return r.toString(); }).join(', '), ")");
848
+ };
849
+ DPolygon.prototype.close = function () {
850
+ var p0 = this.first;
851
+ if (p0 && !this.closed) {
852
+ this.push(p0.clone());
853
+ }
854
+ return this;
855
+ };
856
+ DPolygon.prototype.open = function () {
857
+ var p = this.first;
858
+ if (this.length > 2 && p && this.closed) {
859
+ this.pop();
860
+ }
861
+ return this;
862
+ };
863
+ DPolygon.prototype.add = function (poly) {
864
+ var res = new DPolygon(__spreadArray(__spreadArray([], __read(this.points), false), __read(poly.points), false)).close();
865
+ res.holes = __spreadArray(__spreadArray([], __read(this.holes), false), __read(poly.holes), false).map(function (h) { return h.clone(); });
866
+ return res;
867
+ };
868
+ DPolygon.prototype.has = function (p) {
869
+ return this.pPoints.some(function (q) { return q.equal(p); });
870
+ };
871
+ DPolygon.prototype.clone = function () {
872
+ var res = new DPolygon(this.points.map(function (r) { return r.clone(); }));
873
+ res.holes = this.holes.map(function (h) { return h.clone(); });
874
+ res.properties = __assign({}, this.properties);
875
+ return res;
876
+ };
877
+ DPolygon.prototype.equal = function (p) {
878
+ if (!(p instanceof DPolygon)) {
879
+ return false;
880
+ }
881
+ if (this.length !== p.length || this.holes.length !== p.holes.length) {
882
+ return false;
883
+ }
884
+ return (this.same(p) &&
885
+ this.holes.reduce(function (a, hole) { return a && p.holes.some(function (pHoles) { return pHoles.same(hole); }); }, true));
886
+ };
887
+ DPolygon.prototype.same = function (p) {
888
+ var pClone = p.clone().close();
889
+ var thisAsString = this.clone()
890
+ .close()
891
+ .toString();
892
+ for (var i = 0; i < pClone.length; i++) {
893
+ if (thisAsString === pClone.toString() || thisAsString === pClone.clone().reverse()
894
+ .toString()) {
895
+ return true;
896
+ }
897
+ pClone.nextStart();
898
+ }
899
+ return false;
900
+ };
901
+ DPolygon.prototype.findIndex = function (a) {
902
+ if (a instanceof DPoint_1.DPoint) {
903
+ return this.points.findIndex(function (t) { return t.equal(a); });
904
+ }
905
+ return this.points.findIndex(a);
906
+ };
907
+ DPolygon.prototype.approximation = function (e) {
908
+ if (e === void 0) { e = Math.sqrt(this.perimeter) * APPROXIMATION_VALUE; }
909
+ return new DPolygon(this.clone().douglasPeucker(this.pPoints, e));
910
+ };
911
+ DPolygon.prototype.insertAfter = function (index) {
912
+ var _a;
913
+ var points = [];
914
+ for (var _i = 1; _i < arguments.length; _i++) {
915
+ points[_i - 1] = arguments[_i];
916
+ }
917
+ (_a = this.pPoints).splice.apply(_a, __spreadArray([index + 1, 0], __read(points), false));
918
+ };
919
+ DPolygon.prototype.removePart = function (index, count) {
920
+ return this.pPoints.splice(index + 1, count);
921
+ };
922
+ DPolygon.prototype.hasSimpleIntersection = function (p) {
923
+ var extend1 = this.extend;
924
+ var extend2 = p.extend;
925
+ var extend1points = extend1.points;
926
+ var extend2points = extend2.points;
927
+ var in1 = extend1points.some(function (t) { return extend2.simpleInclude(t); });
928
+ var in2 = extend2points.some(function (t) { return extend1.simpleInclude(t); });
929
+ return in1 || in2;
930
+ };
931
+ DPolygon.prototype.simpleInclude = function (p) {
932
+ return this.simpleIncludeX(p) && this.simpleIncludeY(p);
933
+ };
934
+ DPolygon.prototype.drawPolygonOnCanvas = function (canvas, fillColor, strokeColor, shadowColor, lineWidth, steps) {
935
+ if (lineWidth === void 0) { lineWidth = 1; }
936
+ if (steps === void 0) { steps = this.length - 1; }
937
+ if (this.length > 1) {
938
+ var ctx = canvas.getContext('2d');
939
+ if (fillColor) {
940
+ ctx.fillStyle = fillColor;
941
+ }
942
+ if (strokeColor) {
943
+ ctx.strokeStyle = strokeColor;
944
+ }
945
+ if (lineWidth) {
946
+ ctx.lineWidth = lineWidth;
947
+ }
948
+ if (fillColor || strokeColor) {
949
+ ctx.beginPath();
950
+ }
951
+ this.goByPath(ctx, steps % this.length);
952
+ if (shadowColor) {
953
+ ctx.shadowColor = shadowColor;
954
+ ctx.shadowBlur = 0;
955
+ ctx.shadowOffsetX = 1;
956
+ ctx.shadowOffsetY = 1;
957
+ }
958
+ if (fillColor) {
959
+ ctx.closePath();
960
+ ctx.fill();
961
+ }
962
+ if (strokeColor) {
963
+ ctx.stroke();
964
+ }
965
+ }
966
+ return this;
967
+ };
968
+ DPolygon.prototype.clearPolygonOnCanvas = function (canvas) {
969
+ var ctx = canvas.getContext('2d');
970
+ var old = ctx.globalCompositeOperation;
971
+ ctx.globalCompositeOperation = 'destination-out';
972
+ this.goByPath(ctx);
973
+ ctx.fill();
974
+ ctx.globalCompositeOperation = old;
975
+ };
976
+ DPolygon.prototype.contain = function (p, isBorderInside) {
977
+ var e_12, _a;
978
+ if (isBorderInside === void 0) { isBorderInside = false; }
979
+ var simpleInclude = this.simpleInclude(p);
980
+ if (!simpleInclude) {
981
+ return false;
982
+ }
983
+ var onBorder = this.onBorder(p);
984
+ if (onBorder) {
985
+ return isBorderInside;
986
+ }
987
+ var poly = this.deintersection;
988
+ var totalFi = 0;
989
+ try {
990
+ for (var _b = __values(poly.loopPointsGenerator()()), _c = _b.next(); !_c.done; _c = _b.next()) {
991
+ var _d = __read(_c.value, 2), _e = _d[0], x = _e.x, y = _e.y, _f = _d[1], a = _f.x, b = _f.y;
992
+ var line1 = new DLine_1.DLine(x - p.x, y - p.y, 0);
993
+ var line2 = new DLine_1.DLine(a - p.x, b - p.y, 0);
994
+ var fiDif = line1.findFi(line2);
995
+ if (line1.vectorProduct(line2).c > 0) {
996
+ totalFi += fiDif;
997
+ }
998
+ else {
999
+ totalFi -= fiDif;
1000
+ }
1001
+ }
1002
+ }
1003
+ catch (e_12_1) { e_12 = { error: e_12_1 }; }
1004
+ finally {
1005
+ try {
1006
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1007
+ }
1008
+ finally { if (e_12) throw e_12.error; }
1009
+ }
1010
+ var eps = Math.PI / 10000;
1011
+ var absTotalFi = Math.abs(totalFi);
1012
+ if (absTotalFi < eps) {
1013
+ return false;
1014
+ }
1015
+ else if (Math.abs(2 * Math.PI - absTotalFi) < eps) {
1016
+ return true;
1017
+ }
1018
+ throw new Error('contains2 faild');
1019
+ };
1020
+ DPolygon.prototype.onBorder = function (p) {
1021
+ var e_13, _a;
1022
+ var simpleInclude = this.simpleInclude(p);
1023
+ if (simpleInclude) {
1024
+ var poly = this.deintersection;
1025
+ var hasSamePoint = this.points.some(function (point) { return point.equal(p); });
1026
+ if (hasSamePoint) {
1027
+ return true;
1028
+ }
1029
+ try {
1030
+ for (var _b = __values(poly.loopPointsGenerator(true)()), _c = _b.next(); !_c.done; _c = _b.next()) {
1031
+ var _d = __read(_c.value, 3), polygonLine = _d[2];
1032
+ var onBorder = polygonLine.x(p).equal(p) && polygonLine.inRange(p);
1033
+ if (onBorder) {
1034
+ return true;
1035
+ }
1036
+ }
1037
+ }
1038
+ catch (e_13_1) { e_13 = { error: e_13_1 }; }
1039
+ finally {
1040
+ try {
1041
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1042
+ }
1043
+ finally { if (e_13) throw e_13.error; }
1044
+ }
1045
+ }
1046
+ return false;
1047
+ };
1048
+ DPolygon.prototype.nextStart = function () {
1049
+ this.open();
1050
+ this.push(this.shift());
1051
+ this.close();
1052
+ return this;
1053
+ };
1054
+ DPolygon.prototype.removeDuplicates = function () {
1055
+ for (var i = 0; i < this.length - 1; i++) {
1056
+ var p1 = this.at(i);
1057
+ var p2 = this.at(i + 1);
1058
+ if (p1.equal(p2)) {
1059
+ this.removePart(i, 1);
1060
+ i--;
1061
+ }
1062
+ }
1063
+ return this;
1064
+ };
1065
+ DPolygon.toGeoJSONFeatureCollection = function (polygons, format) {
1066
+ if (format === void 0) { format = utils_1.DGeo.parseFormat; }
1067
+ return {
1068
+ type: 'FeatureCollection',
1069
+ features: polygons.map(function (polygon) { return polygon.toGeoJSONFeature(format); })
1070
+ };
1071
+ };
1072
+ DPolygon.parse = function (a, format) {
1073
+ if (format === void 0) { format = utils_1.DGeo.parseFormat; }
1074
+ if (a.type) {
1075
+ switch (a.type) {
1076
+ case 'FeatureCollection':
1077
+ return a.features.reduce(function (ak, f) {
1078
+ var t = DPolygon.parse(f, format);
1079
+ if (Array.isArray(t)) {
1080
+ ak.push.apply(ak, __spreadArray([], __read(t), false));
1081
+ }
1082
+ else {
1083
+ ak.push(t);
1084
+ }
1085
+ return ak;
1086
+ }, []);
1087
+ case 'Feature': {
1088
+ var t = DPolygon.parse(a.geometry, format);
1089
+ var props_1 = __assign(__assign({}, a.properties), (a.id ? ({
1090
+ id: a.id
1091
+ }) : ({})));
1092
+ if (Array.isArray(t)) {
1093
+ t.forEach(function (record) {
1094
+ record.properties = __assign({}, props_1);
1095
+ });
1096
+ }
1097
+ else {
1098
+ t.properties = __assign({}, props_1);
1099
+ }
1100
+ return t;
1101
+ }
1102
+ case 'LineString':
1103
+ case 'MultiPoint':
1104
+ return new DPolygon(a.coordinates.map(function (c) { return DPoint_1.DPoint.parse(c, format); }));
1105
+ case 'Polygon':
1106
+ return a.coordinates.reduce(function (ak, line, index) {
1107
+ if (index === 0) {
1108
+ ak.points = line.map(function (c) { return DPoint_1.DPoint.parse(c, format); });
1109
+ }
1110
+ else {
1111
+ ak.holes.push(new DPolygon(line.map(function (c) { return DPoint_1.DPoint.parse(c, format); })));
1112
+ }
1113
+ return ak;
1114
+ }, new DPolygon());
1115
+ case 'MultiLineString':
1116
+ return a.coordinates.reduce(function (ak, line) {
1117
+ ak.push(new DPolygon(line.map(function (c) { return DPoint_1.DPoint.parse(c, format); })));
1118
+ return ak;
1119
+ }, []);
1120
+ case 'MultiPolygon':
1121
+ return a.coordinates.reduce(function (ak, coordinates) {
1122
+ ak.push(DPolygon.parse({
1123
+ type: 'Polygon',
1124
+ coordinates: coordinates
1125
+ }, format));
1126
+ return ak;
1127
+ }, []);
1128
+ case 'GeometryCollection':
1129
+ return a.geometries.reduce(function (ak, line) {
1130
+ ak.push(DPolygon.parse(line, format));
1131
+ return ak;
1132
+ }, []);
1133
+ default:
1134
+ }
1135
+ }
1136
+ return new DPolygon(a
1137
+ .map(function (r) { return DPoint_1.DPoint.parse(r, format); }));
1138
+ };
1139
+ DPolygon.prototype.toArrayOfCoords = function (format) {
1140
+ if (format === void 0) { format = utils_1.DGeo.parseFormat; }
1141
+ return this.mapArray(function (r) { return r.toCoords(format); });
1142
+ };
1143
+ DPolygon.prototype.toGeoJSONFeature = function (format) {
1144
+ if (format === void 0) { format = utils_1.DGeo.parseFormat; }
1145
+ return {
1146
+ type: 'Feature',
1147
+ properties: __assign({}, this.properties),
1148
+ geometry: this.toGeoJSON(format)
1149
+ };
1150
+ };
1151
+ DPolygon.prototype.toGeoJSON = function (format) {
1152
+ if (format === void 0) { format = utils_1.DGeo.parseFormat; }
1153
+ if (this.closed) {
1154
+ return {
1155
+ type: 'Polygon',
1156
+ coordinates: __spreadArray([
1157
+ this.toArrayOfCoords(format)
1158
+ ], __read(this.holes.map(function (h) { return h.toArrayOfCoords(format); })), false)
1159
+ };
1160
+ }
1161
+ return {
1162
+ type: 'LineString',
1163
+ coordinates: this.toArrayOfCoords(format)
1164
+ };
1165
+ };
1166
+ DPolygon.prototype.divideToPieces = function (piecesCount, withAltitude) {
1167
+ var e_14, _a;
1168
+ if (withAltitude === void 0) { withAltitude = false; }
1169
+ var fullLength = this.fullLength;
1170
+ var pieceLength = fullLength / piecesCount;
1171
+ var currentPieceLength = pieceLength;
1172
+ var _loop_3 = function (p1, p2, i) {
1173
+ var d = p1.distance(p2);
1174
+ if (d === currentPieceLength) {
1175
+ p2.properties.pieceBorder = true;
1176
+ currentPieceLength = pieceLength;
1177
+ }
1178
+ else if (d - currentPieceLength > 0) {
1179
+ var circle = new DCircle_1.DCircle(p1, currentPieceLength);
1180
+ var line_1 = p1.findLine(p2);
1181
+ var intersectionPoint = line_1.intersectionWithCircle(circle)
1182
+ .filter(function (p) { return line_1.inRange(p, CLOSE_TO_INTERSECTION_DISTANCE); })[0];
1183
+ intersectionPoint.properties.pieceBorder = true;
1184
+ this_1.insertAfter(i, intersectionPoint);
1185
+ if (withAltitude) {
1186
+ var p1z = p1.z;
1187
+ intersectionPoint.z = p1z - (p1z - p2.z) * (p1.distance(intersectionPoint) / d);
1188
+ }
1189
+ currentPieceLength = pieceLength;
1190
+ }
1191
+ else {
1192
+ currentPieceLength -= d;
1193
+ }
1194
+ };
1195
+ var this_1 = this;
1196
+ try {
1197
+ for (var _b = __values(this.loopPointsGenerator()()), _c = _b.next(); !_c.done; _c = _b.next()) {
1198
+ var _d = __read(_c.value, 4), p1 = _d[0], p2 = _d[1], i = _d[3];
1199
+ _loop_3(p1, p2, i);
1200
+ }
1201
+ }
1202
+ catch (e_14_1) { e_14 = { error: e_14_1 }; }
1203
+ finally {
1204
+ try {
1205
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1206
+ }
1207
+ finally { if (e_14) throw e_14.error; }
1208
+ }
1209
+ return this;
1210
+ };
1211
+ DPolygon.prototype.prepareToFastSearch = function () {
1212
+ var e_15, _a;
1213
+ this.searchStore = {};
1214
+ try {
1215
+ for (var _b = __values(this.points), _c = _b.next(); !_c.done; _c = _b.next()) {
1216
+ var _d = _c.value, x = _d.x, y = _d.y, z = _d.z;
1217
+ if (!this.searchStore[x]) {
1218
+ this.searchStore[x] = {};
1219
+ }
1220
+ if (!this.searchStore[x][y]) {
1221
+ this.searchStore[x][y] = {};
1222
+ }
1223
+ this.searchStore[x][y][z || 'undefined'] = true;
1224
+ }
1225
+ }
1226
+ catch (e_15_1) { e_15 = { error: e_15_1 }; }
1227
+ finally {
1228
+ try {
1229
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1230
+ }
1231
+ finally { if (e_15) throw e_15.error; }
1232
+ }
1233
+ };
1234
+ DPolygon.prototype.fastHas = function (_a) {
1235
+ var x = _a.x, y = _a.y, z = _a.z;
1236
+ if (!this.searchStore[x]) {
1237
+ return false;
1238
+ }
1239
+ if (!this.searchStore[x][y]) {
1240
+ return false;
1241
+ }
1242
+ if (!this.searchStore[x][y][z || 'undefined']) {
1243
+ return false;
1244
+ }
1245
+ return this.searchStore[x][y][z || 'undefined'];
1246
+ };
1247
+ Object.defineProperty(DPolygon.prototype, "growingPiecesGenerator", {
1248
+ get: function () {
1249
+ var polygon = this;
1250
+ return function () {
1251
+ var r, _a, _b, p, e_16_1;
1252
+ var e_16, _c;
1253
+ return __generator(this, function (_d) {
1254
+ switch (_d.label) {
1255
+ case 0:
1256
+ r = new DPolygon();
1257
+ _d.label = 1;
1258
+ case 1:
1259
+ _d.trys.push([1, 6, 7, 8]);
1260
+ _a = __values(polygon.pPoints), _b = _a.next();
1261
+ _d.label = 2;
1262
+ case 2:
1263
+ if (!!_b.done) return [3, 5];
1264
+ p = _b.value;
1265
+ r.push(p);
1266
+ if (!p.properties.pieceBorder) return [3, 4];
1267
+ return [4, r.clone()];
1268
+ case 3:
1269
+ _d.sent();
1270
+ _d.label = 4;
1271
+ case 4:
1272
+ _b = _a.next();
1273
+ return [3, 2];
1274
+ case 5: return [3, 8];
1275
+ case 6:
1276
+ e_16_1 = _d.sent();
1277
+ e_16 = { error: e_16_1 };
1278
+ return [3, 8];
1279
+ case 7:
1280
+ try {
1281
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
1282
+ }
1283
+ finally { if (e_16) throw e_16.error; }
1284
+ return [7];
1285
+ case 8: return [2, r.clone()];
1286
+ }
1287
+ });
1288
+ };
1289
+ },
1290
+ enumerable: false,
1291
+ configurable: true
1292
+ });
1293
+ DPolygon.prototype.simpleUnion = function (p) {
1294
+ try {
1295
+ var res = this.simpleLogicFunction(p, true, true);
1296
+ if (res === null) {
1297
+ return null;
1298
+ }
1299
+ if (res instanceof DPolygon) {
1300
+ return res;
1301
+ }
1302
+ return null;
1303
+ }
1304
+ catch (ex) {
1305
+ return null;
1306
+ }
1307
+ };
1308
+ DPolygon.prototype.simpleIntersection = function (p) {
1309
+ return this.simpleLogicFunction(p, false, false);
1310
+ };
1311
+ DPolygon.prototype.simpleDifference = function (p) {
1312
+ return this.simpleLogicFunction(p, true, false);
1313
+ };
1314
+ DPolygon.prototype.smartUnion = function (p) {
1315
+ var e_17, _a;
1316
+ var res = this.clone().simpleUnion(p);
1317
+ if (res) {
1318
+ var allHoles = __spreadArray(__spreadArray(__spreadArray([], __read(this.holes), false), __read(p.holes), false), __read(res.holes), false).map(function (h) { return h.clone(); });
1319
+ var _loop_4 = function (a) {
1320
+ var e_18, _b;
1321
+ var _loop_5 = function (b) {
1322
+ if (a.equal(b)) {
1323
+ return "continue";
1324
+ }
1325
+ var r = a.simpleUnion(b);
1326
+ if (r) {
1327
+ allHoles = allHoles.filter(function (v) { return !v.equal(a) && !v.equal(b); });
1328
+ allHoles.push(r);
1329
+ }
1330
+ };
1331
+ try {
1332
+ for (var allHoles_2 = (e_18 = void 0, __values(allHoles)), allHoles_2_1 = allHoles_2.next(); !allHoles_2_1.done; allHoles_2_1 = allHoles_2.next()) {
1333
+ var b = allHoles_2_1.value;
1334
+ _loop_5(b);
1335
+ }
1336
+ }
1337
+ catch (e_18_1) { e_18 = { error: e_18_1 }; }
1338
+ finally {
1339
+ try {
1340
+ if (allHoles_2_1 && !allHoles_2_1.done && (_b = allHoles_2.return)) _b.call(allHoles_2);
1341
+ }
1342
+ finally { if (e_18) throw e_18.error; }
1343
+ }
1344
+ };
1345
+ try {
1346
+ for (var allHoles_1 = __values(allHoles), allHoles_1_1 = allHoles_1.next(); !allHoles_1_1.done; allHoles_1_1 = allHoles_1.next()) {
1347
+ var a = allHoles_1_1.value;
1348
+ _loop_4(a);
1349
+ }
1350
+ }
1351
+ catch (e_17_1) { e_17 = { error: e_17_1 }; }
1352
+ finally {
1353
+ try {
1354
+ if (allHoles_1_1 && !allHoles_1_1.done && (_a = allHoles_1.return)) _a.call(allHoles_1);
1355
+ }
1356
+ finally { if (e_17) throw e_17.error; }
1357
+ }
1358
+ res.holes = allHoles;
1359
+ }
1360
+ return res;
1361
+ };
1362
+ DPolygon.prototype.toTriangles = function () {
1363
+ var q = this.clone().removeDuplicates()
1364
+ .open();
1365
+ if (q.length < 3) {
1366
+ return [];
1367
+ }
1368
+ if (q.length === 3) {
1369
+ return [q];
1370
+ }
1371
+ var innerAndNotIntersect = function (poly, p1, p2) {
1372
+ var l = p1.findLine(p2);
1373
+ var center = l.center;
1374
+ var intersections = poly.holes.reduce(function (a, hole) { return a && Boolean(hole.clone().close()
1375
+ .intersection(l, true).length); }, Boolean(poly.clone().close()
1376
+ .intersection(l, true).length));
1377
+ var contain = poly.holes.reduce(function (a, hole) { return a && !hole
1378
+ .contain(center); }, poly.contain(center));
1379
+ return !intersections && contain;
1380
+ };
1381
+ var getTriangle = function (poly) {
1382
+ for (var i = 0; i < poly.length; i++) {
1383
+ var p0 = poly.at(0);
1384
+ var p1 = poly.at(1);
1385
+ var p2 = poly.at(2);
1386
+ if (innerAndNotIntersect(poly, p0, p2)) {
1387
+ poly.removePart(0, 1);
1388
+ return new DPolygon([
1389
+ p0.clone(),
1390
+ p1.clone(),
1391
+ p2.clone()
1392
+ ]);
1393
+ }
1394
+ poly.push(poly.shift());
1395
+ }
1396
+ return undefined;
1397
+ };
1398
+ var p = this.clone().removeDuplicates().clockWise.open();
1399
+ while (p.holes.length) {
1400
+ var h = p.holes.shift()
1401
+ .clone()
1402
+ .clockWise
1403
+ .reverse()
1404
+ .close();
1405
+ for (var i = 0; i < p.length; i++) {
1406
+ if (innerAndNotIntersect(p, p.first, h.first)) {
1407
+ p.insertAfter.apply(p, __spreadArray(__spreadArray([0], __read(h.points), false), [p.first], false));
1408
+ break;
1409
+ }
1410
+ p.push(p.shift());
1411
+ }
1412
+ }
1413
+ var res = [];
1414
+ while (p.length > 3) {
1415
+ var triangle = getTriangle(p);
1416
+ if (triangle) {
1417
+ res.push(triangle);
1418
+ }
1419
+ }
1420
+ res.push(p);
1421
+ return res;
1422
+ };
1423
+ DPolygon.prototype.getTrianglesPointIndexes = function () {
1424
+ var innerAndNotIntersect = function (poly, p1, p2) {
1425
+ var l = p1.findLine(p2);
1426
+ var center = l.center;
1427
+ var intersections = poly.holes.reduce(function (a, hole) { return a && Boolean(hole.clone().close()
1428
+ .intersection(l, true).length); }, Boolean(poly.clone().close()
1429
+ .intersection(l, true).length));
1430
+ var contain = poly.holes.reduce(function (a, hole) { return a && !hole
1431
+ .contain(center); }, poly.contain(center));
1432
+ return !intersections && contain;
1433
+ };
1434
+ var getTriangle = function (poly) {
1435
+ for (var i = 0; i < poly.length; i++) {
1436
+ var p0 = poly.at(0);
1437
+ var p1 = poly.at(1);
1438
+ var p2 = poly.at(2);
1439
+ if (innerAndNotIntersect(poly, p0, p2)) {
1440
+ poly.removePart(0, 1);
1441
+ return [
1442
+ p0.properties.index,
1443
+ p1.properties.index,
1444
+ p2.properties.index
1445
+ ];
1446
+ }
1447
+ poly.push(poly.shift());
1448
+ }
1449
+ return undefined;
1450
+ };
1451
+ var p = this.clone();
1452
+ var index = 0;
1453
+ p.points.forEach(function (f) {
1454
+ f.properties.index = index++;
1455
+ });
1456
+ p.holes.forEach(function (h) {
1457
+ h.pPoints.forEach(function (f) {
1458
+ f.properties.index = index++;
1459
+ });
1460
+ });
1461
+ p = p.clockWise.open();
1462
+ while (p.holes.length) {
1463
+ var h = p.holes.shift()
1464
+ .clone()
1465
+ .clockWise
1466
+ .reverse()
1467
+ .close();
1468
+ for (var i = 0; i < p.length; i++) {
1469
+ if (innerAndNotIntersect(p, p.first, h.first)) {
1470
+ p.insertAfter.apply(p, __spreadArray(__spreadArray([0], __read(h.points), false), [p.first], false));
1471
+ break;
1472
+ }
1473
+ p.push(p.shift());
1474
+ }
1475
+ }
1476
+ var res = [];
1477
+ while (p.length > 3) {
1478
+ var triangle = getTriangle(p);
1479
+ if (triangle) {
1480
+ res.push.apply(res, __spreadArray([], __read(triangle), false));
1481
+ }
1482
+ }
1483
+ res.push.apply(res, __spreadArray([], __read(p.points.map(function (f) { return f.properties.index; })), false));
1484
+ return res;
1485
+ };
1486
+ Object.defineProperty(DPolygon.prototype, "closed", {
1487
+ get: function () {
1488
+ return this.first.equal(this.last);
1489
+ },
1490
+ enumerable: false,
1491
+ configurable: true
1492
+ });
1493
+ DPolygon.prototype.buffer = function (v, quadrantSegments, type) {
1494
+ if (quadrantSegments === void 0) { quadrantSegments = 64; }
1495
+ if (type === void 0) { type = DPolygon.CAP_ROUND; }
1496
+ var reader = new jsts_1.io.WKTReader();
1497
+ var _a = this, noHoles = _a.noHoles, closed = _a.closed;
1498
+ var points = reader
1499
+ .read(noHoles.toWKT(closed ? DPolygon.WKT_POLYGON : DPolygon.WKT_LINESTRING))
1500
+ .buffer(v, quadrantSegments, type)
1501
+ .getCoordinates();
1502
+ return new DPolygon(points.map(function (_a) {
1503
+ var x = _a.x, y = _a.y;
1504
+ return new DPoint_1.DPoint(x, y);
1505
+ }));
1506
+ };
1507
+ DPolygon.prototype.sideBuffers = function (v, quadrantSegments) {
1508
+ if (quadrantSegments === void 0) { quadrantSegments = 64; }
1509
+ var _a = this, first = _a.first, last = _a.last;
1510
+ var buffer = this.buffer(v, quadrantSegments, DPolygon.CAP_FLAT).open();
1511
+ var _b = __read(first.sortByDistance(buffer).points.map(function (r) { return r.properties.index; }), 2), start0 = _b[0], start1 = _b[1];
1512
+ var _c = __read(last.sortByDistance(buffer).points.map(function (r) { return r.properties.index; }), 2), end0 = _c[0], end1 = _c[1];
1513
+ var fromPoint = Math.min(Math.max(start0, start1), Math.max(end0, end1));
1514
+ var toPoint = Math.max(Math.min(start0, start1), Math.min(end0, end1));
1515
+ var linePart = new DPolygon(buffer.removePart(fromPoint - 1, toPoint - fromPoint + 1));
1516
+ buffer.unshift(buffer.pop());
1517
+ return [linePart.reverse(), buffer];
1518
+ };
1519
+ DPolygon.prototype.setProperties = function (v) {
1520
+ this.properties = typeof v === 'object' ? v : v(this);
1521
+ return this;
1522
+ };
1523
+ DPolygon.prototype.bezier = function (step) {
1524
+ if (step === void 0) { step = 0.1; }
1525
+ var res = new DPolygon();
1526
+ for (var i = 0; i < 1; i += step) {
1527
+ res.push(this.clone().getBezierPoint(i));
1528
+ }
1529
+ return res;
1530
+ };
1531
+ DPolygon.prototype.setGrowingHeight = function (from, to) {
1532
+ var fullLength = this.fullLength;
1533
+ var prevPoint = this.first;
1534
+ var d = to - from;
1535
+ var currentDistance = 0;
1536
+ this.loop()
1537
+ .setZ(function (p) {
1538
+ currentDistance += prevPoint.distance(p);
1539
+ prevPoint = p;
1540
+ return from + currentDistance / fullLength * d;
1541
+ })
1542
+ .run();
1543
+ return this;
1544
+ };
1545
+ DPolygon.prototype.loopPointsGenerator = function (withLine) {
1546
+ if (withLine === void 0) { withLine = false; }
1547
+ var that = this;
1548
+ return function () {
1549
+ var i, p1, p2;
1550
+ return __generator(this, function (_a) {
1551
+ switch (_a.label) {
1552
+ case 0:
1553
+ i = 0;
1554
+ _a.label = 1;
1555
+ case 1:
1556
+ if (!(i < that.length - 1)) return [3, 4];
1557
+ p1 = that.at(i);
1558
+ p2 = that.at(i + 1);
1559
+ return [4, [p1, p2, withLine ? p1.findLine(p2) : undefined, i]];
1560
+ case 2:
1561
+ _a.sent();
1562
+ _a.label = 3;
1563
+ case 3:
1564
+ i++;
1565
+ return [3, 1];
1566
+ case 4: return [2];
1567
+ }
1568
+ });
1569
+ };
1570
+ };
1571
+ DPolygon.prototype.getBezierPoint = function (v) {
1572
+ var e_19, _a;
1573
+ if (this.length === 1) {
1574
+ return this.first;
1575
+ }
1576
+ try {
1577
+ for (var _b = __values(this.loopPointsGenerator()()), _c = _b.next(); !_c.done; _c = _b.next()) {
1578
+ var _d = __read(_c.value, 2), p1 = _d[0], p2 = _d[1];
1579
+ p1.move(p2.clone().move(p1.clone().minus())
1580
+ .scale(v));
1581
+ }
1582
+ }
1583
+ catch (e_19_1) { e_19 = { error: e_19_1 }; }
1584
+ finally {
1585
+ try {
1586
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1587
+ }
1588
+ finally { if (e_19) throw e_19.error; }
1589
+ }
1590
+ this.pop();
1591
+ return this.getBezierPoint(v);
1592
+ };
1593
+ DPolygon.prototype.simpleIncludeX = function (p) {
1594
+ var x = p.x;
1595
+ return this.minX <= x && this.maxX >= x;
1596
+ };
1597
+ DPolygon.prototype.simpleIncludeY = function (p) {
1598
+ var y = p.y;
1599
+ return this.minY <= y && this.maxY >= y;
1600
+ };
1601
+ DPolygon.prototype.douglasPeucker = function (points, e) {
1602
+ var dMax = 0;
1603
+ var index = 0;
1604
+ var end = points.length - 1;
1605
+ var line = points[0].findLine(points[end]);
1606
+ for (var i = 1; i < end; i++) {
1607
+ var d = line.perpendicularDistance(points[i]);
1608
+ if (d > dMax) {
1609
+ index = i;
1610
+ dMax = d;
1611
+ }
1612
+ }
1613
+ if (dMax >= e) {
1614
+ var recResult1 = this.douglasPeucker(points.slice(0, index + 1), e);
1615
+ var recResult2 = this.douglasPeucker(points.slice(index), e);
1616
+ recResult1.pop();
1617
+ return __spreadArray(__spreadArray([], __read(recResult1), false), __read(recResult2), false);
1618
+ }
1619
+ return [points[0], points[end]];
1620
+ };
1621
+ DPolygon.prototype.goByPath = function (ctx, steps) {
1622
+ if (steps === void 0) { steps = this.length - 1; }
1623
+ var start = this.first;
1624
+ ctx.moveTo(start.x, start.y);
1625
+ for (var i = 1; i <= (steps % this.length); i++) {
1626
+ var _a = this.at(i), x = _a.x, y = _a.y;
1627
+ ctx.lineTo(x, y);
1628
+ }
1629
+ };
1630
+ DPolygon.prototype.getJSTSGeometry = function (p, unionThis, unionThat) {
1631
+ var unionOrIntersection = unionThat === unionThis;
1632
+ var reader = new jsts_1.io.WKTReader();
1633
+ var a = reader.read(this.noHoles.toWKT());
1634
+ var b = reader.read(p.noHoles.toWKT());
1635
+ if (!unionOrIntersection) {
1636
+ return a.difference(b);
1637
+ }
1638
+ else if (unionThis) {
1639
+ return a.union(b);
1640
+ }
1641
+ else if (!unionThis) {
1642
+ return a.intersection(b);
1643
+ }
1644
+ return undefined;
1645
+ };
1646
+ DPolygon.prototype.simpleLogicFunction = function (p, unionThis, unionThat) {
1647
+ var e_20, _a, e_21, _b, e_22, _c, e_23, _d;
1648
+ var c = this.getJSTSGeometry(p, unionThis, unionThat);
1649
+ if (c) {
1650
+ var coordinates_1 = c.getCoordinates();
1651
+ if (coordinates_1.length) {
1652
+ var result = coordinates_1.reduce(function (ak, _a, index) {
1653
+ var x = _a.x, y = _a.y;
1654
+ var lastIndex = ak.length - 1;
1655
+ var t = new DPoint_1.DPoint(x, y);
1656
+ var first = ak[lastIndex].first;
1657
+ if (t.equal(first)) {
1658
+ if (coordinates_1[index + 1]) {
1659
+ var nextPoint = new DPoint_1.DPoint(coordinates_1[index + 1].x, coordinates_1[index + 1].y);
1660
+ if (ak[lastIndex].length > 1) {
1661
+ ak.push(new DPolygon([nextPoint]));
1662
+ }
1663
+ }
1664
+ }
1665
+ else {
1666
+ ak[lastIndex].push(t);
1667
+ }
1668
+ return ak;
1669
+ }, [new DPolygon([new DPoint_1.DPoint(coordinates_1[0].x, coordinates_1[0].y)])]);
1670
+ if (unionThat && unionThis && result.length > 1) {
1671
+ try {
1672
+ for (var result_1 = __values(result), result_1_1 = result_1.next(); !result_1_1.done; result_1_1 = result_1.next()) {
1673
+ var q = result_1_1.value;
1674
+ var _loop_6 = function (r) {
1675
+ var _e;
1676
+ if (q.has(r.first) && !q.equal(r)) {
1677
+ var index = q.findIndex(r.first);
1678
+ (_e = q.points).splice.apply(_e, __spreadArray([index, 0], __read(r.points), false));
1679
+ result = result.filter(function (h) { return !h.equal(r); });
1680
+ return "continue";
1681
+ }
1682
+ if (result.length < 2) {
1683
+ return "break";
1684
+ }
1685
+ };
1686
+ try {
1687
+ for (var result_2 = (e_21 = void 0, __values(result)), result_2_1 = result_2.next(); !result_2_1.done; result_2_1 = result_2.next()) {
1688
+ var r = result_2_1.value;
1689
+ var state_1 = _loop_6(r);
1690
+ if (state_1 === "break")
1691
+ break;
1692
+ }
1693
+ }
1694
+ catch (e_21_1) { e_21 = { error: e_21_1 }; }
1695
+ finally {
1696
+ try {
1697
+ if (result_2_1 && !result_2_1.done && (_b = result_2.return)) _b.call(result_2);
1698
+ }
1699
+ finally { if (e_21) throw e_21.error; }
1700
+ }
1701
+ if (result.length < 2) {
1702
+ break;
1703
+ }
1704
+ }
1705
+ }
1706
+ catch (e_20_1) { e_20 = { error: e_20_1 }; }
1707
+ finally {
1708
+ try {
1709
+ if (result_1_1 && !result_1_1.done && (_a = result_1.return)) _a.call(result_1);
1710
+ }
1711
+ finally { if (e_20) throw e_20.error; }
1712
+ }
1713
+ }
1714
+ result = result.filter(function (h) { return h.length > 2; }).map(function (h) { return h.close(); });
1715
+ try {
1716
+ for (var result_3 = __values(result), result_3_1 = result_3.next(); !result_3_1.done; result_3_1 = result_3.next()) {
1717
+ var q = result_3_1.value;
1718
+ var _loop_7 = function (r) {
1719
+ if (result.length < 2) {
1720
+ return "break";
1721
+ }
1722
+ if (!q.equal(r)) {
1723
+ if (q.contain(r.first, true)) {
1724
+ q.holes.push(r);
1725
+ result = result.filter(function (h) { return !h.equal(r); });
1726
+ }
1727
+ }
1728
+ };
1729
+ try {
1730
+ for (var result_4 = (e_23 = void 0, __values(result)), result_4_1 = result_4.next(); !result_4_1.done; result_4_1 = result_4.next()) {
1731
+ var r = result_4_1.value;
1732
+ var state_2 = _loop_7(r);
1733
+ if (state_2 === "break")
1734
+ break;
1735
+ }
1736
+ }
1737
+ catch (e_23_1) { e_23 = { error: e_23_1 }; }
1738
+ finally {
1739
+ try {
1740
+ if (result_4_1 && !result_4_1.done && (_d = result_4.return)) _d.call(result_4);
1741
+ }
1742
+ finally { if (e_23) throw e_23.error; }
1743
+ }
1744
+ if (result.length < 2) {
1745
+ break;
1746
+ }
1747
+ }
1748
+ }
1749
+ catch (e_22_1) { e_22 = { error: e_22_1 }; }
1750
+ finally {
1751
+ try {
1752
+ if (result_3_1 && !result_3_1.done && (_c = result_3.return)) _c.call(result_3);
1753
+ }
1754
+ finally { if (e_22) throw e_22.error; }
1755
+ }
1756
+ if (result.length === 0) {
1757
+ return null;
1758
+ }
1759
+ if (result.length === 1) {
1760
+ return result[0].close();
1761
+ }
1762
+ return result.map(function (g) { return g.close(); });
1763
+ }
1764
+ }
1765
+ return null;
1766
+ };
1767
+ DPolygon.CAP_ROUND = CAP_ROUND;
1768
+ DPolygon.CAP_FLAT = CAP_FLAT;
1769
+ DPolygon.CAP_SQUARE = CAP_SQUARE;
1770
+ DPolygon.WKT_LINESTRING = 'LINESTRING';
1771
+ DPolygon.WKT_POLYGON = 'POLYGON';
1772
+ return DPolygon;
1773
+ }());
1774
+ exports.DPolygon = DPolygon;