TerraLand Tutorials

Geo-Location & Geo-Referencing In TerraLand

TerraLand

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

Back to list

Related Posts