开发者

What is the unit in returned by MySQL GLength method?

开发者 https://www.devze.com 2023-01-02 00:49 出处:网络
I want to get the leng开发者_开发知识库th in meters between 2 points in the surface of the Earth. But the GLength method returns an unexpected value

I want to get the leng开发者_开发知识库th in meters between 2 points in the surface of the Earth. But the GLength method returns an unexpected value

http://dev.mysql.com/doc/refman/5.0/en/geometry-property-functions.html#function_glength

SELECT GLength(GeomFromText(
   'LINESTRING(-67.8246 -10.0073,-67.8236 -10.0082)', 4326))

actual result

0.00134536240471071

expected value in meters:

147


I guess it's about 3 years too late for the OP, but I happened across this question while researching a similar topic, so here's my tuppence worth.

According to www.movable-type.co.uk/scripts/latlong.html there are 3 ways of calculating distance across the Earth's surface, these being, from easiest to most complex (and thus from least to most accurate): Equirectangular Approximation, Spherical Law of Cosines and the Haversine Formula. The site also provides JavaScript. This is the function for Equirectangular approximation:


function _lineDistanceC(lat1, lon1, lat2, lon2) {   
    var R = 6371; // km
    lat1 = lat1.toRad();
    lon1 = lon1.toRad();
    lat2 = lat2.toRad();
    lon2 = lon2.toRad();
    var x = (lon2-lon1) * Math.cos((lat1+lat2)/2);
    var y = (lat2-lat1);
    return Math.sqrt(x*x + y*y) * R;
}

This is my attempt at a SQL equivalent:


drop function if exists lineDistanceC;
delimiter //
CREATE FUNCTION lineDistanceC (la1 DOUBLE, lo1 DOUBLE, la2 DOUBLE, lo2 DOUBLE) RETURNS DOUBLE 
BEGIN 
    SET @r = 6371;
    SET @lat1 = RADIANS(la1);
    SET @lon1 = RADIANS(lo1);
    SET @lat2 = RADIANS(la2);
    SET @lon2 = RADIANS(lo2);
    SET @x = (@lon2-@lon1) * COS((@lat1+@lat2)/2);
    SET @y = (@lat2 - @lat1);
    RETURN (SQRT((@x*@x) + (@y*@y)) * @r);
END
//
delimiter ;

I have no idea how accurate this formula is, but the site seems very credible and definitely worth a visit for more detail. Using the coordinates in the question, the results (in kilometres) from the above JavaScript and SQL are 0.14834420231840376 and 0.1483442023182845, which makes them the same to a fraction of a millimetre, but not, curiously, the 147 metres that the OP was expecting. Enjoy.


1 degree (your input unit) ~= 69 miles. So, if you multiply your results by 69 miles and then convert to meters, you get roughly 149 meters. Not exactly what you expected but pretty close. Of course I don't think that accurately reproduces the spherical nature of the globe, but maybe I'm wrong.


I haven't found a source, but I was playing with this method and I concluded that the result is given in the same unit used in the WKT.

And the distance is calculated in planar coordinates. The SRID is not taken into account.


In a degree of latitude, there is about 69 miles. Looking at longitude, there is about 49 miles per degree. When i am trying to calculate distance between NYC and LONDON right number is 49.

Select Round(GLength(GeomFromText('LineString(40.756054 -73.986951,51.5001524 -0.1262362)')))*49;

0

精彩评论

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