Geo-Location & Geo-Referencing In TerraLand

April 25, 2018 TerraLand Tutorials

We need the following values in order to do proper geo-referencing operations in Unity:

(The following 2 values are found in TerraLand Downloader’s “Area Location” section)

  • public double originLatitude; Origin Latitude coordinate of the database & generated terrain area
  • public double originLongitude; Origin Longitude coordinate of the database & generated terrain area

36.87916, -111.5105

TerraLand_AreaLocation.jpg

(The following 2 values are found in TerraLand Downloader’s “Area Size” section)

  • public double areaHeight; Length of the generated area in meters
  • public double areaWidth; Width of the generated area in meters


12000, 12000

TerraLand_AreaSize.jpg

(The following 4 values are found in TerraLand Downloader’s “Terrain Bounds” tab)

  • public double areaTop; Top Latitude coordinate of the generated area
  • public double areaBottom; Bottom Latitude coordinate of the generated area
  • public double areaLeft; Left Longitude coordinate of the generated area
  • public double areaRight; Right Longitude coordinate of the generated area

Top: 36.9330589170472, Bottom: 36.8252610829528, Left: -111.577881816826 & Right: -111.443118183174

TerraLand_AreaBoundaries.jpg

And then we can simply convert any Latitude/Longitude points of interests into Unity’s world space in our scene. Here is the following script to get geo-referenced object position in Unity world space and move it to desired Latitude/Longitude coordinates right in the Editor.

Code (CSharp):
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [ExecuteInEditMode]
  5. public class LatLon2Unity : MonoBehaviour
  6. {
  7.     public double originLatitude;
  8.     public double originLongitude;
  9.     public double areaHeight;
  10.     public double areaWidth;
  11.     public double areaTop;
  12.     public double areaBottom;
  13.     public double areaLeft;
  14.     public double areaRight;
  15.     public bool forceMoveToLatLon = false;
  16.     public double destinationLat;
  17.     public double destinationLon;
  18.     private double currentLatitude;
  19.     private double currentLongitude;
  20.     void Update ()
  21.     {
  22.         if(transform.position.z >= 0)
  23.             currentLatitude = originLatitude + ((areaTop – areaBottom) * (Mathf.InverseLerp(0, (float)areaHeight, transform.position.z)));
  24.         else
  25.             currentLatitude = originLatitude – ((areaTop – areaBottom) * (Mathf.InverseLerp(0, (float)areaHeight, -transform.position.z)));
  26.         if(transform.position.x >= 0)
  27.             currentLongitude = originLongitude + ((areaRight – areaLeft) * (Mathf.InverseLerp(0, (float)areaWidth, transform.position.x)));
  28.         else
  29.             currentLongitude = originLongitude – ((areaRight – areaLeft) * (Mathf.InverseLerp(0, (float)areaWidth, -transform.position.x)));
  30.         print(“Current Lat: ” + currentLatitude + ”   Lon: ” + currentLongitude);
  31.         if(forceMoveToLatLon)
  32.             MoveToLatLon();
  33.     }
  34.     private void MoveToLatLon ()
  35.     {
  36.         transform.position = new Vector3
  37.             (
  38.                 (float)(((destinationLon – areaLeft) / (areaRight – areaLeft)) * (areaWidth)) – (float)(areaWidth / 2f),
  39.                 0,
  40.                 (float)(((destinationLat – areaBottom) / (areaTop – areaBottom)) * (areaHeight)) – (float)(areaHeight / 2f)
  41.             );
  42.     }
  43. }

Following picture shows a geo-referenced object placed exactly at given coordinates of “36.9005219376448, -111.458823759922”. Location on Google Maps

TerraLand_GeoreferencedObject_LatLonToUnityWorldSpace.jpg

Here is the free download of full scene for the above implementation:
terraunity.com/freedownload/TerraLand_LatLon2UnityWorldSpace.unitypackage

FYI, the “TerraLand Terrain” component has built-in coordinates converter between “Minute, Degree, Second” (MDS) to Decimal Degrees (DD) GIS formats and vice versa, so no need to go for online converters such as the one you mentioned.

 

Source: https://forum.unity.com/threads/terraland-2-high-quality-photo-realistic-terrains-from-real-world-gis-data.377858/page-6#post-3084315