MySQL implementation of ray-casting Algorithm?

后端 未结 7 1989
小蘑菇
小蘑菇 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<m DO 
    SET poly = GeometryN(multipoly,x);
    SET ls = ExteriorRing(poly); 
    SET poly2 = EndPoint(ls); 
    SET poly2X = X(poly2); 
    SET poly2Y = Y(poly2); 
    SET n = NumPoints(ls); 
    WHILE i<n DO 
    SET poly1 = PointN(ls, (i+1)); 
    SET poly1X = X(poly1); 
    SET poly1Y = Y(poly1); 
    IF ( ( ( ( poly1X <= pX ) && ( pX < poly2X ) ) || ( ( poly2X <= pX ) && ( pX < poly1X ) ) ) && ( pY > ( 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; 
    
    0 讨论(0)
提交回复
热议问题