简体   繁体   English

MySQL:如何将存储过程与另一个函数结合在一起?

[英]MySQL: How do I combine a Stored procedure with another Function?

I need some help in combining a stored procedure with another function. 在将存储过程与另一个功能结合在一起时,我需要一些帮助。

I've got a stored procedure that pulls latitudes and longitudes from a database. 我有一个存储过程,可以从数据库中提取纬度和经度。 I've got another function that checks whether a point is inside a polygon. 我还有另一个功能可以检查点是否在多边形内。 My goal is to combine the two functions, so that I can check whether the latitude and longitude points pulled from the db are inside a specific area. 我的目标是结合这两个功能,以便可以检查从数据库中提取的纬度和经度点是否在特定区域内。

This stored procedure pulls latitude and longitudes from the database based on offense: 该存储过程基于攻击从数据库中提取纬度和经度:

DROP PROCEDURE IF EXISTS latlongGrabber;
DELIMITER $$
CREATE PROCEDURE latlongGrabber(IN offense_in VARCHAR(255))

BEGIN
     DECLARE latitude_val VARCHAR(255);
     DECLARE longitude_val VARCHAR(255);
     DECLARE no_more_rows BOOLEAN;
     DECLARE latlongGrabber_cur CURSOR FOR

     SELECT latitude, longitude FROM myTable WHERE offense = offense_in;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_rows = TRUE;

OPEN latlongGrabber_cur;


the_loop: LOOP

FETCH latlongGrabber_cur
INTO latitude_val, longitude_val;

IF no_more_rows THEN CLOSE latlongGrabber_cur;
LEAVE the_loop;
END IF;
SELECT latitude_val, longitude_val;
END LOOP the_loop;
END
$$
DELIMITER ;

This function checks whether a point is inside a polygon. 此函数检查点是否在多边形内。 I'd like the function to test the points produced by the procedure. 我想要该功能来测试过程产生的点。 I can hard-code the polygon for now. 我现在可以对多边形进行硬编码。 (Once, I know how to combine these two functions, I'll use the same pattern to pull the polygons from the database). (一旦知道了如何组合这两个功能,我将使用相同的模式从数据库中提取多边形)。

DROP FUNCTION IF EXISTS myWithin;
DELIMITER $$
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
$$
DELIMITER ;

This function is called as follows: 该函数的调用方式如下:

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

I've tested the stored procedure and the function and they work well. 我已经测试了存储过程和功能,它们运作良好。 I just have to figure out how to combine them. 我只需要弄清楚如何合并它们。 I'd like to call the procedure with the offense parameter and have it test all of the latitudes and longitudes pulled from the database to see whether they are inside or outside of the polygon. 我想调用带有Attack参数的过程,让它测试从数据库中提取的所有纬度和经度,以查看它们是否在多边形内部或外部。

Any advice or suggestions? 有什么建议吗?

Thank you. 谢谢。

-Laxmidi -拉克西米

Chad at mysql's forum helped me solve this: mysql论坛上的Chad帮助我解决了这个问题:

SELECT myWithin(
POINTFROMTEXT( CONCAT( 'POINT(', latitude, ' ', longitude, ')' ) ) , POLYFROMTEXT( 'POLYGON(( 0 0, 0 10, 10 10, 10 0, 0 0))' )
)AS result, latitude, longitude
FROM myTable

I can simply use a select statement calling the function. 我可以简单地使用一条选择语句来调用该函数。 I didn't need the stored procedure after all. 我根本不需要存储过程。

Thanks. 谢谢。

-Laxmidi -拉克西米

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM