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!
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.
-
Thanks for your help. This seems to be the best way to go, and I've found a MapServer document (mapserver.org/MapServer.pdf) which has a section on PHP MapScript installation and some examples of how to use it. I've got just a basic knowledge of PHP so far, and have in my head what I want to do, so I've worked through a few exercises and have read a couple of books to make sure PHP and mySQL are able to do what I need. It looks good so far, and MapScript looks like just the thing. Thanks for your help - I might ask you for some specific help when I get into it a little bit more. – Reg H Dec 21 '09 at 15:52
-
Update - with my hosting setup, it's looking like it might be a problem to add the php_mapscript.dll. I got an error when I used the command "dl('php_mapscript.dll');". The error was: "Warning: dl() [function.dl]: Not supported in multithreaded Web servers - use extension=php_mapscript.dll in your php.ini in.......", so they're seeing if they can change the PHP.ini file on their server. – Reg H Dec 21 '09 at 21:16
-
Be aware that the functions regarding the PROJ4 functionality are not included in the mapscript library (php_mapscript.dll), but in php_proj.dll. It is a different library that you must compile yourself from php_proj.c. I only have experience compiling it under Linux, but maybe you can try on Windows. – amercader Dec 21 '09 at 21:53
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.
-
proj4php is very helpful, I've tested it, the result is quite precise. – Fedir RYKHTIK Sep 3 '13 at 14:53
-
Can you run ArcGIS Server? ESRI has a new service called a Geometry service that lets you do geometry manipulation/conversion/etc through a variety of service interfaces.
You can find a sample version at http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer that you can test with.