-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
75 lines (67 loc) · 1.99 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#! /usr/bin/env node
var s2 = require('s2'),
argv = require('minimist')(process.argv.slice(2)),
fc = require('turf-featurecollection'),
geocolor = require('geocolor'),
center = require('turf-center'),
linestring = require('turf-linestring'),
fs = require('fs'),
cover = require('geojson-cover')
if(argv.h || argv.help){
help();
}
else{
var geojson = JSON.parse(fs.readFileSync(argv._[0]));
var cells = cover.geometryGeoJSON(geojson);
//sort cells by alphanumeric token
cells.features.sort(function(a,b){
return b-a;
})
//rank cells
for(var i=0;i<cells.features.length;i++){
cells.features[i].properties.rank = i+1
}
if(argv.c || argv.color){
//color cells
var cells = geocolor.jenks(cells, 'rank', 50, ['white', 'purple'],
{'fill-opacity':.5, 'stroke-width':1})
}
if(argv.l || argv.line){
//calculate hilburt curve by creating a linestring with vertices from the center of each cell in order
var hilburt = fc([linestring([])]);
cells.features.forEach(function(cell){
hilburt.features[0].geometry.coordinates.push(center(cell).geometry.coordinates);
})
hilburt.features[0].properties = {};
hilburt = geocolor.all(hilburt, {'stroke':'red'});
//concat the cells with the curve
cells.features = cells.features.concat(hilburt.features);
}
console.log(JSON.stringify(cells));
}
function help(){
var h = ''
h+='\ns2cover\n'
h+='===\n\n'
h+='-h --help : show options and usage\n'
h+='-l --line : draw a hilburt curve throught the cells by index order\n'
h+='-c --color color the cells from white->purple by index order\n\n'
h+='example: s2cover myFile.geojson\n'
h+='\n\n\x1B[1m\x1B[31m \\\n'
h+=' /\\/\n'
h+=' / /\\\n'
h+=' \\/\\ \\ \\\n'
h+=' /\\ / / /\n'
h+=' / / \\/ \\/\\\n'
h+=' \\ \\/\\ /\\ \\\n'
h+=' \\/ / / / /\\\/\n'
h+=' /\\/ / \ \\ \\\n'
h+=' \\ \\/\\ \\/\n'
h+=' \\/\\ \\\n'
h+=' / /\\/\n'
h+=' \\ \\\n'
h+=' \\/\n'
h+=''
h+=''
console.log(h);
}