geohash.go 8.2 KB

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