-
Notifications
You must be signed in to change notification settings - Fork 2
/
toGeojson.js
37 lines (32 loc) · 1020 Bytes
/
toGeojson.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
import { feature } from 'topojson-client'
import { getLayersName } from '../helpers/layers.js'
/**
* Convert topojson to geojson
*
* @param {GeoJSON} 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]}
* @returns {GeoJSON}
*/
export function toGeojson(topo, options = {}) {
let { layer } = options
layer = getLayersName(topo, layer)
// Conversion to geojson + add layer name as property
const convert = (layer) => {
const geojson = feature(topo, topo.objects[layer])
if (geojson.type === 'Feature') {
return {
type: 'FeatureCollection',
features: [geojson],
name: layer,
proj: topo.proj
}
}
geojson.name = layer
geojson.proj = topo.proj
return geojson
}
return layer.length > 1
? layer.map(lyr => convert(lyr))
: convert(layer)
}