views:

25

answers:

1

Hi

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.

Any advice or suggestions?

Thank you.

-Laxmidi

A: 

Hi,

Chad at mysql's forum helped me solve this:

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

Laxmidi