全部產品
Search
文件中心

:關係函數

更新時間:Apr 16, 2025

時空函數中的關係函數用於判斷兩個Geometry對象之間的關係,例如是否相交、是否重疊、是否包含、空間結構是否相等。關係函數可以與建構函式、輸出函數等其他時空函數搭配使用。本文介紹Lindorm流引擎支援的時空關係函數。

函數列表

函數

說明

ST_Contains

如果Geometry對象A包含Geometry對象B,ST_Contains函數則返回true。

ST_DWithin

如果兩個Geometry對象的二維平面距離在指定範圍內,ST_DWithin函數則返回true。

ST_DWithinSphere

如果兩個Geometry對象的球面距離在指定範圍內,ST_DWithinSphere函數則返回true。

ST_Intersects

判斷兩個Geometry對象是否相交。如果兩個Geometry對象有任意共用空間的部分,那麼兩個Geometry對象相交,ST_Intersects函數則返回true。

ST_Overlaps

如果兩個Geometry對象在空間上有重疊的部分,但不存在其中一個完全包含另一個的情況,ST_Overlaps函數則返回true。

ST_Within

如果Geometry對象A完全在Geometry對象B內,ST_Within函數則返回true。

ST_Equals

如果給定的Geometry對象在空間上相等,則返回true。

ST_Contains

如果Geometry對象A包含Geometry對象B,ST_Contains函數則返回true。

文法

  • 判斷對象geomA是否包含對象geomB

    boolean ST_Contains(geometry geomA,geometry geomB)
  • 判斷對象geom和點座標(x,y)的內含項目關聯性

    boolean ST_Contains(geometry geom,double x,double y)

參數說明

參數

描述

geomA

指定的第一個Geometry對象。

geomB

指定的第二個Geometry對象。

geom

指定的Geometry對象。

x

座標經度x值。

y

座標緯度y值。

說明
  • geomB對象的所有點均在geomA對象的內部或者邊界上,則geomA對象包含geomB對象。

  • ST_Contains函數是ST_Within的反函數,即ST_Contains(A,B)=ST_Within(B,A)

樣本

  • 樣本1

    SELECT ST_Contains(ST_GeomFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))'), ST_GeomFromText('POINT(5 5)')) AS iscontain;

    返回結果:

    +-----------+
    | iscontain |
    +-----------+
    | true      |
    +-----------+
  • 樣本2

    SELECT ST_Contains(ST_GeomFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))'),5,5) AS iscontain;

    返回結果:

    +-----------+
    | iscontain |
    +-----------+
    | true      |
    +-----------+
  • 樣本3

    SELECT ST_Contains(ST_GeomFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))'), ST_GeomFromText('POINT(180 23)')) AS iscontain;

    返回結果:

    +-----------+
    | iscontain |
    +-----------+
    | false     |
    +-----------+
  • 樣本4

    SELECT ST_Contains(ST_GeomFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))'),180,23) AS iscontain;

    返回結果:

    +-----------+
    | iscontain |
    +-----------+
    | false     |
    +-----------+

ST_DWithin

如果兩個Geometry對象的二維平面距離在指定範圍內,ST_DWithin函數則返回true。

文法

  • 判斷對象geomA和對象geomB的二維平面距離是否在指定範圍內

    boolean ST_DWithin(geometry geomA, geometry geomB, double distanceOfSrid)
  • 判斷對象geom和點座標(x,y)的二維平面距離是否在指定範圍內

    boolean ST_DWithin(geometry geom, double x, double y, double distanceOfSrid)

參數說明

參數

描述

geomA

指定的第一個Geometry對象。

geomB

指定的第二個Geometry對象。

distanceOfSrid

在SRID 4326下的距離,單位為degree。

geom

指定的Geometry對象。

x

座標經度x值。

y

座標緯度y值。

說明

Geometry對象支援Point、LineString、Polygon、MultiPoint、MultiLineString、MultiPolygon和GeometryCollection類型。

樣本

  • 樣本1

    SELECT ST_DWithin(ST_GeomFromText('POINT(5 5)'), ST_GeomFromText('POINT(6 6)'), 10) AS iswithin;

    返回結果:

    +----------+
    | iswithin |
    +----------+
    | true     |
    +----------+
  • 樣本2

    SELECT ST_DWithin(ST_GeomFromText('POINT(5 5)'),6,6,10) AS iswithin;

    返回結果:

    +----------+
    | iswithin |
    +----------+
    | true     |
    +----------+

ST_DWithinSphere

如果兩個Geometry對象的球面距離在指定範圍內,ST_DWithinSphere函數則返回true。

文法

  • 判斷對象geomA和對象geomB的球面距離是否在指定範圍內

    boolean ST_DWithinSphere(geometry geomA, geometry geomB, double distance)
  • 判斷對象geom和點座標(x,y)的球面距離是否在指定範圍內

    boolean ST_DWithinSPhere(geometry geom, double x, double y, double distance)

參數說明

參數

描述

geomA

指定的第一個Geometry對象。

geomB

指定的第二個Geometry對象。

distance

球面距離,單位為米。球面距離的判斷存在一定的誤差,誤差為厘米層級。

geom

指定的Geometry對象。

x

座標經度x值。

y

座標緯度y值。

說明

Geometry對象支援Point、LineString、Polygon、MultiPoint、MultiLineString、MultiPolygon和GeometryCollection類型。

樣本

  • 樣本1:球面上POINT(120 36)和POINT(116 40)之間的距離為566034.7930717631米,距離在指定的範圍內,ST_DWithinSphere函數則返回true。

    SELECT ST_DWithinSphere(ST_GeomFromText('POINT(120 36)'), ST_GeomFromText('POINT(116 40)'), 570000) AS iswithin;

    返回結果:

    +----------+
    | iswithin |
    +----------+
    | true     |
    +----------+
  • 樣本2

    SELECT ST_DWithinSphere(ST_GeomFromText('POINT(120 36)'),116,40,570000) AS iswithin;

    返回結果:

    +----------+
    | iswithin |
    +----------+
    | true     |
    +----------+

ST_Intersects

判斷兩個Geometry對象是否相交。如果兩個Geometry對象有任意共用空間的部分,那麼兩個Geometry對象相交,ST_Intersects函數則返回true。

文法

  • 判斷對象geomA和對象geomB的是否相交

    boolean ST_Intersects(geometry geomA, geometry geomB)
  • 判斷對象geom和點座標(x,y)是否相交

    boolean ST_Intersects(geometry geom, double x, double y)

參數說明

參數

描述

geomA

指定的第一個Geometry對象。

geomB

指定的第二個Geometry對象。

geom

指定的Geometry對象。

x

座標經度x值。

y

座標緯度y值。

說明

Geometry對象支援Point、LineString、Polygon、MultiPoint、MultiLineString、MultiPolygon和GeometryCollection類型。

樣本

  • 樣本1

    SELECT ST_Intersects(ST_GeomFromText('POINT(0 0)'), ST_GeomFromText('LINESTRING ( 2 0, 0 2 )')) AS isinter;

    返回結果:

    +---------+
    | isinter |
    +---------+
    | false   |
    +---------+
  • 樣本2

    SELECT ST_Intersects(ST_GeomFromText('LINESTRING ( 2 0, 0 2 )'), 0, 0) AS isinter;

    返回結果:

    +---------+
    | isinter |
    +---------+
    | false   |
    +---------+
  • 樣本3

    SELECT ST_Intersects(ST_GeomFromText('POINT(0 0)'), ST_GeomFromText('LINESTRING ( 0 0, 0 2 )')) AS isinter;

    返回結果:

    +---------+
    | isinter |
    +---------+
    | true    |
    +---------+
  • 樣本4

    SELECT ST_Intersects(ST_GeomFromText('LINESTRING ( 0 0, 0 2 )'), 0, 0) AS isinter;

    返回結果:

    +---------+
    | isinter |
    +---------+
    | true    |
    +---------+

ST_Overlaps

如果兩個Geometry對象在空間上有重疊的部分,但不存在其中一個完全包含另一個的情況,ST_Overlaps函數則返回true。

文法

  • 判斷對象geomA和對象geomB在空間上是否有重疊的部分

    boolean ST_Overlaps(geometry geomA, geometry geomB)
  • 判斷對象geom和點座標(x,y)在空間上是否有重疊的部分

    boolean ST_Overlaps(geometry geom, double x, double y)

參數說明

參數

描述

geomA

指定的第一個Geometry對象。

geomB

指定的第二個Geometry對象。

geom

指定的Geometry對象。

x

座標經度x值。

y

座標緯度y值。

說明

Geometry對象支援Point、LineString、Polygon、MultiPoint、MultiLineString、MultiPolygon和GeometryCollection類型。

樣本

  • 樣本1

    SELECT ST_Overlaps(ST_GeomFromText('LINESTRING(0 0,0 2)'),ST_GeomFromText('LINESTRING(0 1,0 3)')) as overlaps;

    返回結果:

    +----------+
    | overlaps |
    +----------+
    | true     |
    +----------+
  • 樣本2

    SELECT ST_Overlaps(ST_GeomFromText('LINESTRING(0 0,0 2)'),ST_GeomFromText('POINT(0 1)')) as overlaps;

    返回結果:

    +----------+
    | overlaps |
    +----------+
    | false    |
    +----------+
  • 樣本3

    SELECT ST_Overlaps(ST_GeomFromText('LINESTRING(0 0,0 2)'), 0, 1) as overlaps;

    返回結果:

    +----------+
    | overlaps |
    +----------+
    | false    |
    +----------+

ST_Within

如果Geometry對象A完全在Geometry對象B內,ST_Within函數則返回true。

文法

  • 判斷對象geomA和對象geomB的內含項目關聯性

    boolean ST_Within(geometry geomA, geometry geomB)
  • 判斷對象geom和點座標(x,y)的內含項目關聯性

    bboolean ST_Within(double x, double y, geometry geom)

參數說明

參數

描述

geomA

指定的第一個Geometry對象。

geomB

指定的第二個Geometry對象。

geom

指定的Geometry對象。

x

座標經度x值。

y

座標緯度y值。

說明
  • Geometry對象支援Point、LineString、Polygon、MultiPoint、MultiLineString、MultiPolygon和GeometryCollection類型。

  • 如果ST_Within(A,B)的返回結果與ST_Within(B,A)的返回結果都為true,則認為Geometry對象A和Geometry對象B在空間上相同。

樣本

  • 樣本1

    SELECT ST_Within(ST_GeomFromText('POINT(5 5)'), ST_GeomFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))')) AS iswithin;

    返回結果:

    +----------+
    | iswithin |
    +----------+
    | true     |
    +----------+
  • 樣本2

    SELECT ST_Within(5, 5, ST_GeomFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))')) AS iswithin;

    返回結果:

    +----------+
    | iswithin |
    +----------+
    | true     |
    +----------+

ST_Equals

如果給定的Geometry對象在空間上相等,則返回true。

文法

boolean ST_Equals(geometry geomA, geometry geomB);

參數說明

參數

描述

geomA

指定的第一個Geometry對象。

geomB

指定的第二個Geometry對象。

說明
  • 空間上相等是指兩個Geometry對象既滿足ST_Within(A,B)=true條件又滿足ST_Within(B,A)=true條件。Geometry對象中的點的順序可能不一致,但空間結構是一致的。

  • 如果任一對象為無效的空間對象,則函數將返回false。

樣本

  • 樣本一:判斷LINESTRING(0 0, 10 10)LINESTRING(0 0, 10 10)的空間結構是否一致。

    SELECT ST_Equals(ST_GeomFromText('LINESTRING(0 0, 10 10)'),
        ST_GeomFromText('LINESTRING(0 0, 5 5, 10 10)'));

    返回結果為true

  • 樣本二

    SELECT ST_Equals(ST_GeomFromText('LINESTRING(0 0, 5 5)'),
        ST_GeomFromText('POINT(0 0)'));

    返回結果為false