X-Git-Url: https://git.openstreetmap.org/nominatim.git/blobdiff_plain/4b8632d1e563f63591e33f8561271552d074a5e2..59de7c5a9b2e16fdda93d3f9aac33f3b67a5bfb1:/sql/functions.sql

diff --git a/sql/functions.sql b/sql/functions.sql
index 48ac80b5..da9223ad 100644
--- a/sql/functions.sql
+++ b/sql/functions.sql
@@ -846,15 +846,13 @@ BEGIN
     IF search_place_id IS NOT NULL THEN
       select * from placex where place_id = search_place_id INTO nextnode;
 
-        IF nodeidpos < array_upper(waynodes, 1) THEN
+        IF nodeidpos > 1 and nodeidpos < array_upper(waynodes, 1) THEN
           -- Make sure that the point is actually on the line. That might
           -- be a bit paranoid but ensures that the algorithm still works
           -- should osm2pgsql attempt to repair geometries.
           splitline := split_line_on_node(linegeo, nextnode.geometry);
           sectiongeo := ST_GeometryN(splitline, 1);
-          IF ST_GeometryType(ST_GeometryN(splitline, 2)) = 'ST_LineString' THEN
-            linegeo := ST_GeometryN(splitline, 2);
-          END IF;
+          linegeo := ST_GeometryN(splitline, 2);
         ELSE
           sectiongeo = linegeo;
         END IF;
@@ -862,7 +860,7 @@ BEGIN
 
         IF startnumber IS NOT NULL AND endnumber IS NOT NULL
            AND @(startnumber - endnumber) < 1000 AND startnumber != endnumber
-           AND ST_GeometryType(linegeo) = 'ST_LineString' THEN
+           AND ST_GeometryType(sectiongeo) = 'ST_LineString' THEN
 
           IF (startnumber > endnumber) THEN
             housenum := endnumber;
@@ -897,6 +895,12 @@ BEGIN
           END LOOP;
         END IF;
 
+        -- early break if we are out of line string,
+        -- might happen when a line string loops back on itself
+        IF ST_GeometryType(linegeo) != 'ST_LineString' THEN
+            RETURN newpoints;
+        END IF;
+
         startnumber := substring(nextnode.housenumber,'[0-9]+')::integer;
         prevnode := nextnode;
     END IF;
