我有一个包含以下架构的表:
Table "public.gps_log"
Column | Type
---------------------------+-----------------------------
id | integer
logged_at | timestamp without time zone
lonlat | geography(Point,4326)
另一个具有以下架构:
Table "public.waypoint"
Column | Type
---------------------------+-----------------------------
id | integer
lonlat | geography(Point,4326)
range | numeric
gps_log
包含来自GPS的每分钟位置报告,waypoint
包含一组带有位置的航点。
我有以下查询:
SELECT gps_log.id as id1, waypoint.id as id2, ST_Distance(gps_log.lonlat, waypoint.lonlat) as dist
FROM gps_log, waypoint
WHERE ST_DWithin(gps_log.lonlat, waypoint.lonlat, waypoint.range)
ORDER BY id1;
返回以下结果:
id1 | id2 | dist
-------+------+----------------
4499 | 1118 | 2580.557160943 <- first and closest dist in this cluster of id2=1118
4500 | 1118 | 2580.557160943
4501 | 1119 | 3861.038787463 <- etc. ...
4502 | 1119 | 3861.038787463
4503 | 1118 | 885.481236082 <-
4504 | 1118 | 885.481236082
4505 | 1119 | 2114.289192152 <-
4506 | 1119 | 2114.289192152
4507 | 1118 | 3209.147139384
4508 | 1118 | 3209.147139384
4510 | 1118 | 2194.494307877 <-
4511 | 1118 | 2194.494307877
4514 | 1118 | 2253.163728865
4515 | 1117 | 3155.907772254 <-
4518 | 1117 | 3875.930499045
4519 | 1118 | 3834.344459575 <-
4522 | 1117 | 2025.333877603 <-
4523 | 1117 | 2025.333877603
4603 | 1116 | 1075.801799628
4604 | 1116 | 1075.801799628
4607 | 1116 | 663.907042351
4608 | 1116 | 663.907042351
4611 | 1116 | 319.142003353 <-
4612 | 1116 | 319.142003353
4614 | 1116 | 535.03813233
4615 | 1116 | 535.03813233
4630 | 1132 | 2134.348193208
4631 | 1181 | 2165.19731156 <-
4784 | 1134 | 337.398349813 <-
4785 | 1134 | 337.398349813
4788 | 1135 | 1388.859874755 <-
4789 | 1135 | 1388.859874755
我最终希望查询返回最接近每个航点的GPS位置报告,并且可以多次传递航点。
上面的箭头表示我希望作为查询结果返回的记录 - 基本上每次有一个新的航点ID集群时,都会返回距航点最近距离的位置报告。
现在,我的解决方案是运行上面的查询,然后在代码中处理结果以遍历数组并找到正确的值。