Tags

, , , , , ,

In the Netherlands, there are a few databases that save geo-coordinates according to the “Rijksdriehoeks”-system (“Rijksdriehoekscoördinaten” or “RD-coördinaten”). Examples of these are the Dutch land register (Kadaster), but also Carerix (Recruitment Software) likes to save RD-coordinates for locations of job openings and employees.

However, Google Maps uses a different system, namely the WGS system (World Geodetic System 1984). So in order to try to show data from, say, Carerix on a map, we need to convert it.

google maps netherlands rd-coordinates

Luckily, I found a web page where this could be done, functionally, so I only needed to reverse-engineer the code and be done. So with many thanks to the people from “hetweer.nu”, I now describe the 3 steps to convert RD-coordinates to Google Maps coordinates:

  1. Verify the RD-coordinates input
  2. Do some complex math to calculate the WGS-coordinates
  3. Add it to the Google Maps marker

So first the function “Convert”, in which you put a x-value and an y-value. For example: 122037 (x) and 483940 (y). This is somewhere in Amsterdam. It just checks if the values are valid, and then pass it to the “DoTheMath” function.

function Convert(x, y) {
  if (x < 1000) {
	   x *= 1000;
	}
  if (y < 1000) {
    y *= 1000;
  }
  var invalidX = (x < 0 || x > 290000);
  if (invalidX) {
    alert("x must be between 0 and 290,000");
	}
  var invalidY = (y <290000 || y > 630000);
  if (invalidY) {
    alert("y must be between 290,000 and 630,000,");
  }
  if (!invalidX && !invalidY) {
    var latLng = DoTheMath(x,y);
    return latLng;
  }
}

Next, we will see what will be behind the mystical “DoTheMath”-function. I do not understand the math myself, I just found it. I did test it thoroughly, and it works. It is just off by one kilometer at the maximum. But mostly it is just a few meters off.

function DoTheMath(x,y) {
  var x0  = 155000.000;
  var y0  = 463000.000;
  var f0 = 52.156160556;
  var l0 =  5.387638889;
  var a01=3236.0331637 ; var b10=5261.3028966;
  var a20= -32.5915821 ; var b11= 105.9780241;
  var a02=  -0.2472814 ; var b12=   2.4576469;
  var a21=  -0.8501341 ; var b30=  -0.8192156;
  var a03=  -0.0655238 ; var b31=  -0.0560092;
  var a22=  -0.0171137 ; var b13=   0.0560089;
  var a40=   0.0052771 ; var b32=  -0.0025614;
  var a23=  -0.0003859 ; var b14=   0.0012770;
  var a41=   0.0003314 ; var b50=   0.0002574;
  var a04=   0.0000371 ; var b33=  -0.0000973;
  var a42=   0.0000143 ; var b51=   0.0000293;
  var a24=  -0.0000090 ; var b15=   0.0000291;

  var dx=(x-x0)*Math.pow(10,-5);
  var dy=(y-y0)*Math.pow(10,-5);

  var df =a01*dy + a20*Math.pow(dx,2) + a02*Math.pow(dy,2) + a21*Math.pow(dx,2)*dy + a03*Math.pow(dy,3);
  df+=a40*Math.pow(dx,4) + a22*Math.pow(dx,2)*Math.pow(dy,2) + a04*Math.pow(dy,4) + a41*Math.pow(dx,4)*dy;
  df+=a23*Math.pow(dx,2)*Math.pow(dy,3) + a42*Math.pow(dx,4)*Math.pow(dy,2) + a24*Math.pow(dx,2)*Math.pow(dy,4);
  var f = f0 + df/3600;

  var dl =b10*dx +b11*dx*dy +b30*Math.pow(dx,3) + b12*dx*Math.pow(dy,2) + b31*Math.pow(dx,3)*dy;
  dl+=b13*dx*Math.pow(dy,3)+b50*Math.pow(dx,5) + b32*Math.pow(dx,3)*Math.pow(dy,2) + b14*dx*Math.pow(dy,4);
  dl+=b51*Math.pow(dx,5)*dy +b33*Math.pow(dx,3)*Math.pow(dy,3) + b15*dx*Math.pow(dy,5);
  var l = l0 + dl/3600

  return {"lat": f, "lng": l};
}

So these functions return a {lat,lng} object, which is convenient because it could be put directly into a Google Maps marker. And because it is just a JSON object containing data, you could also use it for other stuff. So, we have a google.maps.Map-object called “map”, and now we convert the RD-coordinate and show it on the map:

var latLng = Convert(122037, 483940);
var marker = new google.maps.Marker({
      position: latLng,
      map: map
    });

Happy Coding!

Advertisements