在从该站点获得大量帮助后,我终于创建了一组功能来完成我想要的工作。谢谢你,但似乎还有最后一个问题,即有效的内存使用。这是问题所在:
在 postgreSQL 9.3 中,使用 postGIS 2.1 和 pgRouting 2.0,我创建了一个函数,该函数使用 pgrouting 函数 pgr_trsp 计算两点之间的路线并返回几何(线串)值。这是代码:
CREATE OR REPLACE FUNCTION fm_pgr2geom(edge1 integer, pos1 double precision, edge2 integer, pos2 double precision)
RETURNS geometry AS
$BODY$
--We have to do a routing query. And declare a cursor for it
DECLARE resc CURSOR FOR
SELECT * FROM pgr_trsp (
'SELECT * FROM th_2po_4pgr',
$1, $2, $3, $4, false, true);
doline geometry[];
temp_point geometry;
geom geometry;
temp_rec RECORD;
n integer;
BEGIN
--Append all the edges
FOR temp_rec IN SELECT * FROM pgr_trsp (
'SELECT * FROM th_2po_4pgr',
$1, $2, $3, $4, false, true) LOOP
doline := array_append(
doline, (SELECT map.geom_way FROM th_2po_4pgr map WHERE map.id = temp_rec.id2));
END LOOP;
--Remove 1st and last edge
n := array_length (doline, 1);
doline := doline [2:n-1];
--Find startpoint and append to doline
doline := array_prepend(
ST_LineInterpolatePoint((SELECT map.geom_way FROM th_2po_4pgr map WHERE map.id = $1),$2),doline);
--Append the endpoint
doline := array_append(
doline,ST_LineInterpolatePoint((SELECT map.geom_way FROM th_2po_4pgr map WHERE map.id = $3),$4));
geom := ST_MakeLine(doline);
RETURN geom;
EXCEPTION
WHEN SQLSTATE 'XX000' THEN RETURN NULL;
WHEN SQLSTATE '38001' THEN RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION fm_pgr2geom(integer, double precision, integer, double precision)
OWNER TO postgres;
此函数在另一个函数中使用几何结果批量更新大表(800k+)。这里供参考:
CREATE OR REPLACE FUNCTION fm_seqrouting()
RETURNS integer AS
$BODY$
--Declarations
DECLARE
r record;
i integer;
BEGIN
--CODE to calculate routes and update table
i := 0;
FOR r IN
SELECT veh_id
,dt
,map_edge_id AS map_id1
,map_edge_pos AS map_pos1
,lead(map_edge_id) OVER w AS map_id2
,lead(map_edge_pos) OVER w AS map_pos2
FROM taxilocs
WINDOW w AS (ORDER BY veh_id, dt)
LOOP
UPDATE taxilocs
SET geom_route = fm_pgr2geom (r.map_id1,r.map_pos1,r.map_id2,r.map_pos2)
WHERE r.veh_id = taxilocs.veh_id AND r.dt=taxilocs.dt;
i := i + 1;
END LOOP;
RETURN i;
END;
$BODY$
LANGUAGE plpgsql;
例外是绝对必要的,因为它们处理某些情况,即上述表中缺少某些数据,或者找不到路由路径。但是,似乎它们导致了问题,即更新查询在几分钟后崩溃。执行几分钟后我收到的消息是:
错误:内存不足
SQL 状态:53200
详细信息:大小为 640000 的请求失败。
所以问题是:我怎样才能有效地使用或重新编码这个函数来更新我想要的表?有任何想法吗?
先感谢您!