开发者

How to use MySQL geospatial extensions with spherical geometries

开发者 https://www.devze.com 2023-02-03 09:37 出处:网络
I would like to store thousands of latitude/longitude points in a MySQL db.I was successful at setting up the tables and adding the data using the geospatial extensions where the column \'coord\' is a

I would like to store thousands of latitude/longitude points in a MySQL db. I was successful at setting up the tables and adding the data using the geospatial extensions where the column 'coord' is a Point(lat, lng).

Problem:

I want to quickly find the 'N' closest entries to latitude 'X' degrees and longitude 'Y' degrees. Since the Distance() function has not yet been implemented, I used GLength() function to calculate the distance between (X,Y) and each of the entries, sorting by ascending distance, and limiting to 'N' results. The problem is that this is not calculating shortest distance with spherical geometry. Which means if Y = 179.9 degrees, the list of closest entries will only include longitudes of starting at 179.9 and decreasing even though closer entries exist with longitudes increasing from -179.9.

How does one typically handle the discontinuity in longitude when working with spherical geometries in databases? There has to be an e开发者_如何学编程asy solution to this, but I must just be searching for the wrong thing because I have not found anything helpful.

Should I just forget the GLength() function and create my own function for calculating angular separation? If I do this, will it still be fast and take advantage of the geospatial extensions?

Thanks!

josh


UPDATE:

This is exactly what I am describing above. However, it is only for SQL Server. Apparently SQL Server has a Geometry and Geography datatypes. The geography does exactly what I need. Is there something similar in MySQL?


How does one typically handle the discontinuity in longitude when working with spherical geometries in databases?

Not many people use MySQL for this, because it's geospatial extensions aren't really up to snuff.

From the docs: "All calculations are done assuming Euclidean (planar) geometry."

The solution is usually to roll your own.

Alternatively, you can fake it -- if your distances are less than a 500 miles or so, then you can treat your latitude and longitude as rectangular coordinates and just use the euclidean distance formula (sqrt(a^2 + b^2)).

0

精彩评论

暂无评论...
验证码 换一张
取 消