@dra2020/baseclient 1.0.34 → 1.0.37
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/baseclient.js +55 -20
- package/dist/baseclient.js.map +1 -1
- package/dist/geo/geo.d.ts +3 -0
- package/lib/geo/geo.ts +25 -9
- package/lib/poly/mapto.ts +18 -5
- package/lib/poly/topo.ts +18 -8
- package/package.json +4 -1
package/dist/geo/geo.d.ts
CHANGED
|
@@ -13,8 +13,11 @@ export declare type GeoCentroidMap = {
|
|
|
13
13
|
export declare function geoEnsureID(col: GeoFeatureCollection): void;
|
|
14
14
|
export declare function geoCollectionToMap(col: GeoFeatureCollection): GeoFeatureMap;
|
|
15
15
|
export declare function geoMapToCollection(map: GeoFeatureMap): GeoFeatureCollection;
|
|
16
|
+
export declare function geoMapToCollectionNonNull(map: GeoFeatureMap): GeoFeatureCollection;
|
|
16
17
|
export declare function geoCollectionToTopo(col: GeoFeatureCollection): Poly.Topo;
|
|
18
|
+
export declare function geoCollectionToTopoNonNull(col: GeoFeatureCollection): Poly.Topo;
|
|
17
19
|
export declare function geoTopoToCollection(topo: Poly.Topo): GeoFeatureCollection;
|
|
20
|
+
export declare function geoTopoToCollectionNonNull(topo: Poly.Topo): GeoFeatureCollection;
|
|
18
21
|
export interface GeoFeatureMap {
|
|
19
22
|
[id: string]: GeoFeature;
|
|
20
23
|
}
|
package/lib/geo/geo.ts
CHANGED
|
@@ -38,7 +38,13 @@ export function geoCollectionToMap(col: GeoFeatureCollection): GeoFeatureMap
|
|
|
38
38
|
|
|
39
39
|
export function geoMapToCollection(map: GeoFeatureMap): GeoFeatureCollection
|
|
40
40
|
{
|
|
41
|
-
if (
|
|
41
|
+
if (Util.countKeys(map) == 0) return null;
|
|
42
|
+
return geoMapToCollectionNonNull(map);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function geoMapToCollectionNonNull(map: GeoFeatureMap): GeoFeatureCollection
|
|
46
|
+
{
|
|
47
|
+
if (map == null) return null;
|
|
42
48
|
let col: GeoFeatureCollection = { type: 'FeatureCollection', features: [] };
|
|
43
49
|
Object.keys(map).forEach((geoid: string) => { col.features.push(map[geoid]) });
|
|
44
50
|
return col;
|
|
@@ -51,6 +57,11 @@ export function geoCollectionToTopo(col: GeoFeatureCollection): Poly.Topo
|
|
|
51
57
|
return topo;
|
|
52
58
|
}
|
|
53
59
|
|
|
60
|
+
export function geoCollectionToTopoNonNull(col: GeoFeatureCollection): Poly.Topo
|
|
61
|
+
{
|
|
62
|
+
return geoCollectionToTopo(col);
|
|
63
|
+
}
|
|
64
|
+
|
|
54
65
|
export function geoTopoToCollection(topo: Poly.Topo): GeoFeatureCollection
|
|
55
66
|
{
|
|
56
67
|
let col = Poly.topoToCollection(topo);
|
|
@@ -58,6 +69,11 @@ export function geoTopoToCollection(topo: Poly.Topo): GeoFeatureCollection
|
|
|
58
69
|
return col;
|
|
59
70
|
}
|
|
60
71
|
|
|
72
|
+
export function geoTopoToCollectionNonNull(topo: Poly.Topo): GeoFeatureCollection
|
|
73
|
+
{
|
|
74
|
+
return geoTopoToCollection(topo);
|
|
75
|
+
}
|
|
76
|
+
|
|
61
77
|
export interface GeoFeatureMap
|
|
62
78
|
{
|
|
63
79
|
[id: string]: GeoFeature; // Maps id to GeoFeature
|
|
@@ -183,9 +199,9 @@ export class GeoMultiCollection
|
|
|
183
199
|
if (! e.col)
|
|
184
200
|
{
|
|
185
201
|
if (e.map)
|
|
186
|
-
e.col =
|
|
202
|
+
e.col = geoMapToCollectionNonNull(e.map);
|
|
187
203
|
else if (e.topo)
|
|
188
|
-
e.col =
|
|
204
|
+
e.col = geoTopoToCollectionNonNull(e.topo);
|
|
189
205
|
}
|
|
190
206
|
return e.col;
|
|
191
207
|
}
|
|
@@ -199,7 +215,7 @@ export class GeoMultiCollection
|
|
|
199
215
|
e.map = geoCollectionToMap(e.col);
|
|
200
216
|
else if (e.topo)
|
|
201
217
|
{
|
|
202
|
-
e.col =
|
|
218
|
+
e.col = geoTopoToCollectionNonNull(e.topo);
|
|
203
219
|
e.map = geoCollectionToMap(e.col);
|
|
204
220
|
}
|
|
205
221
|
}
|
|
@@ -212,11 +228,11 @@ export class GeoMultiCollection
|
|
|
212
228
|
if (! e.topo)
|
|
213
229
|
{
|
|
214
230
|
if (e.col)
|
|
215
|
-
e.topo =
|
|
231
|
+
e.topo = geoCollectionToTopoNonNull(e.col);
|
|
216
232
|
else if (e.map)
|
|
217
233
|
{
|
|
218
|
-
e.col =
|
|
219
|
-
e.topo =
|
|
234
|
+
e.col = geoMapToCollectionNonNull(e.map);
|
|
235
|
+
e.topo = geoCollectionToTopoNonNull(e.col);
|
|
220
236
|
}
|
|
221
237
|
}
|
|
222
238
|
return e.topo;
|
|
@@ -242,7 +258,7 @@ export class GeoMultiCollection
|
|
|
242
258
|
this.all.col = this._col(this.nthEntry(0));
|
|
243
259
|
else
|
|
244
260
|
// Going from map to collection guarantees that any duplicates are removed
|
|
245
|
-
this.all.col =
|
|
261
|
+
this.all.col = geoMapToCollectionNonNull(this.allMap());
|
|
246
262
|
}
|
|
247
263
|
return this.all.col;
|
|
248
264
|
}
|
|
@@ -276,7 +292,7 @@ export class GeoMultiCollection
|
|
|
276
292
|
if (n == 1)
|
|
277
293
|
this.all.topo = this._topo(this.nthEntry(0));
|
|
278
294
|
else
|
|
279
|
-
this.all.topo =
|
|
295
|
+
this.all.topo = geoCollectionToTopoNonNull(this.allCol());
|
|
280
296
|
}
|
|
281
297
|
return this.all.topo;
|
|
282
298
|
}
|
package/lib/poly/mapto.ts
CHANGED
|
@@ -38,22 +38,35 @@ export function polyMapToByCentroid(districts: G.GeoFeatureCollection, centroids
|
|
|
38
38
|
let map: any = {};
|
|
39
39
|
|
|
40
40
|
// Cache district boundboxes for quick containment exclusion
|
|
41
|
-
let
|
|
41
|
+
let fs: G.GeoFeature[] = districts.features;
|
|
42
|
+
let bbDistricts: BB.BoundBox[] = fs.map(f => BB.boundbox(f));
|
|
43
|
+
let aDistricts: number[] = fs.map(f => P.polyArea(f));
|
|
42
44
|
|
|
43
45
|
// Walk over blocks, mapping centroid to district
|
|
44
46
|
Object.keys(centroids).forEach(blockid => {
|
|
45
47
|
let x = centroids[blockid].x;
|
|
46
48
|
let y = centroids[blockid].y;
|
|
47
49
|
|
|
48
|
-
let fIn:
|
|
49
|
-
|
|
50
|
+
let fIn: number[] = [];
|
|
51
|
+
fs.forEach((fDistrict: G.GeoFeature, i: number) => {
|
|
50
52
|
if (BB.boundboxContains(bbDistricts[i], x, y))
|
|
51
53
|
if (polyContainsPoint(fDistrict, x, y))
|
|
52
|
-
fIn.push(
|
|
54
|
+
fIn.push(i);
|
|
53
55
|
});
|
|
54
56
|
|
|
55
57
|
if (fIn.length == 1)
|
|
56
|
-
map[blockid] = fIn[0].properties.id;
|
|
58
|
+
map[blockid] = fs[fIn[0]].properties.id;
|
|
59
|
+
else if (fIn.length > 1)
|
|
60
|
+
{
|
|
61
|
+
// Pick district with smallest area since some times we get malformed content that doesn't
|
|
62
|
+
// reflect holes in districts when one is nested in another. So theory is smaller district
|
|
63
|
+
// is a hole in containing larger one(s).
|
|
64
|
+
let iLow = fIn[0];
|
|
65
|
+
for (let i = 1; i < fIn.length; i++)
|
|
66
|
+
if (aDistricts[fIn[i]] < aDistricts[iLow])
|
|
67
|
+
iLow = fIn[i];
|
|
68
|
+
map[blockid] = fs[iLow].properties.id;
|
|
69
|
+
}
|
|
57
70
|
});
|
|
58
71
|
|
|
59
72
|
return map;
|
package/lib/poly/topo.ts
CHANGED
|
@@ -19,11 +19,16 @@ export type Topo = any;
|
|
|
19
19
|
|
|
20
20
|
function getGEOID(f: any): string
|
|
21
21
|
{
|
|
22
|
-
if (f.features && f.features.length)
|
|
23
|
-
|
|
22
|
+
if (f.features && f.features.length)
|
|
23
|
+
f = f.features[0];
|
|
24
|
+
else if (Array.isArray(f))
|
|
25
|
+
f = f[0];
|
|
26
|
+
else
|
|
27
|
+
return '';
|
|
24
28
|
if (f.properties.id !== undefined) return 'id';
|
|
25
29
|
if (f.properties.GEOID !== undefined) return 'GEOID';
|
|
26
30
|
if (f.properties.GEOID10 !== undefined) return 'GEOID10';
|
|
31
|
+
return '';
|
|
27
32
|
}
|
|
28
33
|
|
|
29
34
|
export function topoFromCollection(col: any): Topo
|
|
@@ -33,10 +38,14 @@ export function topoFromCollection(col: any): Topo
|
|
|
33
38
|
let prop = getGEOID(col);
|
|
34
39
|
let objects: any = {};
|
|
35
40
|
col.features.forEach((f: any) => objects[f.properties[prop]] = f);
|
|
36
|
-
let topo
|
|
41
|
+
let topo: any;
|
|
42
|
+
if (Util.isEmpty(objects))
|
|
43
|
+
topo = { objects: objects }
|
|
44
|
+
else
|
|
45
|
+
topo = TopoServer.topology(objects);
|
|
37
46
|
PP.featureRepack(col, save);
|
|
38
47
|
if (col.datasets)
|
|
39
|
-
|
|
48
|
+
topo.datasets = col.datasets;
|
|
40
49
|
return topo;
|
|
41
50
|
}
|
|
42
51
|
|
|
@@ -109,10 +118,11 @@ export function topoToFeature(topo: Topo, geoid: string): any
|
|
|
109
118
|
export function topoToCollection(topo: Topo): any
|
|
110
119
|
{
|
|
111
120
|
let col: any = { type: 'FeatureCollection', features: [] };
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
121
|
+
if (topo)
|
|
122
|
+
Object.keys(topo.objects).forEach((geoid: string) => {
|
|
123
|
+
col.features.push(topoToFeature(topo, geoid));
|
|
124
|
+
});
|
|
125
|
+
if (topo && topo.datasets) col.datasets = topo.datasets;
|
|
116
126
|
return col;
|
|
117
127
|
}
|
|
118
128
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dra2020/baseclient",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.37",
|
|
4
4
|
"description": "Utility functions for Javascript projects.",
|
|
5
5
|
"main": "dist/baseclient.js",
|
|
6
6
|
"types": "./dist/all/all.d.ts",
|
|
@@ -40,6 +40,9 @@
|
|
|
40
40
|
"webpack-cli": "^4.4.0"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
+
"@dra2020/baseclient": "^1.0.36",
|
|
44
|
+
"@dra2020/baseserver": "^1.0.21",
|
|
45
|
+
"@dra2020/dra-types": "^1.8.26",
|
|
43
46
|
"@dra2020/topojson-client": "^3.2.7",
|
|
44
47
|
"@dra2020/topojson-server": "^3.0.103",
|
|
45
48
|
"@dra2020/topojson-simplify": "^3.0.102",
|