HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

MySQL Syntax - Searching points within polygons using GeomFromText

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
searchingpointssyntaxwithinmysqlusinggeomfromtextpolygons

Problem

I am trying to select points within polygons using the GIS/spatial extensions for MySQL. I'm fairly new to this, so bear with me if this is an elementary question.

I have points defined as text within the database, and am trying to get a boolean value for whether a point falls within a certain defined polygon. Here are my queries, with a slow WHERE statement to ensure that all should return TRUE (note that these are LAT/LNG values, and the polygon is a square starting in the northwest corner, being defined clockwise):

mysql> SET @zone = GeomFromText('Polygon((-73.9650952400 40.8072101100,-73.9531904800 40.8072101100,-73.9531904800 40.7982011000,-73.9650952400 40.7982011000,-73.9650952400 40.8072101100))');
Query OK, 0 rows affected (0.00 sec)


Checking that this query worked:

mysql> SELECT AsText(@zone);
+---------------------------------------------------------------------------------------------------------------------------------------+
| AsText(@zone)                                                                                                                         |
+---------------------------------------------------------------------------------------------------------------------------------------+
| POLYGON((-73.96509524 40.80721011,-73.95319048 40.80721011,-73.95319048 40.7982011,-73.96509524 40.7982011,-73.96509524 40.80721011)) |
+---------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


Then the problematic statement:

mysql> SELECT id,latitude,longitude, MBRWithin(GeomFromText(geo),@zone), AsText(geo)
FROM image_meta WHERE 
(latitude BETWEEN 40.7982011 AND 40.80721011) AND
(longitude BETWEEN -73.96509524 AND -73.95319048)
LIMIT 10;


and the result:

```
+---------+---------------+----------------+------------------------------------+-----------------------------------+
| id | latitude | longitude | MB

Solution

I may be wrong, but I believe that MBRWithin(GeomFromText(geo),@zone), should be MBRWithin(geo, @zone) -no need to convert anything, it is stored already in spatial format.

When functions like these get bad-type operands, they return null.

Context

StackExchange Database Administrators Q#71573, answer score: 4

Revisions (0)

No revisions yet.