# Georeferencing in UNIGINE ## Why Georeferencing Is Needed **Georeferencing makes it possible to convert GPS coordinates (latitude, longitude, and altitude), as shown on a phone or navigator, into the engine's world coordinates (X, Y, Z) and back.** ![](image_01.jpg) Terrain georeferencing establishes a link between a digital surface model and the Earth's coordinate system or a selected local reference system. It is needed both for real-world terrain created from geodata and for synthetic relief generated or modeled manually. For real-world terrain, georeferencing makes it possible to correctly match elevations, textures, objects, roads, airfields, and navigation data with their actual positions on Earth. For synthetic terrain, it defines the virtual world's conventional position, scale, and orientation so that the same simulator subsystems can use it. With georeferencing, the simulator can determine an aircraft's position relative to the surface, calculate its height above terrain, align different scene layers, and ensure consistent operation of navigation, sensors, routes, and external systems. **In addition to GPS, users work with coordinates in various map projections (EPSG). For example, in some projections object coordinates are specified not in degrees, but in the meters of a particular projection. Such objects also need to be placed in the scene without manual recalculation.** ## Basic Concepts ### Coordinate Systems | System | Description | |---|---| | WGS-84 (GPS) | A global geodetic system: latitude, longitude, and altitude. Used in GPS navigators. | | EPSG | Standardized map projections. Each has a code (for example, EPSG:3857 - Web Mercator). | | WKT | Well-Known Text - a text description of an arbitrary projection. It makes it possible to define any custom coordinate system that is not registered in EPSG. | | Geocentric | The coordinate origin is at the center of the Earth. Used for calculating missile trajectories, orbital mechanics, and similar tasks. | | World (UNIGINE) | The engine's internal coordinates (X, Y, Z). Convention: Y = North, X = East | UNIGINE works **only with projected coordinate systems**. It does not work with geocentric systems, but [conversion from geocentric coordinates to geodetic coordinates](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#geocentricToGeodetic_dvec3_double_double_dvec3) is available if necessary. For more information on choosing a suitable projected coordinate system, see [here](../../editor2/sandworm/faq/index.md#projection). ### Origin Origin (0, 0, 0) is the starting point that links the local scene coordinates X, Y, Z with geodetic coordinates: latitude, longitude, and altitude. Object positions in the scene are specified relative to the origin. This allows the engine to work with convenient coordinates in meters and, when necessary, convert them to geographic coordinates and back. The origin is usually selected near the main area of the scene: an airfield, training ground, route, or terrain center. The farther objects are from the origin, the more noticeable errors related to the Earth's curvature, projection, and calculation precision may become. For example, when projecting Earth data onto a flat surface with the coordinate origin in the center, we can see how the position is distorted toward the edges of the projection: ![](earth_projection_0_0_0.jpg) *Origin at (0, 0, 0)* In real-world terrain, the origin comes from geodata; in a synthetic scene, it can be set manually. ### Axis Direction UNIGINE uses the following convention: Y = North, X = East. In many EPSG projections, the axes are rotated differently. The most common option is X = North, Y = West. This must be taken into account when converting coordinates. ![](image_02.jpg) In Sandworm, the *[Coordinate System](../../editor2/sandworm/generation/projection/index.md#coordinate_system)* parameter controls axis direction: - *Unigine World* - forcibly rotates the axes to the UNIGINE convention (Y=North, X=East). - *Source* - preserves the original axis orientation of the projection. ### Terrain Types - ***[Landscape Terrain](../../objects/objects/terrain/landscape_terrain/index.md)*** - a flat terrain generated by [Sandworm](../../editor2/sandworm/index.md) with an EPSG projection. It does not support Earth curvature when using [Geodetic Pivot](../../objects/geodetics/geodeticpivot/index.md). - ***[Terrain Global](../../objects/objects/terrain/terrain_global/index.md)*** - supports Earth curvature (curved mode) and can work both with EPSG and with [Geodetic Pivot](../../objects/geodetics/geodeticpivot/index.md). ### Reference Types There are two fundamentally different ways to reference a scene: - [Geodetic Pivot](../../objects/geodetics/geodeticpivot/index.md) - a special object that allows certain types of nodes to be placed in the scene according to their geocoordinates and also bends them along the ellipsoid, simulating the curvature of the Earth's surface. - Sandworm tags (EPSG or WKT) ## Georeferencing Options in UNIGINE There are three different scene georeferencing options: | | EPSG | WKT | Geodetic Pivot | |---|---|---|---| | Used with: | Terrain Global Landscape Terrain | Terrain Global Landscape Terrain | Terrain Global | | Defined via: | EPSG code (for example, 3857) | WKT string (text description of the projection) | A GeodeticPivot node placed in the scene | | Origin: | Calculated automatically or set manually (the "*[Calculate origin automatically](../../editor2/sandworm/generation/export_area/index.md#calculate_origin)*" checkbox in Sandworm) | Set manually in the WKT string | Pivot coordinates in the geodetic system (latitude, longitude, altitude) | | Feature: | World coordinates are shifted relative to the projection coordinates by the Origin value | Sandworm cannot generate terrain directly in WKT, but the [Sandworm Projection](../../editor2/sandworm/generation/projection/index.md) field supports this value. | - Curved / Flat: the terrain under the pivot can be curved or flat, which affects the conversion method - Up axis: a separate setting - Geocentric Normal or Geodetic Normal | | Use case: | Suitable for most simulators where horizon curvature does not need to be taken into account | In practice, useful for customers who need to work with their own modified projection | A synthetic area for a drone simulator, referenced to an arbitrary point on the globe | ## Projection Data Storage All projection information is stored in the UNIGINE project `*.world` file as follows: ```xml source 3857 41.894535731898102 12.469390982746518 0 PROJCRS["WGS 84 / Pseudo-Mercator",BASEGEOGCRS["WGS 84",DATUM["World Geodetic System 1984",ELLIPSOID["WGS 84",6378137,298.257223563,LENGTHUNIT["metre",1]]],PRIMEM["Greenwich",0,ANGLEUNIT["degree",0.0174532925199433]],ID["EPSG",4326]],CONVERSION["Popular Visualisation Pseudo-Mercator",METHOD["Popular Visualisation Pseudo Mercator",ID["EPSG",1024]],PARAMETER["Latitude of natural origin",0,ANGLEUNIT["degree",0.0174532925199433],ID["EPSG",8801]],PARAMETER["Longitude of natural origin",0,ANGLEUNIT["degree",0.0174532925199433],ID["EPSG",8802]],PARAMETER["False easting",0,LENGTHUNIT["metre",1],ID["EPSG",8806]],PARAMETER["False northing",0,LENGTHUNIT["metre",1],ID["EPSG",8807]]],CS[Cartesian,2],AXIS["easting (X)",east,ORDER[1],LENGTHUNIT["metre",1]],AXIS["northing (Y)",north,ORDER[2],LENGTHUNIT["metre",1]],USAGE[SCOPE["Web mapping and visualisation."],AREA["World between 85.06�S and 85.06�N."],BBOX[-85.06,-180,85.06,180]],ID["EPSG",3857]] Landscape ... ``` ## How Geodetics Converter Works The *[Converter](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md)* class in *[Geodetics Plugin](../../code/plugins/geodetics/index_cpp.md)* hides all parameter-detection logic. When loading a world, it **automatically**: - finds the active terrain - reads Sandworm XML tags - determines the reference mode (EPSG / WKT / Pivot) - determines the terrain type (Landscape Terrain / Terrain Global) - determines axis rotation - initializes the internal Geodetics Transformer The user only needs to call the [conversion methods](#converter_api). ### Initialization Automatic initialization occurs when the world is loaded. Immediately after the world is loaded, the [conversion methods](#converter_api) can be used: ```cpp auto converter = GeodeticsPlugin::getConverter(); dvec3 world_pos = converter->geodeticToWorld(dvec3(lat, lon, alt)); ``` By default, *[setAutoWorldInit(true)](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#setAutoWorldInit_bool_void)* is used, so the converter is initialized automatically. If necessary, automatic initialization can be disabled by calling *[setAutoWorldInit(false)](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#setAutoWorldInit_bool_void)* before loading the world. During initialization, the converter prints the current projection and mode to the console, which is useful for debugging. ### Conversion API | Method | Description | |---|---| | *[geodeticToWorld(dvec3)](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#geodeticToWorld_dvec3_Vec3)* | GPS coordinates (lat, lon, alt) -> World coordinates (x, y, z) | | *[worldToGeodetic(dvec3)](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#worldToGeodetic_Vec3_dvec3)* | World coordinates -> GPS coordinates | | *[projectionToWorld(dvec3)](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#projectionToWorld_Vec3_Vec3)* | EPSG/WKT coordinates -> World coordinates | | *[worldToProjection(dvec3)](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#worldToProjection_Vec3_Vec3)* | World coordinates -> EPSG/WKT coordinates | | *[geodeticEulerToRotation(...)](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#geodeticEulerToRotation_dvec3_vec3_quat)* | Geodetic rotation (Euler) -> rotation in the engine world | | *[rotationToGeodeticEuler(...)](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#rotationToGeodeticEuler_dvec3_quat_vec3)* | Rotation in the engine world -> geodetic rotation (Euler) | | *[geodeticToGeocentric(dvec3)](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#geodeticToGeocentric_dvec3_double_double_dvec3)* | GPS coordinates -> geocentric coordinates (from the center of the Earth) | | *[geocentricToGeodetic(dvec3)](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#geocentricToGeodetic_dvec3_double_double_dvec3)* | Geocentric coordinates (from the center of the Earth) -> GPS coordinates | ### Initialization Priorities Converter determines the operating mode according to the following priorities: 1. Geodetic Pivot (HIGHEST PRIORITY) If an enabled GeodeticPivot exists in the scene, the converter works through it. This makes it easy to override the reference: create a pivot and place it at the required point. 2. Terrain Global > Landscape Terrain If both terrain types are present, Terrain Global is selected (this is because Terrain Global appeared in the engine earlier). 3. The first enabled terrain created in the editor The first terrain node that meets the following conditions is used: - enabled = true - isShownInEditorEnabled = true (not cached and not a preview node) 4. If the world contains several identical terrains and their priority cannot be determined, the selected terrain may change from one launch to another. The best solution in this case is to disable the extra terrains and leave only one active. ### Runtime Reinitialization If locations are far apart and placing them in a single projection causes noticeable distortions, use a separate terrain with a suitable projection for each location and switch between them as follows: ```cpp // Disable the current terrain current_terrain->setEnabled(false); // Enable the new one next_terrain->setEnabled(true); // Reinitialize the converter converter->initialize(); // The converter will find the enabled terrain and configure accordingly ``` The reinitialization event allows subscribers to recalculate their coordinates when georeferencing changes. For terrains created at runtime (rather than in the editor), the following must be set: ```cpp terrain->setShownInEditorEnabled(true); ``` Otherwise, the converter will not find it, because it filters out cached and preview nodes. Automatic converter initialization can be disabled: ```cpp converter->setAutoWorldInit(false); // call BEFORE the world load ``` This is useful if you already have your own complex GeodeticsTransformer initialization logic. ## Manual Geodetics Transformer Initialization Manual Geodetics Transformer initialization is available when full control is required. ### API: Geodetics Transformer The Geodetics *[Transformer](../../api/library/geodetics/geodetics_plugin/class.transformer_cpp.md)* class is a low-level conversion tool. It requires manual initialization. Main methods (C++): | Method | Description | |---|---| | *[setProjectionEpsg(int code)](../../api/library/geodetics/geodetics_plugin/class.transformer_cpp.md#setProjectionEpsg_int_dvec3_cstr_bool_int)* | Set a projection by EPSG code | | *[setProjectionWkt(String wkt)](../../api/library/geodetics/geodetics_plugin/class.transformer_cpp.md#setProjectionWkt_cstr_dvec3_cstr_bool_int)* | Set a custom projection using a WKT string | | *[geodeticToWorld(dvec3 geodetic)](../../api/library/geodetics/geodetics_plugin/class.transformer_cpp.md#geodeticToWorld_dvec3_bool_dmat4)* | GPS coordinates -> World coordinates | | *[worldToGeodetic(dvec3 world)](../../api/library/geodetics/geodetics_plugin/class.transformer_cpp.md#worldToGeodetic_dvec3_bool_dvec3)* | World coordinates -> GPS coordinates | The main issue in this case is that this class does not know which terrain is in the scene, which reference mode it uses, or where to get the parameters. The user must implement all detection logic. ```cpp #include #include bool manual_init_geodetic() { Unigine::Plugins::Geodetics::Transformer *geodetic_plugin = Unigine::Plugins::Geodetics::Transformer::get(); if (!geodetic_plugin) { Unigine::Log::warning("Can not find 'UnigineGeodetics' plugin\n"); return false; } Unigine::ObjectLandscapeTerrainPtr terrain_landscape = Unigine::Landscape::getActiveTerrain(); if (!terrain_landscape) { Unigine::Log::warning("Can not find LandscapeTerrain\n"); return false; } if (terrain_landscape->hasVariable("sandworm_epsg")) { int epsg = terrain_landscape->getVariable("sandworm_epsg").getInt(); Unigine::String wkt2 = terrain_landscape->getVariable("sandworm_projection").getString(); Unigine::Math::dvec3 geodetic_origin = terrain_landscape->getVariable("sandworm_origin").getDVec3(); if (!geodetic_plugin->setProjectionEpsg(epsg, geodetic_origin)) { if (!geodetic_plugin->setProjectionWkt(wkt2.get(), geodetic_origin)) { Unigine::Log::warning("Can not set projection epsg '%d' wkt '%s'\n", epsg, wkt2.get()); return false; } } return true; } else { Unigine::Log::warning("Can not find 'sandworm_epsg' tag for terrain %s(%d)\n", terrain_landscape->getName(), terrain_landscape->getID()); return false; } return false; } ``` ### Solving Standard Tasks The following methods can be used to solve most standard tasks: | Task | Solution | |---|---| | GPS coordinates -> World coordinates | *[Converter::geodeticToWorld()](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#geodeticToWorld_dvec3_Vec3)* (automatically) | | World coordinates -> GPS coordinates | *[Converter::worldToGeodetic()](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#worldToGeodetic_Vec3_dvec3)* | | EPSG coordinates -> World coordinates | *[Converter::projectionToWorld()](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#projectionToWorld_Vec3_Vec3)* | | World coordinates -> EPSG coordinates | *[Converter::worldToProjection()](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#worldToProjection_Vec3_Vec3)* | | Geodetic rotation | *[Converter::geodeticEulerToRotation()](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#geodeticEulerToRotation_dvec3_vec3_quat)* | | GPS coordinates -> geocentric coordinates | *[Converter::geodeticToGeocentric()](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#geodeticToGeocentric_dvec3_double_double_dvec3)* | | Get the reference type | *[Converter::getGeodeticMode()](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#getGeodeticMode_int)* | | Get the terrain type | *[Converter::getTerrainMode()](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#getTerrainMode_int)* | | Get Origin | *[Converter::getOrigin()](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#getOrigin_dvec3)* | | World vs Projection offset | *[Transformer::getOriginProjectionSpace()](../../api/library/geodetics/geodetics_plugin/class.transformer_cpp.md#getOriginProjectionSpace_dvec3)* | | Check whether the axes are rotated | *[Converter::isForceRotatedCoordinatesToENU()](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md#isForceRotateCoordinatesToENU_int)* | ## Known Limitations and Notes - *[Sandworm](../../editor2/sandworm/index.md)* cannot generate in WKT. The [Sandworm Projection](../../editor2/sandworm/generation/projection/index.md) field supports the WKT value, but the Sandworm UI does not allow setting a custom projection. - [Geodetic Pivot](../../objects/geodetics/geodeticpivot/index.md) is available only for *Terrain Global*. *Landscape Terrain* does not support the pivot (there is no such option in Sandworm). - One active terrain. Converter works with the first enabled terrain it finds. There can be only one *Landscape Terrain* (engine limitation). There can be several *Terrain Global* objects, but the converter will use the first one it finds. - Earth curvature when moving away from the pivot. The farther from the Origin/Pivot, the greater the Z-coordinate discrepancy due to the Earth's curvature, even in flat coordinates. - Geocentric coordinates. The majorAxis/minorAxis parameters (Earth radii) are set to WGS-84 by default. They should be changed only for special tasks. ## See Also - [Converter Class API](../../api/library/geodetics/geodetics_plugin/class.converter_cpp.md) - a class that simplifies all work with georeferencing - [GeodeticPivot Class API](../../api/library/geodetics/class.geodeticpivot_cpp.md) - pivot node API - [Geo Coordinates in the editor](../../editor2/node_parameters/geo/index.md) - [Sandworm: Projection](../../editor2/sandworm/generation/projection/index.md) - projection settings during generation - [Sandworm Generation Settings - Projection: Curved](../../editor2/sandworm/generation/projection/index.md#curved) - Earth surface curvature settings - [Geodetics Overview](../../objects/geodetics/index.md) - general overview of the geodetic system - [GeodeticsTransformer Class API](../../api/library/geodetics/geodetics_plugin/class.transformer_cpp.md) - low-level conversion class - [Geodetics Plugin Overview](../../code/plugins/geodetics/index_cpp.md) - [Sandworm FAQ (EPSG)](../../editor2/sandworm/faq/index.md)