.. _earthdistance:

=============
earthdistance
=============


插件earthdistance简介
--------------------------

earthdistance支持了与地球表面相关的数据操作。

- 插件名为earthdistance

- 插件版本 V1.0


插件earthdistance加载方式
------------------------------

使用时需要create extension earthdistance;

示例

.. code::

	create extension earthdistance;


插件earthdistance参数配置
------------------------------

无需配置任何参数。


插件citext使用方法
-------------------------

geo_distance
^^^^^^^^^^^^^^

计算两点之间的地理距离。

**语法**

.. code::

	geo_distance(p1 point, p2 point ) RETURNS float8;

**描述**

计算点p1与p2之间的地理距离。

**示例**

.. code::

	TEST=# select geo_distance('(2,1)'::point,'(2,0)'::point);
	   geo_distance
	------------------
	 69.0931819000054
	(1 行记录)


earth
^^^^^^^^^^

获取地球半径。

**语法**

.. code::

   earth() returns float8

**描述**

earth会获取地球的半径长度。

**示例**

.. code::

	TEST=# select earth();
	  earth
	---------
	 6378168
	(1 行记录)


sec_to_gc
^^^^^^^^^^^

把直线距离转换为标准圆的距离。

**语法**

.. code::

	sec_to_gc(float8) returns float8;

**示例**

.. code::

	SELECT sec_to_gc(0)::numeric(20,5);
	 sec_to_gc 
	-----------
	   0.00000
	(1 row)

	SELECT sec_to_gc(2*earth())::numeric(20,5);
	   sec_to_gc    
	----------------
	 20037605.73216
	(1 row)

	SELECT sec_to_gc(10*earth())::numeric(20,5);
	   sec_to_gc    
	----------------
	 20037605.73216
	(1 row)

	SELECT sec_to_gc(-earth())::numeric(20,5);
	 sec_to_gc 
	-----------
	   0.00000
	(1 row)

	SELECT sec_to_gc(1000)::numeric(20,5);
	 sec_to_gc  
	------------
	 1000.00000
	(1 row)


gc_to_sec
^^^^^^^^^^^

把标准圆的距离转换为直线距离。

**语法**

.. code::

	gc_to_sec(float8) returns float8;

**示例**

.. code::

	SELECT gc_to_sec(0)::numeric(20,5);
	 gc_to_sec 
	-----------
	   0.00000
	(1 row)

	SELECT gc_to_sec(sec_to_gc(2*earth()))::numeric(20,5);
	   gc_to_sec    
	----------------
	 12756336.00000
	(1 row)

	SELECT gc_to_sec(10*earth())::numeric(20,5);
	   gc_to_sec    
	----------------
	 12756336.00000
	(1 row)

	SELECT gc_to_sec(pi()*earth())::numeric(20,5);
	   gc_to_sec    
	----------------
	 12756336.00000
	(1 row)

	SELECT gc_to_sec(-1000)::numeric(20,5);
	 gc_to_sec 
	-----------
	   0.00000
	(1 row)

	SELECT gc_to_sec(1000)::numeric(20,5);
	 gc_to_sec  
	------------
	 1000.00000
	(1 row)

	SELECT gc_to_sec(10000)::numeric(20,5);
	 gc_to_sec  
	------------
	 9999.99898
	(1 row)


ll_to_earth
^^^^^^^^^^^^^

通过经度和纬度计算地球表面距离地心的距离

**语法**

.. code::

	ll_to_earth(float8, float8) returns earth;

**示例**

.. code::

	SELECT cube_ll_coord(ll_to_earth(0,0),1)::numeric(20,5),
	 cube_ll_coord(ll_to_earth(0,0),2)::numeric(20,5),
	 cube_ll_coord(ll_to_earth(0,0),3)::numeric(20,5);
	 cube_ll_coord | cube_ll_coord | cube_ll_coord 
	---------------+---------------+---------------
	 6378168.00000 |       0.00000 |       0.00000
	(1 row)

	SELECT cube_ll_coord(ll_to_earth(360,360),1)::numeric(20,5),
	 cube_ll_coord(ll_to_earth(360,360),2)::numeric(20,5),
	 cube_ll_coord(ll_to_earth(360,360),3)::numeric(20,5);
	 cube_ll_coord | cube_ll_coord | cube_ll_coord 
	---------------+---------------+---------------
	 6378168.00000 |       0.00000 |       0.00000
	(1 row)

	SELECT cube_ll_coord(ll_to_earth(180,180),1)::numeric(20,5),
	 cube_ll_coord(ll_to_earth(180,180),2)::numeric(20,5),
	 cube_ll_coord(ll_to_earth(180,180),3)::numeric(20,5);
	 cube_ll_coord | cube_ll_coord | cube_ll_coord 
	---------------+---------------+---------------
	 6378168.00000 |       0.00000 |       0.00000
	(1 row)

	SELECT cube_ll_coord(ll_to_earth(180,360),1)::numeric(20,5),
	 cube_ll_coord(ll_to_earth(180,360),2)::numeric(20,5),
	 cube_ll_coord(ll_to_earth(180,360),3)::numeric(20,5);
	 cube_ll_coord  | cube_ll_coord | cube_ll_coord 
	----------------+---------------+---------------
	 -6378168.00000 |       0.00000 |       0.00000
	(1 row)

	SELECT cube_ll_coord(ll_to_earth(-180,-360),1)::numeric(20,5),
	 cube_ll_coord(ll_to_earth(-180,-360),2)::numeric(20,5),
	 cube_ll_coord(ll_to_earth(-180,-360),3)::numeric(20,5);
	 cube_ll_coord  | cube_ll_coord | cube_ll_coord 
	----------------+---------------+---------------
	 -6378168.00000 |       0.00000 |       0.00000
	(1 row)


latitude
^^^^^^^^^^

通过地球表面一点计算纬度。

**语法**

.. code::

	latitude(earth) returns float8;

**示例**

.. code::

	SELECT latitude(ll_to_earth(0,0))::numeric(20,10);
	   latitude   
	--------------
	 0.0000000000
	(1 row)

	SELECT latitude(ll_to_earth(45,0))::numeric(20,10);
	   latitude    
	---------------
	 45.0000000000
	(1 row)

	SELECT latitude(ll_to_earth(90,0))::numeric(20,10);
	   latitude    
	---------------
	 90.0000000000
	(1 row)

	SELECT latitude(ll_to_earth(-45,0))::numeric(20,10);
		latitude    
	----------------
	 -45.0000000000
	(1 row)

	SELECT latitude(ll_to_earth(-90,0))::numeric(20,10);
		latitude    
	----------------
	 -90.0000000000
	(1 row)

	SELECT latitude(ll_to_earth(0,90))::numeric(20,10);
	   latitude   
	--------------
	 0.0000000000
	(1 row)

	SELECT latitude(ll_to_earth(45,90))::numeric(20,10);
	   latitude    
	---------------
	 45.0000000000
	(1 row)

	SELECT latitude(ll_to_earth(90,90))::numeric(20,10);
	   latitude    
	---------------
	 90.0000000000
	(1 row)

	SELECT latitude(ll_to_earth(-45,90))::numeric(20,10);
		latitude    
	----------------
	 -45.0000000000
	(1 row)


longitude
^^^^^^^^^^^^

通过地球表面一点计算纬度。

**语法**

.. code::

	longitude(earth) returns float8;

**示例**

.. code::

	SELECT longitude(ll_to_earth(0,0))::numeric(20,10);
	  longitude   
	--------------
	 0.0000000000
	(1 row)

	SELECT longitude(ll_to_earth(45,0))::numeric(20,10);
	  longitude   
	--------------
	 0.0000000000
	(1 row)

	SELECT longitude(ll_to_earth(90,0))::numeric(20,10);
	  longitude   
	--------------
	 0.0000000000
	(1 row)

	SELECT longitude(ll_to_earth(-45,0))::numeric(20,10);
	  longitude   
	--------------
	 0.0000000000
	(1 row)

	SELECT longitude(ll_to_earth(-90,0))::numeric(20,10);
	  longitude   
	--------------
	 0.0000000000
	(1 row)

	SELECT longitude(ll_to_earth(0,90))::numeric(20,10);
	   longitude   
	---------------
	 90.0000000000
	(1 row)

	SELECT longitude(ll_to_earth(45,90))::numeric(20,10);
	   longitude   
	---------------
	 90.0000000000
	(1 row)

	SELECT longitude(ll_to_earth(90,90))::numeric(20,10);
	   longitude   
	---------------
	 90.0000000000
	(1 row)

	SELECT longitude(ll_to_earth(-45,90))::numeric(20,10);
	   longitude   
	---------------
	 90.0000000000
	(1 row)

	SELECT longitude(ll_to_earth(-90,90))::numeric(20,10);
	   longitude   
	---------------
	 90.0000000000
	(1 row)

	SELECT longitude(ll_to_earth(0,180))::numeric(20,10);
	   longitude    
	----------------
	 180.0000000000
	(1 row)


earth_distance
^^^^^^^^^^^^^^^^

计算地球表面两点之间的距离。

**语法**

.. code::

	earth_distance(earth, earth) returns float8;

**示例**

.. code::

	SELECT earth_distance(ll_to_earth(0,0),ll_to_earth(0,0))::numeric(20,5);
	 earth_distance 
	----------------
			0.00000
	(1 row)

	SELECT earth_distance(ll_to_earth(0,0),ll_to_earth(0,180))::numeric(20,5);
	 earth_distance 
	----------------
	 20037605.73216
	(1 row)

	SELECT earth_distance(ll_to_earth(0,0),ll_to_earth(90,0))::numeric(20,5);
	 earth_distance 
	----------------
	 10018802.86608
	(1 row)

	SELECT earth_distance(ll_to_earth(0,0),ll_to_earth(0,90))::numeric(20,5);
	 earth_distance 
	----------------
	 10018802.86608
	(1 row)

	SELECT earth_distance(ll_to_earth(0,0),ll_to_earth(0,1))::numeric(20,5);
	 earth_distance 
	----------------
	   111320.03185
	(1 row)

	SELECT earth_distance(ll_to_earth(0,0),ll_to_earth(1,0))::numeric(20,5);
	 earth_distance 
	----------------
	   111320.03185
	(1 row)

	SELECT earth_distance(ll_to_earth(30,0),ll_to_earth(30,1))::numeric(20,5);
	 earth_distance 
	----------------
		96405.66962
	(1 row)

	SELECT earth_distance(ll_to_earth(30,0),ll_to_earth(31,0))::numeric(20,5);
	 earth_distance 
	----------------
	   111320.03185
	(1 row)

	SELECT earth_distance(ll_to_earth(60,0),ll_to_earth(60,1))::numeric(20,5);
	 earth_distance 
	----------------
		55659.48608
	(1 row)

	SELECT earth_distance(ll_to_earth(60,0),ll_to_earth(61,0))::numeric(20,5);
	 earth_distance 
	----------------
	   111320.03185
	(1 row)

	SELECT earth_distance(ll_to_earth(41.8,87.6),ll_to_earth(35.1,106.7))::numeric(20,5);
	 earth_distance 
	----------------
	  1819303.21265
	(1 row)

	SELECT (earth_distance(ll_to_earth(41.8,87.6),ll_to_earth(35.1,106.7))*
		  100./2.54/12./5280.)::numeric(20,5);
	  numeric   
	------------
	 1130.46261
	(1 row)


earth_box
^^^^^^^^^^^^

得到环绕表面一点得到的物体。

**语法**

.. code::

	earth_box(earth, float8) returns float8;

**示例**

.. code::

	SELECT cube_ll_coord(earth_box(ll_to_earth(0,0),112000),1)::numeric(20,5),
	cube_ll_coord(earth_box(ll_to_earth(0,0),112000),2)::numeric(20,5),
	cube_ll_coord(earth_box(ll_to_earth(0,0),112000),3)::numeric(20,5),
	cube_ur_coord(earth_box(ll_to_earth(0,0),112000),1)::numeric(20,5),
	cube_ur_coord(earth_box(ll_to_earth(0,0),112000),2)::numeric(20,5),
	cube_ur_coord(earth_box(ll_to_earth(0,0),112000),3)::numeric(20,5);
	cube_ll_coord | cube_ll_coord | cube_ll_coord | cube_ur_coord | cube_ur_coord | cube_ur_coord 
	---------------+---------------+---------------+---------------+---------------+---------------
	6266169.43896 | -111998.56104 | -111998.56104 | 6490166.56104 |  111998.56104 |  111998.56104
	(1 row)

	SELECT cube_ll_coord(earth_box(ll_to_earth(0,0),pi()*earth()),1)::numeric(20,5),
	cube_ll_coord(earth_box(ll_to_earth(0,0),pi()*earth()),2)::numeric(20,5),
	cube_ll_coord(earth_box(ll_to_earth(0,0),pi()*earth()),3)::numeric(20,5),
	cube_ur_coord(earth_box(ll_to_earth(0,0),pi()*earth()),1)::numeric(20,5),
	cube_ur_coord(earth_box(ll_to_earth(0,0),pi()*earth()),2)::numeric(20,5),
	cube_ur_coord(earth_box(ll_to_earth(0,0),pi()*earth()),3)::numeric(20,5);
	cube_ll_coord  |  cube_ll_coord  |  cube_ll_coord  | cube_ur_coord  | cube_ur_coord  | cube_ur_coord  
	----------------+-----------------+-----------------+----------------+----------------+----------------
	-6378168.00000 | -12756336.00000 | -12756336.00000 | 19134504.00000 | 12756336.00000 | 12756336.00000
	(1 row)

	SELECT cube_ll_coord(earth_box(ll_to_earth(0,0),10*earth()),1)::numeric(20,5),
	cube_ll_coord(earth_box(ll_to_earth(0,0),10*earth()),2)::numeric(20,5),
	cube_ll_coord(earth_box(ll_to_earth(0,0),10*earth()),3)::numeric(20,5),
	cube_ur_coord(earth_box(ll_to_earth(0,0),10*earth()),1)::numeric(20,5),
	cube_ur_coord(earth_box(ll_to_earth(0,0),10*earth()),2)::numeric(20,5),
	cube_ur_coord(earth_box(ll_to_earth(0,0),10*earth()),3)::numeric(20,5);
	cube_ll_coord  |  cube_ll_coord  |  cube_ll_coord  | cube_ur_coord  | cube_ur_coord  | cube_ur_coord  
	----------------+-----------------+-----------------+----------------+----------------+----------------
	-6378168.00000 | -12756336.00000 | -12756336.00000 | 19134504.00000 | 12756336.00000 | 12756336.00000
	(1 row)


geo_distance
^^^^^^^^^^^^^^^

计算两点的地理距离

**语法**

.. code::

	geo_distance(point, point) returns float8;

**示例**

.. code::

	SELECT geo_distance('(0,0)'::point,'(0,0)'::point)::numeric(20,5);
	 geo_distance 
	--------------
		  0.00000
	(1 row)

	SELECT geo_distance('(0,0)'::point,'(180,0)'::point)::numeric(20,5);
	 geo_distance 
	--------------
	  12436.77274
	(1 row)

	SELECT geo_distance('(0,0)'::point,'(0,90)'::point)::numeric(20,5);
	 geo_distance 
	--------------
	   6218.38637
	(1 row)

	SELECT geo_distance('(0,0)'::point,'(90,0)'::point)::numeric(20,5);
	 geo_distance 
	--------------
	   6218.38637
	(1 row)

	SELECT geo_distance('(0,0)'::point,'(1,0)'::point)::numeric(20,5);
	 geo_distance 
	--------------
		 69.09318
	(1 row)

	SELECT geo_distance('(0,0)'::point,'(0,1)'::point)::numeric(20,5);
	 geo_distance 
	--------------
		 69.09318
	(1 row)

	SELECT geo_distance('(0,30)'::point,'(1,30)'::point)::numeric(20,5);
	 geo_distance 
	--------------
		 59.83626
	(1 row)


插件earthdistance卸载方法
----------------------------

不需要earthdistance插件只需要卸载插件即可。

示例：

.. code::

	drop extension earthdistance;


插件earthdistance升级方法:=
-------------------------

earthdistance扩展插件通常随KingbaseES安装包一并升级。通常情况下用户无法单独升级插件。
