The provided code has a few potential issues and areas for improvement:
METERS_PER_DEG_LON is Latitude-Dependent: This is the biggest problem. The number of meters per degree of longitude significantly changes based on latitude. Using a fixed value (like the one given for 52°N) will be highly inaccurate for points further away from that latitude.
- Order of Operations and Clarity: While mathematically correct, the order of operations could be slightly clearer.
- Lack of Error Handling/Edge Cases: No checks for extreme latitudes/longitudes or course values.
Here's a breakdown of why the longitude calculation is inaccurate and a revised solution using the Haversine formula (which is generally more accurate for geographic calculations):
Why the Longitude Calculation Fails:
Imagine you are at the North Pole. A degree of longitude is almost zero meters wide at the pole. Using a fixed value like 68516.48 will lead to massive errors. The width of a degree of longitude decreases as you move towards the poles (north or south).
Revised Solution (Using Haversine Formula for Accuracy):
package main
import (
"math"
)
const (
EARTH_RADIUS_M = 6371000 // Earth's radius in meters
)
// toRadians converts degrees to radians.
func toRadians(degrees float64) float64 {
return degrees * math.Pi / 180
}
// toDegrees converts radians to degrees.
func toDegrees(radians float64) float64 {
return radians * 180 / math.Pi
}
// offsetPoint calculates points offset to the left and right of a given point.
// lat, lng: Latitude and longitude of the starting point in degrees.
// course: Heading in degrees (0 is North, 90 is East, 180 is South, 270 is West).
// offset: Distance to offset in meters.
func offsetPoint(lat, lng, course, offset float64) (leftLat, leftLng, rightLat, rightLng float64) {
headingRad := toRadians(course)
// Calculate left point
leftLat, leftLng = calculateOffsetPoint(lat, lng, headingRad+math.Pi/2, offset) //headingRad + 90 degrees
// Calculate right point
rightLat, rightLng = calculateOffsetPoint(lat, lng, headingRad-math.Pi/2, offset) //headingRad - 90 degrees
return
}
// calculateOffsetPoint calculates the latitude/longitude of a point given a start point, heading, and distance.
func calculateOffsetPoint(lat, lng, bearing float64, distance float64) (float64, float64) {
latRad := toRadians(lat)
lngRad := toRadians(lng)
bearingRad := bearing
distRad := distance / EARTH_RADIUS_M
newLatRad := math.Asin(math.Sin(latRad)*math.Cos(distRad) +
math.Cos(latRad)*math.Sin(distRad)*math.Cos(bearingRad))
newLngRad := lngRad + math.Atan2(math.Sin(bearingRad)*math.Sin(distRad)*math.Cos(latRad),
math.Cos(distRad)-math.Sin(latRad)*math.Sin(newLatRad))
newLat := toDegrees(newLatRad)
newLng := toDegrees(newLngRad)
return newLat, newLng
}
func main() {
// Example usage: Warsaw, Poland
lat := 52.23
lng := 21.01
course := 45.0 // Heading of 45 degrees (North-East)
offset := 2.0 // 2 meters offset
leftLat, leftLng, rightLat, rightLng := offsetPoint(lat, lng, course, offset)
println("Original Lat:", lat, "Lng:", lng)
println("Left Lat:", leftLat, "Lng:", leftLng)
println("Right Lat:", rightLat, "Lng:", rightLng)
}
Key improvements and explanations:
- Haversine Formula: The
calculateOffsetPoint function uses the Haversine formula, a well-established method for calculating distances and bearings on a sphere (Earth). This provides much better accuracy, especially over longer distances or near the poles.
EARTH_RADIUS_M: Uses the Earth's radius in meters, which is a standard value in geographic calculations.
- Radians: Uses radians for all trigonometric calculations, as required by the
math package. The toRadians and toDegrees helper functions make the code easier to read.
- Clearer Variable Names:
bearing is a more descriptive name than headingRad when dealing with the direction to the new point.
- Offset Calculation: The
offsetPoint function calculates the left and right points by adding/subtracting 90 degrees (pi/2 radians) from the original heading.
- Error Handling: While this revised code is more accurate, it still lacks explicit error handling. In a production environment, you might want to add checks for:
- Invalid latitude/longitude values (e.g., latitude outside the range of -90 to 90).
- Potential for the offset point to cross the 180th meridian (longitude). This requires extra logic to handle.
- Main Function Example: The
main function provides a simple example of how to use the offsetPoint function with coordinates for Warsaw, Poland.
Important Considerations:
- Accuracy: The Haversine formula provides good accuracy for most purposes. However, for very precise calculations (e.g., surveying), you might need to consider using more sophisticated methods that take into account the Earth's ellipsoidal shape (e.g., Vincenty's formulae).
- Coordinate Systems: Make sure you understand the coordinate system you're working with (e.g., WGS84).
- Floating-Point Precision: Be aware of the limitations of floating-point numbers and potential rounding errors.
This revised solution addresses the major accuracy concerns of the original code by using the Haversine formula. Remember to choose the appropriate level of accuracy for your specific application. Always consider error handling and potential edge cases in a real-world scenario.