geo.tcl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. # Helper functions to simulate search-in-radius in the Tcl side in order to
  2. # verify the Redis implementation with a fuzzy test.
  3. proc geo_degrad deg {expr {$deg*atan(1)*8/360}}
  4. proc geo_distance {lon1d lat1d lon2d lat2d} {
  5. set lon1r [geo_degrad $lon1d]
  6. set lat1r [geo_degrad $lat1d]
  7. set lon2r [geo_degrad $lon2d]
  8. set lat2r [geo_degrad $lat2d]
  9. set v [expr {sin(($lon2r - $lon1r) / 2)}]
  10. set u [expr {sin(($lat2r - $lat1r) / 2)}]
  11. expr {2.0 * 6372797.560856 * \
  12. asin(sqrt($u * $u + cos($lat1r) * cos($lat2r) * $v * $v))}
  13. }
  14. proc geo_random_point {lonvar latvar} {
  15. upvar 1 $lonvar lon
  16. upvar 1 $latvar lat
  17. # Note that the actual latitude limit should be -85 to +85, we restrict
  18. # the test to -70 to +70 since in this range the algorithm is more precise
  19. # while outside this range occasionally some element may be missing.
  20. set lon [expr {-180 + rand()*360}]
  21. set lat [expr {-70 + rand()*140}]
  22. }
  23. # Return elements non common to both the lists.
  24. # This code is from http://wiki.tcl.tk/15489
  25. proc compare_lists {List1 List2} {
  26. set DiffList {}
  27. foreach Item $List1 {
  28. if {[lsearch -exact $List2 $Item] == -1} {
  29. lappend DiffList $Item
  30. }
  31. }
  32. foreach Item $List2 {
  33. if {[lsearch -exact $List1 $Item] == -1} {
  34. if {[lsearch -exact $DiffList $Item] == -1} {
  35. lappend DiffList $Item
  36. }
  37. }
  38. }
  39. return $DiffList
  40. }
  41. # The following list represents sets of random seed, search position
  42. # and radius that caused bugs in the past. It is used by the randomized
  43. # test later as a starting point. When the regression vectors are scanned
  44. # the code reverts to using random data.
  45. #
  46. # The format is: seed km lon lat
  47. set regression_vectors {
  48. {1482225976969 7083 81.634948934258375 30.561509253718668}
  49. {1482340074151 5416 -70.863281847379767 -46.347003465679947}
  50. {1499014685896 6064 -89.818768962202014 -40.463868561416803}
  51. {1412 156 149.29737817929004 15.95807862745508}
  52. {441574 143 59.235461856813856 66.269555127373678}
  53. {160645 187 -101.88575239939883 49.061997951502917}
  54. {750269 154 -90.187939661642517 66.615930412251487}
  55. {342880 145 163.03472387745728 64.012747720821181}
  56. {729955 143 137.86663517256579 63.986745399416776}
  57. {939895 151 59.149620271823181 65.204186651485145}
  58. {1412 156 149.29737817929004 15.95807862745508}
  59. {564862 149 84.062063109158544 -65.685403922426232}
  60. }
  61. set rv_idx 0
  62. start_server {tags {"geo"}} {
  63. test {GEOADD create} {
  64. r geoadd nyc -73.9454966 40.747533 "lic market"
  65. } {1}
  66. test {GEOADD update} {
  67. r geoadd nyc -73.9454966 40.747533 "lic market"
  68. } {0}
  69. test {GEOADD invalid coordinates} {
  70. catch {
  71. r geoadd nyc -73.9454966 40.747533 "lic market" \
  72. foo bar "luck market"
  73. } err
  74. set err
  75. } {*valid*}
  76. test {GEOADD multi add} {
  77. r geoadd nyc -73.9733487 40.7648057 "central park n/q/r" -73.9903085 40.7362513 "union square" -74.0131604 40.7126674 "wtc one" -73.7858139 40.6428986 "jfk" -73.9375699 40.7498929 "q4" -73.9564142 40.7480973 4545
  78. } {6}
  79. test {Check geoset values} {
  80. r zrange nyc 0 -1 withscores
  81. } {{wtc one} 1791873972053020 {union square} 1791875485187452 {central park n/q/r} 1791875761332224 4545 1791875796750882 {lic market} 1791875804419201 q4 1791875830079666 jfk 1791895905559723}
  82. test {GEORADIUS simple (sorted)} {
  83. r georadius nyc -73.9798091 40.7598464 3 km asc
  84. } {{central park n/q/r} 4545 {union square}}
  85. test {GEORADIUS withdist (sorted)} {
  86. r georadius nyc -73.9798091 40.7598464 3 km withdist asc
  87. } {{{central park n/q/r} 0.7750} {4545 2.3651} {{union square} 2.7697}}
  88. test {GEORADIUS with COUNT} {
  89. r georadius nyc -73.9798091 40.7598464 10 km COUNT 3
  90. } {{central park n/q/r} 4545 {union square}}
  91. test {GEORADIUS with COUNT but missing integer argument} {
  92. catch {r georadius nyc -73.9798091 40.7598464 10 km COUNT} e
  93. set e
  94. } {ERR*syntax*}
  95. test {GEORADIUS with COUNT DESC} {
  96. r georadius nyc -73.9798091 40.7598464 10 km COUNT 2 DESC
  97. } {{wtc one} q4}
  98. test {GEORADIUS HUGE, issue #2767} {
  99. r geoadd users -47.271613776683807 -54.534504198047678 user_000000
  100. llength [r GEORADIUS users 0 0 50000 km WITHCOORD]
  101. } {1}
  102. test {GEORADIUSBYMEMBER simple (sorted)} {
  103. r georadiusbymember nyc "wtc one" 7 km
  104. } {{wtc one} {union square} {central park n/q/r} 4545 {lic market}}
  105. test {GEORADIUSBYMEMBER withdist (sorted)} {
  106. r georadiusbymember nyc "wtc one" 7 km withdist
  107. } {{{wtc one} 0.0000} {{union square} 3.2544} {{central park n/q/r} 6.7000} {4545 6.1975} {{lic market} 6.8969}}
  108. test {GEOHASH is able to return geohash strings} {
  109. # Example from Wikipedia.
  110. r del points
  111. r geoadd points -5.6 42.6 test
  112. lindex [r geohash points test] 0
  113. } {ezs42e44yx0}
  114. test {GEOPOS simple} {
  115. r del points
  116. r geoadd points 10 20 a 30 40 b
  117. lassign [lindex [r geopos points a b] 0] x1 y1
  118. lassign [lindex [r geopos points a b] 1] x2 y2
  119. assert {abs($x1 - 10) < 0.001}
  120. assert {abs($y1 - 20) < 0.001}
  121. assert {abs($x2 - 30) < 0.001}
  122. assert {abs($y2 - 40) < 0.001}
  123. }
  124. test {GEOPOS missing element} {
  125. r del points
  126. r geoadd points 10 20 a 30 40 b
  127. lindex [r geopos points a x b] 1
  128. } {}
  129. test {GEODIST simple & unit} {
  130. r del points
  131. r geoadd points 13.361389 38.115556 "Palermo" \
  132. 15.087269 37.502669 "Catania"
  133. set m [r geodist points Palermo Catania]
  134. assert {$m > 166274 && $m < 166275}
  135. set km [r geodist points Palermo Catania km]
  136. assert {$km > 166.2 && $km < 166.3}
  137. }
  138. test {GEODIST missing elements} {
  139. r del points
  140. r geoadd points 13.361389 38.115556 "Palermo" \
  141. 15.087269 37.502669 "Catania"
  142. set m [r geodist points Palermo Agrigento]
  143. assert {$m eq {}}
  144. set m [r geodist points Ragusa Agrigento]
  145. assert {$m eq {}}
  146. set m [r geodist empty_key Palermo Catania]
  147. assert {$m eq {}}
  148. }
  149. test {GEORADIUS STORE option: syntax error} {
  150. r del points
  151. r geoadd points 13.361389 38.115556 "Palermo" \
  152. 15.087269 37.502669 "Catania"
  153. catch {r georadius points 13.361389 38.115556 50 km store} e
  154. set e
  155. } {*ERR*syntax*}
  156. test {GEORANGE STORE option: incompatible options} {
  157. r del points
  158. r geoadd points 13.361389 38.115556 "Palermo" \
  159. 15.087269 37.502669 "Catania"
  160. catch {r georadius points 13.361389 38.115556 50 km store points2 withdist} e
  161. assert_match {*ERR*} $e
  162. catch {r georadius points 13.361389 38.115556 50 km store points2 withhash} e
  163. assert_match {*ERR*} $e
  164. catch {r georadius points 13.361389 38.115556 50 km store points2 withcoords} e
  165. assert_match {*ERR*} $e
  166. }
  167. test {GEORANGE STORE option: plain usage} {
  168. r del points
  169. r geoadd points 13.361389 38.115556 "Palermo" \
  170. 15.087269 37.502669 "Catania"
  171. r georadius points 13.361389 38.115556 500 km store points2
  172. assert_equal [r zrange points 0 -1] [r zrange points2 0 -1]
  173. }
  174. test {GEORANGE STOREDIST option: plain usage} {
  175. r del points
  176. r geoadd points 13.361389 38.115556 "Palermo" \
  177. 15.087269 37.502669 "Catania"
  178. r georadius points 13.361389 38.115556 500 km storedist points2
  179. set res [r zrange points2 0 -1 withscores]
  180. assert {[lindex $res 1] < 1}
  181. assert {[lindex $res 3] > 166}
  182. assert {[lindex $res 3] < 167}
  183. }
  184. test {GEORANGE STOREDIST option: COUNT ASC and DESC} {
  185. r del points
  186. r geoadd points 13.361389 38.115556 "Palermo" \
  187. 15.087269 37.502669 "Catania"
  188. r georadius points 13.361389 38.115556 500 km storedist points2 asc count 1
  189. assert {[r zcard points2] == 1}
  190. set res [r zrange points2 0 -1 withscores]
  191. assert {[lindex $res 0] eq "Palermo"}
  192. r georadius points 13.361389 38.115556 500 km storedist points2 desc count 1
  193. assert {[r zcard points2] == 1}
  194. set res [r zrange points2 0 -1 withscores]
  195. assert {[lindex $res 0] eq "Catania"}
  196. }
  197. test {GEOADD + GEORANGE randomized test} {
  198. set attempt 30
  199. while {[incr attempt -1]} {
  200. set rv [lindex $regression_vectors $rv_idx]
  201. incr rv_idx
  202. unset -nocomplain debuginfo
  203. set srand_seed [clock milliseconds]
  204. if {$rv ne {}} {set srand_seed [lindex $rv 0]}
  205. lappend debuginfo "srand_seed is $srand_seed"
  206. expr {srand($srand_seed)} ; # If you need a reproducible run
  207. r del mypoints
  208. if {[randomInt 10] == 0} {
  209. # From time to time use very big radiuses
  210. set radius_km [expr {[randomInt 50000]+10}]
  211. } else {
  212. # Normally use a few - ~200km radiuses to stress
  213. # test the code the most in edge cases.
  214. set radius_km [expr {[randomInt 200]+10}]
  215. }
  216. if {$rv ne {}} {set radius_km [lindex $rv 1]}
  217. set radius_m [expr {$radius_km*1000}]
  218. geo_random_point search_lon search_lat
  219. if {$rv ne {}} {
  220. set search_lon [lindex $rv 2]
  221. set search_lat [lindex $rv 3]
  222. }
  223. lappend debuginfo "Search area: $search_lon,$search_lat $radius_km km"
  224. set tcl_result {}
  225. set argv {}
  226. for {set j 0} {$j < 20000} {incr j} {
  227. geo_random_point lon lat
  228. lappend argv $lon $lat "place:$j"
  229. set distance [geo_distance $lon $lat $search_lon $search_lat]
  230. if {$distance < $radius_m} {
  231. lappend tcl_result "place:$j"
  232. }
  233. lappend debuginfo "place:$j $lon $lat [expr {$distance/1000}] km"
  234. }
  235. r geoadd mypoints {*}$argv
  236. set res [lsort [r georadius mypoints $search_lon $search_lat $radius_km km]]
  237. set res2 [lsort $tcl_result]
  238. set test_result OK
  239. if {$res != $res2} {
  240. set rounding_errors 0
  241. set diff [compare_lists $res $res2]
  242. foreach place $diff {
  243. set mydist [geo_distance $lon $lat $search_lon $search_lat]
  244. set mydist [expr $mydist/1000]
  245. if {($mydist / $radius_km) > 0.999} {incr rounding_errors}
  246. }
  247. # Make sure this is a real error and not a rounidng issue.
  248. if {[llength $diff] == $rounding_errors} {
  249. set res $res2; # Error silenced
  250. }
  251. }
  252. if {$res != $res2} {
  253. set diff [compare_lists $res $res2]
  254. puts "*** Possible problem in GEO radius query ***"
  255. puts "Redis: $res"
  256. puts "Tcl : $res2"
  257. puts "Diff : $diff"
  258. puts [join $debuginfo "\n"]
  259. foreach place $diff {
  260. if {[lsearch -exact $res2 $place] != -1} {
  261. set where "(only in Tcl)"
  262. } else {
  263. set where "(only in Redis)"
  264. }
  265. lassign [lindex [r geopos mypoints $place] 0] lon lat
  266. set mydist [geo_distance $lon $lat $search_lon $search_lat]
  267. set mydist [expr $mydist/1000]
  268. puts "$place -> [r geopos mypoints $place] $mydist $where"
  269. if {($mydist / $radius_km) > 0.999} {incr rounding_errors}
  270. }
  271. set test_result FAIL
  272. }
  273. unset -nocomplain debuginfo
  274. if {$test_result ne {OK}} break
  275. }
  276. set test_result
  277. } {OK}
  278. }