i have the following query to access the nearest locations around the given lat-lon. I followed Mr.Ollie's blog Nearest-location finder for MySQL to find nearest locations around given lat-long using haversine formula.
But due to lack of much knowledge in spatial data query i failed to execute it properly, so looking for an expert's advice to solve this.
Here is my query
SELECT z.id,
p.distance_unit
* DEGREES(ACOS(COS(RADIANS(p.latpoint))
* COS(RADIANS(z.(x(property))))
* COS(RADIANS(p.longpoint) - RADIANS(z.(y(property))))
+ SIN(RADIANS(p.latpoint))
* SIN(RADIANS(z.(x(property)))))) AS distance_in_km
FROM mytable AS z
JOIN ( /* these are the query parameters */
SELECT 12.00 AS latpoint, 77.00 AS longpoint,
20.0 AS radius, 111.045 AS distance_unit
) AS p
WHERE z.(x(property))
BETWEEN p.latpoint - (p.radius / p.distance_unit)
AND p.latpoint + (p.radius / p.distance_unit)
AND z.(y(property)
BETWEEN p.longpoint - (p.radius / (p.distance_unit * COS(RADIANS(p.latpoint))))
AND p.longpoint + (p.radius / (p.distance_unit * COS(RADIANS(p.latpoint))))
ORDER BY distance_in_km
LIMIT 15;
when i run this query i'm getting error as
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 '(x(property)))) * COS(RADIANS(p.longpoint) - RADIANS(z.(y(geo' at line 1
i also tried z.(GeomFromText(x.property)))
this is my table desc
+-----------+----------------+
| Field | Type |
+-----------+----------------+
| id | Int(10) |
| property | geometry |
+-----------+----------------+
select x(property) from mytable; //gives me lat
select y(property) from mytable; //gives me lan
Where am i going wrong? is this the way to achieve this.?
Please suggest.
Finally i followed this link and ended with this. query is not optimized yet but working great.
here is my query
It seems to me that you are assuming that once you have selected
z.id
in the query, that this gives you you direct access to thex(property)
andy(property)
(Aside - do those names really have parentheses in them?)
So to me it looks like you should replace things like
with something like
However on further thinking about it, I think that your
mytable
doesn't have the required structure. From looking at the link, I believe that yourmytable
should have a structure more like:So that you can do something like
NOTE
The above was based on me not understanding that MySQL supports spatial data types (for which I have no idea how to use)
Update
I just did some googling to understand the spatial types and found this:
How do you use MySQL spatial queries to find all records in X radius? [closed]
which suggests that you can't do what you want to do with spatial data types in mysql. Which thus brings you back to using a non-optimal way of storing data in
mutable
However in re-reading that link, the comments to the answer suggest that you may now be able to use spatial data types. (I told you I didn't have a clue here) This would mean replacing the query code with things like
ST_Distance(g1,g2)
, which effectively means totally rewriting the example.To put it another way
update 2
There are three paths you can follow:
Deny that spatial data types exists in MySQL and use a table that has explicit columns for
lat
andlong
, and use the sample code as originally written on that blog.Embrace the MySQL spatial data types (warts and all) and take a look at things like this answer https://stackoverflow.com/a/21231960/31326 that seem to do what you want directly with spatial data types, but as noted in that answer there are some caveats.
Use a spatial type to hold your data, and use a
pre-query
to extractlat
andlong
before passing it into the original sample code.