I'm using the proj4s javascript library (based on advice from this question I asked previously) to convert lat/long values on my webpage. This page receives the lat/long values from google maps and converts them to the British National Grid (ORD SURV GB), which wikipedia tells me has the following code: EPSG:27700. I want to calculate the maximum and minimum values of both the latitude and longitude, so I can query a database.

My code is as follows:

var source = new Proj4js.Proj('WGS84');    
var dest = new Proj4js.Proj('EPSG:27700');

bounds = map.getBounds()  // Get bounds of map after user pans around   
zoom = map.getZoom()      // Get zoom level

xMax = bounds.getNorthEast().lat()  //get max and min values for lat/long
yMax = bounds.getNorthEast().lng()  
xMin = bounds.getSouthWest().lat()
yMin = bounds.getSouthWest().lng()

alert('xMin: ' + xMin + '\nxMax: ' + xMax)
alert('yMin: ' + yMin + '\nyMax: ' + yMax)          

// transforming point coordinates   
var latXNew = new Proj4js.Point( xMin, xMax );   
var latYNew = new Proj4js.Point( yMin, yMax ); 

Proj4js.transform(source, dest, latXNew);      
Proj4js.transform(source, dest, latYNew);      

However, I'm not getting the correct results from my webpage. I've loaded in a kml file that has a rectangle with boundaries that I know, and the values don't match up. They appear to be off by at least a factor of approximately 8.

NE Lat: 52.25635981528854
NE Lng: 1.3534606328125243

Expected Lat: 500000.0
Expected Lng: 150000.0

Returned Lat: -5527596.989640327 (incorrect)
Returned Lng: 622727.1071581601 (incorrect)

What am I doing incorrectly?

  • Perhaps you have mixed up the latitude and longitude. I would be sure this is the case if your "returned lng" were exactly ten times larger than you report. – whuber Jan 19 '11 at 3:53

Make sure that you're converting the correct value when performing the transformation.

You're creating new Points using the same axis. Try this:

// transforming point coordinates   
var southWestOld = new Proj4js.Point( xMin, yMin );   
var northEastOld = new Proj4js.Point( xMax, yMax ); 

var southWestNew = Proj4js.transform(source, dest, southWestOld);      
var northEastNew = Proj4js.transform(source, dest, northEastOld); 

Also, speaking convention is to say "x,y" or "lat,long" but the x coordinate is usually Easting (longitude) and y is usually Northing (latitude).

If using the right coordinates still does not produce the expected result, try switching lat and long and see if your numbers land where you expect.

For the following code I am not getting the values you expect, but I am getting values that agree with the calculator found here: http://www.nearby.org.uk/tests/GeoTools.html

Proj4js.defs["WGS84"] = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs";
Proj4js.defs["EPSG:27700"] = "+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs";

var source = new Proj4js.Proj('WGS84');    
var dest = new Proj4js.Proj('EPSG:27700');

var testPt = new Proj4js.Point(1.3534606328125,52.25635981528);

Proj4js.transform(source, dest, testPt);

My result coordinate is 628970.515543512,267319.1785250341, and the one from the website is TM 28971 67319.

  • Good point - I was not aware of that. However, I'm still not getting values that are correct even considering that. – djq Jan 18 '11 at 23:14
  • Please add some sample input/output values to the question. – mwalker Jan 18 '11 at 23:28
  • I just added some. – djq Jan 19 '11 at 0:15

are you sure that your source projection is right? Google Maps is usually in epsg:900913.

  • I'm not sure at all, so I will try that. Googling (!) for it has given me a lot of different suggestions. – djq Jan 18 '11 at 20:19
  • EPSG:900913 parameters – Mapperz Jan 18 '11 at 20:35
  • I tried using spatialreference.org/ref/sr-org/6627 and it still is not returning correct values. – djq Jan 18 '11 at 21:05
  • 1
    celenius: you can't just guess! - what do the values of your source bounding box look like? – Ian Turton Jan 18 '11 at 21:09
  • 1
    You need to determine what the input data projection is. If your input data is in OSGB (27700) then you don't need to reproject it. Other wise you need to determine if your data is in 4326 (small numbers) or google (900913) (big numbers), then you need to use one or the other epsg code in your proj4js code. – Ian Turton Jan 18 '11 at 22:04

epsg:2770 in your question and 27770 in your code are 2 different values.



Make sure you're using the right one. From your question it seems that 27770 is the right one to use (second link).

Proj4js.defs["EPSG:27700"] = "+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs";

  • Thanks @Vadim, i have edited my question to include that. You are correct that I wrote the wrong one in my text. – djq Jan 18 '11 at 20:55

EPSG:4326 is built in. Note the order, its [x,y], so [lon,lat]

proj4.defs("EPSG:27700", "+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs");
proj4('EPSG:27700','EPSG:4326').forward( [480069, 186342] ).reverse(); 
// [51.57027349352602, -0.8461130514492281]
proj4('EPSG:27700','EPSG:4326').inverse( [ -0.846, 51.57 ] ); // lon, lat !!! 
// [480077.3157135821, 186311.70711789717]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.