PostGIS之空间关系

postgis,空间,关系 · 浏览次数 : 114

小编点评

**ST_Within (A, B)** 测试每个点A是否位于B的几何空间内,以及其内点与B几何空间内点的点在共同点。 **ST_Contains (geomA, geomB)** 测试每个点B是否位于A的几何空间内,以及其内点与A几何空间内点的点在共同点。 **ST_Distance (A, B)** 返回两点之间的距离,其中两点是几何或地理空间的值。 **ST_DWithin(A, B, distance)** 测试两点之间的距离是否小于或等于给定的距离。 **ST_Disjiont** 测试两点是否在给定的距离内相离。

正文

1. 概述

PostGIS 是PostgreSQL数据库一个空间数据库扩展,它添加了对地理对象的支持,允许在 SQL 中运行空间查询

PostGIS官网:About PostGIS | PostGIS

PostGIS官方教程:PostGIS 简介 — Introduction to PostGIS

PostGIS相关教程:文章目录汇总 - 知乎 (zhihu.com)

本文基于官方教程描述PostGIS中的空间关系

数据准备可参考:

数据介绍可参考:

2. 空间关系

2.1 空间相等

空间相等即所有点坐标相同,使用的函数主要为:

  • ST_Equals (A, B) Tests if two geometries include the same set of points

这里先查询一个点的空间位置,再通过查空间位置得到相等的点

--  先查询一个点的空间位置再通过查空间位置相等的点
SELECT name, geom FROM nyc_subway_stations WHERE name = 'Broad St';

得到Broad St的geom: 0101000020266900000EEBD4CF27CF2141BC17D69516315141

SELECT name, geom FROM nyc_subway_stations WHERE ST_Equals(geom, '0101000020266900000EEBD4CF27CF2141BC17D69516315141');

image-20230104004131435

2.2 空间相交

空间相交即空间物体存在交集,空间相离即不存在交集,使用的函数主要为:

  • ST_Intersects (A, B)Tests if two geometries intersect (they have at least one point in common)
  • ST_Disjoint (A, B) Tests if two geometries have no points in common
  • ST_Crosses (g1, g2) Tests if two geometries have some, but not all, interior points in common
  • ST_Overlaps (A, B) Tests if two geometries have the same dimension and intersect, but each has at least one point not in the other

查询与Broad St相交的社区,即所在的社区:

SELECT name, boroname
FROM nyc_neighborhoods
WHERE ST_Intersects(geom, '0101000020266900000EEBD4CF27CF2141BC17D69516315141');

image-20230104010158661

查询与Broad St相离的社区:

SELECT name, boroname
FROM nyc_neighborhoods
WHERE ST_Disjoint(geom, '0101000020266900000EEBD4CF27CF2141BC17D69516315141');

image-20230104010415908

  • 注意:ST_Disjoint和NOT ST_Intersects相同,但是NOT ST_Intersects由于使用空间索引速度更快

2.3 空间相接

空间相接即空间物体接触在一起但没有相交,使用的函数主要为:

  • ST_Touches (A, B) Tests if two geometries have at least one point in common, but their interiors do not intersect

查询与Financial District相接的社区:

SELECT name, boroname
FROM nyc_neighborhoods
WHERE ST_Touches(geom, '010600002026690000010000000103000000010000003E000000AE26601878CD2141A8154649043251416FD27013A2CE2141028D0997EA31514140B5133529D121411CA81D6DBA31514108E68423BBD22141B677D235AE3151417E26E8B3BED621418B4ABFFE383151415DE06CD1E5D42141CC24CC8C173151417E83EE8BB5D5214181364920FA30514186F762DD71D521411FC0E6C3F2305141E57CAD13AED42141D445EB861131514186ED801882D421410CA0A9D20E31514102B52A1A2AD5214103851C0FEC305141DFDD3D5706D5214161A7A062E7305141C73BCF8D4ED4214109F31DD306315141361F76C016D4214125D34D7601315141594A198FDED421417C62AFB4E23051412705A793B2D42141D0BD6C00E03051410F1A30F8E2D321411F0DF51AFC3051413955283AB7D3214103C388C2F63051415455A56792D42141F82177E5D7305141E2BF68695FD4214192A3C503D230514106F5E64887D321412CF4CB63F33051412C20E7A33FD32141FD87FD5CEB30514151C44B53FBD32141F7C9F23FCD3051415E52B3AED7D32141BD4D6541C7305141D361F4F90BD32141194693AFE430514171FBED91C4D22141E0419A04DA3051414D11DC1D94D32141C97EFD92BE3051415AA0819764D321419789EE94B63051415EB1130B7DD221410206BCABD03051418C20A84FF5D12141423080E9C530514197A4BACBB8D221410D456D73AA3051419929621795D221416A53F21DA5305141EA88BA7CD1D121416CD920E6C1305141458706338CD1214143DF03BFB53051411C9F4EE28AD22141C92023A190305141ECDFA4B826D221417B2706CF89305141E4CF36C31AD1214197E78734A9305141599DD95F30D02141CA753BDB8930514154D14E76DCCF21415A211D6E83305141EEDF4CD062D021410B2D607871305141A1E95BBE1AD02141F32D06116E305141EA5B2DBC95CF2141073CBE307D305141AB2BB0BB77CF21418BA3F0997830514194115BBBEBCE21413B3DAD7A703051416A379B7F02CF2141D7C1213F693051417817C35646CE2141E0F8F81060305141543781C758CD2141FCC490B15A3051415ECACC0248CD2141300236446B305141573E8FAF13CD214159517BDA6B3051415584FB8EFDCC2141703D1CB860305141DC857DD55ACC21419ACD859A6A305141B3C7A643E5CB2141EB98F85575305141BE9D309AFACA214161D6387B9730514104E75F4093CA21414F3A0F1FAE305141BAF3E7DE96CA2141EB1C5599BB305141CB16F111D8CA21413374C9E5BA30514139C75D5236CB2141FF265085C63051411A6F66C354CB2141394772D1C7305141B3C51CE272CB214173853A5ADA30514145238FAA46CB214185B878C1EB30514104AD1CE5B9CC21413F9E327D8B315141AE26601878CD2141A815464904325141');

image-20230104011941818

2.4 空间包含

空间包含即一个空间物体包含在另一个物体内,主要使用的函数有:

  • ST_Within (A, B) Tests if every point of A lies in B, and their interiors have a point in common
  • ST_Contains (geomA, geomB) Tests if every point of B lies in A, and their interiors have a point in common

查询包含Broad St的社区:

SELECT name, boroname
FROM nyc_neighborhoods
WHERE ST_Contains(geom, '0101000020266900000EEBD4CF27CF2141BC17D69516315141');

image-20230104012329155

2.5 空间相离

空间相离即Disjoint,前面已叙述有ST_Disjiont函数,这里主要叙述判断相离距离,主要函数有:

  • ST_Distance (A, B) Returns the distance between two geometry or geography values.
  • ST_DWithin(A, B, distance) Tests if two geometries are within a given distance

查询距离Broad St的10米内的街道:

SELECT name
FROM nyc_streets
WHERE ST_DWithin(geom, '0101000020266900000EEBD4CF27CF2141BC17D69516315141', 10);

image-20230104013550848

3. 参考资料

[1]11. Spatial Relationships — Introduction to PostGIS

[2]PostGIS教程八:空间关系 - 知乎 (zhihu.com)

[3]PostGIS 3.3.3dev Manual

[4]PostGIS Cheat Sheet

与PostGIS之空间关系相似的内容: