var fs = require('graceful-fs');
var mkdir = require('mkdirp');
var rmdir = require('rimraf');

var repo = "datasets/belgium-haltes-de-lijn/"
var data = "haltes_de_lijn.json"

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");




@@ -92,87 +92,337 @@
 ');%0A
-%0A
 var 
-repo = %22datasets/belgium-haltes-de-lijn/%22%0Avar data = %22haltes_de_lijn.json%22
+commandLineArgs = require(%22command-line-args%22);%0A %0Avar cli = commandLineArgs(%5B%0A    %7B name: %22dataset%22, alias: %22d%22, type: String %7D,%0A    %7B name: %22reponame%22, alias: %22r%22, type: String %7D,%0A%5D);%0A%0Avar options = cli.parse();%0Avar repo = options.reponame;%0Avar data = options.dataset;%0A%0Aif (repo.charAt(repo.length - 1) != %22/%22)%0A%09repo += %22/%22;
 %0A%0Ava

Support CLI arguments for the tile splitter