2
0

geo.tcl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. {1412 156 149.29737817929004 15.95807862745508}
  49. {441574 143 59.235461856813856 66.269555127373678}
  50. {160645 187 -101.88575239939883 49.061997951502917}
  51. {750269 154 -90.187939661642517 66.615930412251487}
  52. {342880 145 163.03472387745728 64.012747720821181}
  53. {729955 143 137.86663517256579 63.986745399416776}
  54. {939895 151 59.149620271823181 65.204186651485145}
  55. {1412 156 149.29737817929004 15.95807862745508}
  56. {564862 149 84.062063109158544 -65.685403922426232}
  57. }
  58. set rv_idx 0
  59. start_server {tags {"geo"}} {
  60. test {GEOADD create} {
  61. r geoadd nyc -73.9454966 40.747533 "lic market"
  62. } {1}
  63. test {GEOADD update} {
  64. r geoadd nyc -73.9454966 40.747533 "lic market"
  65. } {0}
  66. test {GEOADD invalid coordinates} {
  67. catch {
  68. r geoadd nyc -73.9454966 40.747533 "lic market" \
  69. foo bar "luck market"
  70. } err
  71. set err
  72. } {*valid*}
  73. test {GEOADD multi add} {
  74. 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
  75. } {6}
  76. test {Check geoset values} {
  77. r zrange nyc 0 -1 withscores
  78. } {{wtc one} 1791873972053020 {union square} 1791875485187452 {central park n/q/r} 1791875761332224 4545 1791875796750882 {lic market} 1791875804419201 q4 1791875830079666 jfk 1791895905559723}
  79. test {GEORADIUS simple (sorted)} {
  80. r georadius nyc -73.9798091 40.7598464 3 km asc
  81. } {{central park n/q/r} 4545 {union square}}
  82. test {GEORADIUS withdist (sorted)} {
  83. r georadius nyc -73.9798091 40.7598464 3 km withdist asc
  84. } {{{central park n/q/r} 0.7750} {4545 2.3651} {{union square} 2.7697}}
  85. test {GEORADIUS with COUNT} {
  86. r georadius nyc -73.9798091 40.7598464 10 km COUNT 3
  87. } {{central park n/q/r} 4545 {union square}}
  88. test {GEORADIUS with COUNT but missing integer argument} {
  89. catch {r georadius nyc -73.9798091 40.7598464 10 km COUNT} e
  90. set e
  91. } {ERR*syntax*}
  92. test {GEORADIUS with COUNT DESC} {
  93. r georadius nyc -73.9798091 40.7598464 10 km COUNT 2 DESC
  94. } {{wtc one} q4}
  95. test {GEORADIUS HUGE, issue #2767} {
  96. r geoadd users -47.271613776683807 -54.534504198047678 user_000000
  97. llength [r GEORADIUS users 0 0 50000 km WITHCOORD]
  98. } {1}
  99. test {GEORADIUSBYMEMBER simple (sorted)} {
  100. r georadiusbymember nyc "wtc one" 7 km
  101. } {{wtc one} {union square} {central park n/q/r} 4545 {lic market}}
  102. test {GEORADIUSBYMEMBER withdist (sorted)} {
  103. r georadiusbymember nyc "wtc one" 7 km withdist
  104. } {{{wtc one} 0.0000} {{union square} 3.2544} {{central park n/q/r} 6.7000} {4545 6.1975} {{lic market} 6.8969}}
  105. test {GEOHASH is able to return geohash strings} {
  106. # Example from Wikipedia.
  107. r del points
  108. r geoadd points -5.6 42.6 test
  109. lindex [r geohash points test] 0
  110. } {ezs42e44yx0}
  111. test {GEOPOS simple} {
  112. r del points
  113. r geoadd points 10 20 a 30 40 b
  114. lassign [lindex [r geopos points a b] 0] x1 y1
  115. lassign [lindex [r geopos points a b] 1] x2 y2
  116. assert {abs($x1 - 10) < 0.001}
  117. assert {abs($y1 - 20) < 0.001}
  118. assert {abs($x2 - 30) < 0.001}
  119. assert {abs($y2 - 40) < 0.001}
  120. }
  121. test {GEOPOS missing element} {
  122. r del points
  123. r geoadd points 10 20 a 30 40 b
  124. lindex [r geopos points a x b] 1
  125. } {}
  126. test {GEODIST simple & unit} {
  127. r del points
  128. r geoadd points 13.361389 38.115556 "Palermo" \
  129. 15.087269 37.502669 "Catania"
  130. set m [r geodist points Palermo Catania]
  131. assert {$m > 166274 && $m < 166275}
  132. set km [r geodist points Palermo Catania km]
  133. assert {$km > 166.2 && $km < 166.3}
  134. }
  135. test {GEODIST missing elements} {
  136. r del points
  137. r geoadd points 13.361389 38.115556 "Palermo" \
  138. 15.087269 37.502669 "Catania"
  139. set m [r geodist points Palermo Agrigento]
  140. assert {$m eq {}}
  141. set m [r geodist points Ragusa Agrigento]
  142. assert {$m eq {}}
  143. set m [r geodist empty_key Palermo Catania]
  144. assert {$m eq {}}
  145. }
  146. test {GEORADIUS STORE option: syntax error} {
  147. r del points
  148. r geoadd points 13.361389 38.115556 "Palermo" \
  149. 15.087269 37.502669 "Catania"
  150. catch {r georadius points 13.361389 38.115556 50 km store} e
  151. set e
  152. } {*ERR*syntax*}
  153. test {GEORANGE STORE option: incompatible options} {
  154. r del points
  155. r geoadd points 13.361389 38.115556 "Palermo" \
  156. 15.087269 37.502669 "Catania"
  157. catch {r georadius points 13.361389 38.115556 50 km store points2 withdist} e
  158. assert_match {*ERR*} $e
  159. catch {r georadius points 13.361389 38.115556 50 km store points2 withhash} e
  160. assert_match {*ERR*} $e
  161. catch {r georadius points 13.361389 38.115556 50 km store points2 withcoords} e
  162. assert_match {*ERR*} $e
  163. }
  164. test {GEORANGE STORE option: plain usage} {
  165. r del points
  166. r geoadd points 13.361389 38.115556 "Palermo" \
  167. 15.087269 37.502669 "Catania"
  168. r georadius points 13.361389 38.115556 500 km store points2
  169. assert_equal [r zrange points 0 -1] [r zrange points2 0 -1]
  170. }
  171. test {GEORANGE STOREDIST option: plain usage} {
  172. r del points
  173. r geoadd points 13.361389 38.115556 "Palermo" \
  174. 15.087269 37.502669 "Catania"
  175. r georadius points 13.361389 38.115556 500 km storedist points2
  176. set res [r zrange points2 0 -1 withscores]
  177. assert {[lindex $res 1] < 1}
  178. assert {[lindex $res 3] > 166}
  179. assert {[lindex $res 3] < 167}
  180. }
  181. test {GEORANGE STOREDIST option: COUNT ASC and DESC} {
  182. r del points
  183. r geoadd points 13.361389 38.115556 "Palermo" \
  184. 15.087269 37.502669 "Catania"
  185. r georadius points 13.361389 38.115556 500 km storedist points2 asc count 1
  186. assert {[r zcard points2] == 1}
  187. set res [r zrange points2 0 -1 withscores]
  188. assert {[lindex $res 0] eq "Palermo"}
  189. r georadius points 13.361389 38.115556 500 km storedist points2 desc count 1
  190. assert {[r zcard points2] == 1}
  191. set res [r zrange points2 0 -1 withscores]
  192. assert {[lindex $res 0] eq "Catania"}
  193. }
  194. test {GEOADD + GEORANGE randomized test} {
  195. set attempt 30
  196. while {[incr attempt -1]} {
  197. set rv [lindex $regression_vectors $rv_idx]
  198. incr rv_idx
  199. unset -nocomplain debuginfo
  200. set srand_seed [clock milliseconds]
  201. if {$rv ne {}} {set srand_seed [lindex $rv 0]}
  202. lappend debuginfo "srand_seed is $srand_seed"
  203. expr {srand($srand_seed)} ; # If you need a reproducible run
  204. r del mypoints
  205. if {[randomInt 10] == 0} {
  206. # From time to time use very big radiuses
  207. set radius_km [expr {[randomInt 50000]+10}]
  208. } else {
  209. # Normally use a few - ~200km radiuses to stress
  210. # test the code the most in edge cases.
  211. set radius_km [expr {[randomInt 200]+10}]
  212. }
  213. if {$rv ne {}} {set radius_km [lindex $rv 1]}
  214. set radius_m [expr {$radius_km*1000}]
  215. geo_random_point search_lon search_lat
  216. if {$rv ne {}} {
  217. set search_lon [lindex $rv 2]
  218. set search_lat [lindex $rv 3]
  219. }
  220. lappend debuginfo "Search area: $search_lon,$search_lat $radius_km km"
  221. set tcl_result {}
  222. set argv {}
  223. for {set j 0} {$j < 20000} {incr j} {
  224. geo_random_point lon lat
  225. lappend argv $lon $lat "place:$j"
  226. set distance [geo_distance $lon $lat $search_lon $search_lat]
  227. if {$distance < $radius_m} {
  228. lappend tcl_result "place:$j"
  229. }
  230. lappend debuginfo "place:$j $lon $lat [expr {$distance/1000}] km"
  231. }
  232. r geoadd mypoints {*}$argv
  233. set res [lsort [r georadius mypoints $search_lon $search_lat $radius_km km]]
  234. set res2 [lsort $tcl_result]
  235. set test_result OK
  236. if {$res != $res2} {
  237. set rounding_errors 0
  238. set diff [compare_lists $res $res2]
  239. foreach place $diff {
  240. set mydist [geo_distance $lon $lat $search_lon $search_lat]
  241. set mydist [expr $mydist/1000]
  242. if {($mydist / $radius_km) > 0.999} {incr rounding_errors}
  243. }
  244. # Make sure this is a real error and not a rounidng issue.
  245. if {[llength $diff] == $rounding_errors} {
  246. set res $res2; # Error silenced
  247. }
  248. }
  249. if {$res != $res2} {
  250. set diff [compare_lists $res $res2]
  251. puts "*** Possible problem in GEO radius query ***"
  252. puts "Redis: $res"
  253. puts "Tcl : $res2"
  254. puts "Diff : $diff"
  255. puts [join $debuginfo "\n"]
  256. foreach place $diff {
  257. if {[lsearch -exact $res2 $place] != -1} {
  258. set where "(only in Tcl)"
  259. } else {
  260. set where "(only in Redis)"
  261. }
  262. lassign [lindex [r geopos mypoints $place] 0] lon lat
  263. set mydist [geo_distance $lon $lat $search_lon $search_lat]
  264. set mydist [expr $mydist/1000]
  265. puts "$place -> [r geopos mypoints $place] $mydist $where"
  266. if {($mydist / $radius_km) > 0.999} {incr rounding_errors}
  267. }
  268. set test_result FAIL
  269. }
  270. unset -nocomplain debuginfo
  271. if {$test_result ne {OK}} break
  272. }
  273. set test_result
  274. } {OK}
  275. }