Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I usually post in StackOverFlow - if this is the incorrect area to post - I apologise.

I have trying to convert a lot of northings / eastings to lat lng

I have code below working: In ruby using this gem

 class PostCodeNode < Node
  require 'ffi-proj4'
   attr_accessor :postcode, :sector, :district

  def self.from_northerns_and_eastings(northing, easting, postcode)
     srcPoint =, northing)
     srcProj ='+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')
     dstProj ='+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
     dstPoint = srcProj.transform(dstProj, easting, northing, 0)
     lat, lon = to_degrees(dstPoint.x), to_degrees(dstPoint.y)
     new lat, lon, postcode

   def initialize(lat, lon, postcode)
     @postcode = postcode.gsub(/\s+/, '')
     @sector   = if @postcode.length == 7 then @postcode[0..4] else @postcode[0..3] end
     @district = @postcode[0..2]
     super(lat, lon)

However, the lat / lon that I am out putting end up in the middle of the sea! So I know I am doing something very wrong!

     dstPoint = srcProj.transform(dstProj, easting, northing, 0)

This line in particular is where the "magic" happens! In the test library on the git repo the test specifies some unknown numbers to me, I am not sure if I have my input data correct!

def setup
    @proj_wgs84 =["init=epsg:4326"])
    @proj_gk    =["init=epsg:31467"])
    @proj_merc  =["proj=merc"])
    @lon =  8.4293092923
    @lat = 48.9896114523
    @rw = 3458305
    @hw = 5428192
    @zw = -5.1790915237

  def test_gk_to_wgs84
    to = @proj_gk.transform(@proj_wgs84, @rw, @hw, @zw)

    assert_in_delta @lon, to.x * Proj4::RAD_TO_DEG, TOLERANCE
    assert_in_delta @lat, to.y * Proj4::RAD_TO_DEG, TOLERANCE
    assert_in_delta 0, to.z, TOLERANCE

  def test_wgs84_to_gk
    point = @proj_wgs84.transform(
      @lon * Proj4::DEG_TO_RAD,
      @lat * Proj4::DEG_TO_RAD,
    assert_equal @rw, point.x.round
    assert_equal @hw, point.y.round
    assert_in_delta @zw, point.z, TOLERANCE

There is no documentation with this ruby gem so I am doing a little bit of guess work, but I think I am nearly there.

Can someone see my error, so I can return the right lon/lat.

I am writing this in Jruby so any better Java Libraries could also be used :) Thanks!

share|improve this question

Eventually I used Jcoord to do this..

For anyone interested in how to do this in Jruby I did it like this:

  require 'java'
  require 'jcoord-1.1-b.jar'
  java_import ""
  java_import ""
  java_import ""
  latlng =,northing.to_f).toLatLng
  lat, lon =, latlng.lng


share|improve this answer

Your Answer


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.