@dra2020/baseclient 1.0.164 → 1.0.166

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.
@@ -10,6 +10,7 @@ export * from './blend';
10
10
  export * from './cartesian';
11
11
  export * from './minbound';
12
12
  export * from './polyround';
13
+ export * from './polyconvert';
13
14
  export * from './topo';
14
15
  export * from './selfintersect';
15
16
  export * from './shamos';
@@ -0,0 +1,10 @@
1
+ export type PointConverter = (pt: [number, number]) => [number, number];
2
+ export declare function polyConvert(o: any, cvt: PointConverter): any;
3
+ export declare function polyFrom32614(o: any): any;
4
+ export declare function colValidateCRS(o: any): any;
5
+ /**
6
+ * Convert UTM Zone 14N (EPSG:32614) coordinates to WGS84 lon/lat.
7
+ * Input: [easting, northing] in meters
8
+ * Output: [longitude, latitude] in degrees
9
+ */
10
+ export declare function utm14ToLonLat([E, N]: [number, number]): [number, number];
@@ -34,13 +34,12 @@ export class OTClientEngine extends OTE.OTEngine
34
34
  this.clientID = cid;
35
35
  this.initialize();
36
36
  this.bReadOnly = false;
37
- this.valCache = {};
38
37
  }
39
38
 
40
39
  initialize(): void
41
40
  {
42
41
  if (this.prefailCache === undefined && this.clientSequenceNo > 0)
43
- this.prefailCache = this.valCache;
42
+ this.prefailCache = this.toValue();
44
43
  this.clientSequenceNo = 0;
45
44
  this.isNeedAck = false;
46
45
  this.isNeedResend = false;
@@ -71,11 +70,15 @@ export class OTClientEngine extends OTE.OTEngine
71
70
 
72
71
  toPartialValue(resourceName: string): any
73
72
  {
73
+ if (! this.valCache)
74
+ this.valCache = {};
74
75
  return this.valCache[resourceName];
75
76
  }
76
77
 
77
78
  toValue(): any
78
79
  {
80
+ if (! this.valCache)
81
+ this.valCache = this.stateLocal.toValue();
79
82
  return this.valCache;
80
83
  }
81
84
 
@@ -155,7 +158,7 @@ export class OTClientEngine extends OTE.OTEngine
155
158
  {
156
159
  console.log('otclientengine: failbackToInitialState');
157
160
  if (this.prefailCache === undefined)
158
- this.prefailCache = this.valCache;
161
+ this.prefailCache = this.toValue();
159
162
  this.initialize();
160
163
  }
161
164
 
@@ -164,7 +167,7 @@ export class OTClientEngine extends OTE.OTEngine
164
167
  {
165
168
  console.log('otclientengine: failbackToServerState');
166
169
  if (this.prefailCache === undefined)
167
- this.prefailCache = this.valCache;
170
+ this.prefailCache = this.toValue();
168
171
  this.stateLocal = this.stateServer.copy();
169
172
  this.isNeedAck = false;
170
173
  this.actionSentClient.empty();
@@ -172,7 +175,7 @@ export class OTClientEngine extends OTE.OTEngine
172
175
  this.actionServerInterposedSentClient.empty();
173
176
  this.actionAllPendingClient.empty();
174
177
  this.actionAllClient.empty();
175
- this.valCache = this.stateLocal.toValue();
178
+ delete this.valCache;
176
179
  this.emit('state');
177
180
  }
178
181
 
@@ -284,7 +287,7 @@ export class OTClientEngine extends OTE.OTEngine
284
287
  this.actionServerInterposedSentClient.compose(orig);
285
288
 
286
289
  // Let clients know
287
- this.valCache = this.stateLocal.toValue();
290
+ delete this.valCache;
288
291
  this.emit('state');
289
292
  }
290
293
  }
@@ -318,7 +321,7 @@ export class OTClientEngine extends OTE.OTEngine
318
321
  this.actionAllClient.compose(orig);
319
322
  this.actionAllPendingClient.compose(orig);
320
323
  this.stateLocal.compose(orig);
321
- this.valCache = this.stateLocal.toValue();
324
+ delete this.valCache;
322
325
  this.emit('state');
323
326
  }
324
327
  catch (err)
package/lib/poly/all.ts CHANGED
@@ -10,6 +10,7 @@ export * from './blend';
10
10
  export * from './cartesian';
11
11
  export * from './minbound';
12
12
  export * from './polyround';
13
+ export * from './polyconvert';
13
14
  export * from './topo';
14
15
  export * from './selfintersect';
15
16
  export * from './shamos';
@@ -0,0 +1,128 @@
1
+ import * as Util from '../util/all';
2
+ import * as PP from './polypack';
3
+
4
+ export type PointConverter = (pt: [number, number]) => [number, number]
5
+
6
+ // Convert geometry given point conversion function. Takes any form of feature, collection or coordinate array(s)
7
+ export function polyConvert(o: any, cvt: PointConverter): any
8
+ {
9
+ if (o)
10
+ {
11
+ if (Array.isArray(o))
12
+ {
13
+ if (o.length && typeof o[0] === 'number')
14
+ {
15
+ for (let i = 0; i < o.length; i += 2)
16
+ {
17
+ const p = cvt([o[i], o[i+1]]);
18
+ o[i] = p[0];
19
+ o[i+1] = p[1];
20
+ }
21
+ }
22
+ else
23
+ o.forEach((e: any) => { polyConvert(e, cvt) });
24
+ }
25
+ else if (o.features !== undefined && Array.isArray(o.features))
26
+ o.features.forEach((f: any) => { polyConvert(f, cvt) });
27
+ else if (o.geometry !== undefined && o.geometry.coordinates !== undefined)
28
+ polyConvert(o.geometry.coordinates, cvt);
29
+ else if (o.geometry !== undefined && o.geometry.packed !== undefined)
30
+ PP.polyPackEachPoint(o.geometry.packed, (b: Float64Array, iPoly: number, iRing: number, iOffset: number) => {
31
+ const p = cvt([b[iOffset], b[iOffset+1]]);
32
+ b[iOffset] = p[0];
33
+ b[iOffset+1] = p[1];
34
+ });
35
+ }
36
+ return o;
37
+ }
38
+
39
+ export function polyFrom32614(o: any): any
40
+ {
41
+ return polyConvert(o, utm14ToLonLat);
42
+ }
43
+
44
+ // If coordinates in 32614 format, convert to WGS84 (GeoJSON standard format)
45
+ //
46
+ export function colValidateCRS(o: any): any
47
+ {
48
+ // "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::32614" } },
49
+
50
+ if (o?.crs?.properties?.name === 'urn:ogc:def:crs:EPSG::32614')
51
+ {
52
+ delete o.crs;
53
+ return polyFrom32614(o);
54
+ }
55
+ return o
56
+ }
57
+
58
+ /**
59
+ * Convert UTM Zone 14N (EPSG:32614) coordinates to WGS84 lon/lat.
60
+ * Input: [easting, northing] in meters
61
+ * Output: [longitude, latitude] in degrees
62
+ */
63
+ export function utm14ToLonLat([E, N]: [number, number]): [number, number] {
64
+ // WGS84 parameters
65
+ const a = 6378137.0;
66
+ const f = 1 / 298.257223563;
67
+ const k0 = 0.9996;
68
+
69
+ const e = Math.sqrt(f * (2 - f)); // eccentricity
70
+ const e1sq = e * e / (1 - e * e);
71
+
72
+ // UTM zone parameters
73
+ const zone = 14;
74
+ const lonOrigin = (zone - 1) * 6 - 180 + 3; // central meridian
75
+ const falseEasting = 500000;
76
+
77
+ // Remove false northing for northern hemisphere (EPSG:326xx always north)
78
+ const x = E - falseEasting;
79
+ const y = N;
80
+
81
+ // Footpoint latitude
82
+ const M = y / k0;
83
+ const mu = M / (a * (1 - e*e/4 - 3*e**4/64 - 5*e**6/256));
84
+
85
+ const e1 = (1 - Math.sqrt(1 - e*e)) / (1 + Math.sqrt(1 - e*e));
86
+
87
+ const J1 = (3 * e1 / 2 - 27 * e1**3 / 32);
88
+ const J2 = (21 * e1**2 / 16 - 55 * e1**4 / 32);
89
+ const J3 = (151 * e1**3 / 96);
90
+ const J4 = (1097 * e1**4 / 512);
91
+
92
+ const fp = mu
93
+ + J1 * Math.sin(2 * mu)
94
+ + J2 * Math.sin(4 * mu)
95
+ + J3 * Math.sin(6 * mu)
96
+ + J4 * Math.sin(8 * mu);
97
+
98
+ // Precompute trig functions
99
+ const sinfp = Math.sin(fp);
100
+ const cosfp = Math.cos(fp);
101
+ const tanfp = Math.tan(fp);
102
+
103
+ const C1 = e1sq * cosfp**2;
104
+ const T1 = tanfp**2;
105
+ const R1 = a * (1 - e*e) / Math.pow(1 - e*e * sinfp**2, 3/2);
106
+ const N1 = a / Math.sqrt(1 - e*e * sinfp**2);
107
+ const D = x / (N1 * k0);
108
+
109
+ // Latitude (φ)
110
+ const lat =
111
+ fp
112
+ - (N1 * tanfp / R1) *
113
+ (D**2 / 2
114
+ - (5 + 3*T1 + 10*C1 - 4*C1**2 - 9*e1sq) * D**4 / 24
115
+ + (61 + 90*T1 + 298*C1 + 45*T1**2 - 252*e1sq - 3*C1**2) * D**6 / 720);
116
+
117
+ // Longitude (λ)
118
+ const lon =
119
+ (D
120
+ - (1 + 2*T1 + C1) * D**3 / 6
121
+ + (5 - 2*C1 + 28*T1 - 3*C1**2 + 8*e1sq + 24*T1**2) * D**5 / 120) / cosfp;
122
+
123
+ // Convert to degrees and apply central meridian offset
124
+ const latitude = lat * (180 / Math.PI);
125
+ const longitude = lonOrigin + lon * (180 / Math.PI);
126
+
127
+ return [longitude, latitude];
128
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dra2020/baseclient",
3
- "version": "1.0.164",
3
+ "version": "1.0.166",
4
4
  "description": "Utility functions for Javascript projects.",
5
5
  "main": "dist/baseclient.js",
6
6
  "types": "./dist/all/all.d.ts",