Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

St_split return the original line

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 ?

like image 212
Ben Avatar asked Jul 26 '26 23:07

Ben


1 Answers

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)

enter image description here

.. 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)

enter image description here enter image description here

like image 87
Jim Jones Avatar answered Jul 28 '26 12:07

Jim Jones