최종 수정: 에
ST_DISTANCE
설명
두 기하 객체 사이의 구면상 최단 거리를 미터 단위로 계산합니다. 이 함수는 구형 지구 모델을 사용하여 계산합니다.
경도와 위도 좌표를 받는 ST_DISTANCE_SPHERE와 달리, ST_DISTANCE는 기하 객체(점, 선, 다각형, 원 등)를 매개변수로 받아 두 객체의 경계 사이의 최단 거리를 계산합니다. 두 도형이 교차하는 경우(서로 접하거나 한 도형이 다른 도형을 포함하는 경우 포함) 0을 반환합니다.
구문
ST_DISTANCE( <shape1>, <shape2> )
매개변수
| 매개변수 | 설명 |
|---|---|
<shape1> | 첫 번째 기하 객체입니다. Point, Line, Polygon, Circle, MultiPolygon을 지원합니다. |
<shape2> | 두 번째 기하 객체입니다. Point, Line, Polygon, Circle, MultiPolygon을 지원합니다. |
반환값
두 기하 객체의 경계 사이의 구면상 최단 거리를 미터 단위로 반환합니다(DOUBLE 타입).
ST_DISTANCE는 다음과 같은 특수한 경우를 처리합니다.
- 입력 매개변수 중 하나라도
NULL이면NULL을 반환합니다. - 입력 매개변수 중 하나라도 유효한 기하 객체로 파싱할 수 없으면
NULL을 반환합니다. - 두 기하 객체가 교차하는 경우(한 객체가 다른 객체를 포함하는 경우 포함)
0.0을 반환합니다. - 지원하는 기하 타입은
POINT,LINESTRING,POLYGON,CIRCLE,MULTIPOLYGON입니다.
예제
점 사이의 거리
-- Calculate the distance between two points with a 1-degree longitude difference on the equator
SELECT ST_DISTANCE(ST_GeometryFromText('POINT(0 0)'), ST_GeometryFromText('POINT(1 0)'));
+------------------------------------------------------------------------------------+
| ST_DISTANCE(ST_GeometryFromText('POINT(0 0)'), ST_GeometryFromText('POINT(1 0)')) |
+------------------------------------------------------------------------------------+
| 111195.1011774839 |
+------------------------------------------------------------------------------------+
점과 선 사이의 거리
-- Calculate the shortest distance from a point to a line
SELECT ST_DISTANCE(ST_GeometryFromText('POINT(2 2)'), ST_GeometryFromText('LINESTRING(0 0, 10 0)'));
+----------------------------------------------------------------------------------------------+
| ST_DISTANCE(ST_GeometryFromText('POINT(2 2)'), ST_GeometryFromText('LINESTRING(0 0, 10 0)')) |
+----------------------------------------------------------------------------------------------+
| 222390.2023549678 |
+----------------------------------------------------------------------------------------------+
다각형과 원이 교차하는 경우(거리 0)
-- Circle intersects polygon; the center is outside but the circle covers part of the polygon's boundary
SELECT ST_DISTANCE(
ST_GeometryFromText('POLYGON ((-0.00045 -0.00045, 0.00045 -0.00045, 0.00045 0.00045, -0.00045 0.00045, -0.00045 -0.00045))'),
ST_Circle(0.0006, 0, 50)
);
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| ST_DISTANCE(
ST_GeometryFromText('POLYGON ((-0.00045 -0.00045, 0.00045 -0.00045, 0.00045 0.00045, -0.00045 0.00045, -0.00045 -0.00045))'),
ST_Circle(0.0006, 0, 50)
) |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 0 |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
유효하지 않은 매개변수(NULL 반환)
-- Invalid WKT string
SELECT ST_DISTANCE(ST_GeometryFromText('NOT_A_GEOMETRY'), ST_GeometryFromText('POINT(1 1)'));
+---------------------------------------------------------------------------------------+
| ST_DISTANCE(ST_GeometryFromText('NOT_A_GEOMETRY'), ST_GeometryFromText('POINT(1 1)')) |
+---------------------------------------------------------------------------------------+
| NULL |
+---------------------------------------------------------------------------------------+
NULL 매개변수
SELECT ST_DISTANCE(NULL, ST_GeometryFromText('POINT(1 1)'));
+------------------------------------------------------+
| ST_DISTANCE(NULL, ST_GeometryFromText('POINT(1 1)')) |
+------------------------------------------------------+
| NULL |
+------------------------------------------------------+