@dra2020/baseclient 1.0.28 → 1.0.29

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.
@@ -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,2 @@
1
+ import * as G from '../geo/all';
2
+ export declare function polyMapTo(districts: G.GeoFeatureCollection, blocks: G.GeoFeatureCollection): any;
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,64 @@
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
+
37
+ export function polyMapTo(districts: G.GeoFeatureCollection, blocks: G.GeoFeatureCollection): any
38
+ {
39
+ let map: any = {};
40
+
41
+ // Cache labelx, labely if necessary
42
+ setLabels(blocks);
43
+
44
+ // Cache district boundboxes for quick containment exclusion
45
+ let bbDistricts: BB.BoundBox[] = districts.features.map(f => BB.boundbox(f));
46
+
47
+ // Walk over blocks, mapping centroid to district
48
+ blocks.features.forEach(fBlock => {
49
+ let x = fBlock.properties.labelx;
50
+ let y = fBlock.properties.labely;
51
+
52
+ let fIn: G.GeoFeature[] = [];
53
+ districts.features.forEach((fDistrict: G.GeoFeature, i: number) => {
54
+ if (BB.boundboxContains(bbDistricts[i], x, y))
55
+ if (polyContainsPoint(fDistrict, x, y))
56
+ fIn.push(fDistrict);
57
+ });
58
+
59
+ if (fIn.length == 1)
60
+ map[fBlock.properties.id] = fIn[0].properties.id;
61
+ });
62
+
63
+ return map;
64
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dra2020/baseclient",
3
- "version": "1.0.28",
3
+ "version": "1.0.29",
4
4
  "description": "Utility functions for Javascript projects.",
5
5
  "main": "dist/baseclient.js",
6
6
  "types": "./dist/all/all.d.ts",