github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/geo/latlng.go (about) 1 // Copyright 2020 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package geo 12 13 import "math" 14 15 // NormalizeLatitudeDegrees normalizes latitudes to the range [-90, 90]. 16 func NormalizeLatitudeDegrees(lat float64) float64 { 17 // math.Remainder(lat, 360) returns in the range [-180, 180]. 18 lat = math.Remainder(lat, 360) 19 // If we are above 90 degrees, we curve back to 0, e.g. 91 -> 89, 100 -> 80. 20 if lat > 90 { 21 return 180 - lat 22 } 23 // If we are below 90 degrees, we curve back towards 0, e.g. -91 -> -89, -100 -> -80. 24 if lat < -90 { 25 return -180 - lat 26 } 27 return lat 28 } 29 30 // NormalizeLongitudeDegrees normalizes longitude to the range [-180, 180]. 31 func NormalizeLongitudeDegrees(lng float64) float64 { 32 // math.Remainder(lng, 360) returns in the range [-180, 180]. 33 return math.Remainder(lng, 360) 34 }