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 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 https://github.com/dark-panda/ffi-proj4

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

  def self.from_northerns_and_eastings(northing, easting, postcode)
     srcPoint = Proj4::Point.new(easting, northing)
     srcProj = Proj4::Projection.new('+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 = Proj4::Projection.new('+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
  end

   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)
   end
  end
end

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! https://github.com/dark-panda/ffi-proj4/blob/master/test/transformation_tests.rb

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

  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
  end

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

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
add comment

1 Answer

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 "uk.me.jstott.jcoord.CoordinateSystem"
  java_import "uk.me.jstott.jcoord.LatLng"
  java_import "uk.me.jstott.jcoord.OSRef"
  latlng = OSRef.new(easting.to_f,northing.to_f).toLatLng
  lat, lon = latlng.lat, latlng.lng

Thanks

share|improve this answer
add comment

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.