Search code examples
c#coordinatesgeospatialdbgeography

DbGeography.Distance returned incorrect result


Here is my simplified code:

var P1 = Common.Geography.CreatePoint((decimal)-73.980000, (decimal)40.7155773);
var P2 = Common.Geography.CreatePoint((decimal)-73.984434, (decimal)40.7155773);
var Distance = P1.Distance(P2);

Here is "CreatePoint" function:

public static DbGeography CreatePoint(decimal Longitude, decimal Latitude)
{
    return DbGeography.FromText(String.Format(CultureInfo.InvariantCulture, "POINT({0} {1})", Longitude, Latitude));
}

I saw in a several sources that results should be in meters so I expected to see 493:

http://boulter.com/gps/distance/?from=-73.98%2C40.7155773&to=-73.984434%2C40.7155773&units=k

Google Maps measure tool shows same result (but it doesn't allow to share link).

However result I got from "Distance" function is 374.65. I checked CoordinateSystemId for both objecs and it has default value 4326.

So what I'm doing wrong? Or in which units is that result?


Solution

  • Check if the latitude / longitude order is swapped somewhere.

    I get 0.37k if I swap the order in your boulter link: http://boulter.com/gps/distance/?from=40.7155773%2C-73.98&to=40.7155773%2C-73.984434&units=k which is probably what you get in C# code.

    Note that for boulter and google maps order should be latitude, longitude; WKT uses the opposite order longitude, latitude.