2

I have a layer of AIS points that I want to make into lines using st_makeline and then use st_intersects to determine if any of the lines intersect a polygon.

So I think I want to nest

st_intersects(st_makeline, polgon.geom)

The SQL I am using for the makeline is:

SELECT mmsi, ST_MakeLine(geom ORDER BY date_time_stamp) As newgeom
 FROM ais.cargo As ais
 GROUP BY mmsi;

How do I wrap the st_intersects around this?

underdark
84.9k22 gold badges237 silver badges418 bronze badges
asked Jun 9, 2018 at 23:59

1 Answer 1

1

If you have a single and simple polygon geometry:

WITH lines AS (
 SELECT mmsi, ST_MakeLine(geom ORDER BY date_time_stamp) As newgeom
 FROM ais.cargo As ais
 GROUP BY mmsi
)
SELECT mmsi FROM lines
 WHERE ST_Intersects(ST_GeomFromText('POLYGON((x1 y1, x2 y2, ...))', EPSG), newgeom);

Substitute the coordinates of your polygon into x1 y1, x2 y2, ..., the EPSG is the EPSG code of the used projection.

If your polygon(s) are stored in a table called po and geom column contains the geometry of the polygons:

WITH lines AS (
 SELECT mmsi, ST_MakeLine(geom ORDER BY date_time_stamp) As newgeom
 FROM ais.cargo As ais
 GROUP BY mmsi
)
SELECT mmsi FROM lines INNER JOIN po ON ST_Intersects(lines.newgeom, po.geom);

Note 1: I supposed the two geometries are in the same spatial reference system (projection).

Note 2: I used CTE (Common Table Expression) to make query more readable

answered Jun 10, 2018 at 6:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.