ecef2geodetic convert ECEF to geodetic coordinates Inputs ------ x,y,z: ECEF coordinates of test point(s) (meters) spheroid: referenceEllipsoid parameter struct angleUnit: string for angular units. Default 'd': degrees Outputs ------- lat,lon, alt: ellipsoid geodetic coordinates of point(s) (degrees, degrees, meters) also see: http://www.oc.nps.edu/oc2902w/coord/coordcvt.pdf Fortran reference at bottom of: http://www.astro.uni.torun.pl/~kb/Papers/geod/Geod-BG.htm
0001 function [lat,lon,alt] = ecef2geodetic(spheroid, x, y, z, angleUnit) 0002 %ecef2geodetic convert ECEF to geodetic coordinates 0003 % 0004 % Inputs 0005 % ------ 0006 % x,y,z: ECEF coordinates of test point(s) (meters) 0007 % spheroid: referenceEllipsoid parameter struct 0008 % angleUnit: string for angular units. Default 'd': degrees 0009 % 0010 % Outputs 0011 % ------- 0012 % lat,lon, alt: ellipsoid geodetic coordinates of point(s) (degrees, degrees, meters) 0013 % 0014 % also see: http://www.oc.nps.edu/oc2902w/coord/coordcvt.pdf 0015 % Fortran reference at bottom of: http://www.astro.uni.torun.pl/~kb/Papers/geod/Geod-BG.htm 0016 0017 if isempty(spheroid), spheroid = wgs84Ellipsoid(); end 0018 0019 % Algorithm is based on 0020 % http://www.astro.uni.torun.pl/~kb/Papers/geod/Geod-BG.htm 0021 % This algorithm provides a converging solution to the latitude equation 0022 % in terms of the parametric or reduced latitude form (v) 0023 % This algorithm provides a uniform solution over all latitudes as it does 0024 % not involve division by cos(phi) or sin(phi) 0025 a = spheroid.SemimajorAxis; 0026 b = spheroid.SemiminorAxis; 0027 r = hypot(x, y); 0028 % Constant required for Latitude equation 0029 rho = atan2(b * z, a * r); 0030 % Constant required for latitude equation 0031 c = (a^2 - b^2) ./ hypot(a*r, b*z); 0032 count = 0; 0033 % Starter for the Newtons Iteration Method 0034 vnew = atan2(a * z, b * r); 0035 % Initializing the parametric latitude 0036 v = 0; 0037 while v ~= vnew && count < 5 0038 % disp([v,vnew]) 0039 v = vnew; 0040 % Derivative of latitude equation 0041 w = 2 * (cos (v - rho) - c .* cos(2 * v)); 0042 % Newtons Method for computing iterations 0043 vnew = v - ((2 * sin (v - rho) - c .* sin(2 * v)) ./ w); 0044 count = count+1; 0045 end %while 0046 0047 %% Computing latitude from the root of the latitude equation 0048 lat = atan2(a * tan (vnew), b); 0049 % Computing longitude 0050 lon = atan2(y, x); 0051 % Computing h from latitude obtained 0052 alt = ((r - a * cos (vnew)) .* cos (lat)) + ... 0053 ((z - b * sin (vnew)) .* sin (lat)); 0054 0055 if nargin < 5 || isempty(angleUnit) || strcmpi(angleUnit(1),'d') 0056 lat = rad2deg(lat); 0057 lon = rad2deg(lon); 0058 end 0059 0060 end % function 0061 0062 % Copyright (c) 2014-2018 Michael Hirsch, Ph.D. 0063 % Copyright (c) 2013, Felipe Geremia Nievinski 0064 % 0065 % Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 0066 % 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 0067 % 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 0068 % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.