forked from morganherlocker/s2cover
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
98 lines (88 loc) · 2.45 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#! /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')
if(argv.h || argv.help){
help();
}
else{
var geojson = JSON.parse(fs.readFileSync(argv._[0]));
var ll = s2.fromGeojson(geojson);
//create a bounding box to be covered
if(!ll & !argv._[0]){
ll = new s2.S2LatLngRect(new s2.S2LatLng(22.500496, -126.562500), new s2.S2LatLng(47.544554, -65.039063));
}
//compute cover
var llcover = s2.getCover(ll, {
max_cells: 300,
min:1,
max:8,
type: 'polygon'
});
//convert cover to geojson
var cells = fc([])
cells.features = llcover.map(function(cover){
return {
type: 'Feature',
geometry: cover.toGeoJSON(),
properties: {
id: cover.id().toToken()
}
}
})
//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);
}