使用 ST_Disjoint() 函数会产生意想不到的结果

Using the ST_Disjoint() Function gives unexpected result

我正在摆弄这个数据集 http://s3.cleverelephant.ca/postgis-workshop-2020.zip. It is used in this workshop http://postgis.net/workshops/postgis-intro/spatial_relationships.html

我想识别所有没有地铁站的地图项。我认为这个空间连接相当简单

SELECT
  census.boroname,
  COUNT(census.boroname)
FROM nyc_census_blocks AS census
    JOIN nyc_subway_stations AS subway
        ON ST_Disjoint(census.geom, subway.geom)
GROUP BY census.boroname;

但是,结果集太大了。

"Brooklyn"  4753693
"Manhattan" 1893156
"Queens"    7244123
"Staten Island" 2473146
"The Bronx" 2683246

当我运行一个测试

SELECT COUNT(id) FROM nyc_census_blocks;

结果是 38794。所以 nyc_census_blocks 中的特征比我在空间连接的结果集中的特征要少。

这是为什么?我哪里出错了?

问题是,对于 ST_Disjoint,您会得到 nyc_census_block 的每条记录与 nyc_subway_stations 不相交的站点总数,这意味着在没有交叉的情况下nyc_subway_stations 的所有记录 (491)。这就是为什么你得到如此高的计数。

或者,您可以计算有多少地铁和人口普查街区相交,例如在 CTE 或子查询中,在另一个查询中计算其中有多少 return 0:

WITH j AS (
 SELECT 
   gid,census.boroname,
  (SELECT count(*) 
   FROM nyc_subway_stations subway
   WHERE ST_Intersects(subway.geom,census.geom)) AS qt
 FROM nyc_census_blocks AS census
)
SELECT boroname,count(*)
FROM j WHERE qt = 0
GROUP BY boroname;

   boroname    | count 
---------------+-------
 Brooklyn      |  9517
 Manhattan     |  3724
 Queens        | 14667
 Staten Island |  5016
 The Bronx     |  5396
(5 rows)