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 5.8KB

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