Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-2025-04-14· OpenAIHugging Face Arena Expert 5K

I need to create a python function that translates from a lat/lng/h(m) ellipsoid to a…

  1. User

    I need to create a python function that translates from a lat/lng/h(m) ellipsoid to a local coordinate system in feet: 65KI 6378137.00000 298.2572229329 D5KI 1.0000000000 0.00000 0.00000 64KI3 45.274211305 -95.011079858 625.03785 1387.69317 1.0000540297
    49KI3 6378137.00000 298.2572229329 0.000000000 0.000000000 0.000000000 0.00000 0.00000 0.00000 0.0000000000 50KI 809.01308 1240.53352 -0.06330 -0.07307 -0.0253147588 0.9999598398 81KI1 625.04704 1387.69545 30.66315 0.0000000000 0.0000000000

  2. Assistant

    Certainly! To translate from latitude, longitude, and ellipsoid height (lat/lon/h) to a local coordinate system in feet, you typically need to:

    1. Convert geodetic coordinates (lat, lon, h) to ECEF (Earth-Centered, Earth-Fixed) XYZ.
    2. Apply a local tangent plane transformation (e.g., ENU: East, North, Up) centered at a reference point.
    3. Apply any additional local transformation (offsets, rotations, scale) as defined by your local system.
    4. Convert meters to feet.

    Your data seems to define:

    • The ellipsoid (WGS84: a=6378137.0, f=1/298.2572229329)
    • A reference point (lat0, lon0, h0)
    • Local system offsets and scale

    Let's write a Python function that does this.
    I'll make some assumptions based on your data and typical workflows.


    Step 1: Convert Geodetic to ECEF

    import numpy as np
    
    def geodetic_to_ecef(lat, lon, h, a=6378137.0, f=1/298.2572229329):
        # Convert degrees to radians
        lat = np.radians(lat)
        lon = np.radians(lon)
        # Ellipsoid constants
        e2 = 2*f - f**2
        N = a / np.sqrt(1 - e2 * np.sin(lat)**2)
        X = (N + h) * np.cos(lat) * np.cos(lon)
        Y = (N + h) * np.cos(lat) * np.sin(lon)
        Z = (N * (1 - e2) + h) * np.sin(lat)
        return np.array([X, Y, Z])
    

    Step 2: ECEF to Local ENU

    def ecef_to_enu(X, Y, Z, lat0, lon0, h0, a=6378137.0, f=1/298.2572229329):
        # Reference point in ECEF
        X0, Y0, Z0 = geodetic_to_ecef(lat0, lon0, h0, a, f)
        # Differences
        dX = X - X0
        dY = Y - Y0
        dZ = Z - Z0
        # Convert degrees to radians
        lat0 = np.radians(lat0)
        lon0 = np.radians(lon0)
        # ENU rotation matrix
        R = np.array([
            [-np.sin(lon0),              np.cos(lon0),             0],
            [-np.sin(lat0)*np.cos(lon0), -np.sin(lat0)*np.sin(lon0), np.cos(lat0)],
            [np.cos(lat0)*np.cos(lon0),  np.cos(lat0)*np.sin(lon0), np.sin(lat0)]
        ])
        enu = R @ np.array([dX, dY, dZ])
        return enu
    

    Step 3: Apply Local Offsets, Scale, and Convert to Feet

    Assume you have local offsets and scale from your data (e.g., from 50KI and 64KI3 lines).

    def enu_to_local(enu, offset_e, offset_n, offset_u, scale=1.0):
        # Apply scale and offsets
        local_e = enu[0] * scale + offset_e
        local_n = enu[1] * scale + offset_n
        local_u = enu[2] * scale + offset_u
        return np.array([local_e, local_n, local_u])
    

    Step 4: Meters to Feet

    def meters_to_feet(x):
        return x * 3.280839895
    

    Step 5: Full Pipeline Function

    def latlonh_to_local_feet(lat, lon, h,
                              lat0, lon0, h0,
                              offset_e, offset_n, offset_u,
                              scale=1.0,
                              a=6378137.0, f=1/298.2572229329):
        # Step 1: Geodetic to ECEF
        X, Y, Z = geodetic_to_ecef(lat, lon, h, a, f)
        # Step 2: ECEF to ENU
        enu = ecef_to_enu(X, Y, Z, lat0, lon0, h0, a, f)
        # Step 3: Apply local system
        local = enu_to_local(enu, offset_e, offset_n, offset_u, scale)
        # Step 4: Convert to feet
        local_ft = meters_to_feet(local)
        return local_ft
    

    Example Usage

    Suppose your reference point is:

    • lat0 = 45.274211305
    • lon0 = -95.011079858
    • h0 = 0.0 (or as per your data)
    • Offsets and scale from your data (e.g., offset_e = 625.03785, offset_n = 1387.69317, scale = 1.0000540297)
    lat = 45.275  # Example input
    lon = -95.012
    h = 350
    
    # Reference and local system parameters
    lat0 = 45.274211305
    lon0 = -95.011079858
    h0 = 0.0
    offset_e = 625.03785
    offset_n = 1387.69317
    offset_u = 0.0
    scale = 1.0000540297
    
    local_coords_ft = latlonh_to_local_feet(lat, lon, h, lat0, lon0, h0, offset_e, offset_n, offset_u, scale)
    print("Local coordinates (feet):", local_coords_ft)
    

    If you have more specific transformation parameters (rotations, more complex offsets), please provide them, and I can adjust the function accordingly!
    Let me know if you need the code to parse your data file or further clarify the transformation steps.

  3. User

    I also need to do the inverse calculation.

  4. Assistant

    Absolutely! To invert the process (i.e., go from local coordinates in feet back to latitude, longitude, and ellipsoid height), you need to:

    1. Convert feet to meters.
    2. Reverse the local transformation (remove offsets, apply inverse scale).
    3. Convert local ENU to ECEF.
    4. Convert ECEF to geodetic (lat, lon, h).

    Below is a step-by-step implementation, matching the forward process.


    1. Feet to Meters

    def feet_to_meters(x):
        return x / 3.280839895
    

    2. Reverse Local Transformation

    def local_to_enu(local, offset_e, offset_n, offset_u, scale=1.0):
        # Remove offsets and scale
        enu_e = (local[0] - offset_e) / scale
        enu_n = (local[1] - offset_n) / scale
        enu_u = (local[2] - offset_u) / scale
        return np.array([enu_e, enu_n, enu_u])
    

    3. ENU to ECEF

    def enu_to_ecef(enu, lat0, lon0, h0, a=6378137.0, f=1/298.2572229329):
        # Reference point in ECEF
        X0, Y0, Z0 = geodetic_to_ecef(lat0, lon0, h0, a, f)
        # Convert degrees to radians
        lat0 = np.radians(lat0)
        lon0 = np.radians(lon0)
        # ENU to ECEF rotation matrix (transpose of ECEF to ENU)
        R = np.array([
            [-np.sin(lon0), -np.sin(lat0)*np.cos(lon0),  np.cos(lat0)*np.cos(lon0)],
            [ np.cos(lon0), -np.sin(lat0)*np.sin(lon0),  np.cos(lat0)*np.sin(lon0)],
            [           0,              np.cos(lat0),              np.sin(lat0)]
        ])
        dX, dY, dZ = R @ enu
        X = X0 + dX
        Y = Y0 + dY
        Z = Z0 + dZ
        return np.array([X, Y, Z])
    

    4. ECEF to Geodetic

    This is a standard but non-trivial calculation. Here’s a robust implementation:

    def ecef_to_geodetic(X, Y, Z, a=6378137.0, f=1/298.2572229329):
        # WGS-84 ellipsoid parameters
        b = a * (1 - f)
        e2 = 2*f - f**2
        ep2 = (a**2 - b**2) / b**2
    
        p = np.sqrt(X**2 + Y**2)
        theta = np.arctan2(Z * a, p * b)
        lon = np.arctan2(Y, X)
        lat = np.arctan2(Z + ep2 * b * np.sin(theta)**3,
                         p - e2 * a * np.cos(theta)**3)
        N = a / np.sqrt(1 - e2 * np.sin(lat)**2)
        h = p / np.cos(lat) - N
    
        # Convert radians to degrees
        lat = np.degrees(lat)
        lon = np.degrees(lon)
        return lat, lon, h
    

    5. Full Inverse Pipeline

    def local_feet_to_latlonh(local_ft,
                              lat0, lon0, h0,
                              offset_e, offset_n, offset_u,
                              scale=1.0,
                              a=6378137.0, f=1/298.2572229329):
        # Step 1: Feet to meters
        local_m = feet_to_meters(np.array(local_ft))
        # Step 2: Reverse local transformation
        enu = local_to_enu(local_m, offset_e, offset_n, offset_u, scale)
        # Step 3: ENU to ECEF
        X, Y, Z = enu_to_ecef(enu, lat0, lon0, h0, a, f)
        # Step 4: ECEF to geodetic
        lat, lon, h = ecef_to_geodetic(X, Y, Z, a, f)
        return lat, lon, h
    

    Example Usage

    # Example local coordinates in feet
    local_ft = [1000, 2000, 50]
    
    # Reference and local system parameters (same as before)
    lat0 = 45.274211305
    lon0 = -95.011079858
    h0 = 0.0
    offset_e = 625.03785
    offset_n = 1387.69317
    offset_u = 0.0
    scale = 1.0000540297
    
    lat, lon, h = local_feet_to_latlonh(local_ft, lat0, lon0, h0, offset_e, offset_n, offset_u, scale)
    print("Latitude, Longitude, Height:", lat, lon, h)
    

    Let me know if you need this as a single script, or if you have more specific transformation parameters (e.g., rotations, more complex scale factors)!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.