Showing posts with label proj. Show all posts
Showing posts with label proj. Show all posts

2011-02-28

Converting WKT projection info to Proj4 (and back) - the PHP edition

As noted in a previous post, it is fairly easy to utilize the language bindings for the GDAL project to work with projections. However, if you're working in PHP, the corresponding GDAL bindings are not maintained and currently do not work as-is from the GDAL project. As-of GDAL 1.8.0, a bit of tinkering will get the PHP modules to compile, but they still do not work as expected.

However, you can get the php_osr.so module to work with some basic functions using the patch posted at http://pastebin.com/SXKKfe6v. Save this to a file, and apply the patch to the gdal 1.8.0 source like so:

cd /path/to/gdal/src
 patch -p0 < /path/to/patch
Then build and install the gdal code as follows:
./configure --with-php
 make
 make install
 sudo cp swig/php/php_osr.so /usr/lib/php5/20090626/
 sudo su -c \
  'echo extension=php_osr.so > /etc/php5/conf.d/osr.ini'
 sudo service apache2 restart
The above assumes: - You're doing this on a typical/recent Ubuntu installation - You already have Apache and PHP configured - You already have all the required dependencies for building gdal and the swig bindings installed (if not, run "
sudo apt-get build-dep libgdal-1.x.x
", and also you may need to install php5-devel and php5-cli packages). Once you've got gdal and the php_osr.so module compiled and installed, try the following PHP script:
<?php
 include("/path/to/gdal/src/swig/php/osr.php");
 $sr = new SpatialReference();
 $sr->ImportFromProj4('+init=epsg:4326');
 echo $sr->ExportToProj4();
?>
The result from that script should be the following Proj4 string:
+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs

Clearly, this is not as good as having all of the GDAL/OGR libraries accessible from PHP, but it's a start. You can review the swig/php/osr.php file to see what objects/functions are available through the php_osr.so module.

2010-11-21

Converting WKT projection info to Proj4 (and back)

I've had many occasions where I've had WKT-style projection text (e.g., the contents of the *.prj files that normally accompany ESRI Shapefiles, or the srtext field in the spatial_ref_sys table in PostGIS), and I needed to get that to an equivalent Proj4-compatible projection string (and vice versa).  I've known for some time there had to be an automated way to do this...as the GDAL/OGR libraries have been doing this internally for some time.  I ran into the issue again today, so I decided to poke around a bit, and found the API functions that are available to do this...I wrote my first ever Python scripts that use the GDAL/OGR python bindings (specifically, OSR).  These scripts simply convert WKT projection text to Proj4 (wkt2proj.py) and back (proj2wkt.py).  Maybe there are some better examples elsewhere...but I didn't quite find what I was looking for.  Since my work is mostly in PHP right now, I'd love to see the GDAL/OGR PHP bindings maintained someday, but this will do for now:

================== wkt2proj.py =======================
#!/usr/bin/env python

import os
import sys
import string
import osgeo.osr

if (len(sys.argv) <> 2):
        print 'Usage: wkt2proj.py [WKT Projection Text]'
else:
        srs = osgeo.osr.SpatialReference()
        srs.ImportFromWkt(sys.argv[1])
        print srs.ExportToProj4()
======================================================

================== proj2wkt.py =======================
#!/usr/bin/env python

import os
import sys
import string
import osgeo.osr

if (len(sys.argv) <> 2):
        print 'Usage: proj2wkt.py [Proj4 Projection Text]'
else:
        srs = osgeo.osr.SpatialReference()
        srs.ImportFromProj4(sys.argv[1])
        print srs.ExportToWkt()
======================================================

On Ubuntu, provided you have the python-gdal package installed, the above files executed by python will accept the first argument as a wkt/proj projection string, and return the proj/wkt equivalent.  For proj2wkt.py, you can also take advantage of known EPSG numbers.  For example, the WKT for the Spherical Mercator projection used by most free online mapping services (e.g., Google Maps, OpenStreetMap, etc) will be returned by: proj2wkt.py '+init=epsg:3857'

I haven't tested this on Windows - but as long as the Proj library is installed and the GDAL Python modules are installed and accessible to the Python environment, I imagine they will work the same.