# How To: Transform between Cartesian (X, Y, Z) and Geodetic (Longitude, Latitude, Altitude) Coordinates

By: ring
In this article, I will show how to use Dynamic Geometry Library (a member of the AGI Components family) to transform between two common coordinate representations. There are many ways to represent a position on or near the Earth's surface - or in orbit, for that matter.  Two of the more common representations are what AGI Components refers to as Cartesian and Cartographic. `Cartesian` coordinates are formed from an X, Y and Z component, where each component is a distance in, say, meters.  The coordinates are expressed in some ReferenceFrame, which defines the origin from which the coordinates are measured and the directions of the X, Y and Z axes.  Two common reference frames are Earth Centered Inertial (ECI) and Earth Centered Fixed (ECF).  For information on transforming between `Cartesian` coordinates expressed in different reference frames, see How To: Transform between the Earth Centered Fixed and Earth Centered Inertial reference frames. `Cartographic` coordinates are also known as Geodetic coordinates.  They are formed from a longitude, latitude and altitude, where longitude and latitude are angles and altitude is a distance.  In AGI Components, Cartographic coordinates are defined with respect to a specific shape, such as the WorldGeodeticSystem1984 (WGS84) ellipsoid.  A ReferenceFrame then defines the position and orientation of that ellipsoid relative to the rest of the known universe.  The WGS84 ellipsoid is centered at the origin of the Earth Centered Fixed (ECF) frame and is oriented by its axes. Transforming from WGS84 `Cartographic` coordinates to ECF `Cartesian` coordinates is very easy! First we need a few `using` declarations: using AGI.Foundation; using AGI.Foundation.Celestial; using AGI.Foundation.Coordinates; using AGI.Foundation.Geometry; using AGI.Foundation.Geometry.Shapes; using AGI.Foundation.Time; Now, let's convert the `Cartographic` location of AGI's office to `Cartesian` ECF coordinates and print them out: double longitudeRadians = Trig.DegreesToRadians(-75.59777); double latitudeRadians = Trig.DegreesToRadians(40.03883); double altitudeMeters = 113.9952; Cartographic cartographic = new Cartographic(longitudeRadians, latitudeRadians, altitudeMeters); Ellipsoid wgs84 = WorldGeodeticSystem1984.Shape; Cartesian cartesian = wgs84.CartographicToCartesian(cartographic); Console.WriteLine(cartesian.X); Console.WriteLine(cartesian.Y); Console.WriteLine(cartesian.Z); This is straightforward enough. We get an instance of the WGS84 ellipsoid shape and then call it `CartographicToCartesian` method. Note that the angles must be expressed in radians and that the first parameter to the `Cartographic` constructor is the longitude! Converting back is equally straightforward: Cartographic backToCartographic = wgs84.CartesianToCartographic(cartesian); Console.WriteLine(backToCartographic.Longitude); Console.WriteLine(backToCartographic.Latitude); Console.WriteLine(backToCartographic.Height); It's important to note that the `Cartesian` coordinates passed to `CartesianToCartographic` must be expressed in the Earth Centered Fixed (ECF) reference frame. Similarly, `CartographicToCartesian` will return `Cartesian` coordinates expressed in that frame. If you pass a `Cartesian` position expressed in a different reference frame (such as ECI) to the `CartesianToCartographic` method, AGI Components has no way of knowing you did this. The returned `Cartographic` will be incorrect. AGI Components also provides an easy way to produce a `Cartographic` from any Point instance. `Points` in AGI Components are higher-level objects that represent points that can (and usually do) change position with time. For example, a single `Point` instance can represent the position and velocity of a satellite throughout its orbit, or the position and velocity of an aircraft as it flies along its route. The `Cartographic` position of the point at a given time can be computed as follows: Point point = ...; EarthCentralBody earth = CentralBodiesFacet.GetFromContext().Earth; MotionEvaluator<Cartographic> evaluator = earth.ObserveCartographicPoint(point); JulianDate now = new JulianDate(DateTime.UtcNow); Cartographic cartographicNow = evaluator.Evaluate(now); JulianDate yesterday = now.AddDays(-1.0); Cartographic cartographicYesterday = evaluator.Evaluate(yesterday); First we get the EarthCentralBody from the current calculation context. Then we call the ObserveCartographicPoint method to get an evaluator. Finally, we evaluate the evaluator at various times to find the `Cartographic` position of the `Point` at those times. When using this approach, it is not necessary to think about reference frames. The position of the `Point` at the given time is automatically transformed to the correct frame before being converted to `Cartographic` coordinates.
Posted: 10/13/2010 8:00:04 PM