I have this function working fine:
CREATE or replace FUNCTION pointscanner(
IN source integer,
IN target integer,
IN k integer,
IN directed boolean,
IN quant integer
)
RETURNS table(way geometry, name text, distance double precision, tags hstore, "operator" text,
admin_level text, z_order integer) AS $$
DECLARE
geomRoute geometry;
routeBB box2d;
BEGIN
SELECT INTO geomRoute ST_Union(geom) from route_agg($1, $2, $3, $4);
routeBB := ST_Extent(geomRoute);
RETURN QUERY
SELECT pt.way, pt.name, ST_Distance( rt.geom, ST_Transform(pt.way,4326) ) as distance,
pt.tags, pt.operator, pt.admin_level, pt.z_order
FROM planet_osm_point pt, route_agg($1, $2, $3, $4) rt
where routeBB && ST_Transform(pt.way,4326)
order by distance asc limit $5;
END; $$
LANGUAGE 'plpgsql' VOLATILE;
But as you can see I'm calculating the route twice (route_agg
) and sometimes it may be very costly. How can I improve this to run route_agg
just once?