
/* Description: Convert RijksDriehoeksstel coordinates (epsg:28992) to WGS84 coordinates (epsg:4326)in decimal
* (longitude,latitude) notation.
*
* Based on "Benaderingsformules voor de transformatie tussen RD- en WGS84-kaartcoördinaten"
* Authors: - ing. F.H. Schreutelkamp, Stichting 'De Koepel', sterrenwacht 'Sonnenborgh', Utrecht, The Netherlands
* - ir. G.L. Strang van Hees, former academic docent of the department of Geodesie, TU Delft. The
Netherlands
* Link available at: http://www.dekoepel.nl/pdf/Transformatieformules.pdf
* Author: Menno Holtkamp
* Company: ICTU
* Programme: AdviesOverheid.nl
* Project: Vergunningen
* Examples: RD(x,y) --> WGS84(long,lat)
* 81 Amsterdam (Westertoren) (120700.723, 487525.501) --> (52.37453253, 4.88352559)
* 21 Groningen (Martinitoren) (233883.131, 582065.167) --> (53.21938317, 6.56820053)
*/

function convertRD2WGS84(rdx, rdy)
	{
		//RD x- and y-coordinates for base-point: Onze-Lieve-Vrouwe tower in Amersfoort
		var rdx0 = 155000.00;
		var rdy0 = 463000.00;


		//WGS84-coordinates for base-point: Onze-Lieve-Vrouwe tower in Amersfoort


		var WGS84Long0 = 52.15517440;
		var WGS84Lat0 = 5.38720621;

		//Result array which will be returned, result[0]->long, result[1]->lat,
		var result = new Array();

		//Determine dX and dY
		var dX = (parseFloat(rdx) - rdx0) * Math.pow(10, -5); // dX = (X - X0)10^-5
		var dY = (parseFloat(rdy) - rdy0) * Math.pow(10, -5); // dY = (Y - Y0)10^-5


		//K-values
		var k = new Array();
		k[0] = new Array();
		k[1] = new Array();
		k[2] = new Array();
		k[3] = new Array();
		k[4] = new Array();
		k[0][1] = 3235.65389;
		k[0][2] = -0.24750;
		k[0][3] = -0.06550;
		k[1][0] = -0.00738;
		k[1][1] = -0.00012;
		k[2][0] = -32.58297;
		k[2][1] = -0.84978;
		k[2][2] = -0.01709;
		k[2][3] = -0.00039;
		k[3][0] = null; //some value required for array looping
		k[4][0] = 0.00530;
		k[4][1] = 0.00033;

		//L-values
		var l = new Array();
		l[0] = new Array();
		l[1] = new Array();
		l[2] = new Array();
		l[3] = new Array();
		l[4] = new Array();
		l[5] = new Array();
		l[0][1] = 0.01199;
		l[0][2] = 0.00022;
		l[1][0] = 5260.52916;
		l[1][1] = 105.94684;
		l[1][2] = 2.45656;
		l[1][3] = 0.05594;
		l[1][4] = 0.00128;
		l[2][0] = -0.00022;
		l[3][0] = -0.81885;
		l[3][1] = -0.05607;
		l[3][2] = -0.00256;
		l[4][0] = null; //some value required for array looping
		l[5][0] = 0.00026;

		//Calculate new WGS84Longitude, iterate over the coefficents in array k
		var WGS84Long = 0;

		for(var x=0; x < k.length ;x++)
			{
			for(var y=0; y < k[x].length;y++)
				{
				if(k[x][y] != null)
					{
					WGS84Long += k[x][y] * (Math.pow(dX,x)) * (Math.pow(dY,y));
					}//if
				}//for
			}//for

		WGS84Long = WGS84Long0 + (WGS84Long / 3600);


		//Calculate new WGS84Latitude, iterate over the coefficents in array l
		var WGS84Lat = 0;


		for(var x=0; x < l.length ;x++)
			{
			for(var y=0; y < l[x].length;y++)
				{
				if(l[x][y] != null)
					{
						WGS84Lat += l[x][y] * (Math.pow(dX,x)) * (Math.pow(dY,y));
					}//if
				}//for
			}//for

		WGS84Lat = WGS84Lat0 + (WGS84Lat / 3600);


		//Fill and return result array


		result.push(WGS84Long, WGS84Lat);
		return result;
	}//function
