Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
    
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

3 Answers 3

up vote 2 down vote accepted

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.

share|improve this answer
    
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.

share|improve this answer
    
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? –  iant 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. –  iant Jan 18 '11 at 22:04

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

http://spatialreference.org/ref/epsg/2770/

http://spatialreference.org/ref/epsg/27700/

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";

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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