Search code examples
mysqlgeospatialspatialpoint-in-polygon

SQLintersects query not working properly


I am trying to select the points which lies in a specific polygon from my table.
Here my Table test1 have data4 column of geometry type and it contains:

2,3  
5,6  
7,10  

When I am trying the following query its returning empty row. Please help me to resolve the error.

DECLARE @g geometry;   
SET @g = geometry::STGeomFromText('POLYGON((0 0, 4 0, 4 4, 0 4, 0 0))', 0);  
SELECT        *  
FROM            test1  
WHERE data4.STIntersects(@g) = 1;  

I have also tried:

WHERE @g.STContains(geometry::STGeomFromText(test1.data4, 0))='True'   

This didn't work as well.


Solution

  • If you're really talking about MySQL as your question is tagged, you're using the entirely wrong methods. STGeomFromText is an SQL Server function.

    The MySQL query that does what you want is as simple as;

    SELECT * 
    FROM test1 
    WHERE MBRContains(GeomFromText('Polygon((0 0,0 4,4 4,4 0,0 0))'), data4)
    

    An SQLfiddle for testing

    Just as a bonus, here is how to write it for SQL Server;

    SELECT * 
    FROM test1 
    WHERE geometry::STGeomFromText('Polygon((0 0,0 4,4 4,4 0,0 0))',0)
              .STContains(data4) = 1
    

    Another SQLfiddle.