Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIS spatial functions in PHP?

Tags:

php

gis

I need to convert data points from one geographic projection (Lat Long, Mercator, UTM) to another and I wonder if there's a set of PHP tools or functions that can do this? I tried writing one myself based on formulas I found, but it wasn't accurate enough and I can't find better formulas anywhere, so I was wondering if there might be some prepackaged functions somewhere. Failing that, what about something like PROJ.4? Thanks!

like image 384
Reg H Avatar asked Dec 21 '09 12:12

Reg H


2 Answers

There is a PHP module of Proj4 available in the MapServer/MapScript distribution. I think it is mantanied by DM Solutions, but I could not find any documentation online. To check the available functions, I had to look at the source code.

Anyway, this is how you can tranform coordinates between projections:

<?php

    //UTM zone 31N
    $projDefSrc = array("proj=utm","zone=31","ellps=intl","units=m","no_defs");
    $pjSrc = pj_init($projDefSrc);

    //WGS84
    $projDefDest = array("proj=longlat","ellps=WGS84","datum=WGS84","no_defs");
    $pjDest = pj_init($projDefDest);


    $x = 446423;
    $y = 4610005;

    $test = pj_transform($pjSrc,$pjDest,$x,$y);

    //Outputs: Array ( [u] => 2.3567240656 [v] => 41.6384346565 ) 
    print_r($test);

?>

If you want to go this way, you will have to compile php_proj.c from the Mapserver source code folder (mapserver-X.X.X/mapscript/php3) and load the extension in PHP. As I said before, there is no documentation online, so let me know if you find any problems.

Hope this helps.

like image 76
amercader Avatar answered Nov 19 '22 23:11

amercader


You can use the api proj4php that I have translated from proj4js and is available here : https://sourceforge.net/projects/proj4php/

It works great from WGS84 to Lambert93, but need some fix to work with the others projections. I can help.

Bye.

like image 1
julien2512 Avatar answered Nov 20 '22 00:11

julien2512