0

I'm experimenting with System.Data.Spatial.DbGeography, that I want to use to determine the distance from one coordinate to another (going to be stored in SQL server).

My coordinates are in lat/long, and I got them from Bing Maps (I've tried with coordinates from Google Maps too, with the same result).

var osloCentralStation = DbGeography.FromText("POINT(59.9109 10.7523)", 4326);
var drammen = DbGeography.FromText("POINT(59.7378 10.2050)", 4326);
Console.WriteLine("Distance: {0}km", osloCentralStation.Distance(drammen) / 1000);

Returns: Distance: 63,4340839088124km

The returned distance is approximately double what it should be.

https://maps.google.com/maps?saddr=59.9109+10.7523&daddr=59.7378+10.2050

Does anybody have any idea as to what's going on?

4

2 回答 2

4

You're not declaring the element in WKT in the right order. WKT should be in your case:

POINT(10.2050 59.7378)

See OGC standard here:

And then it has to be declared like:

POINT(LONGITUDE LATITUDE)

Also keep in mind that it won't be the driving distance but the distance by air.

于 2013-04-15T21:18:26.343 回答
0

It turns out that lat/long are given as long/lat when creating new DbGeography objects.

I've written a little helper method so that I don't get it wrong again in the future:

private static DbGeography CreateDbGeography(double latitude, double longitude, int srid = 0)
{
    var text = string.Format(CultureInfo.InvariantCulture.NumberFormat, "POINT({0} {1})", longitude, latitude);

    if (srid > 0)
    {
        return DbGeography.FromText(text, srid);
    }

    return DbGeography.FromText(text);
} 
于 2013-04-15T20:06:59.440 回答