In case not (have you checked the docs?), use a simple (K)NN search:
SELECT CASE WHEN ST_LineLocatePoint(geom, <point>) < 0.5
THEN source
ELSE target
END as node
FROM edges
ORDER BY
geom <-> <point>
LIMIT 1
;
ST_LineLocatePoint
in the CASE
finds the fraction of line length of the projected point from <point>
on the closest edge.geom
, and selects either source
or target
.
A spatial index on edges
is mandatory.
You can outsource the <point>
creation into a CTE if you want.
You could also wrap this up in a function, but note that for a batch solution on a table of points (or more than one point) you'd better CROSS JOIN LATERAL
the points table/expression to a (K)NN subquery. See e.g.