<commit_before>var request = require('request')
	, fs = require('fs-extra')
	, turf = require('turf')
	, async = require('async')
	, xyzTileWorker = require('./xyzTileWorker')
	, geojsonvt = require('geojson-vt')
	, path = require('path')
	, CacheModel = require('../models/cache')
	, Readable = require('stream').Readable
 	, config = require('../config.json');

Math.radians = function(degrees) {
  return degrees * Math.PI / 180;
};

// Converts from radians to degrees.
Math.degrees = function(radians) {
  return radians * 180 / Math.PI;
};

function tile2lon(x,z) {
  return (x/Math.pow(2,z)*360-180);
}

function tile2lat(y,z) {
  var n=Math.PI-2*Math.PI*y/Math.pow(2,z);
  return (180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n))));
}

exports.tileBboxCalculator = function(x, y, z) {
  console.log('tile box calculator for ' + x + ' ' + y + ' ' + z);
  x = Number(x);
  y = Number(y);
  var tileBounds = {
    north: tile2lat(y, z),
    east: tile2lon(x+1, z),
    south: tile2lat(y+1, z),
    west: tile2lon(x, z)
  };

  return tileBounds;
}

exports.xCalculator = function(bbox,z) {
	var x = [];
	var x1 = exports.getX(Number(bbox[0]), z);
	var x2 = exports.getX(Number(bbox[2]), z);
	x.max = Math.max(x1, x2);
	x.min = Math.min(x1, x2);
	if (z == 0){
		x.current = Math.min(x1, x2);
	}
	return x;
}

exports.yCalculator = function(bbox,z) {
	var y = [];
	var y1 = exports.getY(Number(bbox[1]), z);
	var y2 = exports.getY(Number(bbox[3]), z);
	y.max = Math.max(y1, y2);
	y.min = Math.min(y1, y2);
	y.current = Math.min(y1, y2);
	return y;
}

exports.getX = function(lon, zoom) {
	var xtile = Math.floor((lon + 180) / 360 * (1 << zoom));
	return xtile;
}

exports.getY = function(lat, zoom) {
	var ytile = Math.floor((1 - Math.log(Math.tan(Math.radians(parseFloat(lat))) + 1 / Math.cos(Math.radians(parseFloat(lat)))) / Math.PI) /2 * (1 << zoom));
	return ytile;
}

exports.generateMetadataTiles = function(source, file, callback) {
	source.status.message = "Generating metadata tiles";
	console.log('wrote the file');
	fs.readFile(file, function(err, fileData) {
	var gjData = JSON.parse(fileData);
		var geometry = turf.envelope(gjData);
		source.geometry = geometry;
		source.properties = [];
		var allProperties = {};
		for (var i = 0; i < gjData.features.length; i++) {
			var feature = gjData.features[i];
			for (var property in feature.properties) {
				allProperties[property] = allProperties[property] || {key: property, values:[]};
				if (allProperties[property].values.indexOf(feature.properties[property]) == -1) {
					allProperties[property].values.push(feature.properties[property]);
				}
			}
		}
		for (var property in allProperties) {
			source.properties.push(allProperties[property]);
		}

		source.style = {
			defaultStyle: {
				style: {
					'fill': "#000000",
					'fill-opacity': 0.5,
					'stroke': "#0000FF",
					'stroke-opacity': 1.0,
					'stroke-width': 1
				}
			},
			styles: []
		};
		source.save(function(err) {

			var tileIndex = geojsonvt(gjData, {
				indexMaxZoom: 18,
				maxZoom: 18
			});
			console.log("tile index tiles", tileIndex.tiles);
			console.log('async.forEachOf', async.forEachOf);

			async.forEachOf(tileIndex.tiles, function(tile, key, callback) {
				var zoom = 0;
				if (tile.z2 != 0) {
					var shifting = tile.z2;
					while(shifting > 1) {
						zoom++;
						shifting = shifting/2;
					}
				}<commit_msg>tweaking<commit_after>var request = require('request')
	, fs = require('fs-extra')
	, turf = require('turf')
	, async = require('async')
	, xyzTileWorker = require('./xyzTileWorker')
	, geojsonvt = require('geojson-vt')
	, path = require('path')
	, CacheModel = require('../models/cache')
	, Readable = require('stream').Readable
 	, config = require('../config.json');

Math.radians = function(degrees) {
  return degrees * Math.PI / 180;
};

// Converts from radians to degrees.
Math.degrees = function(radians) {
  return radians * 180 / Math.PI;
};

function tile2lon(x,z) {
  return (x/Math.pow(2,z)*360-180);
}

function tile2lat(y,z) {
  var n=Math.PI-2*Math.PI*y/Math.pow(2,z);
  return (180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n))));
}

exports.tileBboxCalculator = function(x, y, z) {
  console.log('tile box calculator for ' + x + ' ' + y + ' ' + z);
  x = Number(x);
  y = Number(y);
  var tileBounds = {
    north: tile2lat(y, z),
    east: tile2lon(x+1, z),
    south: tile2lat(y+1, z),
    west: tile2lon(x, z)
  };

  return tileBounds;
}

exports.xCalculator = function(bbox,z) {
	var x = [];
	var x1 = exports.getX(Number(bbox[0]), z);
	var x2 = exports.getX(Number(bbox[2]), z);
	x.max = Math.max(x1, x2);
	x.min = Math.min(x1, x2);
	if (z == 0){
		x.current = Math.min(x1, x2);
	}
	return x;
}

exports.yCalculator = function(bbox,z) {
	var y = [];
	var y1 = exports.getY(Number(bbox[1]), z);
	var y2 = exports.getY(Number(bbox[3]), z);
	y.max = Math.max(y1, y2);
	y.min = Math.min(y1, y2);
	y.current = Math.min(y1, y2);
	return y;
}

exports.getX = function(lon, zoom) {
	var xtile = Math.floor((lon + 180) / 360 * (1 << zoom));
	return xtile;
}

exports.getY = function(lat, zoom) {
	var ytile = Math.floor((1 - Math.log(Math.tan(Math.radians(parseFloat(lat))) + 1 / Math.cos(Math.radians(parseFloat(lat)))) / Math.PI) /2 * (1 << zoom));
	return ytile;
}

exports.generateMetadataTiles = function(source, file, callback) {
	source.status.message = "Generating metadata tiles";
	console.log('wrote the file');
	fs.readFile(file, function(err, fileData) {
	var gjData = JSON.parse(fileData);
		var geometry = turf.envelope(gjData);
		source.geometry = geometry;
		source.properties = [];
		var allProperties = {};
		for (var i = 0; i < gjData.features.length; i++) {
			var feature = gjData.features[i];
			for (var property in feature.properties) {
				allProperties[property] = allProperties[property] || {key: property, values:[]};
				if (allProperties[property].values.indexOf(feature.properties[property]) == -1) {
					allProperties[property].values.push(feature.properties[property]);
				}
			}
		}
		for (var property in allProperties) {
			source.properties.push(allProperties[property]);
		}

		source.style = {
			defaultStyle: {
				style: {
					'fill': "#000000",
					'fill-opacity': 0.5,
					'stroke': "#0000FF",
					'stroke-opacity': 1.0,
					'stroke-width': 1
				}
			},
			styles: []
		};
		source.save(function(err) {

			var tileIndex = geojsonvt(gjData, {
				indexMaxZoom: 18,
				maxZoom: 18
			});
			console.log("tile index tiles", tileIndex.tiles);
			console.log('async.forEachOf', async.forEachOf);

			async.forEachOf(tileIndex.tiles, function(tile, key, callback) {
				console.log('going to get tile ', tile.z2, tile.x, tile.y);
				var zoom = 0;
				if (tile.z2 != 0) {
					var shifting = tile.z2;
					while(shifting > 1) {
						zoom++;
						shifting = shifting/2;
						console.log('zoom is ' + zoom + ' shifting is ' + shifting);
					}
				}
{'fraction_non_alphanumeric': 0.18312101910828024, 'fraction_numerical': 0.0253329473074696, 'mean_word_length': 3.509791122715405, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '767837', 'n_tokens_mistral': 2942, 'n_tokens_neox': 2596, 'n_words': 525}