Name

ST_Distance — 두 도형 사이의 3차원 최장(longest) 라인을 반환합니다.

Synopsis

float ST_Distance(geometry g1, geometry g2);

float ST_Distance(geography geog1, geography geog2, boolean use_spheroid = true);

설명

도형 유형에 대해, 두 도형 사이의 3차원 데카르트 최단 거리를 두 도형의 투영 단위(SRS 단위)로 반환합니다.

For geography types defaults to return the minimum geodesic distance between two geographies in meters, compute on the spheroid determined by the SRID. If use_spheroid is false, a faster spherical calculation is used.

This method implements the OGC Simple Features Implementation Specification for SQL 1.1.

This method implements the SQL/MM specification.

SQL-MM 3: 5.1.23

This method supports Circular Strings and Curves.

1.5.0 버전부터 지리형을 지원합니다. 대용량 또는 수많은 꼭짓점을 가진 도형을 더 잘 처리하기 위해 평면에 대한 속도를 향상시켰습니다.

개선 사항: 2.1.0 버전부터 지리형에 대한 속도가 향상됐습니다. 자세한 내용은 Making Geography faster 를 참조하십시오.

개선 사항: 2.1.0 버전부터 만곡 도형을 지원하기 시작했습니다.

개선 사항: 2.2.0 버전부터 회전타원체 측정시 정확도와 강력함을 향상시키기 위해 GeographicLib을 이용합니다. 이 새 기능의 장점을 취하려면 Proj 4.9.0 이상 버전이 필요합니다.

Changed: 3.0.0 - does not depend on SFCGAL anymore.

도형 예시

Geometry example - units in planar degrees 4326 is WGS 84 long lat, units are degrees.

SELECT ST_Distance(
    'SRID=4326;POINT(-72.1235 42.3521)'::geometry,
    'SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)'::geometry );
-----------------
0.00150567726382282

Geometry example - units in meters (SRID: 3857, proportional to pixels on popular web maps). Although the value is off, nearby ones can be compared correctly, which makes it a good choice for algorithms like KNN or KMeans.

SELECT ST_Distance(
    ST_Transform('SRID=4326;POINT(-72.1235 42.3521)'::geometry, 3857),
    ST_Transform('SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)'::geometry, 3857) );
-----------------
167.441410065196

Geometry example - units in meters (SRID: 3857 as above, but corrected by cos(lat) to account for distortion)

SELECT ST_Distance(
    ST_Transform('SRID=4326;POINT(-72.1235 42.3521)'::geometry, 3857),
    ST_Transform('SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)'::geometry, 3857)
                ) * cosd(42.3521);
-----------------
123.742351254151

Geometry example - units in meters (SRID: 26986 Massachusetts state plane meters) (most accurate for Massachusetts)

SELECT ST_Distance(
    ST_Transform('SRID=4326;POINT(-72.1235 42.3521)'::geometry, 26986),
    ST_Transform('SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)'::geometry, 26986) );
-----------------
123.797937878454

Geometry example - units in meters (SRID: 2163 US National Atlas Equal area) (least accurate)

SELECT ST_Distance(
    ST_Transform('SRID=4326;POINT(-72.1235 42.3521)'::geometry, 2163),
    ST_Transform('SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)'::geometry, 2163) );
------------------
126.664256056812

지리형 예시

Same as geometry example but note units in meters - use sphere for slightly faster and less accurate computation.

SELECT ST_Distance(gg1, gg2) As spheroid_dist, ST_Distance(gg1, gg2, false) As sphere_dist
FROM (SELECT
    'SRID=4326;POINT(-72.1235 42.3521)'::geography as gg1,
    'SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)'::geography as gg2
        ) As foo  ;

  spheroid_dist   |   sphere_dist
------------------+------------------
 123.802076746848 | 123.475736916397

참고

ST_3DDistance, ST_DWithin, ST_DistanceSphere, ST_DistanceSpheroid, ST_MaxDistance, ST_HausdorffDistance, ST_FrechetDistance, ST_Transform