MySQL将Degree,Minutes,Seconds转换为Degree十进制

时间:2015-01-09 19:57:19

标签: mysql decimal degrees

我需要使用查询转换多行度数分钟秒。

36°19'11.46“N = 36.31985

95°36'02.22“W = 95.600617

每一行都会有所不同。我被困在这两天了。这甚至可能吗?

5 个答案:

答案 0 :(得分:2)

不错的生活方式:使用SEC_TO_TIME内置的MySQL函数反向解决问题(程度达到DMS):

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

答案 1 :(得分:1)

以下内容应该有效:

SELECT D + M/60 + S/3600;

例如,在MySQL中:

SELECT 36 + 19/60 + 11.46/3600; 

返回:36.319850

答案 2 :(得分:0)

我最终构建了它,它完美地完成了我需要的工作。你会注意到我在数字上添加了一个C,这是为了标记它们,所以如果它已被转换,它将不会继续转换。

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`

答案 3 :(得分:0)

这会将 21°48'6.384" 形式的字符串转换为十进制

CREATE FUNCTION database.DMS_TO_DECIMAL(dms varchar(20))
RETURNS decimal(12,9)
BEGIN
    DECLARE deg decimal(12,9);
    DECLARE mins decimal(12,9);
    DECLARE secs decimal(12,9);

    SET deg = CAST(  SUBSTRING_INDEX(dms, '°', 1) AS decimal(12,9));
    SET mins = CAST( (SUBSTR(dms, POSITION('°' IN dms) + 1, POSITION("'" IN dms) -  POSITION('°' IN dms) - 1)) AS decimal(12,9));
    SET secs = CAST( (SUBSTR(dms, POSITION("'" IN dms) + 1, POSITION("\"" IN dms) -  POSITION("'" IN dms) - 1)) AS decimal(12,9));

    RETURN deg + mins/60 + secs/3600;
END

答案 4 :(得分:0)

添加方向(W/S):

sprintf('%g', value_here)