# How to get intersected points between polygon and line in desired order

I have polygon for example "POLYGON((5 1, 6 1, 6 5, 5 5, 5 1))" that I want to intersect with line "LINESTRING(0 2, 10 2, 10 3, 0 3, 0 4, 10 4)" and get intersection points in the order that they intersect between both geometries as if you move sequentially from the start of the line to the end. Not sure this makes sense, so here is an example:

``````WITH impact_zone AS (
SELECT ST_ExteriorRing(ST_GeomFromEWKT('SRID=4326;POLYGON((5 1, 6 1, 6 5, 5 5, 5 1))')) AS geom
), line AS (
SELECT ST_GeomFromEWKT('SRID=4326;LINESTRING(0 2, 10 2, 10 3, 0 3, 0 4, 10 4)') AS geom
)
SELECT
ST_AsText(ST_Intersection(
(SELECT geom FROM impact_zone),
(SELECT geom FROM line)
)) AS iz_path_intersection_points
``````

The result is "MULTIPOINT(5 2,5 3,5 4,6 2,6 3,6 4)"

but I want to get "MULTIPOINT(5 2,6 2,6 3,5 3,5 4,6 4)"

How can I achieve this?

I have two ideas for now that am not sure how to implement with PostGIS:

1. Use intersection function that will preserve the order from a geometry

2. Use intersection function that will preserve M attribute of the point (I have saved there the time)

Not sure if both are achievable. Any other ideas are welcome.