Skip to content

Instantly share code, notes, and snippets.

@pixeltrix
Last active January 27, 2025 07:54
Show Gist options
  • Select an option

  • Save pixeltrix/d0ff87aba59f91bc2d23c01527d26bfe to your computer and use it in GitHub Desktop.

Select an option

Save pixeltrix/d0ff87aba59f91bc2d23c01527d26bfe to your computer and use it in GitHub Desktop.

Revisions

  1. pixeltrix revised this gist Jan 27, 2025. No changes.
  2. pixeltrix created this gist Jan 27, 2025.
    1 change: 1 addition & 0 deletions 01.prompt.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Generate a Ruby function to convert UK Ordnance Survey OSGB36 co-ordinates to WGS84 latitude and longitude
    48 changes: 48 additions & 0 deletions 02.responses.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    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:

    ```bash
    gem install proj
    ```

    Here's a Ruby function that performs the conversion:

    ```ruby
    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]}"
    ```

    ### Explanation:
    - **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.

    ### Example Output:
    If you run the example with `easting = 400000` and `northing = 300000`, the output will be the corresponding WGS84 latitude and longitude.

    ### Note:
    - 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.