MySQL implementation of ray-casting Algorithm?

后端 未结 7 2001
小蘑菇
小蘑菇 2021-02-01 08:56

We need to figure out a quick and fairly accurate method for point-in-polygon for lat/long values and polygons over google maps. After some research - came across some posts abo

7条回答
  •  余生分开走
    2021-02-01 09:33

    Here is a version that works with MULTIPOLYGONS (an adaptation of Zarun's one which only works for POLYGONS).

    CREATE FUNCTION GISWithin(p POINT, multipoly MULTIPOLYGON) RETURNS INT(1) DETERMINISTIC 
    BEGIN 
    DECLARE n,i,m,x INT DEFAULT 0; 
    DECLARE pX,pY,poly1X,poly1Y,poly2X,poly2Y DECIMAL(13,10); 
    DECLARE ls LINESTRING; 
    DECLARE poly MULTIPOLYGON;
    DECLARE poly1,poly2 POINT; 
    DECLARE result INT(1) DEFAULT 0; 
    SET pX = X(p); 
    SET pY = Y(p); 
    SET m = NumGeometries(multipoly);
    WHILE x ( poly2Y - poly1Y ) * ( pX - poly1X ) / ( poly2X - poly1X ) + poly1Y ) ) THEN 
    SET result = !result; 
    END IF; 
    SET poly2X = poly1X; 
    SET poly2Y = poly1Y; 
    SET i = i + 1; 
    END WHILE; 
    SET x = x + 1;
    END WHILE;
    RETURN result; 
    End; 
    

提交回复
热议问题