I would like to split a linestring with postgis.
My function :
@distance_b_to_a = Track.find_by_sql(
["SELECT
ST_Split(line, ST_Closestpoint(pta,line)) AS dst_line
FROM (
SELECT
'SRID=4326;LINESTRING(1.391991 46.441430,1.506078 46.556788)'::geometry line,
'SRID=4326;POINT(1.396636962890625 46.442352066959174)'::geometry pta,
) data"
])
and my show :
<p>Test : <%= @distance_b_to_a.first.dst_line %></p>
return my original linestring :
Test : GEOMETRYCOLLECTION (LINESTRING (1.391991 46.44143, 1.506078 46.556788))
What's wrong ?
ST_Split returns a collection of geometries resulting by splitting a geometry, so I'm afraid it's the expected result, since your LineString has only two points.
Consider the following LineString ...
LINESTRING(18.6435 42.5412,18.8440 42.5453,18.846 42.3994)

.. and run this query, which applies ST_Split using the second point from the above mentioned LineString:
WITH j AS (
SELECT
'SRID=4326;POINT(18.8440 42.5453)'::GEOMETRY AS pta,
'SRID=4326;LINESTRING(18.6435 42.5412,18.8440 42.5453,18.846 42.3994)'::GEOMETRY AS line
)
SELECT
ST_AsText(
ST_Split(line,
ST_ClosestPoint(pta,line))) FROM j
... returning a GeometryCollection with two LineStrings:
st_astext
----------------------------------------------------------------------------------------------------------
GEOMETRYCOLLECTION(LINESTRING(18.6435 42.5412,18.844 42.5453),LINESTRING(18.844 42.5453,18.846 42.3994))
(1 Zeile)

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With