In a mysql database I have table of geographical areas where each row has an id and a set of coordinates (lat, long) of the vertices of the polygon which makes up the area (can be an unlimited number of vertices) and a marker as such:
id | poly_coords | marker
------|--------------------------------------|-------------
0 | x1,y1|x2,y2|x3,y3|x4,y4|x4,y4|x4,y4| | 0
---------------------------------------------|-------------
1 | x1,y1|x2,y2|x3,y3|x4,y4|x4,y4|x4,y4| | 0
---------------------------------------------|-------------
. . .
. . .
. . .
What I am trying to do is when given a specific X,Y coordinate:
-
If marker == 0
I need to select the row(s) in which the given X,Y coordinates will fall within the polygon or where the X,Y coordinates are within a certain distance of the polygon. -
If marker == 1
The verticies are points on a line (i.e. area of poly=0) and I need to select the row(s) where the X,Y coordinates are within a certain distance of the line.
I am not sure how to start here. How can I accomplish this using a MySql query? Should I be using other methods combined with multiple queries to make this work?
You could check out this link: http://www.sql-statements.com/point-in-polygon.html
It should provide a basic start to approach your problem.
I would start by creating a stored procedure for transforming the poly_coords column in an array of strings each representing “x,y” (or “x|y” if you consider using 0,0 as value for the coordinates instead of 0.0). After than you could make some changes to the method provided in the link to achieve the required functionality. I would provide more help but I don’t have access neither to MySql nor other sql.
Another approach would be to design the table as in the link , that way you wouldn’t require to actually parse the coordinates strings