     IF ST_Numpoints(fullSegment) > 50
     THEN
       -- ST_BuildArea will only return true if the full line segment is noded and closed
+      -- Force 2D to remove the elevation (Z-) component during this pattern check
       patternPoly = ST_BuildArea(ST_Node(ST_Force2D(fullSegment)));
 
       IF patternPoly IS NOT NULL

    b.rownum
  FROM pts AS a,
    pts AS b
  -- Work-around to get the current point and the next point in order, since rownum sorted by timestamp
  WHERE a.rownum = b.rownum - 1
        AND
        b.rownum > 1

DROP FUNCTION adsb.public.find_pattern_num(itineraryid CHAR );

-- Example usage: pass in the ID of the itinerary to check for patterns in the point data
--   SELECT * FROM find_pattern_count(itineraryid);

CREATE FUNCTION adsb.public.find_pattern_num(
  IN  itineraryid             CHAR,
