PostGISSpatial Relationships

ST_LineCrossingDirection

What is ST_LineCrossingDirection?

ST_LineCrossingDirection is a PostGIS function that returns an integer describing how two linestrings cross. It goes beyond a boolean "do they cross?" and encodes the directionality of the crossing, which is useful for traffic, hydrology, and network analyses.

SQL
ST_LineCrossingDirection(geometry linestringA, geometry linestringB)integer

Return values:

  • 0 — no crossing
  • 1 — line B crosses line A from left to right
  • -1 — line B crosses line A from right to left
  • 2 — multiple crossings, last one left to right
  • -2 — multiple crossings, last one right to left
  • 3 — multiple crossings, alternating with final left-to-right
  • -3 — multiple crossings, alternating with final right-to-left

When would you use ST_LineCrossingDirection?

Use ST_LineCrossingDirection for problems where the side of a crossing matters — identifying whether a migrating herd entered or exited a park, whether a vehicle crossed a toll gantry in the tolled direction, or whether a pipeline enters or leaves a jurisdictional zone. It is also useful in hydrological connectivity checks between streams and contour lines.

SQL
1SELECT v.vehicle_id,
2       ST_LineCrossingDirection(gate.geom, v.track) AS direction
3FROM vehicle_tracks v, toll_gates gate
4WHERE ST_Intersects(v.track, gate.geom);

FAQs

How is "left" and "right" defined?

Direction is taken with respect to the orientation of the first linestring (A). Walking along A from its start to its end, B crosses from left to right (positive) or right to left (negative). Reversing A flips the sign of the result.

What does a return value of 0 mean exactly?

It means B does not properly cross A — either they do not intersect, or they only touch without fully crossing. Use ST_Intersects alongside to distinguish "no contact" from "tangent contact".

Does it work on MultiLineStrings?

No, ST_LineCrossingDirection is defined for two single LINESTRING inputs. If you have multilinestrings, either iterate over their component lines with ST_Dump or use ST_GeometryN to pick specific parts.

Can I use it with polygon boundaries?

Not directly — cast the polygon to its boundary with ST_Boundary(polygon) or ST_ExteriorRing(polygon) to obtain a linestring. This is a common pattern when asking "did this track enter or exit a zone?"