6

How to find the center point of MULTILINESTRING, which lies on the multiline?

I am using SQL in PostGIS

2
  • What software are you using? The more details you give, the better an answer you will receive.
    – sgrieve
    Commented Sep 17, 2012 at 13:22
  • I am using SQL in PostGIS.
    – Marko
    Commented Sep 17, 2012 at 13:26

5 Answers 5

7

Here's an algorithm that will work for a use case such as a single road with a T-intersection.

  1. Find the ST_Centroid(geometry g1);
  2. Choose the linestring from the MULTILINESTRING with the min(ST_Distance(centroid,linestring))
  3. ST_Line_Interpolate_Point(line, ST_Line_Locate_Point(line, centroid))

or even simpler

SELECT ST_ClosestPoint(geom, ST_Centroid(geom) FROM table;

2
  • ... But, why not use ST_PointOnSurface? You expected a different result? Commented Jul 9, 2020 at 12:11
  • Thank you, exactly what I needed :-)
    – ffrosch
    Commented Nov 8, 2023 at 2:38
4

Have a look at ST_Line_Interpolate_Point(). If you give 0.5 as the second parameter, it will return a point 1/2 way along the line. BTW, if your layer is a MULTILINESTRING, then "center point" really doesn't have any meaning. For example, a MULTILINESTRING can be non-contiguous or intersecting LINESTRINGs.

3

I have found, the function is called ST_PointOnSurface.

1

Technically speaking , you won't find it everytime.

MULTILINESTRING may be collection of strings which may or may not touch eachother.

If Your data type is LINESTRING then you can have middle point every time, @Micha has correct answer for that.

0

In case anyone else was struggling with this like me...

ST_PointOnSurface() didn't give me the midpoint of the line string.

First I had to merge the multilinestrings with ST_LineMerge() and pass that to ST_LineInterpolatePoint().

Like so...

Select ST_LineInterpolatePoint(
            ST_LineMerge(geom)
       , 0.50)
From FeatureTable

According to this answer you have to have valid geometries in order to do this. Basically your multilinestrings cannot cross or intersect.

Lastly I'm very new to PostGIS so if anyone has anything to add or correct here please share.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.