geohash.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  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 main
  15. import (
  16. "fmt"
  17. "github.com/mmcloughlin/geohash"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. )
  20. type geohashEncode struct{}
  21. type geohashEncodeInt struct{}
  22. type geohashDecode struct{}
  23. type geohashDecodeInt struct{}
  24. type geohashBoundingBox struct{}
  25. type geohashBoundingBoxInt struct{}
  26. type geohashNeighbor struct{}
  27. type geohashNeighborInt struct{}
  28. type geohashNeighbors struct{}
  29. type geohashNeighborsInt struct{}
  30. type position struct {
  31. Longitude float64
  32. Latitude float64
  33. }
  34. var (
  35. GeohashEncode geohashEncode
  36. GeohashEncodeInt geohashEncodeInt
  37. GeohashDecode geohashDecode
  38. GeohashDecodeInt geohashDecodeInt
  39. GeohashBoundingBox geohashBoundingBox
  40. GeohashBoundingBoxInt geohashBoundingBoxInt
  41. GeohashNeighbor geohashNeighbor
  42. GeohashNeighborInt geohashNeighborInt
  43. GeohashNeighbors geohashNeighbors
  44. GeohashNeighborsInt geohashNeighborsInt
  45. g_direction = map[string]geohash.Direction{
  46. "North": geohash.North,
  47. "NorthEast": geohash.NorthEast,
  48. "East": geohash.East,
  49. "SouthEast": geohash.SouthEast,
  50. "South": geohash.South,
  51. "SouthWest": geohash.SouthWest,
  52. "West": geohash.West,
  53. "NorthWest": geohash.NorthWest,
  54. }
  55. )
  56. func (r *geohashEncode) IsAggregate() bool {
  57. return false
  58. }
  59. func (r *geohashEncodeInt) IsAggregate() bool {
  60. return false
  61. }
  62. func (r *geohashDecode) IsAggregate() bool {
  63. return false
  64. }
  65. func (r *geohashDecodeInt) IsAggregate() bool {
  66. return false
  67. }
  68. func (r *geohashBoundingBox) IsAggregate() bool {
  69. return false
  70. }
  71. func (r *geohashBoundingBoxInt) IsAggregate() bool {
  72. return false
  73. }
  74. func (r *geohashNeighbor) IsAggregate() bool {
  75. return false
  76. }
  77. func (r *geohashNeighborInt) IsAggregate() bool {
  78. return false
  79. }
  80. func (r *geohashNeighbors) IsAggregate() bool {
  81. return false
  82. }
  83. func (r *geohashNeighborsInt) IsAggregate() bool {
  84. return false
  85. }
  86. func (r *geohashEncode) Validate(args []interface{}) error {
  87. if len(args) != 2 {
  88. return fmt.Errorf("The geohashEncode function supports 2 parameters, but got %d", len(args))
  89. }
  90. return nil
  91. }
  92. func (r *geohashEncodeInt) Validate(args []interface{}) error {
  93. if len(args) != 2 {
  94. return fmt.Errorf("The geohashEncodeInt function supports 2 parameters, but got %d", len(args))
  95. }
  96. return nil
  97. }
  98. func (r *geohashDecode) Validate(args []interface{}) error {
  99. if len(args) != 1 {
  100. return fmt.Errorf("The geohashDecode function supports 1 parameters, but got %d", len(args))
  101. }
  102. return nil
  103. }
  104. func (r *geohashDecodeInt) Validate(args []interface{}) error {
  105. if len(args) != 1 {
  106. return fmt.Errorf("The geohashDecodeInt function supports 1 parameters, but got %d", len(args))
  107. }
  108. return nil
  109. }
  110. func (r *geohashBoundingBox) Validate(args []interface{}) error {
  111. if len(args) != 1 {
  112. return fmt.Errorf("The geohashBoundingBox function supports 1 parameters, but got %d", len(args))
  113. }
  114. return nil
  115. }
  116. func (r *geohashBoundingBoxInt) Validate(args []interface{}) error {
  117. if len(args) != 1 {
  118. return fmt.Errorf("The geohashBoundingBoxInt function supports 1 parameters, but got %d", len(args))
  119. }
  120. return nil
  121. }
  122. func (r *geohashNeighbor) Validate(args []interface{}) error {
  123. if len(args) != 2 {
  124. return fmt.Errorf("The geohashNeighbor function supports 2 parameters, but got %d", len(args))
  125. }
  126. return nil
  127. }
  128. func (r *geohashNeighborInt) Validate(args []interface{}) error {
  129. if len(args) != 2 {
  130. return fmt.Errorf("The geohashNeighborInt function supports 2 parameters, but got %d", len(args))
  131. }
  132. return nil
  133. }
  134. func (r *geohashNeighbors) Validate(args []interface{}) error {
  135. if len(args) != 1 {
  136. return fmt.Errorf("The geohashNeighbors function supports 1 parameters, but got %d", len(args))
  137. }
  138. return nil
  139. }
  140. func (r *geohashNeighborsInt) Validate(args []interface{}) error {
  141. if len(args) != 1 {
  142. return fmt.Errorf("The geohashNeighborsInt function supports 1 parameters, but got %d", len(args))
  143. }
  144. return nil
  145. }
  146. func (r *geohashEncode) Exec(args []interface{}, _ api.FunctionContext) (interface{}, bool) {
  147. la, ok := args[0].(float64)
  148. if !ok {
  149. return fmt.Errorf("arg[0] is not a float, got %v", args[0]), false
  150. }
  151. lo, ok := args[1].(float64)
  152. if !ok {
  153. return fmt.Errorf("arg[1] is not a float, got %v", args[1]), false
  154. }
  155. return geohash.Encode(la, lo), true
  156. }
  157. func (r *geohashEncodeInt) Exec(args []interface{}, _ api.FunctionContext) (interface{}, bool) {
  158. la, ok := args[0].(float64)
  159. if !ok {
  160. return fmt.Errorf("arg[0] is not a float, got %v", args[0]), false
  161. }
  162. lo, ok := args[1].(float64)
  163. if !ok {
  164. return fmt.Errorf("arg[1] is not a float, got %v", args[1]), false
  165. }
  166. return geohash.EncodeInt(la, lo), true
  167. }
  168. func (r *geohashDecode) Exec(args []interface{}, _ api.FunctionContext) (interface{}, bool) {
  169. hash, ok := args[0].(string)
  170. if !ok || 0 == len(hash) {
  171. return fmt.Errorf("arg[0] is not a string, got %v", args[0]), false
  172. }
  173. if err := geohash.Validate(hash); nil != err {
  174. return err, false
  175. }
  176. la, lo := geohash.Decode(hash)
  177. return position{Longitude: lo, Latitude: la}, true
  178. }
  179. func (r *geohashDecodeInt) Exec(args []interface{}, _ api.FunctionContext) (interface{}, bool) {
  180. hash, ok := args[0].(uint64)
  181. if !ok || 0 > hash {
  182. return fmt.Errorf("arg[0] is not a bigint, got %v", args[0]), false
  183. }
  184. la, lo := geohash.DecodeInt(hash)
  185. return position{Longitude: lo, Latitude: la}, true
  186. }
  187. func (r *geohashBoundingBox) Exec(args []interface{}, _ api.FunctionContext) (interface{}, bool) {
  188. hash, ok := args[0].(string)
  189. if !ok || 0 == len(hash) {
  190. return fmt.Errorf("arg[0] is not a string, got %v", args[0]), false
  191. }
  192. if err := geohash.Validate(hash); nil != err {
  193. return err, false
  194. }
  195. return geohash.BoundingBox(hash), true
  196. }
  197. func (r *geohashBoundingBoxInt) Exec(args []interface{}, _ api.FunctionContext) (interface{}, bool) {
  198. hash, ok := args[0].(uint64)
  199. if !ok || 0 > hash {
  200. return fmt.Errorf("arg[0] is not a bigint, got %v", args[0]), false
  201. }
  202. return geohash.BoundingBoxInt(hash), true
  203. }
  204. func (r *geohashNeighbor) Exec(args []interface{}, _ api.FunctionContext) (interface{}, bool) {
  205. hash, ok := args[0].(string)
  206. if !ok || 0 == len(hash) {
  207. return fmt.Errorf("arg[0] is not a string, got %v", args[0]), false
  208. }
  209. if err := geohash.Validate(hash); nil != err {
  210. return err, false
  211. }
  212. var directionCode geohash.Direction
  213. direction, ok := args[1].(string)
  214. if !ok || 0 == len(direction) {
  215. return fmt.Errorf("arg[1] is not a string, got %v", args[1]), false
  216. } else {
  217. directionCode, ok = g_direction[direction]
  218. if !ok {
  219. return fmt.Errorf("arg[1] is valid, got %v", args[1]), false
  220. }
  221. }
  222. return geohash.Neighbor(hash, directionCode), true
  223. }
  224. func (r *geohashNeighborInt) Exec(args []interface{}, _ api.FunctionContext) (interface{}, bool) {
  225. hash, ok := args[0].(uint64)
  226. if !ok || 0 > hash {
  227. return fmt.Errorf("arg[0] is not a bigint, got %v", args[0]), false
  228. }
  229. var directionCode geohash.Direction
  230. direction, ok := args[1].(string)
  231. if !ok || 0 == len(direction) {
  232. return fmt.Errorf("arg[1] is not a string, got %v", args[1]), false
  233. } else {
  234. directionCode, ok = g_direction[direction]
  235. if !ok {
  236. return fmt.Errorf("arg[1] is valid, got %v", args[1]), false
  237. }
  238. }
  239. return geohash.NeighborInt(hash, directionCode), true
  240. }
  241. func (r *geohashNeighbors) Exec(args []interface{}, _ api.FunctionContext) (interface{}, bool) {
  242. hash, ok := args[0].(string)
  243. if !ok || 0 == len(hash) {
  244. return fmt.Errorf("arg[0] is not a string, got %v", args[0]), false
  245. }
  246. if err := geohash.Validate(hash); nil != err {
  247. return err, false
  248. }
  249. return geohash.Neighbors(hash), true
  250. }
  251. func (r *geohashNeighborsInt) Exec(args []interface{}, _ api.FunctionContext) (interface{}, bool) {
  252. hash, ok := args[0].(uint64)
  253. if !ok || 0 > hash {
  254. return fmt.Errorf("arg[0] is not a bigint, got %v", args[0]), false
  255. }
  256. return geohash.NeighborsInt(hash), true
  257. }