I have a longitude and latitude stored as a geometry with SRID 4326.
I want to make a line that is exactly 1000 meters long that is 90 degrees (to the right).
I know that the conversion from a longitudal/latitudal degree to a meter varies about where you are on the globe. That is why I will pass a reference long/lat that can be taken into consideration.
I am looking for something “good enough” that assumes that the distance you want will be no greater than 100 miles.
Given a long/lat and a meter distance of 1000 meters, return to me the size of the line long/lat degrees.
This question is specific to one example but I am looking for a general solution, because I have many functions that work upon “SRID Units” and the SRID I work with is long/lat (4326) but I always want to deal with meters and not degrees.
The hope is that I can call scaler function to convert the meters I want to the 4326 units.
Some hacks I have considered for finding X meters is converting the geometry into a geography and using the STBuffer(X) to make a circle with a radius equal to that of X, then create a long line that intersects the buffer and to find the point of the intersection, which will be the long/lat of exactly X meters away. This seems very hacky and inefficient but might be the most accurate
Edit:
To find the deltaX and deltaY the function is doing the the following:
DeltaX = Cos(@AngleRads) * @DistanceInDegrees; DeltaY = Sin(@AngleRads) * @DistanceInDegrees;
I can supply @AngleRads and @DistanceInDegrees. The problem is that @DistanceInDegrees has to match 4326 units (degrees). Is it even possible to find a @DistanceInDegrees that will correspond to 1000 meters no matter what angle is given?
When I use the formula
@DistanceInDegrees = (@Meters / 6371008.7714) * (180 / pi()) / cos(@Lat1 * pi()/180)
and a angle of 90 degrees, then the length of the line is 1002 meters, close but not identically 1000.. If I use a degree of 45 the length of the line is 1191.67 meters.
Advertisement
Answer
After your EDIT, It seems you are looking for DISTANCE and BEARING
Here is a Table-Valued Function which may help
Example
Select * from [dbo].[tvf-Geo-Distance-Bearing](-37.786570,145.178179,1000,-45)
Returns
RetLat RetLng -37.7802105711301 145.170133170589
Now, when I calculate the distance, I get 999.99999448737 meters
The Table-Valued Function If Interested
CREATE Function [dbo].[tvf-Geo-Distance-Bearing] (@Lat1 float,@Lng1 float,@Dist Float,@Degr float) Returns @LatLng Table (RetLat float,RetLng Float) As Begin Declare @Lat2 float,@Lng2 float,@eRad float = 6371008.7714 Select @Lat1 = RADIANS(@Lat1) ,@Lng1 = RADIANS(@Lng1) ,@Degr = RADIANS(@Degr) Set @Lat2 = Asin(Sin(@Lat1) * Cos(@Dist / @eRad ) + Cos(@Lat1) * Sin(@Dist / @eRad ) * Cos(@Degr )) Set @Lng2 = @Lng1 + Atn2(Sin(@Degr) * Sin(@Dist / @eRad ) * Cos(@Lat1), Cos(@Dist / @eRad ) - Sin(@Lat1) * Sin(@Lat2)) Insert Into @LatLng Select Degrees(@Lat2) ,Degrees(@Lng2) Return; End