@dra2020/baseclient 1.0.13 → 1.0.14

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 (185) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +37 -0
  3. package/dist/all/all.d.ts +36 -0
  4. package/dist/all/allclient.d.ts +18 -0
  5. package/dist/base.js +33010 -0
  6. package/dist/base.js.map +1 -0
  7. package/dist/baseclient.js +8991 -0
  8. package/dist/baseclient.js.map +1 -0
  9. package/dist/context/all.d.ts +1 -0
  10. package/dist/context/context.d.ts +13 -0
  11. package/dist/dbabstract/all.d.ts +1 -0
  12. package/dist/dbabstract/db.d.ts +83 -0
  13. package/dist/dbdynamo/all.d.ts +1 -0
  14. package/dist/dbdynamo/dbdynamo.d.ts +190 -0
  15. package/dist/filterexpr/all.d.ts +1 -0
  16. package/dist/filterexpr/filterexpr.d.ts +64 -0
  17. package/dist/fsm/all.d.ts +1 -0
  18. package/dist/fsm/fsm.d.ts +118 -0
  19. package/dist/fsmfile/all.d.ts +1 -0
  20. package/dist/fsmfile/fsmfile.d.ts +47 -0
  21. package/dist/jsonstream/all.d.ts +1 -0
  22. package/dist/jsonstream/jsonstream.d.ts +130 -0
  23. package/dist/lambda/all.d.ts +1 -0
  24. package/dist/lambda/env.d.ts +10 -0
  25. package/dist/lambda/lambda.d.ts +18 -0
  26. package/dist/logabstract/all.d.ts +1 -0
  27. package/dist/logabstract/log.d.ts +26 -0
  28. package/dist/logclient/all.d.ts +1 -0
  29. package/dist/logclient/log.d.ts +6 -0
  30. package/dist/logserver/all.d.ts +5 -0
  31. package/dist/logserver/log.d.ts +11 -0
  32. package/dist/logserver/logaccum.d.ts +154 -0
  33. package/dist/logserver/logblob.d.ts +24 -0
  34. package/dist/logserver/logconcat.d.ts +55 -0
  35. package/dist/logserver/logkey.d.ts +28 -0
  36. package/dist/memsqs/all.d.ts +4 -0
  37. package/dist/memsqs/client.d.ts +13 -0
  38. package/dist/memsqs/loopback.d.ts +11 -0
  39. package/dist/memsqs/orderedlist.d.ts +19 -0
  40. package/dist/memsqs/queue.d.ts +84 -0
  41. package/dist/memsqs/server.d.ts +37 -0
  42. package/dist/ot-editutil/all.d.ts +2 -0
  43. package/dist/ot-editutil/oteditutil.d.ts +14 -0
  44. package/dist/ot-editutil/otmaputil.d.ts +21 -0
  45. package/dist/ot-js/all.d.ts +9 -0
  46. package/dist/ot-js/otarray.d.ts +111 -0
  47. package/dist/ot-js/otclientengine.d.ts +38 -0
  48. package/dist/ot-js/otcomposite.d.ts +37 -0
  49. package/dist/ot-js/otcounter.d.ts +17 -0
  50. package/dist/ot-js/otengine.d.ts +22 -0
  51. package/dist/ot-js/otmap.d.ts +19 -0
  52. package/dist/ot-js/otserverengine.d.ts +38 -0
  53. package/dist/ot-js/otsession.d.ts +111 -0
  54. package/dist/ot-js/ottypes.d.ts +29 -0
  55. package/dist/poly/all.d.ts +15 -0
  56. package/dist/poly/blend.d.ts +1 -0
  57. package/dist/poly/boundbox.d.ts +16 -0
  58. package/dist/poly/cartesian.d.ts +5 -0
  59. package/dist/poly/graham-scan.d.ts +8 -0
  60. package/dist/poly/hash.d.ts +1 -0
  61. package/dist/poly/matrix.d.ts +24 -0
  62. package/dist/poly/minbound.d.ts +1 -0
  63. package/dist/poly/poly.d.ts +52 -0
  64. package/dist/poly/polybin.d.ts +5 -0
  65. package/dist/poly/polylabel.d.ts +7 -0
  66. package/dist/poly/polypack.d.ts +30 -0
  67. package/dist/poly/polyround.d.ts +1 -0
  68. package/dist/poly/polysimplify.d.ts +1 -0
  69. package/dist/poly/quad.d.ts +48 -0
  70. package/dist/poly/selfintersect.d.ts +1 -0
  71. package/dist/poly/shamos.d.ts +1 -0
  72. package/dist/poly/simplify.d.ts +2 -0
  73. package/dist/poly/topo.d.ts +46 -0
  74. package/dist/poly/union.d.ts +48 -0
  75. package/dist/storage/all.d.ts +4 -0
  76. package/dist/storage/datablob.d.ts +9 -0
  77. package/dist/storage/env.d.ts +10 -0
  78. package/dist/storage/splitsblob.d.ts +13 -0
  79. package/dist/storage/storage.d.ts +166 -0
  80. package/dist/storages3/all.d.ts +1 -0
  81. package/dist/storages3/s3.d.ts +62 -0
  82. package/dist/util/all.d.ts +5 -0
  83. package/dist/util/bintrie.d.ts +93 -0
  84. package/dist/util/countedhash.d.ts +19 -0
  85. package/dist/util/gradient.d.ts +15 -0
  86. package/dist/util/indexedarray.d.ts +15 -0
  87. package/dist/util/util.d.ts +68 -0
  88. package/docs/context.md +2 -0
  89. package/docs/dbabstract.md +2 -0
  90. package/docs/dbdynamo.md +2 -0
  91. package/docs/fsm.md +243 -0
  92. package/docs/fsmfile.md +2 -0
  93. package/docs/jsonstream.md +44 -0
  94. package/docs/lambda.md +2 -0
  95. package/docs/logabstract.md +2 -0
  96. package/docs/logclient.md +2 -0
  97. package/docs/logserver.md +2 -0
  98. package/docs/ot-editutil.md +2 -0
  99. package/docs/ot-js.md +95 -0
  100. package/docs/poly.md +103 -0
  101. package/docs/storage.md +2 -0
  102. package/docs/storages3.md +2 -0
  103. package/docs/util.md +2 -0
  104. package/lib/all/all.ts +41 -0
  105. package/lib/all/allclient.ts +19 -0
  106. package/lib/context/all.ts +1 -0
  107. package/lib/context/context.ts +82 -0
  108. package/lib/dbabstract/all.ts +1 -0
  109. package/lib/dbabstract/db.ts +246 -0
  110. package/lib/dbdynamo/all.ts +1 -0
  111. package/lib/dbdynamo/dbdynamo.ts +1551 -0
  112. package/lib/filterexpr/all.ts +1 -0
  113. package/lib/filterexpr/filterexpr.ts +625 -0
  114. package/lib/fsm/all.ts +1 -0
  115. package/lib/fsm/fsm.ts +549 -0
  116. package/lib/fsmfile/all.ts +1 -0
  117. package/lib/fsmfile/fsmfile.ts +236 -0
  118. package/lib/jsonstream/all.ts +1 -0
  119. package/lib/jsonstream/jsonstream.ts +940 -0
  120. package/lib/lambda/all.ts +1 -0
  121. package/lib/lambda/env.ts +13 -0
  122. package/lib/lambda/lambda.ts +120 -0
  123. package/lib/logabstract/all.ts +1 -0
  124. package/lib/logabstract/log.ts +55 -0
  125. package/lib/logclient/all.ts +1 -0
  126. package/lib/logclient/log.ts +105 -0
  127. package/lib/logserver/all.ts +5 -0
  128. package/lib/logserver/log.ts +565 -0
  129. package/lib/logserver/logaccum.ts +1445 -0
  130. package/lib/logserver/logblob.ts +84 -0
  131. package/lib/logserver/logconcat.ts +313 -0
  132. package/lib/logserver/logkey.ts +125 -0
  133. package/lib/memsqs/all.ts +4 -0
  134. package/lib/memsqs/client.ts +268 -0
  135. package/lib/memsqs/loopback.ts +64 -0
  136. package/lib/memsqs/orderedlist.ts +74 -0
  137. package/lib/memsqs/queue.ts +395 -0
  138. package/lib/memsqs/server.ts +262 -0
  139. package/lib/ot-editutil/all.ts +2 -0
  140. package/lib/ot-editutil/oteditutil.ts +180 -0
  141. package/lib/ot-editutil/otmaputil.ts +209 -0
  142. package/lib/ot-js/all.ts +9 -0
  143. package/lib/ot-js/otarray.ts +1168 -0
  144. package/lib/ot-js/otclientengine.ts +327 -0
  145. package/lib/ot-js/otcomposite.ts +247 -0
  146. package/lib/ot-js/otcounter.ts +145 -0
  147. package/lib/ot-js/otengine.ts +71 -0
  148. package/lib/ot-js/otmap.ts +144 -0
  149. package/lib/ot-js/otserverengine.ts +329 -0
  150. package/lib/ot-js/otsession.ts +199 -0
  151. package/lib/ot-js/ottypes.ts +98 -0
  152. package/lib/poly/all.ts +15 -0
  153. package/lib/poly/blend.ts +27 -0
  154. package/lib/poly/boundbox.ts +102 -0
  155. package/lib/poly/cartesian.ts +130 -0
  156. package/lib/poly/graham-scan.ts +401 -0
  157. package/lib/poly/hash.ts +15 -0
  158. package/lib/poly/matrix.ts +309 -0
  159. package/lib/poly/minbound.ts +211 -0
  160. package/lib/poly/poly.ts +767 -0
  161. package/lib/poly/polybin.ts +218 -0
  162. package/lib/poly/polylabel.ts +204 -0
  163. package/lib/poly/polypack.ts +458 -0
  164. package/lib/poly/polyround.ts +30 -0
  165. package/lib/poly/polysimplify.ts +24 -0
  166. package/lib/poly/quad.ts +272 -0
  167. package/lib/poly/selfintersect.ts +87 -0
  168. package/lib/poly/shamos.ts +297 -0
  169. package/lib/poly/simplify.ts +119 -0
  170. package/lib/poly/topo.ts +525 -0
  171. package/lib/poly/union.ts +371 -0
  172. package/lib/storage/all.ts +4 -0
  173. package/lib/storage/datablob.ts +36 -0
  174. package/lib/storage/env.ts +14 -0
  175. package/lib/storage/splitsblob.ts +63 -0
  176. package/lib/storage/storage.ts +604 -0
  177. package/lib/storages3/all.ts +1 -0
  178. package/lib/storages3/s3.ts +576 -0
  179. package/lib/util/all.ts +5 -0
  180. package/lib/util/bintrie.ts +603 -0
  181. package/lib/util/countedhash.ts +83 -0
  182. package/lib/util/gradient.ts +108 -0
  183. package/lib/util/indexedarray.ts +80 -0
  184. package/lib/util/util.ts +695 -0
  185. package/package.json +8 -8
@@ -0,0 +1,458 @@
1
+ // Shared libraries
2
+ import * as Util from '../util/all';
3
+
4
+ // Format for efficient packing of multipolygon coordinates
5
+ //
6
+ // [n] number of polygons
7
+ // [n polygon headers]...
8
+ // [n] number of rings
9
+ // [n]... number of coordinates in ring
10
+ // followed by [x,y]... for all the coordinates
11
+
12
+ export interface PolyPack
13
+ {
14
+ offset: number;
15
+ length: number;
16
+ buffer: any;
17
+ }
18
+
19
+ export function polyPackSize(coords: any): number
20
+ {
21
+ // Null feature?
22
+ if (coords == null || (coords.length !== undefined && coords.length == 0)) return 0;
23
+
24
+ let depth = Util.depthof(coords);
25
+ if (depth == 3) coords = [[ coords ]];
26
+ else if (depth == 4) coords = [ coords ]; // normalize to multipolygon
27
+ let nFloats: number = 2; // for number of polygons and size of header block
28
+ let i: number, j: number;
29
+ let p: any, r: any;
30
+ for (i = 0; i < coords.length; i++)
31
+ {
32
+ p = coords[i];
33
+ nFloats++; // For number of rings in this polygon
34
+ for (j = 0; j < p.length; j++)
35
+ {
36
+ nFloats++; // For number of coordinates in this ring
37
+ r = p[j];
38
+ nFloats += r.length * 2; // for each x, y pair
39
+ }
40
+ }
41
+ return nFloats;
42
+ }
43
+
44
+ type EachRingCB = (buffer: Float64Array, iPoly: number, iRing: number, iOffset: number, nPoints: number) => void;
45
+ type EachPointCB = (buffer: Float64Array, iPoly: number, iRing: number, iOffset: number) => void;
46
+
47
+ export function polyPackEachRing(pack: PolyPack, cb: EachRingCB): void
48
+ {
49
+ if (pack == null || pack.buffer == null) return;
50
+
51
+ if (pack.offset === undefined) throw 'oops: not a PolyPack';
52
+
53
+ let b = pack.buffer;
54
+ let iRing: number;
55
+ let iOffset: number = pack.offset;
56
+ let cOffset: number = b[iOffset+1] + pack.offset;
57
+
58
+ // Grab number of polygons
59
+ let nPoly: number = b[iOffset++];
60
+ // Move past header length
61
+ iOffset++;
62
+ for (let iPoly: number = 0; iPoly < nPoly; iPoly++)
63
+ {
64
+ // Grab number of rings in this polygon
65
+ let nRing: number = b[iOffset++];
66
+ for (let iRing: number = 0; iRing < nRing; iRing++)
67
+ {
68
+ // Grab number of points in this ring
69
+ let nPoints: number = b[iOffset++];
70
+ cb(b, iPoly, iRing, cOffset, nPoints);
71
+ cOffset += nPoints * 2;
72
+ }
73
+ }
74
+ }
75
+
76
+ export function polyPackEachPoint(pack: PolyPack, cb: EachPointCB): void
77
+ {
78
+ polyPackEachRing(pack, (b: Float64Array, iPoly: number, iRing: number, iOffset: number, nPoints: number) => {
79
+ let iEnd: number = iOffset + (nPoints * 2);
80
+ for (; iOffset < iEnd; iOffset += 2)
81
+ cb(b, iPoly, iRing, iOffset);
82
+ });
83
+ }
84
+
85
+ export function polyPackCountPoints(pack: PolyPack): number
86
+ {
87
+ let n = 0;
88
+ polyPackEachRing(pack, (b: Float64Array, iPoly: number, iRing: number, iOffset: number, nPoints: number) => { n += nPoints });
89
+ return n;
90
+ }
91
+
92
+ export function polyPacked(f: any): boolean
93
+ {
94
+ if (f == null) return false;
95
+ if (f.offset !== undefined) return true;
96
+ if (f.geometry !== undefined) return f.geometry.packed !== undefined;
97
+ if (f.type === 'FeatureCollection' && f.features !== undefined && f.features.length > 0)
98
+ return polyPacked(f.features[0]);
99
+ return false;
100
+ }
101
+
102
+ // Convert to multi-polygon format if necessary
103
+ export function normalizeArcs(arcs: any): any
104
+ {
105
+ let d = Util.depthof(arcs);
106
+ while (d < 4) { arcs = [arcs], d++ } // note that depth is n-1 compared to coords because leaf is just number, not [x,y]
107
+ return arcs;
108
+ }
109
+
110
+ export function countArcPolygons(arcs: any): number
111
+ {
112
+ arcs = normalizeArcs(arcs);
113
+ return arcs.length;
114
+ }
115
+
116
+ export function countArcRings(arcs: any, iPoly?: number): number
117
+ {
118
+ arcs = normalizeArcs(arcs);
119
+ let r = 0;
120
+ arcs.forEach((a: any, ip: number) => { if (iPoly === undefined || iPoly === ip) r += a.length })
121
+ return r;
122
+ }
123
+
124
+ export function countOneArcPoints(topo: any, ring: any): number
125
+ {
126
+ // for each ring
127
+ let n = 0;
128
+ ring.forEach((i: number, index: number) => {
129
+ // for each arc
130
+ let a = topo.arcs[i < 0 ? ~i : i];
131
+ if (index > 0) n--;
132
+ n += a.length;
133
+ });
134
+ if (n < 4) n = 4;
135
+ return n;
136
+ }
137
+
138
+ export function countArcPoints(topo: any, arcs: any, iPoly?: number): number
139
+ {
140
+ arcs = normalizeArcs(arcs);
141
+ let npoints = 0;
142
+ arcs.forEach((polygon: any, ip: number) => {
143
+ if (iPoly === undefined || iPoly === ip)
144
+ polygon.forEach((ring: any) => { npoints += countOneArcPoints(topo, ring) })
145
+ });
146
+ return npoints;
147
+ }
148
+
149
+ // This packs the TopoJSON arc format into a PolyPack. Basics:
150
+ // 1) Negative indices indicate arc should be traversed backward.
151
+ // 2) First and last point of sequential arcs are duplicated, so only include one instance. Easiest way
152
+ // of implementing this (to match topojson implementation) is first delete last point.
153
+ // 3) If total number of points < 4, duplicate the first point until you get 4 to meet GeoJSON standard.
154
+ // 4) Can also request to pack just a single polygon of a multi-polygon
155
+ //
156
+ export function polyPackTopoArcs(topo: any, arcs: any, iPoly?: number): PolyPack
157
+ {
158
+ if (topo == null || arcs == null || arcs.length == 0) return null;
159
+ arcs = normalizeArcs(arcs);
160
+ let nPoints = countArcPoints(topo, arcs, iPoly);
161
+ if (nPoints == 0) return null;
162
+ let nFloats = 2 + (2 * countArcRings(arcs, iPoly)) + (2 * nPoints);
163
+ let ab = new ArrayBuffer(nFloats * 8);
164
+ let prepack: PolyPack = { offset: 0, length: nFloats, buffer: new Float64Array(ab) };
165
+ let af = prepack.buffer as Float64Array;
166
+ let z = 0;
167
+ af[z++] = iPoly === undefined ? countArcPolygons(arcs) : 1;
168
+ z++; // spot for header size
169
+
170
+ // Fill in header - number of rings in each polygon and number of points in each ring
171
+ arcs.forEach((polygon: any, ip: number) => {
172
+ if (iPoly === undefined || iPoly === ip)
173
+ {
174
+ af[z++] = polygon.length;
175
+ polygon.forEach((ring: any) => af[z++] = countOneArcPoints(topo, ring));
176
+ }
177
+ });
178
+
179
+ // go back and fill in header size
180
+ af[1] = z;
181
+
182
+ // now copy in the points
183
+ arcs.forEach((polygon: any, ip: number) => {
184
+ if (iPoly === undefined || iPoly === ip)
185
+ polygon.forEach((ring: any) => {
186
+ let b = z;
187
+ ring.forEach((i: number, index: number) => {
188
+ let a = topo.arcs[i < 0 ? ~i : i];
189
+ if (index > 0) z -= 2;
190
+ let n = a.length;
191
+ let j;
192
+ if (i < 0)
193
+ for (j = n-1; j >= 0; j--) { let pt: any = a[j]; af[z++] = pt[0], af[z++] = pt[1] }
194
+ else
195
+ for (j = 0; j < n; j++) { let pt: any = a[j]; af[z++] = pt[0], af[z++] = pt[1] }
196
+ });
197
+ // need at least 4 points in each ring
198
+ if (z != b)
199
+ while (z < b+8) { af[z++] = af[b], af[z++] = af[b+1] }
200
+ });
201
+ });
202
+
203
+ return prepack;
204
+ }
205
+
206
+ export function polyPack(coords: any, prepack?: PolyPack): PolyPack
207
+ {
208
+ // Null feature?
209
+ if (coords == null || (coords.length !== undefined && coords.length == 0)) return null;
210
+
211
+ // Actually already a PolyPack?
212
+ if (coords.offset !== undefined) return coords as PolyPack;
213
+
214
+ // Packed feature?
215
+ if (coords && coords.geometry && coords.geometry.packed !== undefined)
216
+ return coords.geometry.packed as PolyPack;
217
+
218
+ // Unpacked feature, grab the coordinates array
219
+ if (coords && coords.geometry)
220
+ coords = coords.geometry.coordinates;
221
+
222
+ // Transparently handle polygon or multi-polygon
223
+ let depth = Util.depthof(coords);
224
+ if (depth == 3) coords = [ [ coords ] ];
225
+ else if (depth == 4) coords = [ coords ];
226
+
227
+ let nFloats = polyPackSize(coords);
228
+ let i: number, j: number, k: number;
229
+ let p: any, r: any;
230
+
231
+ // Allocate typed buffer if not passed in
232
+ if (prepack === undefined)
233
+ {
234
+ let ab = new ArrayBuffer(nFloats * 8);
235
+ prepack = { offset: 0, length: nFloats, buffer: new Float64Array(ab) };
236
+ }
237
+ else
238
+ {
239
+ prepack = Util.shallowCopy(prepack);
240
+ prepack.length = nFloats;
241
+ }
242
+
243
+ let af: Float64Array = prepack.buffer as Float64Array;
244
+
245
+ // Fill in header
246
+ let z: number = prepack.offset;
247
+ af[z++] = coords.length;
248
+ z++; // spot for header size
249
+ for (i = 0; i < coords.length; i++)
250
+ {
251
+ p = coords[i];
252
+ af[z++] = p.length;
253
+ for (j = 0; j < p.length; j++)
254
+ af[z++] = p[j].length;
255
+ }
256
+
257
+ // set header size back in header
258
+ af[prepack.offset+1] = z - prepack.offset;
259
+
260
+ // Fill in coords
261
+ for (i = 0; i < coords.length; i++)
262
+ {
263
+ p = coords[i];
264
+ for (j = 0; j < p.length; j++)
265
+ {
266
+ r = p[j];
267
+ for (k = 0; k < r.length; k++)
268
+ {
269
+ af[z++] = r[k][0];
270
+ af[z++] = r[k][1];
271
+ }
272
+ }
273
+ }
274
+
275
+ return prepack;
276
+ }
277
+
278
+ export function polyPackCopy(coords: any): PolyPack
279
+ {
280
+ let pp = polyPack(coords);
281
+
282
+ if (pp == null) return null;
283
+
284
+ let copy: PolyPack = { offset: 0, length: pp.length, buffer: null };
285
+ let ab = new ArrayBuffer(pp.length * 8);
286
+ let afDst = new Float64Array(ab);
287
+ copy.buffer = afDst;
288
+ let afSrc = pp.buffer as Float64Array;
289
+ for (let i = 0; i < pp.length; i++)
290
+ afDst[i] = afSrc[i+pp.offset];
291
+ return copy;
292
+ }
293
+
294
+ export function polyUnpack(prepack: any): any
295
+ {
296
+ // Check if not packed
297
+ if (prepack == null) return [];
298
+ if (prepack.offset === undefined) return prepack;
299
+
300
+ let i: number, j: number, k: number;
301
+ let nPolys: number, nRings: number, nCoords: number;
302
+ let p: any, r: any;
303
+ let af: Float64Array = prepack.buffer as Float64Array;
304
+ let coords: any = [];
305
+ let h: number = prepack.offset as number;
306
+ nPolys = af[h++];
307
+ let z = af[h++] + prepack.offset; // start of coordinates
308
+ for (i = 0; i < nPolys; i++)
309
+ {
310
+ p = [];
311
+ coords[i] = p;
312
+ nRings = af[h++];
313
+ for (j = 0; j < nRings; j++)
314
+ {
315
+ r = [];
316
+ p[j] = r;
317
+ nCoords = af[h++];
318
+ for (k = 0; k < nCoords; k++)
319
+ r[k] = [ af[z++], af[z++] ];
320
+ }
321
+ }
322
+
323
+ return coords.length > 1 ? coords : coords[0];
324
+ }
325
+
326
+ export function featurePackSize(f: any): number
327
+ {
328
+ if (f && f.geometry && f.geometry.coordinates)
329
+ return polyPackSize(f.geometry.coordinates);
330
+ return 0;
331
+ }
332
+
333
+ export function featurePack(f: any, prepack?: PolyPack): any
334
+ {
335
+ if (f && f.type === 'Feature')
336
+ {
337
+ if (f.geometry && f.geometry.coordinates)
338
+ {
339
+ f.geometry.packed = polyPack(f.geometry.coordinates, prepack);
340
+ delete f.geometry.coordinates;
341
+ }
342
+ else if (f.geometry === undefined)
343
+ {
344
+ f.geometry = {}
345
+ f.geometry.packed = prepack ? { offset: prepack.offset, length: 0, buffer: prepack.buffer } : { offset: 0, length: 0, buffer: null };
346
+ }
347
+ return f.geometry.packed;
348
+ }
349
+ else if (f.type && f.type === 'FeatureCollection' && f.features)
350
+ {
351
+ // Allocate one large buffer
352
+ let nFloats: number = 0;
353
+ let i: number;
354
+ for (i = 0; i < f.features.length; i++)
355
+ nFloats += featurePackSize(f.features[i]);
356
+ let ab = new ArrayBuffer(nFloats * 8);
357
+ prepack = { offset: 0, length: nFloats, buffer: new Float64Array(ab) };
358
+
359
+ // Now pack
360
+ for (i = 0; i < f.features.length; i++)
361
+ {
362
+ let postpack = featurePack(f.features[i], prepack);
363
+ prepack.offset += postpack ? postpack.length : 0;
364
+ }
365
+ if (prepack.offset != nFloats)
366
+ throw 'oops, packing bug';
367
+ return prepack;
368
+ }
369
+ return null;
370
+ }
371
+
372
+ export function featureUnpack(f: any): any
373
+ {
374
+ if (f && f.geometry && f.geometry.packed !== undefined)
375
+ {
376
+ f.geometry.coordinates = polyUnpack(f.geometry.packed);
377
+ // Check for oops, optimized away the multipolygon in polyUnpack
378
+ if (f.geometry.type === 'MultiPolygon' && Util.depthof(f.geometry.coordinates) === 4)
379
+ f.geometry.coordinates = [ f.geometry.coordinates ];
380
+ delete f.geometry.packed;
381
+ }
382
+ else if (f.type && f.type === 'FeatureCollection' && f.features)
383
+ {
384
+ for (let i: number = 0; i < f.features.length; i++)
385
+ featureUnpack(f.features[i]);
386
+ }
387
+ return f;
388
+ }
389
+
390
+ export type SavePack = WeakMap<any,any>;
391
+
392
+ export function featureUnpackTemporarily(f: any, save?: SavePack): SavePack
393
+ {
394
+ if (save === undefined) save = new WeakMap<any,any>();
395
+
396
+ if (f && f.type === 'Feature')
397
+ {
398
+ if (f.geometry && f.geometry.packed)
399
+ {
400
+ save.set(f, f.geometry.packed);
401
+ featureUnpack(f);
402
+ }
403
+ }
404
+ else if (f.type && f.type === 'FeatureCollection' && f.features)
405
+ {
406
+ f.features.forEach((ff: any) => {
407
+ if (ff.geometry && ff.geometry.packed)
408
+ save.set(ff, ff.geometry.packed);
409
+ });
410
+ featureUnpack(f);
411
+ }
412
+ return save;
413
+ }
414
+
415
+ export function featureRepack(f: any, save: SavePack): any
416
+ {
417
+ if (f && f.type === 'Feature')
418
+ {
419
+ if (f.geometry)
420
+ {
421
+ let packed = save.get(f);
422
+ if (packed)
423
+ {
424
+ f.geometry.packed = packed;
425
+ delete f.geometry.coordinates;
426
+ }
427
+ }
428
+ }
429
+ else if (f.type && f.type === 'FeatureCollection' && f.features)
430
+ {
431
+ f.features.forEach((ff: any) => featureRepack(ff, save));
432
+ }
433
+ return f;
434
+ }
435
+
436
+ export function featurePackString(f: any): string
437
+ {
438
+ let packed = f.offset ? f : (f.geometry.packed ? f.geometry.packed : polyPack(f.geometry.coordinates));
439
+ let af = packed.buffer as Float64Array;
440
+ let a: string[] = [];
441
+ for (let i: number = 0; i < packed.length; i++)
442
+ a.push(String(af[i]));
443
+ return a.join(' ');
444
+ }
445
+
446
+ export function featureUnpackString(s: string): any
447
+ {
448
+ let a = s.split(' ');
449
+ let ab = new ArrayBuffer(a.length * 8);
450
+ let af = new Float64Array(ab);
451
+ let i: number = 0;
452
+ a.forEach((n: string) => { af[i++] = Number(n) });
453
+ let f: any = { type: 'Feature', geometry: {type: 'Polygon', packed: { offset: 0, length: a.length, buffer: af } } };
454
+ f = featureUnpack(f);
455
+ if (Util.depthof(f.geometry.coordinates) === 5)
456
+ f.geometry.type = 'MultiPolygon';
457
+ return f;
458
+ }
@@ -0,0 +1,30 @@
1
+ import * as Util from '../util/all';
2
+ import * as PP from './polypack';
3
+
4
+ // Round feature geometry to 6 decimal precision
5
+ export function polyRound(o: any): any
6
+ {
7
+ if (o)
8
+ {
9
+ if (Array.isArray(o))
10
+ {
11
+ if (o.length && typeof o[0] === 'number')
12
+ {
13
+ for (let i = 0; i < o.length; i++)
14
+ o[i] = Util.precisionRound(o[i], 6);
15
+ }
16
+ else
17
+ o.forEach((e: any) => { polyRound(e) });
18
+ }
19
+ else if (o.features !== undefined && Array.isArray(o.features))
20
+ o.features.forEach((f: any) => { polyRound(f) });
21
+ else if (o.geometry !== undefined && o.geometry.coordinates !== undefined)
22
+ polyRound(o.geometry.coordinates);
23
+ else if (o.geometry !== undefined && o.geometry.packed !== undefined)
24
+ PP.polyPackEachPoint(o.geometry.packed, (b: Float64Array, iPoly: number, iRing: number, iOffset: number) => {
25
+ b[iOffset] = Util.precisionRound(b[iOffset], 6);
26
+ b[iOffset+1] = Util.precisionRound(b[iOffset+1], 6);
27
+ });
28
+ }
29
+ return o;
30
+ }
@@ -0,0 +1,24 @@
1
+ import * as Util from '../util/all';
2
+
3
+ import { simplify } from './simplify';
4
+
5
+ export function polySimplify(poly: any, tolerance: number = 0.0001): void
6
+ {
7
+ if (poly == null) return;
8
+ if (poly.geometry && poly.geometry.coordinates) poly = poly.geometry.coordinates;
9
+ let depth = Util.depthof(poly);
10
+ if (depth == 4)
11
+ poly = [ poly ];
12
+ else if (depth != 5)
13
+ return;
14
+
15
+ // Poly is multi-polygon, array of polygons
16
+ for (let i: number = 0; i < poly.length; i++)
17
+ {
18
+ // p is polygon, array of rings, each ring is array of points which is what gets fed to underlying simplify routine.
19
+ let p = poly[i];
20
+
21
+ for (let j: number = 0; j < p.length; j++)
22
+ p[j] = simplify(p[j], tolerance, true);
23
+ }
24
+ }