-
Notifications
You must be signed in to change notification settings - Fork 2
/
toTopojson.js
51 lines (41 loc) · 1.5 KB
/
toTopojson.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { getLayersName } from '../helpers/layers.js'
import { filter } from 'topojson-simplify'
import { quantize } from 'topojson-client'
/**
* Convert a topohelper topojson to a classic topojson
* Option to select layers
*
* @param {TopoJSON} topo - A valid topojson object
* @param {Object} options - optional parameters
* @param {String|String[]|Number|Number[]} options.layer - target layers (name or index). If omit or "all" use all layers. Ex: {layer: "lyr"} | {layer: ["lyr", 1]}
* @param {Boolean|Number} options.q - level of quantization. if true quantization is applied with a value of 1e4.
* @returns {TopoJSON}
*/
export function toTopojson (topo, options = {}) {
let {layer, q} = options
q = q ?? 1e4
layer = getLayersName(topo, layer)
// Deep copy of topojson
topo = structuredClone(topo)
const allLayers = Object.keys(topo.objects)
// Store proj property to reinject later
const {proj} = topo
// SELECT LAYERS
if (layer.length != allLayers.length) {
// Non intersection = layers to delete
const deleteLayers = allLayers.filter(lyr => !layer.includes(lyr))
// Delete non wanted layers
deleteLayers.forEach(lyr => delete topo.objects[lyr])
// Drop unused arcs
topo = filter(topo) // /!\ proj property is dropped !!!
}
// QUANTIZATION
if (q == false) {
delete topo.lastLayer
return topo
}
topo = quantize(topo, q)
// Reinject proj property
topo.proj = proj
return topo
}