forked from POI-Importer/POI-Importer.github.io
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tile_geojson.js
83 lines (68 loc) · 1.97 KB
/
tile_geojson.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
var fs = require('graceful-fs');
var mkdir = require('mkdirp');
var rmdir = require('rimraf');
var commandLineArgs = require("command-line-args");
var cli = commandLineArgs([
{ name: "dataset", alias: "d", type: String },
{ name: "reponame", alias: "r", type: String },
]);
var options = cli.parse();
var repo = options.reponame;
var data = options.dataset;
if (!repo)
{
console.log("ERROR: No --reponame provided.");
process.exit(1);
}
if (!data)
{
console.log("ERROR: No --dataset provided.");
process.exit(1);
}
if (repo.charAt(repo.length - 1) != "/")
repo += "/";
var zoom = JSON.parse(fs.readFileSync(repo + "dataset.json", "utf8")).zoom;
// TODO import function from geoHelpers, so tile equality is guaranteed
function latlonToTilenumber(lat, lon)
{
var n = Math.pow(2, zoom);
lat_rad = lat * Math.PI / 180;
return {
"x": Math.floor(n * ((lon + 180) / 360)),
"y": Math.floor(n * (1 - (Math.log(Math.tan(lat_rad) + 1/Math.cos(lat_rad)) / Math.PI)) / 2) }
}
var data = JSON.parse(fs.readFileSync(data, "utf8"));
var tiledData = {};
for (var i = 0; i < data.features.length; i++)
{
var feature = data.features[i];
var co = feature.geometry.coordinates;
var tileNum = latlonToTilenumber(co[1], co[0]);
if (!tiledData[tileNum.x])
tiledData[tileNum.x] = {};
if (!tiledData[tileNum.x][tileNum.y])
tiledData[tileNum.x][tileNum.y] = [];
tiledData[tileNum.x][tileNum.y].push(feature);
}
// Make sure the dir is empty and exists afterwards
rmdir.sync(repo + "data");
mkdir.sync(repo + "data");
var numTiles = 0;
for (var x in tiledData)
{
for (var y in tiledData[x])
{
numTiles++;
var objectToWrite = {
"type": "FeatureCollection",
"features": tiledData[x][y]
}
var fileName = repo + "data/" + x + "_" + y + ".json";
fs.writeFile(fileName, JSON.stringify(objectToWrite, null, 4), function(err) {
if(err) {
return console.log(err);
}
});
}
}
console.log("Data density: " + (data.features.length / numTiles).toFixed(2) + " POI per tile");