PostGIS 是PostgreSQL数据库一个空间数据库扩展,它添加了对地理对象的支持,允许在 SQL 中运行空间查询
PostGIS官网:About PostGIS | PostGIS
PostGIS官方教程:PostGIS 简介 — Introduction to PostGIS
PostGIS相关教程:文章目录汇总 - 知乎 (zhihu.com)
本文基于官方教程描述PostGIS中的空间关系
数据准备可参考:
数据介绍可参考:
空间相等即所有点坐标相同,使用的函数主要为:
这里先查询一个点的空间位置,再通过查空间位置得到相等的点
-- 先查询一个点的空间位置再通过查空间位置相等的点
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');
空间相交即空间物体存在交集,空间相离即不存在交集,使用的函数主要为:
查询与Broad St相交的社区,即所在的社区:
SELECT name, boroname
FROM nyc_neighborhoods
WHERE ST_Intersects(geom, '0101000020266900000EEBD4CF27CF2141BC17D69516315141');
查询与Broad St相离的社区:
SELECT name, boroname
FROM nyc_neighborhoods
WHERE ST_Disjoint(geom, '0101000020266900000EEBD4CF27CF2141BC17D69516315141');
空间相接即空间物体接触在一起但没有相交,使用的函数主要为:
查询与Financial District相接的社区:
SELECT name, boroname
FROM nyc_neighborhoods
WHERE ST_Touches(geom, '010600002026690000010000000103000000010000003E000000AE26601878CD2141A8154649043251416FD27013A2CE2141028D0997EA31514140B5133529D121411CA81D6DBA31514108E68423BBD22141B677D235AE3151417E26E8B3BED621418B4ABFFE383151415DE06CD1E5D42141CC24CC8C173151417E83EE8BB5D5214181364920FA30514186F762DD71D521411FC0E6C3F2305141E57CAD13AED42141D445EB861131514186ED801882D421410CA0A9D20E31514102B52A1A2AD5214103851C0FEC305141DFDD3D5706D5214161A7A062E7305141C73BCF8D4ED4214109F31DD306315141361F76C016D4214125D34D7601315141594A198FDED421417C62AFB4E23051412705A793B2D42141D0BD6C00E03051410F1A30F8E2D321411F0DF51AFC3051413955283AB7D3214103C388C2F63051415455A56792D42141F82177E5D7305141E2BF68695FD4214192A3C503D230514106F5E64887D321412CF4CB63F33051412C20E7A33FD32141FD87FD5CEB30514151C44B53FBD32141F7C9F23FCD3051415E52B3AED7D32141BD4D6541C7305141D361F4F90BD32141194693AFE430514171FBED91C4D22141E0419A04DA3051414D11DC1D94D32141C97EFD92BE3051415AA0819764D321419789EE94B63051415EB1130B7DD221410206BCABD03051418C20A84FF5D12141423080E9C530514197A4BACBB8D221410D456D73AA3051419929621795D221416A53F21DA5305141EA88BA7CD1D121416CD920E6C1305141458706338CD1214143DF03BFB53051411C9F4EE28AD22141C92023A190305141ECDFA4B826D221417B2706CF89305141E4CF36C31AD1214197E78734A9305141599DD95F30D02141CA753BDB8930514154D14E76DCCF21415A211D6E83305141EEDF4CD062D021410B2D607871305141A1E95BBE1AD02141F32D06116E305141EA5B2DBC95CF2141073CBE307D305141AB2BB0BB77CF21418BA3F0997830514194115BBBEBCE21413B3DAD7A703051416A379B7F02CF2141D7C1213F693051417817C35646CE2141E0F8F81060305141543781C758CD2141FCC490B15A3051415ECACC0248CD2141300236446B305141573E8FAF13CD214159517BDA6B3051415584FB8EFDCC2141703D1CB860305141DC857DD55ACC21419ACD859A6A305141B3C7A643E5CB2141EB98F85575305141BE9D309AFACA214161D6387B9730514104E75F4093CA21414F3A0F1FAE305141BAF3E7DE96CA2141EB1C5599BB305141CB16F111D8CA21413374C9E5BA30514139C75D5236CB2141FF265085C63051411A6F66C354CB2141394772D1C7305141B3C51CE272CB214173853A5ADA30514145238FAA46CB214185B878C1EB30514104AD1CE5B9CC21413F9E327D8B315141AE26601878CD2141A815464904325141');
空间包含即一个空间物体包含在另一个物体内,主要使用的函数有:
查询包含Broad St的社区:
SELECT name, boroname
FROM nyc_neighborhoods
WHERE ST_Contains(geom, '0101000020266900000EEBD4CF27CF2141BC17D69516315141');
空间相离即Disjoint,前面已叙述有ST_Disjiont函数,这里主要叙述判断相离距离,主要函数有:
查询距离Broad St的10米内的街道:
SELECT name
FROM nyc_streets
WHERE ST_DWithin(geom, '0101000020266900000EEBD4CF27CF2141BC17D69516315141', 10);
[1]11. Spatial Relationships — Introduction to PostGIS