我有3张桌子:闪电,电力线,桅杆。
主要领域:
lightnings.geo_belief
- 可能击中的椭圆。powerlines.geo_path
- 电力线路径的地理折线。masts.geo_coordinates
- 放置桅杆的地理位置。任务:
powerline_corridor()
)lightning.geo_ellipse
升级到masts.geo_coordinates
。所以我可以选择闪电:
SELECT l.*
FROM lightnings l
JOIN ( SELECT geo_path, powerline_corridor(geo_path, 5000::smallint) AS geo_zone
FROM powerlines WHERE id=1)
AS by_pl
ON ST_Intersects(by_pl.geo_zone, l.geo_belief)
此外,我还有namos_nearest_mast(powerlines.id, lightnings.geo_belief)
函数:
CREATE OR REPLACE FUNCTION public.namos_nearest_mast (
powerline_id integer,
geo public.geometry
)
RETURNS public.obj_powerline_masts AS
$body$
SELECT *
FROM obj_powerline_masts
WHERE powerline_id=$1
ORDER BY $2 <-> geo_coordinates ASC
LIMIT 1
$body$
LANGUAGE 'sql';
难道你不能建议选择好的解决方案吗?
答案 0 :(得分:1)
以下是我自己完成的所有事情:
SELECT
t.*,
ROUND(st_distance(namos_transform_meters(m.geo_coordinates), namos_transform_meters(t.geo_belief))) AS dist_m
FROM obj_powerline_masts AS m
JOIN
(
SELECT
l.*,
(SELECT id FROM nearest_mast(1, l.geo_belief)) AS mast_id
FROM lightnings l
JOIN (SELECT geo_path, powerline_corridor(geo_path, 5000::smallint) AS geo_zone FROM powerlines WHERE id=1) AS by_pl ON ST_Intersects(by_pl.geo_zone, l.geo_belief)
LIMIT 50 OFFSET 50
) AS t
ON t.mast_id=m.id
但我不确定这是否是最佳解决方案。例如,在PHP中,我无法对此类查询(例如使用分页进行抽象)应用dataProviders,因为我们不能以微不足道的方式影响子查询。