Question

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 !!

Was it helpful?

Solution

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.

OTHER TIPS

The purpose of the Geography data type is to allow you to perform these types of calculations without any hassle. The geometry data type is intended for a flat surface, so the calculations that it performs are fairly straight forward, which is probably why they're slightly faster.

The geography data type, given the correct SRID, will account for the curvature of the Earth ad give you precise results. Depending on the distance between your two objects that curvature could result in a substantial difference.

I found a post on the MSDN that has a good explanation. Look at the second response to the user's question. There is a "rule of thumb" calculation that you could use to convert your STDistance result from degrees to meters, but it won't be precise:

http://social.msdn.microsoft.com/Forums/en-US/4f4648ef-9ef3-4a5c-974a-f34c5a325971/huge-difference-between-geography-and-geometry-results?forum=sqlspatial

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top