<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'><div>Basically I can't get my head around the syntax of plpgsql and would appreciate some help with the following efforts.</div><div>I have a table containing 1000's of wgs84 points. The following SQL will retrieve a set of points within a bounding box on this table:</div><div><br></div><div>    SELECT id, ST_X(wgs_geom), ST_Y(wgs_geom), ST_Z(wgs_geom) FROM points_table INNER JOIN (SELECT ST_Transform(ST_GeomFromText('POLYGON((-1.73576102027 51.5059743629,-1.73591122397 51.5061067655,-1.73548743495 51.5062838333,-1.73533186682 51.5061514313,-1.73576102027 51.5059743629))',4326),) AS bgeom) AS t2 ON ST_Within(local_geom, t2.bgeom)</div><div><br></div><div>What I need to do is add a bearing/azimuth column to the results that describes the bearing at each point in the returned data set.</div><div>So the approach I'm trying to implement is to build a plpgsql function that can select the data as per above and calculate the bearing between each set of points in a loop.</div><div>However my efforts at understanding basic data access and handling within a plpgsql function are failing miserably.</div><div><br></div><div>An example of the current version of the function I'm trying to create is as follows:</div><div><br></div><div>    CREATE TYPE bearing_type AS (x numeric, y numeric, z numeric, bearing numeric);</div><div>    DROP FUNCTION IF EXISTS get_bearings_from_points();</div><div>    CREATE OR REPLACE FUNCTION get_bearings_from_points()</div><div>    RETURNS SETOF bearing_type AS</div><div>    $BODY$</div><div>    DECLARE</div><div>        rowdata points_table%rowtype;</div><div>        returndata bearing_type;</div><div>    BEGIN</div><div>        FOR rowdata IN</div><div>            SELECT nav_id, wgs_geom FROM points_table INNER JOIN (SELECT ST_Transform(ST_GeomFromText('POLYGON((-1.73576102027 53.5059743629,-1.73591122397 53.5061067655,-1.73548743495 53.5062838333,-1.73533186682 53.5061514313,-1.73576102027 53.5059743629))',4326),27700) AS bgeom) AS t2 ON ST_Within(local_geom, t2.bgeom)</div><div>        LOOP</div><div>            returndata.x := ST_X(rowdata.wgs_geom);</div><div>            returndata.y := ST_Y(rowdata.wgs_geom);</div><div>            returndata.z := ST_Z(rowdata.wgs_geom);</div><div>            returndata.bearing := ST_Azimuth(<current_point> , <next_point>)</div><div>        RETURN NEXT returndata;</div><div>        END LOOP;</div><div>        RETURN;</div><div>    END</div><div>    $BODY$</div><div>    LANGUAGE plpgsql;</div><div><br></div><div>I should just be able to call this function as follows:</div><div><br></div><div>    SELECT get_bearings_from_points();</div><div><br></div><div>and get the desired result.</div><div>Basically the problems are understanding how to access the rowdata properly such that I can read the current and next points.</div><div><br></div><div>In the above example I've had various problems from how to call the ST_X etc SQL functions and have tried EXECUTE select statements with errors re geometry data types.</div><div><br></div><div>Thanks</div><div><br></div><div>Paul</div><div><br></div><div><br></div><div>Any insights/help would be much appreciated</div>                                         </div></body>
</html>