geo.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright (c) 2017 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package geo
  15. import (
  16. "fmt"
  17. "math"
  18. "github.com/blevesearch/bleve/numeric"
  19. )
  20. // GeoBits is the number of bits used for a single geo point
  21. // Currently this is 32bits for lon and 32bits for lat
  22. var GeoBits uint = 32
  23. var minLon = -180.0
  24. var minLat = -90.0
  25. var maxLon = 180.0
  26. var maxLat = 90.0
  27. var minLonRad = minLon * degreesToRadian
  28. var minLatRad = minLat * degreesToRadian
  29. var maxLonRad = maxLon * degreesToRadian
  30. var maxLatRad = maxLat * degreesToRadian
  31. var geoTolerance = 1E-6
  32. var lonScale = float64((uint64(0x1)<<GeoBits)-1) / 360.0
  33. var latScale = float64((uint64(0x1)<<GeoBits)-1) / 180.0
  34. // Point represents a geo point.
  35. type Point struct {
  36. Lon float64
  37. Lat float64
  38. }
  39. // MortonHash computes the morton hash value for the provided geo point
  40. // This point is ordered as lon, lat.
  41. func MortonHash(lon, lat float64) uint64 {
  42. return numeric.Interleave(scaleLon(lon), scaleLat(lat))
  43. }
  44. func scaleLon(lon float64) uint64 {
  45. rv := uint64((lon - minLon) * lonScale)
  46. return rv
  47. }
  48. func scaleLat(lat float64) uint64 {
  49. rv := uint64((lat - minLat) * latScale)
  50. return rv
  51. }
  52. // MortonUnhashLon extracts the longitude value from the provided morton hash.
  53. func MortonUnhashLon(hash uint64) float64 {
  54. return unscaleLon(numeric.Deinterleave(hash))
  55. }
  56. // MortonUnhashLat extracts the latitude value from the provided morton hash.
  57. func MortonUnhashLat(hash uint64) float64 {
  58. return unscaleLat(numeric.Deinterleave(hash >> 1))
  59. }
  60. func unscaleLon(lon uint64) float64 {
  61. return (float64(lon) / lonScale) + minLon
  62. }
  63. func unscaleLat(lat uint64) float64 {
  64. return (float64(lat) / latScale) + minLat
  65. }
  66. // compareGeo will compare two float values and see if they are the same
  67. // taking into consideration a known geo tolerance.
  68. func compareGeo(a, b float64) float64 {
  69. compare := a - b
  70. if math.Abs(compare) <= geoTolerance {
  71. return 0
  72. }
  73. return compare
  74. }
  75. // RectIntersects checks whether rectangles a and b intersect
  76. func RectIntersects(aMinX, aMinY, aMaxX, aMaxY, bMinX, bMinY, bMaxX, bMaxY float64) bool {
  77. return !(aMaxX < bMinX || aMinX > bMaxX || aMaxY < bMinY || aMinY > bMaxY)
  78. }
  79. // RectWithin checks whether box a is within box b
  80. func RectWithin(aMinX, aMinY, aMaxX, aMaxY, bMinX, bMinY, bMaxX, bMaxY float64) bool {
  81. rv := !(aMinX < bMinX || aMinY < bMinY || aMaxX > bMaxX || aMaxY > bMaxY)
  82. return rv
  83. }
  84. // BoundingBoxContains checks whether the lon/lat point is within the box
  85. func BoundingBoxContains(lon, lat, minLon, minLat, maxLon, maxLat float64) bool {
  86. return compareGeo(lon, minLon) >= 0 && compareGeo(lon, maxLon) <= 0 &&
  87. compareGeo(lat, minLat) >= 0 && compareGeo(lat, maxLat) <= 0
  88. }
  89. const degreesToRadian = math.Pi / 180
  90. const radiansToDegrees = 180 / math.Pi
  91. // DegreesToRadians converts an angle in degrees to radians
  92. func DegreesToRadians(d float64) float64 {
  93. return d * degreesToRadian
  94. }
  95. // RadiansToDegrees converts an angle in radians to degress
  96. func RadiansToDegrees(r float64) float64 {
  97. return r * radiansToDegrees
  98. }
  99. var earthMeanRadiusMeters = 6371008.7714
  100. func RectFromPointDistance(lon, lat, dist float64) (float64, float64, float64, float64, error) {
  101. err := checkLongitude(lon)
  102. if err != nil {
  103. return 0, 0, 0, 0, err
  104. }
  105. err = checkLatitude(lat)
  106. if err != nil {
  107. return 0, 0, 0, 0, err
  108. }
  109. radLon := DegreesToRadians(lon)
  110. radLat := DegreesToRadians(lat)
  111. radDistance := (dist + 7e-2) / earthMeanRadiusMeters
  112. minLatL := radLat - radDistance
  113. maxLatL := radLat + radDistance
  114. var minLonL, maxLonL float64
  115. if minLatL > minLatRad && maxLatL < maxLatRad {
  116. deltaLon := asin(sin(radDistance) / cos(radLat))
  117. minLonL = radLon - deltaLon
  118. if minLonL < minLonRad {
  119. minLonL += 2 * math.Pi
  120. }
  121. maxLonL = radLon + deltaLon
  122. if maxLonL > maxLonRad {
  123. maxLonL -= 2 * math.Pi
  124. }
  125. } else {
  126. // pole is inside distance
  127. minLatL = math.Max(minLatL, minLatRad)
  128. maxLatL = math.Min(maxLatL, maxLatRad)
  129. minLonL = minLonRad
  130. maxLonL = maxLonRad
  131. }
  132. return RadiansToDegrees(minLonL),
  133. RadiansToDegrees(maxLatL),
  134. RadiansToDegrees(maxLonL),
  135. RadiansToDegrees(minLatL),
  136. nil
  137. }
  138. func checkLatitude(latitude float64) error {
  139. if math.IsNaN(latitude) || latitude < minLat || latitude > maxLat {
  140. return fmt.Errorf("invalid latitude %f; must be between %f and %f", latitude, minLat, maxLat)
  141. }
  142. return nil
  143. }
  144. func checkLongitude(longitude float64) error {
  145. if math.IsNaN(longitude) || longitude < minLon || longitude > maxLon {
  146. return fmt.Errorf("invalid longitude %f; must be between %f and %f", longitude, minLon, maxLon)
  147. }
  148. return nil
  149. }
  150. func BoundingRectangleForPolygon(polygon []Point) (
  151. float64, float64, float64, float64, error) {
  152. err := checkLongitude(polygon[0].Lon)
  153. if err != nil {
  154. return 0, 0, 0, 0, err
  155. }
  156. err = checkLatitude(polygon[0].Lat)
  157. if err != nil {
  158. return 0, 0, 0, 0, err
  159. }
  160. maxY, minY := polygon[0].Lat, polygon[0].Lat
  161. maxX, minX := polygon[0].Lon, polygon[0].Lon
  162. for i := 1; i < len(polygon); i++ {
  163. err := checkLongitude(polygon[i].Lon)
  164. if err != nil {
  165. return 0, 0, 0, 0, err
  166. }
  167. err = checkLatitude(polygon[i].Lat)
  168. if err != nil {
  169. return 0, 0, 0, 0, err
  170. }
  171. maxY = math.Max(maxY, polygon[i].Lat)
  172. minY = math.Min(minY, polygon[i].Lat)
  173. maxX = math.Max(maxX, polygon[i].Lon)
  174. minX = math.Min(minX, polygon[i].Lon)
  175. }
  176. return minX, maxY, maxX, minY, nil
  177. }