Bug #27804 funtion Contains(g1,g2) return more g1 completely contains g2
Submitted: 13 Apr 2007 9:40 Modified: 23 Jun 2007 19:58
Reporter: tuan tran Email Updates:
Status: No Feedback Impact on me:
None 
Category:MySQL Server: General Severity:S2 (Serious)
Version:5.0 OS:Windows
Assigned to: CPU Architecture:Any

[13 Apr 2007 9:40] tuan tran
Description:
i have g1 is multipolygon : g3, g4. g3 contains g2(point P1) and g4 is not.
But when i try funtion Contains(g1,g2) return g3 and g4 both completely contains g2.

How to repeat:
i have g1 is multipolygon : g3, g4. g3 contains g2(point P1) and g4 is not.
But when i try funtion Contains(g1,g2) return g3 and g4 both completely contains g2.
[13 Apr 2007 10:36] Valeriy Kravchuk
Thank you for a problem report. Please, specify the exact version of MySQL server used, 5.0.x, and send complete test case that demonstrates the behaviour described. Just copy and paste statements and results from mysql command line client.
[13 May 2007 23:02] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
[14 May 2007 10:51] tuan tran
Hi you!
i have the table tbl_commune(Commune_Code INTEGER, Commune_Name VARCHAR(50), geom GEOMETRY, ...).
the colums geom contains polygons ( it can be multipolygon). When i try funtion Contains(g1,g2). g1 is geom, g2 is Point(x,y).  The function return more than 1 object contain g2.
i found the problem that MySQL server 5.0 (mysql-essential-5.0.24a-win32.msi) use function Contains(g1,g2) with the results is minimum bound  rectangle (MBR) .
when i user the function contains in mysql i  check  with  my function:
public class findcontains {
    /********************************************************
     *
     * @param P0
     * @param P1
     * @param P2
     * @return
     */
     private static double isLeft(Coordinate P0, Coordinate P1,Coordinate P2){
            double tem= (P1.x - P0.x) * (P2.y - P0.y) - (P2.x - P0.x) * (P1.y - P0.y);
            return tem;
     }

    /***********************************************************
     * wn_PnPoly(): winding number test for a point in a polygon
     * @param P= a point
     * @param V [] = vertex points of a polygon V[n+1] with V[n]=V[0]
     * @return wn = the winding number (=0 only if P is outside V[])
     */
    private static int wn_PnPoly( Coordinate P, Coordinate[] V )
    {
            int    wn = 0;    // the winding number counter
            int n=V.length-1;
           
            // loop through all edges of the polygon
            for (int i=0; i<n; i++) {   // edge from V[i] to V[i+1]
                if (V[i].y <= P.y) {         // start y <= P.y
                    if (V[i+1].y > P.y)      // an upward crossing
                        if (isLeft( V[i], V[i+1], P) > 0)  // P left of edge
                            ++wn;            // have a valid up intersect
                }
                else {                       // start y > P.y (no test needed)
                    if (V[i+1].y <= P.y)     // a downward crossing
                        if (isLeft( V[i], V[i+1], P) < 0)  // P right of edge
                            --wn;            // have a valid down intersect
                }
            }
            //System.out.println("wn="+wn);
            return wn;
    }
   
    /***********************************************************
     * Caculate Geometry contains Point P?
     * @param geom :object Geometry
     * @param p: Point
     * @return :
     * true: contain
     * false: not contain
     */
     public static boolean Contains(Geometry geom,Coordinate p){     
         Coordinate[] V=geom.getCoordinates ();
         return (wn_PnPoly(p,V)>0);
     }
}
The results is same the function in Postgree .
Thanks!
[23 May 2007 12:33] Valeriy Kravchuk
Please, try to repeat with a newer version, 5.0.41, and inform about the results.
[23 May 2007 13:46] tuan tran
Do you understand my question?
[23 May 2007 19:58] Valeriy Kravchuk
> Do you understand my question?

Maybe, not. Please, help me, by providing a complete test case, with exact CREATE TABLE, some INSERT statements and SELECT with Contains() function that demonstrates wrong results.

All I understand now is that you have a "proper" implementation of Contains() for your test case, and that you got "incorrect" results with old MySQL version.
[23 Jun 2007 23:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".