MySQL convert Degree, Minutes, Seconds to Degree decimal

孤人 提交于 2019-12-02 02:12:34

Nice lifehack: reverse problem solution (degree to DMS) using SEC_TO_TIME built-in MySQL function:

CREATE FUNCTION `geocoords`(lon double, lat double) RETURNS varchar(24) CHARSET cp1251
    NO SQL
    DETERMINISTIC
begin    
  declare alon double;
  declare alat double;
  declare slon varchar(12);
  declare slat varchar(12);
  set alon = abs(lon);
  set alat = abs(lat);
  set slon = TIME_FORMAT(SEC_TO_TIME(alon*3600), '%H°%i''%s"');
  set slat = TIME_FORMAT(SEC_TO_TIME(alat*3600), '%H°%i''%s"');
  if lon>0 then
    set slon = concat(slon, 'E');
  elseif lon<0 then  
    set slon = concat(slon, 'W');
  end if;  
  if lat>0 then
    set slat = concat(slat, 'N');
  elseif lat<0 then  
    set slat = concat(slat, 'S');
  end if;  
  return concat(slat, ' ', slon);
end

SELECT geocoords(30.550157546997, 50.344024658203)

50°20'38"N 30°33'01"E

The following should work:

SELECT D + M/60 + S/3600;

For example, in MySQL:

SELECT 36 + 19/60 + 11.46/3600; 

returns: 36.319850

I ended up building this, and it worked flawlessly with what I needed. You will note that I added a C to the numbers, this is to flag them, so if it had already been converted it wouldn't continue to convert.

UPDATE
    MasterTable
SET
    MasterTable.Latitude_A = MasterTable.Latitude,
    MasterTable.Longitude_A = MasterTable.Longitude
WHERE
    ProjectID = 'ProjectAlpha'
    and Sequence = '0'
    and MasterTable.Latitude NOT LIKE '%C%'
    and MasterTable.Longitude NOT LIKE '%C%';

TRUNCATE TABLE gpsconvert;
    INSERT into gpsconvert(gpsconvert.`Account Number`,gpsconvert.Latitude,gpsconvert.Longitude)
SELECT
        MasterTable.AccountNumber,
        MasterTable.Latitude,
        MasterTable.Longitude
FROM
    MasterTable
WHERE
    MasterTable.ProjectID = 'ProjectAlpha'
    and MasterTable.Sequence = '0'
    and MasterTable.Latitude NOT LIKE '%c%'
    and MasterTable.Longitude NOT LIKE '%c%'
    and MasterTable.Latitude <> ''
    and MasterTable.Longitude <> '';

UPDATE
    gpsconvert
SET
    gpsconvert.LatDegree = LEFT(gpsconvert.Latitude,2),
    gpsconvert.LatMinutes = SUBSTRING(gpsconvert.Latitude,-7,2),
    gpsconvert.LatSeconds = SUBSTRING(gpsconvert.latitude,-5,5),
    gpsconvert.LatDecimal = gpsconvert.LatDegree + (gpsconvert.LatMinutes/60) + (gpsconvert.LatSeconds/3600),
    gpsconvert.LongDegree = LEFT(gpsconvert.Longitude,2),
    gpsconvert.LongMinutes = SUBSTRING(gpsconvert.Longitude,-7,2),
    gpsconvert.LongSeconds = SUBSTRING(gpsconvert.Longitude,-5,5),
    gpsconvert.LongDecimal = gpsconvert.LongDegree +       (gpsconvert.LongMinutes/60) + (gpsconvert.LongSeconds/3600);

UPDATE
    MasterTable
INNER JOIN
    gpsconvert on gpsconvert.`Account Number` = MasterTable.AccountNumber
SET
    MasterTable.Latitude = CONCAT(gpsconvert.LatDecimal,'c'),
    MasterTable.Longitude = CONCAT(gpsconvert.LongDecimal,'c')
WHERE
    MasterTable.ProjectID = 'ProjectAlpha'
    and MasterTable.Sequence = '0'
    and MasterTable.AccountNumber = gpsconvert.`Account Number`
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!