Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql Server spatial data type Geometry, STDistance and units : Get meters?

I'm using Sql server 2012 and google maps client-side, I use SRID 3785, as it's mercator that maps uses. I use geometry point data type to store locations. I read geometry is faster than geography. But when I'm trying to calculate distance between points and when I use STDistance in particular, I get a distance ... in decimal degrees... which is great .. but I really need meters to display them to users... So, when you know your SRID, when you know which location on Earth your targeting exactly, how can you convert the distance in decimal degrees to meters ? I know geography deals with meter... but how do you get meters from geometry distances calculations ? I really wonder how developers were doin' before geography types appeared... thank you !!

like image 342
KitAndKat Avatar asked Feb 14 '23 19:02

KitAndKat


1 Answers

Sounds like you've got several issues here:

Firstly, the correct EPSG code for the Google Maps projection is 3857, not 3785. See my blog post at http://alastaira.wordpress.com/2011/01/23/the-google-maps-bing-maps-spherical-mercator-projection/ for an explanation of the rather complicated history behind the code.

Secondly, if you're using that SRS correctly, your coordinates should already be in metres. The coordinates of Big Ben in London, in EPSG:3857, for example, should be about (-13900, 6710330). If you've got coordinates like (51.5, -0.12) then those are WGS84 lat/lon, and you should be using the geography datatype with SRID:4326

Thirdly, 3857 is the projected coordinate system Google Maps uses to display spatial data, but the operations in its API use standard WGS84 (SRID 4326), so it might make sense to use that to store your data.

Very simply: if you want results in metres, you should either use the geography datatype with coordinates entered in degrees, or the geometry datatype with coordinates entered in metres.

like image 73
Alastair Aitchison Avatar answered May 01 '23 01:05

Alastair Aitchison