I have a spatial table of polygons
. I am trying to find what polygon
a lat/lng point
lies within in a MySQL spatial table efficiently.
I know ST_Within
is available for MySQL 5.6
however my version pre-dates this so I am combining two functions I have at my disposal into something more efficient/accurate than either individually.
These are the native MBRWITHIN
function and custom GISWithin from a MySQL forum post. MBRWITHIN usually gives me about 2 rows for a point where there should only be one but is quick, GISWithin gives the correct row, however is quite slow.
So I am trying to write a query that :
Initially selects the few rows that could contain the point using the quick MBRWITHIN funciton.
Then refine this list to the single correct row using the slower GISWithin that is now operating on about 2 rows rather than 9000.
I have tried all sorts of subqueries like
SET @x = -0.3578;
SET @y = 51.477;
SET @point = CONCAT('POINT(',@x,' ',@y,')');
SELECT * FROM
(SELECT `geometry` FROM world_borders WHERE MBRWITHIN( @point , `geometry` )) AS TT
WHERE GISWithin(GeomFromText(@point), `geometry`);
and
SELECT * FROM world_borders WHERE GISWithin(GeomFromText(@point), `geometry`) IN (SELECT ogr_fid FROM world_borders WHERE MBRWITHIN( @point , `geometry` ));
only to get the error message
#1416 - Cannot get geometry object from data you send to the GEOMETRY field
Does anyone know how I might write such a query or function?
The polygon field in my table is called geometry
and is of type Geometry
- imported from ogr2ogr for those that know about that.