This code selects all the rows in the table when the point is hard coded with no errors or warnings:
SELECT *
FROM lat_lng
WHERE Contains(
GeomFromText('POLYGON((0 0,0 100,100 100,100 0,0 0))'),
GeomFromText('Point(50 50)') )
OR defined by variable $var = "50 50" (with no errors or warnings)
SELECT *
FROM lat_lng
WHERE Contains(
GeomFromText('POLYGON((0 0,0 100,100 100,100 0,0 0))'),
GeomFromText('Point($var)') )
However, when I use a column called "location" to define the point, zero rows are selected (with no errors or warnings):
SELECT *
FROM lat_lng
WHERE Contains(
GeomFromText('POLYGON((0 0,0 100,100 100,100 0,0 0))'),
GeomFromText('Point(location)') )
based on this two row sample table:
id | location
1 | 50 50
2 | 500 500
why?
How to check if a point is inside a polygon in Python. To perform a Point in Polygon (PIP) query in Python, we can resort to the Shapely library's functions . within(), to check if a point is within a polygon, or . contains(), to check if a polygon contains a point.
The idea is rather simple. Given that specific point, compute a sum of signed solid angles of all faces of the polyhedron as viewed from that point. If the point is outside, that sum gotta be zero.
The point will be inside a convex polygon if and only if it lies on the same side of the support line of each of the segments. That is, either it lies on the left of every line or it lines on the right of every line.
Construct the direction vector from the cube center to the point in consideration and project it onto each local axis and check if the projection spans beyond the extent of the cube along that axis. If the projection lies inside the extent along each axis, then point is inside, otherwise it is outside of the cube.
You are providing a string which is Point(location)
(verbatim). What you want is reference the location column, so you'll need to build the string:
CONCAT('Point(',location,')')
Also you probably want to store the Point
directly in that other table (rather than text), and then reference it without the GeomFromText
Update
Storing Point
s directly in you database:
Schema:
CREATE TABLE locations (id integer primary key auto_increment,location point);
INSERT INTO locations (location) VALUES (Point(50,50));
INSERT INTO locations (location) VALUES (Point(500,500));
Request:
SELECT id
FROM locations
WHERE Contains(
GeomFromText('POLYGON((0 0,0 100,100 100,100 0,0 0))'),
location);
Update 2
SQL fiddle: http://sqlfiddle.com/#!2/b5d1f/9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With