<font size="2">
<p>Hi Ahmet,</p>
<p>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.
</p>
<div>Cheers,<br>Hartmut<br> </div>
<div>Hartmut Tschauner<br>Department of Archaeology<br>Seoul National University<br>Gwanak-Gu Sillim 9-dong San 56-1<br>Seoul, 151-742<br>Korea<br>ph. +82 (2) 880-9260</div>
<p> </p>
<p>CREATE OR REPLACE FUNCTION public.azimuth(geometry, geometry)</p>
<p>RETURNS float8 AS</p>
<p>'</p>
<p>DECLARE</p>
<p>geom1 ALIAS FOR $1;</p>
<p>geom2 ALIAS FOR $2;</p>
<p>geom2trans geometry;</p>
<p>BEGIN</p>
<p>IF geom1 IS NULL OR geom2 IS NULL THEN</p>
<p>RETURN NULL;</p>
<p>ELSE</p>
<p>IF isempty(geom1) OR isempty(geom2) OR geometrytype(geom1) != \'POINT\' OR geometrytype(geom2) != \'POINT\' THEN </p>
<p>RETURN NULL;</p>
<p>END IF;</p>
<p>END IF;</p>
<p>IF srid(geom1) != -1 AND srid(geom2) != srid(geom1) THEN</p>
<p>geom2trans := transform(geom2, srid(geom1));</p>
<p>ELSE</p>
<p>geom2trans := geom2;</p>
<p>END IF;</p>
<p>IF x(geom1) = x(geom2trans) AND y(geom1) < y(geom2trans) THEN </p>
<p>RETURN 0;</p>
<p>ELSIF x(geom1) = x(geom2trans) AND y(geom1) > y(geom2trans) THEN </p>
<p>RETURN 180;</p>
<p>ELSIF y(geom1) = y(geom2trans) AND x(geom1) < x(geom2trans) THEN </p>
<p>RETURN 90;</p>
<p>ELSIF y(geom1) = y(geom2trans) AND x(geom1) > x(geom2trans) THEN </p>
<p>RETURN 270;</p>
<p>ELSIF x(geom1) < x(geom2trans) AND y(geom1) < y(geom2trans) THEN </p>
<p>RETURN degrees(atan(abs(x(geom1) - x(geom2trans)) / abs(y(geom1) - y(geom2trans))));</p>
<p>ELSIF x(geom1) < x(geom2trans) AND y(geom1) > y(geom2trans) THEN </p>
<p>RETURN degrees(atan(abs(y(geom1) - y(geom2trans)) / abs(x(geom1) - x(geom2trans)))) + 90;</p>
<p>ELSIF x(geom1) > x(geom2trans) AND y(geom1) > y(geom2trans) THEN </p>
<p>RETURN degrees(atan(abs(x(geom1) - x(geom2trans)) / abs(y(geom1) - y(geom2trans)))) + 180;</p>
<p>ELSIF x(geom1) > x(geom2trans) AND y(geom1) < y(geom2trans) THEN </p>
<p>RETURN degrees(atan(abs(y(geom1) - y(geom2trans)) / abs(x(geom1) - x(geom2trans)))) + 270;</p>
<p>ELSE</p>
<p>RETURN 0;</p>
<p>END IF;</p>
<p>END;</p>
<p>'</p>
<div>LANGUAGE 'plpgsql' VOLATILE; </div>
<div> </div></font>