[postgis-users] a line' s direction

Hartmut Tschauner hartmut.tschauner at gmail.com
Thu Sep 15 02:15:22 PDT 2005


Hi Ahmet,

The function below will calculate the azimuth between two points. You would 
pass the start and end points to get the direction of a line. I attach a SQL 
script that you can execute to create the function in your database.
Cheers,
Hartmut
 Hartmut Tschauner
Department of Archaeology
Seoul National University
Gwanak-Gu Sillim 9-dong San 56-1
Seoul, 151-742
Korea
ph. +82 (2) 880-9260

 CREATE OR REPLACE FUNCTION public.azimuth(geometry, geometry)

RETURNS float8 AS

'

DECLARE

geom1 ALIAS FOR $1;

geom2 ALIAS FOR $2;

geom2trans geometry;

BEGIN

IF geom1 IS NULL OR geom2 IS NULL THEN

RETURN NULL;

ELSE

IF isempty(geom1) OR isempty(geom2) OR geometrytype(geom1) != \'POINT\' OR 
geometrytype(geom2) != \'POINT\' THEN 

RETURN NULL;

END IF;

END IF;

IF srid(geom1) != -1 AND srid(geom2) != srid(geom1) THEN

geom2trans := transform(geom2, srid(geom1));

ELSE

geom2trans := geom2;

END IF;

IF x(geom1) = x(geom2trans) AND y(geom1) < y(geom2trans) THEN 

RETURN 0;

ELSIF x(geom1) = x(geom2trans) AND y(geom1) > y(geom2trans) THEN 

RETURN 180;

ELSIF y(geom1) = y(geom2trans) AND x(geom1) < x(geom2trans) THEN 

RETURN 90;

ELSIF y(geom1) = y(geom2trans) AND x(geom1) > x(geom2trans) THEN 

RETURN 270;

ELSIF x(geom1) < x(geom2trans) AND y(geom1) < y(geom2trans) THEN 

RETURN degrees(atan(abs(x(geom1) - x(geom2trans)) / abs(y(geom1) - 
y(geom2trans))));

ELSIF x(geom1) < x(geom2trans) AND y(geom1) > y(geom2trans) THEN 

RETURN degrees(atan(abs(y(geom1) - y(geom2trans)) / abs(x(geom1) - 
x(geom2trans)))) + 90;

ELSIF x(geom1) > x(geom2trans) AND y(geom1) > y(geom2trans) THEN 

RETURN degrees(atan(abs(x(geom1) - x(geom2trans)) / abs(y(geom1) - 
y(geom2trans)))) + 180;

ELSIF x(geom1) > x(geom2trans) AND y(geom1) < y(geom2trans) THEN 

RETURN degrees(atan(abs(y(geom1) - y(geom2trans)) / abs(x(geom1) - 
x(geom2trans)))) + 270;

ELSE

RETURN 0;

END IF;

END;

'
LANGUAGE 'plpgsql' VOLATILE;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20050915/1491effe/attachment.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: azimuth.sql
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20050915/1491effe/attachment.ksh>


More information about the postgis-users mailing list