MySQL Forums
Forum List  »  GIS

Point in Polygon function
Posted by: Sinead Martin
Date: September 16, 2010 07:25AM

Hi there,

I am using the myWithin function, which works perfectly but I need to call my function from a java program which will give me the polygon parameters in the following format:

SELECT point_in_polygon('(5|5)','(0|0)|(0|10)|(10|10)|(10|0)|(0|0)');

So I wrote a function to modify the parameters and use them with the myWithin function - see below:

This may be of use to others.

Drop function point_in_polygon;

DELIMITER //
CREATE FUNCTION point_in_polygon(str1 TEXT,str2 TEXT)
RETURNS INT(1) DETERMINISTIC
BEGIN
DECLARE result INT(1) DEFAULT 0;
DECLARE pt TEXT;
DECLARE poly1 TEXT;
DECLARE poly2 TEXT;
SET pt = CONCAT('POINT',REPLACE(str1, “|”,” “));
SET poly1 = CONCAT('POLYGON(', REPLACE(str2, ")|(",", "),')');
SET poly2 = REPLACE(poly1, "|"," ");
SELECT myWithin(pointfromtext(pt),polyfromtext(poly2)) into result;
RETURN result;

END;
//

DELIMITER ;

usage:
SELECT point_in_polygon('(5|5)','(0|0)|(0|10)|(10|10)|(10|0)|(0|0)') as result;



Edited 1 time(s). Last edit at 09/16/2010 09:13AM by Sinead Martin.

Options: ReplyQuote


Subject
Views
Written By
Posted
Point in Polygon function
5698
September 16, 2010 07:25AM
2377
September 16, 2010 08:31AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.