【问题标题】:PostGIS bounding box query returned strange resultsPostGIS 边界框查询返回奇怪的结果
【发布时间】:2014-12-31 20:10:56
【问题描述】:

我尝试了以下 SQL 命令:

CREATE TABLE places(
    lat_lng geography(Point,4326),
    place_name varchar(50)
);

CREATE INDEX places_lat_lng_idx ON places USING gist(lat_lng);

INSERT INTO places values ('POINT(-126.4 45.32)', 'Food Bar1');
INSERT INTO places values ('POINT(-126.4 47.32)', 'Food Bar2');
INSERT INTO places values ('POINT(-125.4 47.42)', 'Food Bar3');

SELECT place_name, ST_AsText(lat_lng) as point
FROM places WHERE places.lat_lng && 
  ST_MakeEnvelope(-130.0, 44.0,
                  -100.0, 46.7, 4326);

结果是:

 place_name |        point        
------------+---------------------
 Food Bar1  | POINT(-126.4 45.32)
 Food Bar2  | POINT(-126.4 47.32)
 Food Bar3  | POINT(-125.4 47.42)

这对我来说看起来不正确,因为 ymax 是 46.7,但是“Food Bar2”和“Food Bar3”的 ymax 值分别为 47.32 和 47.42。问题出在哪里?

【问题讨论】:

  • 使用 ST_Intersects 或 ST_Contains 代替 &&。

标签: postgresql geospatial postgis


【解决方案1】:

Here is your geography envelope:

在这里,您的查询点。使用 ST_Segmentize 将信封展平为笛卡尔空间:

SELECT ST_Segmentize(
   ST_MakeEnvelope(-130.0, 44.0,
                   -100.0, 46.7, 4326)::geography,50000);

因此,点应该在地理信封内是正确的,但是您使用了&& 边界框运算符,它忽略了几何形状。地理信封的边界框如下所示:

显示边界框中的所有点。

使用以下内容修复查询:

SELECT place_name, ST_AsText(lat_lng) as point
FROM places
WHERE ST_Intersects(
    ST_MakeEnvelope(-130.0, 44.0,
                    -100.0, 46.7, 4326), 
    places.lat_lng)

【讨论】:

  • 谢谢迈克。这是一个很好的解释。
猜你喜欢
  • 2019-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-06
  • 2014-08-27
  • 2018-09-01
  • 2023-03-25
  • 2012-05-30
相关资源
最近更新 更多