Search Unity

GPS Encoder by Michael Taylor Question [Solved]

Discussion in 'Scripting' started by nathanjams, Aug 21, 2017.

  1. nathanjams

    nathanjams

    Joined:
    Jul 27, 2016
    Posts:
    304
    Hello,

    I've been looking for a way to have game objects positioned dynamically on start. I would like to give GPS coordinates to the objects and have them set accordingly to XYZ positions in unity.

    I found MIcahel Taylor's GPSencoder script here https://github.com/MichaelTaylor3D/UnityGPSConverter but can not figure out how to access it's functions. The script has no public variables so I'm assuming that these need to be set through other scripts. However, I can't figure out how to access the functions I want.

    Here is the documentation:

    It's primarily
    and
    (which i assume that I can set by adding localOrigin = new Vector2 (90.0f, -46.0f) I'm interesting in using/accessing.

    As usual, I feel like there is something obvious that I am missing, or are simply ignorant to. Does anyone have any suggestions on how I could go about this?

    Thanks for your time,
    Nathan

    And, here's the code:

    Code (CSharp):
    1. //Copyright 2013 MichaelTaylor3D
    2. //www.michaeltaylor3d.com
    3.  
    4. using UnityEngine;
    5.  
    6. public sealed class GPSEncoder : MonoBehaviour {
    7.  
    8.     /////////////////////////////////////////////////
    9.     //////-------------Public API--------------//////
    10.     /////////////////////////////////////////////////
    11.  
    12.     /// <summary>
    13.     /// Convert UCS (X,Y,Z) coordinates to GPS (Lat, Lon) coordinates
    14.     /// </summary>
    15.     /// <returns>
    16.     /// Returns Vector2 containing Latitude and Longitude
    17.     /// </returns>
    18.     /// <param name='position'>
    19.     /// (X,Y,Z) Position Parameter
    20.     /// </param>
    21.     public static Vector2 USCToGPS(Vector3 position)
    22.     {
    23.         return GetInstance().ConvertUCStoGPS(position);
    24.     }
    25.  
    26.     /// <summary>
    27.     /// Convert GPS (Lat, Lon) coordinates to UCS (X,Y,Z) coordinates
    28.     /// </summary>
    29.     /// <returns>
    30.     /// Returns a Vector3 containing (X, Y, Z)
    31.     /// </returns>
    32.     /// <param name='gps'>
    33.     /// (Lat, Lon) as Vector2
    34.     /// </param>
    35.     public static Vector3 GPSToUCS(Vector2 gps)
    36.     {
    37.         return GetInstance().ConvertGPStoUCS(gps);
    38.     }
    39.  
    40.     /// <summary>
    41.     /// Convert GPS (Lat, Lon) coordinates to UCS (X,Y,Z) coordinates
    42.     /// </summary>
    43.     /// <returns>
    44.     /// Returns a Vector3 containing (X, Y, Z)
    45.     /// </returns>
    46.     public static Vector3 GPSToUCS(float latitude, float longitude)
    47.     {
    48.         return GetInstance().ConvertGPStoUCS(new Vector2(latitude,longitude));
    49.     }
    50.  
    51.     /// <summary>
    52.     /// Change the relative GPS offset (Lat, Lon), Default (0,0),
    53.     /// used to bring a local area to (0,0,0) in UCS coordinate system
    54.     /// </summary>
    55.     /// <param name='localOrigin'>
    56.     /// Referance point.
    57.     /// </param>
    58.     public static void SetLocalOrigin(Vector2 localOrigin)
    59.     {
    60.         GetInstance()._localOrigin = localOrigin;
    61.     }
    62.      
    63.     /////////////////////////////////////////////////
    64.     //////---------Instance Members------------//////
    65.     /////////////////////////////////////////////////
    66.  
    67.     #region Singleton
    68.     private static GPSEncoder _singleton;
    69.  
    70.     private GPSEncoder()
    71.     {
    72.      
    73.     }
    74.  
    75.     private static GPSEncoder GetInstance()
    76.     {
    77.         if(_singleton == null)
    78.         {
    79.             _singleton = new GPSEncoder();
    80.         }
    81.         return _singleton;
    82.     }
    83.     #endregion
    84.  
    85.     #region Instance Variables
    86.     private Vector2 _localOrigin = Vector2.zero;
    87.     private float _LatOrigin { get{ return _localOrigin.x; }}
    88.     private float _LonOrigin { get{ return _localOrigin.y; }}
    89.  
    90.     private float metersPerLat;
    91.     private float metersPerLon;
    92.     #endregion
    93.  
    94.     #region Instance Functions
    95.     private void FindMetersPerLat(float lat) // Compute lengths of degrees
    96.     {
    97.         // Set up "Constants"
    98.         float m1 = 111132.92f;    // latitude calculation term 1
    99.         float m2 = -559.82f;        // latitude calculation term 2
    100.         float m3 = 1.175f;      // latitude calculation term 3
    101.         float m4 = -0.0023f;        // latitude calculation term 4
    102.         float p1 = 111412.84f;    // longitude calculation term 1
    103.         float p2 = -93.5f;      // longitude calculation term 2
    104.         float p3 = 0.118f;      // longitude calculation term 3
    105.      
    106.         lat = lat * Mathf.Deg2Rad;
    107.  
    108.         // Calculate the length of a degree of latitude and longitude in meters
    109.         metersPerLat = m1 + (m2 * Mathf.Cos(2 * (float)lat)) + (m3 * Mathf.Cos(4 * (float)lat)) + (m4 * Mathf.Cos(6 * (float)lat));
    110.         metersPerLon = (p1 * Mathf.Cos((float)lat)) + (p2 * Mathf.Cos(3 * (float)lat)) + (p3 * Mathf.Cos(5 * (float)lat));    
    111.     }
    112.  
    113.     private Vector3 ConvertGPStoUCS(Vector2 gps)
    114.     {
    115.         FindMetersPerLat(_LatOrigin);
    116.         float zPosition  = metersPerLat * (gps.x - _LatOrigin); //Calc current lat
    117.         float xPosition  = metersPerLon * (gps.y - _LonOrigin); //Calc current lat
    118.         return new Vector3((float)xPosition, 0, (float)zPosition);
    119.     }
    120.  
    121.     private Vector2 ConvertUCStoGPS(Vector3 position)
    122.     {
    123.         FindMetersPerLat(_LatOrigin);
    124.         Vector2 geoLocation = new Vector2(0,0);
    125.         geoLocation.x = (_LatOrigin + (position.z)/metersPerLat); //Calc current lat
    126.         geoLocation.y = (_LonOrigin + (position.x)/metersPerLon); //Calc current lon
    127.         return geoLocation;
    128.     }
    129.     #endregion
    130. }
    131.  
     
    Last edited: Aug 21, 2017
  2. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    268
    Code (CSharp):
    1. GPSEncoder.USCToGPS(yourVectorHere);
    I don't think I'm missing anything...
     
    nathanjams likes this.
  3. nathanjams

    nathanjams

    Joined:
    Jul 27, 2016
    Posts:
    304
    Thanks @Scabbage, I thought it had to be something simple.

    It was this line that was causing me the problem:

    Code (CSharp):
    1. if(_singleton.Equals(null))
    I needed to change it to

    Code (CSharp):
    1.  if (_singleton == null)
    Also, if anyone is interested, its coding is slightly different than i'm used to so referencing it was a little different. but this works:

    Code (CSharp):
    1.  private void Start()
    2.     {
    3.         local = new Vector2(_lat, _long);
    4.         GPSEncoder.SetLocalOrigin(local);
    5.     }
    6.  
    7.     public void Update()
    8.     {
    9.         Vector3 unityLocal = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
    10.  
    11.         local = GPSEncoder.USCToGPS(unityLocal);
    12.  
    13. }