[Proj] Using pj_transform API to convert WGS84 to OSGB

Keith Sharp kms at passback.co.uk
Fri Jan 20 01:53:46 PST 2006


Hello,

I am trying to use the Proj API to convert WGS84 values to OSGB values
such that I can eventually create a Landranger Grid Reference.

Using the cs2cs command and some values taken from a previous posting on
the list I can get the result I want:

[kms at animal include]$ /home/kms/tmp/install/bin/cs2cs +proj=latlong +ellps=WGS84 +towgs84=0,0,0 +nodefs +to +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894 +units=m
-4.432298 55.863157
247878.12       666006.03 -53.39

(Glasgow Airport in the UK).

I then tried to code this up using the API:

#include <stdio.h>
#include <proj_api.h>

int
main (int argc, char *argv[])
{
	char *wgs84[] = { "proj=latlong", "ellps=WGS84", "towgs84=0,0,0", "nodef" };

	char *osgb[] = { "proj=tmerc", "lat_0=49", "lon_0=-2", "k=0.9996012717", "x_0=400000", "y_0=-100000", "ellps=airy", "towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894", "units=m" };


	projPJ wgs84PJ;
	projPJ osgbPJ;

	double x = 55.863157, y = -4.432298, z = 0;
	int e = 0;

	if (!(wgs84PJ = pj_init (4, wgs84))) {
		printf ("Failed to initialise WGS84\n");
		return 1;
	}

	if (!(osgbPJ = pj_init (2, osgb))) {
		printf ("Failed to initialise OSGB\n");
		return 1;
	}

	printf ("Converting from WGS84 to OSGB\n");
	printf ("X: %e  Y: %e  Z: %e\n", x, y, z);
	if ((e = pj_transform (wgs84PJ, osgbPJ, 1, 0, &y, &x, &z)) != 0) {
		printf ("Transform failed: %s\n", pj_strerrno (e));
		return e;
	}
	printf ("X: %e  Y: %e  Z: %e\n", x, y, z);
	printf ("Expected result was X: 247878 and Y: 666006\n");

	return 0;
}

But when I run my test program I get the following:

[kms at animal test]$ ./test
Converting from WGS84 to OSGB
X: 5.586316e+01  Y: -4.432298e+00  Z: 0.000000e+00
Transform failed: latitude or longitude exceeded limits

I have to confess that I am VERY new to this type of thing, so I may
well be making a number of fundamental mistakes.  If I am could someone
point me on the correct path?

Many thanks,

Keith.




More information about the Proj mailing list