     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
+  -- Work-around to get the current point and the next point in the correct order,
+  -- since rownum sorted by timestamp
   WHERE a.rownum = b.rownum - 1
         AND
         b.rownum > 1

    IF ST_Numpoints(fullSegment) > 50
    THEN
      -- ST_BuildArea will only return true if the full line segment is noded and closed
      patternPoly = ST_BuildArea(ST_Node(ST_Force2D(fullSegment)));

      IF patternPoly IS NOT NULL

  RETURNS SETOF RECORD AS
$BODY$

-- Iterate through all of the points for this specific mode_s_hex_code, building a line as we go.
-- The line is built using subsegments that are created between every pair of neighboring points.
-- If the line creates a pattern then we count it and start over building a new line from the next point

-- Each time a pattern is detected, 2 points are calculated and stored:
--     The intersection point at which the last point of the segment hit the line of the pattern.
--     The centroid of the pattern.


DECLARE
