@dra2020/baseclient 1.0.29 → 1.0.32

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.
package/dist/geo/geo.d.ts CHANGED
@@ -4,6 +4,12 @@ export declare type GeoProperties = geojson.GeoJsonProperties;
4
4
  export declare type GeoFeature = geojson.Feature;
5
5
  export declare type GeoFeatureArray = GeoFeature[];
6
6
  export declare type GeoFeatureCollection = geojson.FeatureCollection;
7
+ export declare type GeoCentroidMap = {
8
+ [geoid: string]: {
9
+ x: number;
10
+ y: number;
11
+ };
12
+ };
7
13
  export declare function geoEnsureID(col: GeoFeatureCollection): void;
8
14
  export declare function geoCollectionToMap(col: GeoFeatureCollection): GeoFeatureMap;
9
15
  export declare function geoMapToCollection(map: GeoFeatureMap): GeoFeatureCollection;
@@ -1,2 +1,3 @@
1
1
  import * as G from '../geo/all';
2
+ export declare function polyMapToByCentroid(districts: G.GeoFeatureCollection, centroids: G.GeoCentroidMap): any;
2
3
  export declare function polyMapTo(districts: G.GeoFeatureCollection, blocks: G.GeoFeatureCollection): any;
package/lib/geo/geo.ts CHANGED
@@ -6,6 +6,7 @@ export type GeoProperties = geojson.GeoJsonProperties;
6
6
  export type GeoFeature = geojson.Feature;
7
7
  export type GeoFeatureArray = GeoFeature[];
8
8
  export type GeoFeatureCollection = geojson.FeatureCollection;
9
+ export type GeoCentroidMap = { [geoid: string]: { x: number, y: number } };
9
10
 
10
11
  export function geoEnsureID(col: GeoFeatureCollection): void
11
12
  {
@@ -19,6 +20,11 @@ export function geoEnsureID(col: GeoFeatureCollection): void
19
20
  props.forEach(p => { if (prop === undefined && f.properties[p] !== undefined) prop = p; });
20
21
  if (prop)
21
22
  col.features.forEach(f => { f.properties.id = f.properties[prop] });
23
+ else
24
+ {
25
+ let n = 1;
26
+ col.features.forEach(f => { f.properties.id = String(n++) });
27
+ }
22
28
  }
23
29
  }
24
30
 
package/lib/poly/mapto.ts CHANGED
@@ -33,21 +33,17 @@ function setLabels(c: G.GeoFeatureCollection): void
33
33
  //
34
34
  // The return value is an object that maps the block feature ids to the district feature id.
35
35
  //
36
-
37
- export function polyMapTo(districts: G.GeoFeatureCollection, blocks: G.GeoFeatureCollection): any
36
+ export function polyMapToByCentroid(districts: G.GeoFeatureCollection, centroids: G.GeoCentroidMap): any
38
37
  {
39
38
  let map: any = {};
40
39
 
41
- // Cache labelx, labely if necessary
42
- setLabels(blocks);
43
-
44
40
  // Cache district boundboxes for quick containment exclusion
45
41
  let bbDistricts: BB.BoundBox[] = districts.features.map(f => BB.boundbox(f));
46
42
 
47
43
  // Walk over blocks, mapping centroid to district
48
- blocks.features.forEach(fBlock => {
49
- let x = fBlock.properties.labelx;
50
- let y = fBlock.properties.labely;
44
+ Object.keys(centroids).forEach(blockid => {
45
+ let x = centroids[blockid].x;
46
+ let y = centroids[blockid].y;
51
47
 
52
48
  let fIn: G.GeoFeature[] = [];
53
49
  districts.features.forEach((fDistrict: G.GeoFeature, i: number) => {
@@ -57,8 +53,21 @@ export function polyMapTo(districts: G.GeoFeatureCollection, blocks: G.GeoFeatur
57
53
  });
58
54
 
59
55
  if (fIn.length == 1)
60
- map[fBlock.properties.id] = fIn[0].properties.id;
56
+ map[blockid] = fIn[0].properties.id;
61
57
  });
62
58
 
63
59
  return map;
64
60
  }
61
+
62
+ export function polyMapTo(districts: G.GeoFeatureCollection, blocks: G.GeoFeatureCollection): any
63
+ {
64
+ // Cache labelx, labely if necessary
65
+ setLabels(blocks);
66
+
67
+ let centroids: G.GeoCentroidMap = {};
68
+ blocks.features.forEach(f => {
69
+ centroids[f.properties.id] = { x: f.properties.labelx, y: f.properties.labely }
70
+ });
71
+
72
+ return polyMapToByCentroid(districts, centroids);
73
+ }
@@ -6,9 +6,21 @@ export function polyContainsPoint(poly: any, x: number, y: number): boolean
6
6
  if (pp == null) return false;
7
7
  let bFound = false;
8
8
  let bInside = false;
9
+ let iCurPoly = -1;
10
+ let bCurInside = false;
9
11
 
10
12
  PP.polyPackEachRing(pp, (b: Float64Array, iPoly: number, iRing: number, iOffset: number, nPoints: number) => {
11
13
  if (bFound) return;
14
+ if (iRing == 0)
15
+ {
16
+ if (iCurPoly >= 0 && bCurInside)
17
+ {
18
+ bInside = true;
19
+ bFound = true;
20
+ }
21
+ iCurPoly = iPoly;
22
+ bCurInside = false;
23
+ }
12
24
  let inside = false;
13
25
  let iEnd = iOffset + (nPoints - 1) * 2;
14
26
  for (let i = iOffset, j = iEnd; i <= iEnd; j = i, i += 2)
@@ -20,10 +32,9 @@ export function polyContainsPoint(poly: any, x: number, y: number): boolean
20
32
  if (intersect) inside = !inside;
21
33
  }
22
34
  if (inside)
23
- {
24
- bFound = iRing > 0; // if inside a hole, don't need to process further
25
- bInside = iRing == 0; // not inside if inside a hole
26
- }
35
+ bCurInside = iRing == 0; // not inside if inside a hole
27
36
  });
37
+ if (iCurPoly >= 0 && bCurInside)
38
+ bInside = true;
28
39
  return bInside;
29
40
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dra2020/baseclient",
3
- "version": "1.0.29",
3
+ "version": "1.0.32",
4
4
  "description": "Utility functions for Javascript projects.",
5
5
  "main": "dist/baseclient.js",
6
6
  "types": "./dist/all/all.d.ts",