[postgis-users] Trouble with ST_ShortestLine: the returned line DOES NOT start in g1 and end in g2

Paul Ramsey pramsey at cleverelephant.ca
Wed Feb 9 11:58:45 PST 2022

```If the shortest line runs vertext-to-vertext I would expect it to touch both lines. If on the other hand it runs vertex-to-midsegment I would expect it is possible it would not touch. Check the distance of the shortest line to the two parent lines. It should be zero or very very very small. That would be expected. It's not possible to place constructed points exactly on the lines they are constructed from, except in some cases and on vertices, thanks to the granularity of floating point representation. (Discrete math, not continuous math)

P

> On Feb 9, 2022, at 11:31 AM, Jorge Gustavo Rocha <jgr at di.uminho.pt> wrote:
>
> Hi,
>
> Help needed :-) I'm having trouble with lines returned by ST_ShortestLine function.
>
> The computed lines *should* start and end on the geometries [1], but it is not happening.
>
> In fact, the computed geometry sometimes crosses the original geometries or does not touch the geometries at all.
>
> EXAMPLE 1: (of a computed geometry between a point and a line, that does not touches the line)
>
> with
> p as (
> 	select st_geometryfromtext('SRID=3763;POINT (-29802.23305474065 138518.7938969792)') as geom
> ),
> l as (
> 	select st_geometryfromtext('SRID=3763;LINESTRING (-29796.696826656284 138522.76848210802, -29804.3911369969 138519.3504205817)') as geom
> ),
> short as (
> 	select ST_ShortestLine(l.geom, p.geom) as geom
> 	from l, p
> )
> select short.geom,
> ST_Disjoint(short.geom, l.geom),
> ST_Intersects(short.geom, l.geom),
> ST_Touches(short.geom, l.geom),
> ST_Crosses(short.geom, l.geom)
> from short, l;
>
> |geom                   |st_disjoint|st_intersects|st_touches|st_crosses|
> |-----------------------------------------------------------------------------------------|-----------|-------------|----------|----------|
> |LINESTRING (-29802.795222153436 138520.05937757515, -29802.23305474065 138518.7938969792)|true       |false        |false     |false     |
>
> EXAMPLE 2: (of a computed geometry between a point and a line. The computed line crosses the original line.)
>
> l as (
> 	select st_geometryfromtext('SRID=3763;LINESTRING (-29790.144327955728 138508.0183209069, -29798.28270998299 138504.40298837385)') as geom
> ),
> short as (
> 	select ST_ShortestLine(l.geom, p.geom) as geom
> 	from l, p
> )
> select short.geom,
> ST_Disjoint(short.geom, l.geom),
> ST_Intersects(short.geom, l.geom),
> ST_Touches(short.geom, l.geom),
> ST_Crosses(short.geom, l.geom)
> from short, l;
>
> |geom                   |st_disjoint|st_intersects|st_touches|st_crosses|
> |-----------------------------------------------------------------------------------------|-----------|-------------|----------|----------|
> |LINESTRING (-29795.33635541747 138505.7118543719, -29794.774188004685 138504.44637377595)|false      |true         |false     |true      |
>
> In both cases, I was expecting that the computed line touch the original line.
>
> Am I understanding the ST_ShortestLine incorrectly or should I report a possible error? Can this be an (math approximation) error?
>
> I'm using Postgis 3.3 dev, GEOS 3.11
>
> POSTGIS="3.3.0dev 3.1.0rc1-1150-g05d3150c9" [EXTENSION] PGSQL="140" GEOS="3.11.0dev-CAPI-1.16.0" PROJ="8.2.0" LIBXML="2.9.12" LIBPROTOBUF="1.3.3" WAGYU="0.5.0 (Internal)"
>
>
>
> Jorge Gustavo
>
> [1] https://postgis.net/docs/ST_ShortestLine.html
> --
> Jorge Gustavo Rocha
> Departamento de Informática
> 4710-057 Braga
> Gabinete 3.29 (Piso 3)
> Tel: +351 253604480
> Fax: +351 253604471
> Móvel: +351 910333888
> skype: nabocudnosor
> _______________________________________________
> postgis-users mailing list
> postgis-users at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/postgis-users

```