@dra2020/baseclient 1.0.43 → 1.0.46

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.
@@ -3,5 +3,6 @@ interface PolyLabelResult {
3
3
  y: number;
4
4
  d: number;
5
5
  }
6
+ export declare function polyDistance(poly: any, x: number, y: number): number;
6
7
  export declare function polyLabel(poly: any, precision?: number, debug?: boolean): PolyLabelResult;
7
8
  export {};
@@ -11,6 +11,28 @@ interface PolyLabelResult
11
11
  d: number;
12
12
  }
13
13
 
14
+ //
15
+ // polyDistance: return (smallest) distance to polygon edge.
16
+ // Positive value indicates point is in poly, negative indicates point is outside.
17
+ //
18
+
19
+ export function polyDistance(poly: any, x: number, y: number): number
20
+ {
21
+ let pp = P.polyNormalize(poly);
22
+ if (pp == null) return 0;
23
+
24
+ let forEachPointPair = (iter: any) => {
25
+ PP.polyPackEachRing(pp, (b: Float64Array, iPoly, iRing: number, iOffset: number, nPoints: number) => {
26
+ let iFirst = iOffset;
27
+ let iLast = iOffset + nPoints * 2;
28
+ let iSecond = iLast - 2;
29
+ for (; iFirst < iLast; iSecond = iFirst, iFirst += 2)
30
+ iter(iPoly, iRing, b[iFirst], b[iFirst+1], b[iSecond], b[iSecond+1]);
31
+ });
32
+ };
33
+ return pointToPolygonDist(x, y, forEachPointPair);
34
+ }
35
+
14
36
  //
15
37
  // polyLabel: given polygon, return contained point furthest from any edge, and that distance
16
38
  //
@@ -47,7 +69,7 @@ export function polyLabel(poly: any, precision?: number, debug?: boolean): PolyL
47
69
  let iLast = iOffset + nPoints * 2;
48
70
  let iSecond = iLast - 2;
49
71
  for (; iFirst < iLast; iSecond = iFirst, iFirst += 2)
50
- iter(b[iFirst], b[iFirst+1], b[iSecond], b[iSecond+1]);
72
+ iter(iPoly, iRing, b[iFirst], b[iFirst+1], b[iSecond], b[iSecond+1]);
51
73
  });
52
74
  };
53
75
 
@@ -142,15 +164,38 @@ class Cell
142
164
  // signed distance from point to polygon outline (negative if point is outside)
143
165
  function pointToPolygonDist(x: number, y: number, forEachPointPair: any): number
144
166
  {
145
- let inside: boolean = false;
167
+ let iPolyLast = -1;
168
+ let iRingLast = -1;
169
+ let inside = false;
170
+ let thisInside = false;
171
+ let useInside = false;
172
+ let isHole = false;
146
173
  let minDistSq: number = Infinity;
147
174
 
148
- forEachPointPair((ax: number, ay: number, bx: number, by: number) => {
175
+ forEachPointPair((iPoly: number, iRing: number, ax: number, ay: number, bx: number, by: number) => {
176
+ if (iPoly != iPolyLast || iRing != iRingLast)
177
+ {
178
+ if (useInside)
179
+ inside = isHole ? ! thisInside : thisInside;
180
+ iPolyLast = iPoly;
181
+ iRingLast = iRing;
182
+ thisInside = false;
183
+ useInside = false;
184
+ isHole = false;
185
+ }
149
186
  if ((ay > y !== by > y) && (x < (bx - ax) * (y - ay) / (by - ay) + ax))
150
- inside = !inside;
151
-
152
- minDistSq = Math.min(minDistSq, getSegDistSq(x, y, ax, ay, bx, by));
187
+ thisInside = !thisInside;
188
+
189
+ let thisDistSq = getSegDistSq(x, y, ax, ay, bx, by);
190
+ if (thisDistSq < minDistSq)
191
+ {
192
+ minDistSq = thisDistSq;
193
+ useInside = true;
194
+ isHole = iRing != 0;
195
+ }
153
196
  });
197
+ if (useInside)
198
+ inside = isHole ? ! thisInside : thisInside;
154
199
 
155
200
  return (inside ? 1 : -1) * Math.sqrt(minDistSq);
156
201
  }
@@ -164,7 +209,7 @@ function getCentroidCell(forEachPointPair: any): Cell
164
209
  let fx: number;
165
210
  let fy: number;
166
211
 
167
- forEachPointPair((ax: number, ay: number, bx: number, by: number) => {
212
+ forEachPointPair((iPoly: number, iRing: number, ax: number, ay: number, bx: number, by: number) => {
168
213
  if (fx === undefined) fx = ax, fy = ay;
169
214
  let f: number = ax * by - bx * ay;
170
215
  x += (ax + bx) * f;
package/lib/poly/topo.ts CHANGED
@@ -184,7 +184,7 @@ function misMatchObject(o1: any, o2: any): boolean
184
184
  return (o1.type === 'MultiPolygon') ? misMatchMulti(o1.arcs, o2.arcs) : misMatchPoly(o1.arcs, o2.arcs);
185
185
  }
186
186
 
187
- const MAX_TRIES = 25;
187
+ const MAX_TRIES = 30;
188
188
 
189
189
  function bigTimeString(ms: number): string
190
190
  {
@@ -323,10 +323,9 @@ export function topoSimplifyCollection(col: any, options?: SimplifyOptions): any
323
323
  let {x,y} = intpt(f);
324
324
  if (x && y && !polyContainsPoint(pp, x, y))
325
325
  {
326
- keepTiny.set(f, f);
327
- keepArcs(topo, oOld.arcs, 0); // keeps all points to avoid reprocessing these tiny shapes
328
- nBad++, nTiny++;
329
- log(`topoSimplifyCollection: ${f.properties.id}: preserving full feature because of intpt problem`);
326
+ keepArcs(topo, oOld.arcs, keepweight);
327
+ nBad++;
328
+ log(`topoSimplifyCollection: ${f.properties.id}: increasing feature fidelity because of intpt problem`);
330
329
  }
331
330
  }
332
331
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dra2020/baseclient",
3
- "version": "1.0.43",
3
+ "version": "1.0.46",
4
4
  "description": "Utility functions for Javascript projects.",
5
5
  "main": "dist/baseclient.js",
6
6
  "types": "./dist/all/all.d.ts",