经纬度格式化转换-数据库函数方式

在进行前台数据展示操作时,有时需要对经纬度格式进行重新的格式化操作

 

一般我们处理的方式可以有一下几种:

1.前台格式化方法处理相关的数据

2.后台相关代码进行数据的格式化

3.通过数据库函数对数据库字段进行格式化的操作

 

以上方法的实现都大致上在方法上是类似的,实现效果的话就见仁见智了,看具体项目的需要了。

 

这里简单做了个第3中方法的实现方式:(后续如果有需要再做补充)

create or replace function GETEARTH(lontitude in NUMBER,
                                    lattitude in NUMBER) RETURN VARCHAR2
/******************************************************************************
     NAME:       GETEARTH

     参数: 经度
            纬度

     PURPOSE:    根据传入的经纬度坐标(十进制)进行,返回经纬度(度分秒)字符串


  ******************************************************************************/
 IS
  sqlStr VARCHAR2(1200);
  lon VARCHAR2(600);
  lat VARCHAR2(600);
  coordinate VARCHAR2(128);
  coordinateStr VARCHAR2(128);

BEGIN
  --处理经度换算
  lon := 'SELECT TO_CHAR(ABS(FLOOR(' || lontitude || '))) ' || '||''°''||' ||
            'replace(lpad(TO_CHAR(FLOOR((' || lontitude || ' - FLOOR(' || lontitude ||
            '))*60)),2),'' '',''0'')' || '||''′''||' || 'TO_CHAR(Round(((' || lontitude ||
            ' - FLOOR(' || lontitude || '))*60 - FLOOR((' || lontitude ||
            ' - FLOOR(' || lontitude ||
            '))*60))*60,2)) || ''″'' as lon , 1 as con  FROM dual ';

  --处理纬度换算
  lat := 'SELECT TO_CHAR(ABS(FLOOR(' || lattitude || '))) ' || '||''°''||' ||
            'replace(lpad(TO_CHAR(FLOOR((' || lattitude || ' - FLOOR(' || lattitude ||
            '))*60)),2),'' '',''0'')' || '||''′''||' || 'TO_CHAR(Round(((' || lattitude ||
            ' - FLOOR(' || lattitude || '))*60 - FLOOR((' || lattitude ||
            ' - FLOOR(' || lattitude ||
            '))*60))*60,2)) || ''″'' as lat , 1 as con  FROM dual ';


  sqlStr :=  'select s1.lon || '','' || s2.lat from  ('|| lon || ') s1 LEFT JOIN  ' || '('|| lat || ') s2 ON s1.con=s2.con ' ;  

  EXECUTE IMMEDIATE sqlStr
    INTO coordinate;

  IF FLOOR(lontitude) > 0  and  FLOOR(lattitude) > 0 THEN
         coordinateStr := replace('E' || replace(coordinate,',',' N'),'′.','′0.');
         --sqlStr :=  'select s1.lon || ''E,'' || s2.lat || ''N'' from  ('|| lon || ') s1 LEFT JOIN  ' || '('|| lat || ') s2 ON s1.con=s2.con' ;


  END IF;

  IF FLOOR(lontitude) > 0  and  FLOOR(lattitude) < 0 THEN
         coordinateStr := replace('E' ||  replace(coordinate,',',' S'),'′.','′0.');
         --sqlStr :=  'select s1.lon || ''E,'' || s2.lat || ''S'' from  ('|| lon || ') s1 LEFT JOIN  ' || '('|| lat || ') s2 ON s1.con=s2.con' ;


  END IF;

  IF FLOOR(lontitude) < 0  and  FLOOR(lattitude) > 0 THEN
         coordinateStr := replace('W' || replace(coordinate,',',' N'),'′.','′0.');
         --sqlStr :=  'select s1.lon || ''W,'' || s2.lat || ''N'' from  ('|| lon || ') s1 LEFT JOIN  ' || '('|| lat || ') s2 ON s1.con=s2.con' ;


  END IF;

  IF FLOOR(lontitude) < 0  and  FLOOR(lattitude) < 0 THEN
         coordinateStr := replace('W' || replace(coordinate,',',' S'),'′.','′0.');
         --sqlStr :=  'select s1.lon || ''W,'' || s2.lat || ''S'' from  ('|| lon || ') s1 LEFT JOIN  ' || '('|| lat || ') s2 ON s1.con=s2.con' ;


  END IF;


  RETURN(coordinateStr);

  EXCEPTION

  WHEN OTHERS THEN
    coordinateStr := 'error calculate';
    RETURN coordinateStr;


END GETEARTH;

 

你可能感兴趣的:(GIS资源)