My goal is to use mysql POINT(lat,long) to find nearby entities in the database. I'm trying to do something like in the bottom of this tutorial http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL . Here is what I have got:
Table:
CREATE TABLE mark (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20) DEFAULT NULL,
loc POINT NOT NULL,
SPATIAL KEY loc (loc)
) ENGINE=MyISAM;
Inserting some test-data:
INSERT INTO mark (loc,name) VALUES (POINT(59.388433,10.415039), 'Somewhere 1');
INSERT INTO mark (loc,name) VALUES (POINT(63.41972,10.39856), 'Somewhere 2');
Declaring the distance function:
DELIMITER $$
CREATE FUNCTION `distance`
(a POINT, b POINT)
RETURNS double DETERMINISTIC
BEGIN
RETURN
round(glength(linestringfromwkb(linestring(asbinary(a),
asbinary(b)))));
END $$
DELIMITER;
Trying to use the function to search ex.:
SELECT name, distance(mark.loc, GeomFromText( ' POINT(31.5 42.2) ' )) AS cdist
FROM mark
ORDER BY
cdist limit 10;
or:
SELECT DISTINCT
dest.name,
distance(orig.loc, dest.loc) as sdistance
FROM
mark orig,
mark dest
having sdistance < 10
ORDER BY
sdistance limit 10;
The problem I am getting is: ERROR 1367 (22007): Illegal non geometric 'aswkb(a@0)' value found during parsing, or ERROR 1416 (22003): Cannot get geometry object from data you send to the GEOMETRY field
I can not seem to figure out how to solve this. The important thing is that the 'distance' function can be used dynamically.
I have also tried this solution: Find the distance between two points in MYSQL. (using the Point Datatype)
This is my mysql version mysql Ver 14.14 Distrib 5.5.23, for Linux (x86_64) using readline 5.1
Hope someones expertise can help me. Cheers!
round(glength(LineStringFromWKB(LineString(GeomFromText(astext(a)),GeomFromText(astext(b))))))
– Gastrulation