Dear Forum,

I have successfully used the following code to select all of the points within a series of polygons. I basically have two mysql tables. One called lat_lon_table (holds all the points) and the other called segment_table. The segment table holds the segments that make up the polygon, plus the POLY_ID of the polygon. It works great to select all of the points that fall within polygons, however some of the points get mislabled because the point may cross over several polygons, therefore when I conduct the GROUP BY p.ID, the last segment it crosses may not be the correct polygon and it will use this label for the point. I noticed that the correct polygon will only have one POLY_ID associated with it because it crosses over one segment for that polygon (since that is the polygon in which it resides). All other selections will repeat the POLY_ID twice. Does anyone know how I can either modify my GROUP BY clause or selection criteria to only SELECT the first segment that my point crosses?

$labelpoint = "UPDATE `lat_lon_table` t1 JOIN ( SELECT p.ID, COUNT(*) AS counter, s.ID AS sid, s.POLY_ID, s.SEGMENT_ID FROM `lat_lon_table` p INNER JOIN `segment_table` s ON
( s.LNGA > p.LONG OR s.LNGB > p.LONG ) AND
(s.LATA > p.LAT AND s.LATB <= p.LAT ) OR
(s.LATB > p.LAT AND s.LATA <= p.LAT )
(p.LONG <= ((p.LAT - s.LATA) * (s.LNGB - s.LNGA) / (s.LATB - s.LATA) + s.LNGA))
HAVING COUNT(*) %2 = 1
) t2 USING (ID)
t1.POLY_ID = t2.POLY_ID";

$result = mysql_query($labelpoint);