<div dir="ltr">A frequent question on GISSE is to find the LineString segment index containing the closest point to a given point, to allow inserting the closest point into the line using AT_AddPoint [1][2][3].<div><br></div><div>AFAIK there's no easy way to do this in PostGIS currently.  It looks like it requires iterating over the segments of the LineString?</div><div><br></div><div>The ST_ClosestPoint function (and the Linear Referencing functions) do have the segment index information internally.  It would be nice if there was an extended version of ST_ClosestPoint which returned a record with both the closest point and the index of the segment containing it.  Or perhaps an extended Linear Referencing function which returns the point, the segment index, and the fractional distance along the line.<br><div><br></div><div>For comparison, both Turf and JTS provide similar functionality.</div><div><br></div><div>[1] <a href="https://gis.stackexchange.com/questions/368479/finding-line-segment-of-point-on-linestring-using-postgis" style="text-decoration-line:none"><span style="font-size:11pt;font-family:Arial;background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;text-decoration-line:underline;vertical-align:baseline;white-space:pre-wrap">https://gis.stackexchange.com/questions/368479/finding-line-segment-of-point-on-linestring-using-postgis</span></a></div><div>[2] <a href="https://gis.stackexchange.com/questions/40622/how-to-add-vertices-to-existing-linestrings?noredirect=1&lq=1" style="text-decoration-line:none"><span style="font-size:11pt;font-family:Arial;background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;text-decoration-line:underline;vertical-align:baseline;white-space:pre-wrap">https://gis.stackexchange.com/questions/40622/how-to-add-vertices-to-existing-linestrings</span></a></div><div>[3] <a href="https://gis.stackexchange.com/questions/370488/find-closest-index-in-line-string-to-insert-new-vertex-using-postgis/370525#370525" style="text-decoration-line:none"><span style="font-size:11pt;font-family:Arial;background-color:transparent;font-variant-numeric:normal;font-variant-east-asian:normal;text-decoration-line:underline;vertical-align:baseline;white-space:pre-wrap">https://gis.stackexchange.com/questions/370488/find-closest-index-in-line-string-to-insert-new-vertex-using-postgis</span></a></div></div></div>