[postgis-users] PLPGSQL Function to Calculate Bearing

Rémi Cura remi.cura at gmail.com
Mon Nov 24 10:34:58 PST 2014

first you don't need plpgsql for this, you can do it with windows functions

for the following I assume you have some order available on your point
data, the order is stored in "id" column :

CREATE TABLE my_azimuth_data AS
SELECT id, wgs_geom --, ST_X(wgs_geom), ST_Y(wgs_geom), ST_Z(wgs_geom) --
X,Y,Z seems unecessary
FROM points_table
WHERE ST_Within(points_table.wgs_geom, ST_GeomFromtext('POLYGON...',4326) =

SELECT *, ST_Azimuth(wgs_geom,lead(wgs_geom) OVER (ORDER BY id ASC)  ) AS
FROM my_azimuth_data

--note : you may need to deal with the last row, because it has nothing
after it , so I don't know what it's azymuth should be...

now if you really want to use plpgsql :

    DROP FUNCTION IF EXISTS get_bearings_from_points();
    CREATE OR REPLACE FUNCTION get_bearings_from_points()
    RETURNS  TABLE(id int, ogeom geometry, my_azimuth double) AS
        r record ; --generic type, will hold any row
        old_point geometry := NULL; --we store the value of previous
        FOR r IN
            SELECT *
            FROM my_azimuth_data --loop on the point
            ORDER BY id ASC
            --note : you may want to deal with the first row in a peculiar
            id := r.id; --filling the output row
            ogeom:= r.wgs_geom ;
            my_azimuth := ST_Azimuth(old_point, r.wgs_geom) ;
            old_point := r.wgs_geom ; --updating the old_point with new
value for next iteration
        RETURN NEXT; --outputing the row
        END LOOP;

    LANGUAGE plpgsql;

If you are more familiar with python you may benefit from using pl/pythonu.


2014-11-24 18:33 GMT+01:00 Paul & Caroline Lewis <paulcaz80 at hotmail.com>:

> Basically I can't get my head around the syntax of plpgsql and would
> appreciate some help with the following efforts.
> 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:
>     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)
> 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.
> 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.
> However my efforts at understanding basic data access and handling within
> a plpgsql function are failing miserably.
> An example of the current version of the function I'm trying to create is
> as follows:
>     CREATE TYPE bearing_type AS (x numeric, y numeric, z numeric, bearing
> numeric);
>     DROP FUNCTION IF EXISTS get_bearings_from_points();
>     CREATE OR REPLACE FUNCTION get_bearings_from_points()
>     RETURNS SETOF bearing_type AS
>     $BODY$
>         rowdata points_table%rowtype;
>         returndata bearing_type;
>     BEGIN
>         FOR rowdata IN
>             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)
>         LOOP
>             returndata.x := ST_X(rowdata.wgs_geom);
>             returndata.y := ST_Y(rowdata.wgs_geom);
>             returndata.z := ST_Z(rowdata.wgs_geom);
>             returndata.bearing := ST_Azimuth(<current_point> ,
> <next_point>)
>         RETURN NEXT returndata;
>         END LOOP;
>         RETURN;
>     END
>     $BODY$
>     LANGUAGE plpgsql;
> I should just be able to call this function as follows:
>     SELECT get_bearings_from_points();
> and get the desired result.
> Basically the problems are understanding how to access the rowdata
> properly such that I can read the current and next points.
> 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.
> Thanks
> Paul
> Any insights/help would be much appreciated
> _______________________________________________
> postgis-users mailing list
> postgis-users at lists.osgeo.org
> http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20141124/993c2932/attachment.html>

More information about the postgis-users mailing list