I have a mysql MyISAM table with many rows that contain information about places, such as geo coordinates and placenames.
I want to select the nearest place from this table depending given an input latitude and longitude. I want to do this creating a column with points of the geocoordinates, using a spatial index and MBRContains to select the data.
I get an error when selecting the data. I will write down the steps that I did here:
Step 1
ALTER TABLE mytable ADD mypoint Point;
UPDATE mytable
SET mypoint = Point(lat, lon);
ALTER TABLE mytable MODIFY mypoint NOT NULL;
CREATE SPATIAL INDEX sx_mytable_coords ON mytable(mypoint );
Result:
I see this in the mypoint column (I'm not sure if that's oke, but it's the same for all records): [GEOMETRY - 25Bytes]
I see that the index was added: Name: mypoint Type: SPATIAL Field: mypoint (32) Collation: A
Step 2
I tried to select the data in two ways:
This is recommended for version lower than MySQL 5.1 (My case)
SET @lat = 40; SET @lon = -10; SELECT * FROM mytable WHERE MBRContains(LineFromText(CONCAT( '(' , @lon + 10 / ( 111.1 / cos(RADIANS(@lon))) , ' ' , @lat + 10 / 111.1 , ',' , @lon - 10 / ( 111.1 / cos(RADIANS(@lat))) , ' ' , @lat - 10 / 111.1 , ')' ) ,mypoint)
I get this error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 0, 30' at line 15
This is recommended for version higher than MySQL 5.1
SET @lat = 40; SET @lon = -10; SELECT * FROM mytable WHERE MBRContains ( LineString ( Point ( @lon + 10 / ( 111.1 / COS(RADIANS(@lat))), @lat + 10 / 111.1 ) Point ( @lon - 10 / ( 111.1 / COS(RADIANS(@lat))), @lat - 10 / 111.1 ) ), mypoint )
I get this error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Point ( ' at line 12