开发者

Does this mySQL Stored Procedure Work?

开发者 https://www.devze.com 2022-12-31 00:30 出处:网络
I got the following stored procedure from http://dev.mysql.com/doc/refman/5.1/en/functions-that-test-spatial-relationships-between-geometries.html

I got the following stored procedure from http://dev.mysql.com/doc/refman/5.1/en/functions-that-test-spatial-relationships-between-geometries.html

Does this work?

CREATE FUNCTION myWithin(p POINT, poly POLYGON) RETURNS IN开发者_StackOverflow中文版T(1) DETERMINISTIC
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE pX DECIMAL(9,6);
DECLARE pY DECIMAL(9,6);
DECLARE ls LINESTRING;
DECLARE poly1 POINT;
DECLARE poly1X DECIMAL(9,6);
DECLARE poly1Y DECIMAL(9,6);
DECLARE poly2 POINT;
DECLARE poly2X DECIMAL(9,6);
DECLARE poly2Y DECIMAL(9,6);
DECLARE i INT DEFAULT 0;
DECLARE result INT(1) DEFAULT 0;
SET pX = X(p);
SET pY = Y(p);
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;
RETURN result;
End;

Usage:

SET @point = PointFromText('POINT(5 5)') ;
SET @polygon = PolyFromText('POLYGON((0 0,10 0,10 10,0 10))') ;
SELECT myWithin(@point, @polygon) AS result ;

I'm using phpMyAdmin and it blows up when using stored procedures. If this one works, then I'll try to figure out how to call it in php instead.

Thanks,

Laxmidi


use:

CALL myWithin(@point, @polygon);

instead


The example will always return no records unless the polygon is 'complete', for example adding the final "0 0" in the POLYGON to close off the polygon.

SET @polygon = PolyFromText('POLYGON((0 0,10 0,10 10,0 10,**0 0**))') ; 
0

精彩评论

暂无评论...
验证码 换一张
取 消