Help with php function

Caporegime
Joined
25 Jul 2005
Posts
28,851
Location
Canada
I have this php function I want to use but as my knowledge of php is restricted to changing small bits in wordpress templates I have no idea how to use it... :p

Basically I need to convert one set of coordinates from a (slightly) obscure coordinate system to lat/long (WGS84). What do I need to do with this function to allow me to do this (I assume input a text/excel file with x and y coords and output to another text/excel file with lat/long)?

PHP:
function rd2wgs ($x, $y) {     // Calculate WGS84 coördinates     $dX = ($x - 155000) * pow(10, - 5);     $dY = ($y - 463000) * pow(10, - 5);     $SomN = (3235.65389 * $dY) + (- 32.58297 * pow($dX, 2)) + (- 0.2475 *          pow($dY, 2)) + (- 0.84978 * pow($dX, 2) *          $dY) + (- 0.0655 * pow($dY, 3)) + (- 0.01709 *          pow($dX, 2) * pow($dY, 2)) + (- 0.00738 *          $dX) + (0.0053 * pow($dX, 4)) + (- 0.00039 *          $dX ^ 2 * pow($dY, 3)) + (0.00033 * pow(             $dX, 4) * $dY) + (- 0.00012 *          $dX * $dY);     $SomE = (5260.52916 * $dX) + (105.94684 * $dX * $dY) + (2.45656 *          $dX * pow($dY, 2)) + (- 0.81885 * pow(             $dX, 3)) + (0.05594 *          $dX * pow($dY, 3)) + (- 0.05607 * pow(             $dX, 3) * $dY) + (0.01199 *          $dY) + (- 0.00256 * pow($dX, 3) * pow(             $dY, 2)) + (0.00128 *          $dX * pow($dY, 4)) + (0.00022 * pow($dY,             2)) + (- 0.00022 * pow(             $dX, 2)) + (0.00026 *          pow($dX, 5));       $Latitude = 52.15517 + ($SomN / 3600);     $Longitude = 5.387206 + ($SomE / 3600);       return array(         'latitude' => $Latitude ,         'longitude' => $Longitude); }
http://www.god-object.com/2009/10/23/convert-rijksdriehoekscordinaten-to-latitudelongitude/

$x and $y are the locations I need to input the coordinates I assume but other than that I have no idea how it works. :o

To run it I assume I'd be able to upload it to my host, with the corresponding coordinate file, and then start it on there?

Thanks.:)

EDIT: No idea why it shows like that in the php section, it's displayed properly in the link.
 
You have to write a small script to read the original set of coordinates and loop through them and call this function each time to convert it. You could either save the converted coordinates to another file or output them to the web page for you to copy/paste yourself.

To run it you will then just have to visit the page once it's on your host.

Someone could probably post the script up to do it if you gave us an example of the source file with the coordinates in.
 
I assume that is what the function I posted does? I just want to know how to use it/convert it into a useable script.:)

The file would just contain a list of coordinates, so for excel it would just be situated with column A having x coods in and column B with y coords. If a text file then it I guess it could just be X and Y coordinates on the same line with a space, with each set of coordinates on a seperate line, like...

124444 549994
139999 466323
124555 366225
 
I assume that is what the function I posted does? I just want to know how to use it/convert it into a useable script.:)

The file would just contain a list of coordinates, so for excel it would just be situated with column A having x coods in and column B with y coords. If a text file then it I guess it could just be X and Y coordinates on the same line with a space, with each set of coordinates on a seperate line, like...

124444 549994
139999 466323
124555 366225

The function you posted just takes two numbers and makes the conversion, so you'd call it like rd2wgs(12, 22); or something.

You need to make a class that would open the file, and then read each line of the file sequentially. You'd split each line it reads from the file into two numbers (using the space as a delimiter) and then call the rd2wgs function on those two numbers (and repeat until you reach the end of the file).

There are plenty of scripts online that show how you open a file and read it line-by-line, you'd just have to modify them to include your function in it, and also a means of output (either to screen or to another file).
 
The function itself is complete, you now just need to run it on two variables.

e.g.

PHP:
$variable1=data
$variable2=data

function rd2wgs ($variable1, $variable2)

So I guess your question is actually about how to feed your coordinate data into the function.

What format is it currently in? You'll need to create a loop that reads the coordinates into the two variables, runs the function, outputs the data, then rinse and repeat until no more data.
 
Hmmm, thought it would be a lot more complex than I hoped...:(

The format would be what I posted above (or preferably that but in cells in excel), numbers are a minimum of 6 digits long but some have decimal places as well.

Think this is going to take a while, we've already spent a day or two on it and it's not even remotely relevant to the project in hand (need to put this data into a GIS program which is part of the actual project).

I'll have a look for the data input/output php then.:)
 
Back
Top Bottom