-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathsample-to-geojson
executable file
·58 lines (53 loc) · 1.27 KB
/
sample-to-geojson
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
52
53
54
55
56
57
58
#!/usr/bin/env node
var through = require('through2')
var tilebelt = require('tilebelt')
var readSample = require('./lib/read-sample')
var geojsonStream = require('geojson-stream')
var argv = require('minimist')(process.argv.slice(2))
readSample(argv)
.pipe(through.obj(function (tile, _, next) {
var bbox = tilebelt.tileToBBOX([tile[1], tile[2], tile[0]])
// quantize based on zoom level
var z = tile[0]
var pixel = 360 / 256 / Math.pow(2, z)
var places = Math.max(0, -Math.floor(Math.log10(pixel)))
bbox = bbox.map(function (c) {
return parseFloat(c.toFixed(places))
})
if (argv.polygons) {
var poly = {
type: 'Polygon',
coordinates: [[
[bbox[0], bbox[1]],
[bbox[0], bbox[3]],
[bbox[2], bbox[3]],
[bbox[2], bbox[1]],
[bbox[0], bbox[1]]
]]
}
this.push(feature(tile, poly))
}
var point = {
type: 'Point',
coordinates: [
(bbox[0] + bbox[2]) / 2,
(bbox[1] + bbox[3]) / 2
]
}
this.push(feature(tile, point))
next()
}))
.pipe(geojsonStream.stringify())
.pipe(process.stdout)
function feature (tile, geometry) {
return {
type: 'Feature',
properties: {
tile: tile.join('/'),
x: tile[1],
y: tile[2],
z: tile[0]
},
geometry: geometry
}
}