@dra2020/baseclient 1.0.28 → 1.0.31

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;
@@ -14,3 +14,4 @@ export * from './topo';
14
14
  export * from './selfintersect';
15
15
  export * from './shamos';
16
16
  export * from './pointinpoly';
17
+ export * from './mapto';
@@ -0,0 +1,3 @@
1
+ import * as G from '../geo/all';
2
+ export declare function polyMapToByCentroid(districts: G.GeoFeatureCollection, centroids: G.GeoCentroidMap): any;
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/all.ts CHANGED
@@ -14,3 +14,4 @@ export * from './topo';
14
14
  export * from './selfintersect';
15
15
  export * from './shamos';
16
16
  export * from './pointinpoly';
17
+ export * from './mapto';
@@ -0,0 +1,73 @@
1
+ import * as G from '../geo/all';
2
+ import * as P from './poly';
3
+ import * as PP from './polypack';
4
+ import * as PL from './polylabel';
5
+ import * as BB from './boundbox';
6
+ import { polyContainsPoint } from './pointinpoly';
7
+
8
+ function setLabels(c: G.GeoFeatureCollection): void
9
+ {
10
+ c.features.forEach(f => {
11
+ if (f.properties.labelx === undefined)
12
+ {
13
+ let {x,y} = PL.polyLabel(f);
14
+ f.properties.labelx = x;
15
+ f.properties.labely = y;
16
+ }
17
+ });
18
+ }
19
+
20
+ // polyMapTo:
21
+ //
22
+ // Given a set of underlying blocks (or precincts), map them to the district (or any other feature)
23
+ // they are contained in.
24
+ //
25
+ // If a block maps to multiple districts or no district, it is left out of the result.
26
+ //
27
+ // Note that the algorithm is to see where the centroid of the block overlaps. So a block
28
+ // could potentially overlap multiple districts and this would return only the one containing
29
+ // the centroid.
30
+ //
31
+ // The thinking is that if you want fine granularity, use real blocks as the base collection.
32
+ // If you provide precincts, you will get a result on precinct granularity.
33
+ //
34
+ // The return value is an object that maps the block feature ids to the district feature id.
35
+ //
36
+ export function polyMapToByCentroid(districts: G.GeoFeatureCollection, centroids: G.GeoCentroidMap): any
37
+ {
38
+ let map: any = {};
39
+
40
+ // Cache district boundboxes for quick containment exclusion
41
+ let bbDistricts: BB.BoundBox[] = districts.features.map(f => BB.boundbox(f));
42
+
43
+ // Walk over blocks, mapping centroid to district
44
+ Object.keys(centroids).forEach(blockid => {
45
+ let x = centroids[blockid].x;
46
+ let y = centroids[blockid].y;
47
+
48
+ let fIn: G.GeoFeature[] = [];
49
+ districts.features.forEach((fDistrict: G.GeoFeature, i: number) => {
50
+ if (BB.boundboxContains(bbDistricts[i], x, y))
51
+ if (polyContainsPoint(fDistrict, x, y))
52
+ fIn.push(fDistrict);
53
+ });
54
+
55
+ if (fIn.length == 1)
56
+ map[blockid] = fIn[0].properties.id;
57
+ });
58
+
59
+ return map;
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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dra2020/baseclient",
3
- "version": "1.0.28",
3
+ "version": "1.0.31",
4
4
  "description": "Utility functions for Javascript projects.",
5
5
  "main": "dist/baseclient.js",
6
6
  "types": "./dist/all/all.d.ts",