Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check for point in a polygon

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?

like image 798
niche Avatar asked Aug 16 '14 13:08

niche


People also ask

How do you check if a point is within a polygon Python?

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.

How do you check if a point is inside a polyhedron?

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.

How do you determine if a point is inside a convex polygon?

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.

How do you check if a point is in a cube?

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.


1 Answers

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 Points 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

like image 53
jcaron Avatar answered Sep 30 '22 07:09

jcaron