postgresql:显示边界框内的行程

kmb7vmvb  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(363)

我有一个 trips 包含用户出行信息的表,如下所示:

select * from trips limit 10;
 trip_id | daily_user_id | session_ids | seconds_start | lat_start  | lon_start  | seconds_end |  lat_end   |  lon_end   |     distance     
---------+---------------+-------------+---------------+------------+------------+-------------+------------+------------+------------------
  594221 |         16772 | {170487}    |    1561324555 | 41.1175475 | -8.6298934 |  1561325119 | 41.1554091 | -8.6283493 | 5875.39697884959
  563097 |          7682 | {128618}    |    1495295471 | 41.1782829 | -8.5950303 |  1495299137 | 41.1783908 | -8.5948965 | 5364.81067787512
  596303 |         17264 | {172851}    |    1578011699 | 41.5195598 | -8.6393526 |  1578012513 | 41.4614024 |  -8.717709 | 11187.7956426909
  595648 |         17124 | {172119}    |    1575620857 | 41.1553116 | -8.6439528 |  1575621885 | 41.1621821 | -8.6383042 | 1774.83365424607
  566061 |          8720 | {133624}    |    1509005051 | 41.1241975 | -8.5958988 |  1509006310 | 41.1424158 | -8.6101461 | 3066.40306678979
  566753 |          8947 | {134662}    |    1511127813 | 41.1887996 | -8.5844238 |  1511129839 | 41.2107519 | -8.5511712 | 5264.64026582458
  561179 |          7198 | {125861}    |    1493311197 | 41.1776935 | -8.5947254 |  1493311859 | 41.1773815 | -8.5947254 | 771.437257541019
  541328 |          2119 | {46950}     |    1461103381 |    41.1779 | -8.5949738 |  1461103613 | 41.1779129 | -8.5950202 | 177.610819150637
  535519 |           908 | {6016}      |    1460140650 | 41.1644658 | -8.6422775 |  1460141201 | 41.1642646 | -8.6423309 | 1484.61552373019
  548460 |          3525 | {102026}    |    1462289206 |  41.177689 |  -8.594679 |  1462289843 | 41.1734476 | -8.5916326 | 1108.05119077308
(10 rows)

任务是过滤在定义的边界框内开始和结束的行程 upper left: 41.24895, -8.68494 以及 lower right: 41.11591, -8.47569 .

70gysomp

70gysomp1#

因为坐标存储在x,y列中,所以必须使用 ST_MakePoint 创建正确的几何图形。之后,可以使用函数创建bbox ST_MakeEnvelope 并使用 ST_Contains ,例如。

WITH bbox(geom) AS (
  VALUES (ST_MakeEnvelope(-8.68494,41.24895,-8.47569,41.11591,4326))
)
SELECT * FROM trips,bbox
WHERE 
  ST_Contains(bbox.geom,ST_SetSRID(ST_MakePoint(lon_start,lat_start),4326)) AND
  ST_Contains(bbox.geom,ST_SetSRID(ST_MakePoint(lon_end,lat_end),4326));

注:以下为 CTE 不是真的需要,只是出于说明的目的在查询中。你可以重复这个步骤 ST_MakeEnvelope 函数的两个条件 WHERE 从句代替 bbox.geom . 此查询还假定srs WGS84 (4326).

r1zk6ea1

r1zk6ea12#

如果我理解正确的话,你可以比较一下起始坐标和结束坐标:

select t.*
from trips t
where lat_start >= 41.11591 and lat_start <= 41.24895 and
      lat_end >= 41.11591 and lat_end <= 41.24895 and
      long_start >= -8.68494 and long_start <= -8.47569 and
      long_end >= -8.68494 and long_end <= -8.47569

相关问题