Skip to content
Advertisement

Using ST_ClosestPoint, ST_StartPoint, and ST_EndPoint to find closest object to beginning and end points of lines

I need to find the closest points to either end of a line using geometry from two separate tables. So far I was able to use the following to find the geometry of the end points:

Using this, I want to use ST_ClosestPoint to find structures that are the closest to the end points of pipes. This is what I’ve come up with so far:

However, this gives the following error:

I have provided some sample data below.

sewers.pipes <– these are the lines

sewers.structures <– these are points

I know that there may be an error that comes from selecting multiple data points, eg. more than one row returned by a subquery used as an expression, so that may be an issue as well. Any help would be greatly appreciated.

Advertisement

Answer

Try with distinct LATERALs for upstream and downstream:

  • To access the geometries from ST_Dump you have to wrap its output with parenthesis, e.g. (ST_Dump(multiline)).geom
  • The operator <-> at the ORDER BY clause corresponds to distance. So, using it combined with a LIMIT 1 you get only the closest distance.

Demo: db<>fiddle

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement