Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

spatial - MySQL latitude and Longitude table setup

I want to store latitude and longitude values of places in a mysql database table. With the future in mind I will want to be able to find these places within a certain radius of a specific location. Having said that, what datatypes should I store the latitude and longitude values in? Please could you provide me with a create table script for columns like so:

place_id  |  lat  |  long

Is there perhaps a column I am missing in the above table that will provide me with additional information that I may not see I need at the current time?

Thanks for any help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should store the points in a singe column of datatype Point which you can index with a SPATIAL index (if your table type is MyISAM):

CREATE SPATIAL INDEX sx_place_location ON place (location)

SELECT  *
FROM    mytable
WHERE   MBRContains
               (
               LineString
                       (
                       Point($x - $radius, $y - $radius),
                       Point($x + $radius, $y + $radius)
                       )
               location
               )
        AND Distance(Point($x, $y), location) <= $radius

This will drastically improve the speed of queries like "find all within a given radius".

Note that it is better to use plain TM metrical coordinates (easting and northing) instead of polar (latitude and longitude). For small radii, they are accurate enough, and the calculations are simplified greatly. If all your points are in one hemishpere and are far from the poles, you can use a single central meridian.

You still can use polar coordinates of course, but the formulae for calculating the MBR and the distance will be more complex.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...