# How to find shortest line from a point on a polygon boundary to a multilinestring, but the line is inside polygon (PostGIS)

I have 3 geometries

• 1 polygon
• 1 point on boundary
• 1 multilinestring

In this image you can see the problem. I need to find shortest line from point (green square) to line (red), but the line must be inside polygon. The green line is the solution, but ST_shortestLine or ST_ClosestPoint give me the pink line, and this line is not within polygon

This is my SQL:

SELECT ST_AsText(st_shortestLine(line.geom,point.geom)) FROM
(SELECT st_geomFromText('POINT(3 5)') AS geom) AS point,
(SELECT st_geomFromText('MULTILINESTRING ((1 3,11 3,11 9,1 9))') geom) AS line,
(SELECT st_geomFromText('POLYGON ((0 5,10 5, 10 4,0 4,0 2,12 2,12 13, 0 13, 0 5))') geom) polygon
WHERE ST_Within( st_shortestLine(line.geom,point.geom), polygon.geom )

Without WHERE condition return the bad line, and with WHERE condition give me a null value.

• How about 1) explode the polygon boundary into segments 2) create shortest lines to each segment 3) remove shortest lines which cross with any of the segments 4) select the shortest of the remaining shortest lines with ST_Length. – user30184 Nov 25 '16 at 13:26