# issue with calculation fo azimuth

I want to calculate azimuth between two points so I did it this way like described here : How to calculate the angle at which two lines intersect in PostGIS?

``````DROP TABLE IF EXISTS angletmp;
CREATE TABLE angletmp AS
WITH
intersections AS
(
SELECT
ST_Transform
(
(ST_Dump
(
ST_Intersection
(
a.geom,
b.geom
)
)).geom,
3785
)
AS intersection,
a.geom as geom1,
b.geom as geom2,
a.id as id1,
b.id as id2
FROM
pipe a,
pipe b
WHERE
ST_Intersects
(
a.geom,
b.geom
)
),
buffers AS
(
SELECT
intersections.intersection,
ST_ExteriorRing
(
ST_Buffer
(
intersections.intersection
, 10
)
)
AS extring,
ST_Transform(intersections.geom1,3785) AS geom1,
ST_Transform(intersections.geom2,3785) AS geom2,
intersections.id1,
intersections.id2
FROM
intersections
),
points AS
(
SELECT
ST_GeometryN
(
ST_Intersection
(
buffers.extring,
buffers.geom1
)
, 1
)
AS point1,
ST_GeometryN
(
ST_Intersection
(
buffers.extring,
buffers.geom2
)
, 1
)
AS point2,
buffers.intersection,
buffers.extring,
buffers.geom1,
buffers.geom2,
buffers.id1,
buffers.id2
FROM
buffers
)
SELECT
points.point1,
points.point2,
points.extring,
points.geom1,
points.geom2,
abs
(
round
(
degrees
(
ST_Azimuth
(
points.point2,
points.intersection
)
-
ST_Azimuth
(
points.point1,
points.intersection
)
)::decimal % 180.0
,2
)
)
AS angle,
points.id1 AS id1,
points.id2 AS id2
FROM
points
LIMIT 1000
;
ALTER TABLE angletmp ADD COLUMN id serial;
``````

but I get this error : ERREUR: Argument must be POINT geometries

I can't understand the origin of the error and I'm wondering if you could help me solve it

I suspect it's your ST_Azimuth call. Both arguments need to be points.

http://postgis.net/docs/ST_Azimuth.html

Check each sub query to make sure your points.point1, points.point2, and points.intersection are all returning points.

You could test with a

``````WITH ....
SELECT DISTINCT ST_GeometryType(points.point1) AS t1,
ST_GeometryType(points.point2) AS t2,
ST_GeometryType(points.intersection) As t3
FROM points;
``````

The above should output exactly one record and all fields should read ST_Point.

• Thanks for your answer. Yes I checked each query and the problem was on the first one where the intersection between lines returns strangely linestrings. I added a contition on the id to the query `a.id!=b.id` to prevent self matching yet the problem is still in there. So I'm making researches to know why do I get such a result in the intersection. – zakaria mouqcit Sep 28 '17 at 7:55

The fixation of the problem is to calculate the centroid of the intersection when the result is a linestring, like described the following query

```````   SELECT
ST_Transform
(
(ST_Dump
(
ST_Centroid
(
ST_Intersection
(
a.geom,
b.geom
)
)).geom,
3785
)
AS intersection`
``````