Home > OS >  Move coordinates a set distance in meters
Move coordinates a set distance in meters

Time:07-14

In SQL Server I want to be able to take a single latitude and longitude (51.500709, -0.124646) and move these coordinates 50 meters north, but I'm not sure how to do this. I am aware of the geography spatial data type and have used the STDistance function to get the distance between points when writing other queries. But I'm struggling to see any information on moving coordinates by a fixed distance.

CodePudding user response:

I was curious about the n/111111 approach in the link provided by Mitch.

I am pleased to report it "Ain't half bad". In this example, we were 0.06 meters (2.3 inches) off.

Example

Declare @Lat  float = 51.500709
Declare @Lng  float = -0.124646
Declare @LatM float = -50
Declare @LngM float = 0

;with cte as (
Select OldLat = @Lat
      ,OldLng = @Lng
      ,NewLat = @Lat   @LatM/111111
      ,NewLng = @Lng   @LngM/(111111 * cos(radians(@Lat)) )
)
Select *
      ,DistanceCheck = geography::Point(OldLat, OldLng, 4326).STDistance(geography::Point(NewLat, NewLng, 4326))
 From  cte 

Results

enter image description here

  • Related