Generate a Ruby function to convert UK Ordnance Survey OSGB36 co-ordinates to WGS84 latitude and longitude
Last active
January 27, 2025 07:54
-
-
Save pixeltrix/d0ff87aba59f91bc2d23c01527d26bfe to your computer and use it in GitHub Desktop.
Deepseek responses when asked to generate a function to convert OSGB36 to WGS84 co-ordinates
To convert UK Ordnance Survey OSGB36 coordinates to WGS84 latitude and longitude, you can use the proj
library in Ruby. The proj
library is a wrapper around the PROJ.4 library, which is commonly used for coordinate transformations.
First, you'll need to install the proj
gem:
gem install proj
Here's a Ruby function that performs the conversion:
require 'proj'
def osgb36_to_wgs84(easting, northing)
# Define the CRS (Coordinate Reference System) for OSGB36 and WGS84
osgb36 = Proj::Crs.new('EPSG:27700') # OSGB36 / British National Grid
wgs84 = Proj::Crs.new('EPSG:4326') # WGS84 (latitude and longitude)
# Create a transformation from OSGB36 to WGS84
transformation = Proj::Transformation.new(osgb36, wgs84)
# Perform the transformation
wgs84_coords = transformation.transform(Proj::Point.new(easting, northing))
# Return the latitude and longitude
{ latitude: wgs84_coords.y, longitude: wgs84_coords.x }
end
# Example usage:
easting = 400000
northing = 300000
wgs84_coords = osgb36_to_wgs84(easting, northing)
puts "Latitude: #{wgs84_coords[:latitude]}, Longitude: #{wgs84_coords[:longitude]}"
- OSGB36 (EPSG:27700): This is the coordinate reference system used by the UK Ordnance Survey.
- WGS84 (EPSG:4326): This is the standard coordinate reference system for latitude and longitude used by GPS.
- Transformation: The
Proj::Transformation
object is used to convert coordinates from one CRS to another.
If you run the example with easting = 400000
and northing = 300000
, the output will be the corresponding WGS84 latitude and longitude.
- The
proj
gem requires the PROJ library to be installed on your system. You can install it using your package manager (e.g.,apt-get install proj-bin
on Ubuntu). - The accuracy of the conversion depends on the precision of the transformation parameters used by the PROJ library.
This function should work well for most use cases involving the conversion of OSGB36 coordinates to WGS84 latitude and longitude.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment