1

我从http://dev.mysql.com/doc/refman/5.1/en/functions-that-test-spatial-relationships-between-geometries.html获得了以下存储过程

这行得通吗?

CREATE FUNCTION myWithin(p POINT, poly POLYGON) RETURNS INT(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;

用法:

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

我正在使用 phpMyAdmin 并且在使用存储过程时它会爆炸。如果这个有效,那么我将尝试找出如何在 php 中调用它。

谢谢,

拉克西米迪

4

2 回答 2

1

利用:

CALL myWithin(@point, @polygon);

反而

于 2010-05-17T02:21:53.467 回答
0

除非多边形“完整”,否则该示例将始终不返回任何记录,例如在 中添加 final"0 0"POLYGON关闭多边形。

SET @polygon = PolyFromText('POLYGON((0 0,10 0,10 10,0 10,**0 0**))') ; 
于 2010-08-19T11:34:33.737 回答