You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

geo.go 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // MortonHash computes the morton hash value for the provided geo point
  35. // This point is ordered as lon, lat.
  36. func MortonHash(lon, lat float64) uint64 {
  37. return numeric.Interleave(scaleLon(lon), scaleLat(lat))
  38. }
  39. func scaleLon(lon float64) uint64 {
  40. rv := uint64((lon - minLon) * lonScale)
  41. return rv
  42. }
  43. func scaleLat(lat float64) uint64 {
  44. rv := uint64((lat - minLat) * latScale)
  45. return rv
  46. }
  47. // MortonUnhashLon extracts the longitude value from the provided morton hash.
  48. func MortonUnhashLon(hash uint64) float64 {
  49. return unscaleLon(numeric.Deinterleave(hash))
  50. }
  51. // MortonUnhashLat extracts the latitude value from the provided morton hash.
  52. func MortonUnhashLat(hash uint64) float64 {
  53. return unscaleLat(numeric.Deinterleave(hash >> 1))
  54. }
  55. func unscaleLon(lon uint64) float64 {
  56. return (float64(lon) / lonScale) + minLon
  57. }
  58. func unscaleLat(lat uint64) float64 {
  59. return (float64(lat) / latScale) + minLat
  60. }
  61. // compareGeo will compare two float values and see if they are the same
  62. // taking into consideration a known geo tolerance.
  63. func compareGeo(a, b float64) float64 {
  64. compare := a - b
  65. if math.Abs(compare) <= geoTolerance {
  66. return 0
  67. }
  68. return compare
  69. }
  70. // RectIntersects checks whether rectangles a and b intersect
  71. func RectIntersects(aMinX, aMinY, aMaxX, aMaxY, bMinX, bMinY, bMaxX, bMaxY float64) bool {
  72. return !(aMaxX < bMinX || aMinX > bMaxX || aMaxY < bMinY || aMinY > bMaxY)
  73. }
  74. // RectWithin checks whether box a is within box b
  75. func RectWithin(aMinX, aMinY, aMaxX, aMaxY, bMinX, bMinY, bMaxX, bMaxY float64) bool {
  76. rv := !(aMinX < bMinX || aMinY < bMinY || aMaxX > bMaxX || aMaxY > bMaxY)
  77. return rv
  78. }
  79. // BoundingBoxContains checks whether the lon/lat point is within the box
  80. func BoundingBoxContains(lon, lat, minLon, minLat, maxLon, maxLat float64) bool {
  81. return compareGeo(lon, minLon) >= 0 && compareGeo(lon, maxLon) <= 0 &&
  82. compareGeo(lat, minLat) >= 0 && compareGeo(lat, maxLat) <= 0
  83. }
  84. const degreesToRadian = math.Pi / 180
  85. const radiansToDegrees = 180 / math.Pi
  86. // DegreesToRadians converts an angle in degrees to radians
  87. func DegreesToRadians(d float64) float64 {
  88. return d * degreesToRadian
  89. }
  90. // RadiansToDegrees converts an angle in radians to degress
  91. func RadiansToDegrees(r float64) float64 {
  92. return r * radiansToDegrees
  93. }
  94. var earthMeanRadiusMeters = 6371008.7714
  95. func RectFromPointDistance(lon, lat, dist float64) (float64, float64, float64, float64, error) {
  96. err := checkLongitude(lon)
  97. if err != nil {
  98. return 0, 0, 0, 0, err
  99. }
  100. err = checkLatitude(lat)
  101. if err != nil {
  102. return 0, 0, 0, 0, err
  103. }
  104. radLon := DegreesToRadians(lon)
  105. radLat := DegreesToRadians(lat)
  106. radDistance := (dist + 7e-2) / earthMeanRadiusMeters
  107. minLatL := radLat - radDistance
  108. maxLatL := radLat + radDistance
  109. var minLonL, maxLonL float64
  110. if minLatL > minLatRad && maxLatL < maxLatRad {
  111. deltaLon := asin(sin(radDistance) / cos(radLat))
  112. minLonL = radLon - deltaLon
  113. if minLonL < minLonRad {
  114. minLonL += 2 * math.Pi
  115. }
  116. maxLonL = radLon + deltaLon
  117. if maxLonL > maxLonRad {
  118. maxLonL -= 2 * math.Pi
  119. }
  120. } else {
  121. // pole is inside distance
  122. minLatL = math.Max(minLatL, minLatRad)
  123. maxLatL = math.Min(maxLatL, maxLatRad)
  124. minLonL = minLonRad
  125. maxLonL = maxLonRad
  126. }
  127. return RadiansToDegrees(minLonL),
  128. RadiansToDegrees(maxLatL),
  129. RadiansToDegrees(maxLonL),
  130. RadiansToDegrees(minLatL),
  131. nil
  132. }
  133. func checkLatitude(latitude float64) error {
  134. if math.IsNaN(latitude) || latitude < minLat || latitude > maxLat {
  135. return fmt.Errorf("invalid latitude %f; must be between %f and %f", latitude, minLat, maxLat)
  136. }
  137. return nil
  138. }
  139. func checkLongitude(longitude float64) error {
  140. if math.IsNaN(longitude) || longitude < minLon || longitude > maxLon {
  141. return fmt.Errorf("invalid longitude %f; must be between %f and %f", longitude, minLon, maxLon)
  142. }
  143. return nil
  144. }