geo.tcl 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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_raddeg rad {expr {$rad/(atan(1)*8/360)}}
  5. proc geo_distance {lon1d lat1d lon2d lat2d} {
  6. set lon1r [geo_degrad $lon1d]
  7. set lat1r [geo_degrad $lat1d]
  8. set lon2r [geo_degrad $lon2d]
  9. set lat2r [geo_degrad $lat2d]
  10. set v [expr {sin(($lon2r - $lon1r) / 2)}]
  11. set u [expr {sin(($lat2r - $lat1r) / 2)}]
  12. expr {2.0 * 6372797.560856 * \
  13. asin(sqrt($u * $u + cos($lat1r) * cos($lat2r) * $v * $v))}
  14. }
  15. proc geo_random_point {lonvar latvar} {
  16. upvar 1 $lonvar lon
  17. upvar 1 $latvar lat
  18. # Note that the actual latitude limit should be -85 to +85, we restrict
  19. # the test to -70 to +70 since in this range the algorithm is more precise
  20. # while outside this range occasionally some element may be missing.
  21. set lon [expr {-180 + rand()*360}]
  22. set lat [expr {-70 + rand()*140}]
  23. }
  24. # Return elements non common to both the lists.
  25. # This code is from http://wiki.tcl.tk/15489
  26. proc compare_lists {List1 List2} {
  27. set DiffList {}
  28. foreach Item $List1 {
  29. if {[lsearch -exact $List2 $Item] == -1} {
  30. lappend DiffList $Item
  31. }
  32. }
  33. foreach Item $List2 {
  34. if {[lsearch -exact $List1 $Item] == -1} {
  35. if {[lsearch -exact $DiffList $Item] == -1} {
  36. lappend DiffList $Item
  37. }
  38. }
  39. }
  40. return $DiffList
  41. }
  42. # return true If a point in circle.
  43. # search_lon and search_lat define the center of the circle,
  44. # and lon, lat define the point being searched.
  45. proc pointInCircle {radius_km lon lat search_lon search_lat} {
  46. set radius_m [expr {$radius_km*1000}]
  47. set distance [geo_distance $lon $lat $search_lon $search_lat]
  48. if {$distance < $radius_m} {
  49. return true
  50. }
  51. return false
  52. }
  53. # return true If a point in rectangle.
  54. # search_lon and search_lat define the center of the rectangle,
  55. # and lon, lat define the point being searched.
  56. # error: can adjust the width and height of the rectangle according to the error
  57. proc pointInRectangle {width_km height_km lon lat search_lon search_lat error} {
  58. set width_m [expr {$width_km*1000*$error/2}]
  59. set height_m [expr {$height_km*1000*$error/2}]
  60. set lon_distance [geo_distance $lon $lat $search_lon $lat]
  61. set lat_distance [geo_distance $lon $lat $lon $search_lat]
  62. if {$lon_distance > $width_m || $lat_distance > $height_m} {
  63. return false
  64. }
  65. return true
  66. }
  67. proc verify_geo_edge_response_bylonlat {expected_response expected_store_response} {
  68. catch {r georadius src{t} 1 1 1 km} response
  69. assert_match $expected_response $response
  70. catch {r georadius src{t} 1 1 1 km store dest{t}} response
  71. assert_match $expected_store_response $response
  72. catch {r geosearch src{t} fromlonlat 0 0 byradius 1 km} response
  73. assert_match $expected_response $response
  74. catch {r geosearchstore dest{t} src{t} fromlonlat 0 0 byradius 1 km} response
  75. assert_match $expected_store_response $response
  76. }
  77. proc verify_geo_edge_response_bymember {expected_response expected_store_response} {
  78. catch {r georadiusbymember src{t} member 1 km} response
  79. assert_match $expected_response $response
  80. catch {r georadiusbymember src{t} member 1 km store dest{t}} response
  81. assert_match $expected_store_response $response
  82. catch {r geosearch src{t} frommember member bybox 1 1 km} response
  83. assert_match $expected_response $response
  84. catch {r geosearchstore dest{t} src{t} frommember member bybox 1 1 m} response
  85. assert_match $expected_store_response $response
  86. }
  87. # The following list represents sets of random seed, search position
  88. # and radius that caused bugs in the past. It is used by the randomized
  89. # test later as a starting point. When the regression vectors are scanned
  90. # the code reverts to using random data.
  91. #
  92. # The format is: seed km lon lat
  93. set regression_vectors {
  94. {1482225976969 7083 81.634948934258375 30.561509253718668}
  95. {1482340074151 5416 -70.863281847379767 -46.347003465679947}
  96. {1499014685896 6064 -89.818768962202014 -40.463868561416803}
  97. {1412 156 149.29737817929004 15.95807862745508}
  98. {441574 143 59.235461856813856 66.269555127373678}
  99. {160645 187 -101.88575239939883 49.061997951502917}
  100. {750269 154 -90.187939661642517 66.615930412251487}
  101. {342880 145 163.03472387745728 64.012747720821181}
  102. {729955 143 137.86663517256579 63.986745399416776}
  103. {939895 151 59.149620271823181 65.204186651485145}
  104. {1412 156 149.29737817929004 15.95807862745508}
  105. {564862 149 84.062063109158544 -65.685403922426232}
  106. {1546032440391 16751 -1.8175081637769495 20.665668878082954}
  107. }
  108. set rv_idx 0
  109. start_server {tags {"geo"}} {
  110. test {GEO with wrong type src key} {
  111. r set src{t} wrong_type
  112. verify_geo_edge_response_bylonlat "WRONGTYPE*" "WRONGTYPE*"
  113. verify_geo_edge_response_bymember "WRONGTYPE*" "WRONGTYPE*"
  114. }
  115. test {GEO with non existing src key} {
  116. r del src{t}
  117. verify_geo_edge_response_bylonlat {} 0
  118. verify_geo_edge_response_bymember {} 0
  119. }
  120. test {GEO BYLONLAT with empty search} {
  121. r del src{t}
  122. r geoadd src{t} 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
  123. verify_geo_edge_response_bylonlat {} 0
  124. }
  125. test {GEO BYMEMBER with non existing member} {
  126. r del src{t}
  127. r geoadd src{t} 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
  128. verify_geo_edge_response_bymember "ERR*" "ERR*"
  129. }
  130. test {GEOADD create} {
  131. r geoadd nyc -73.9454966 40.747533 "lic market"
  132. } {1}
  133. test {GEOADD update} {
  134. r geoadd nyc -73.9454966 40.747533 "lic market"
  135. } {0}
  136. test {GEOADD update with CH option} {
  137. assert_equal 1 [r geoadd nyc CH 40.747533 -73.9454966 "lic market"]
  138. lassign [lindex [r geopos nyc "lic market"] 0] x1 y1
  139. assert {abs($x1) - 40.747 < 0.001}
  140. assert {abs($y1) - 73.945 < 0.001}
  141. } {}
  142. test {GEOADD update with NX option} {
  143. assert_equal 0 [r geoadd nyc NX -73.9454966 40.747533 "lic market"]
  144. lassign [lindex [r geopos nyc "lic market"] 0] x1 y1
  145. assert {abs($x1) - 40.747 < 0.001}
  146. assert {abs($y1) - 73.945 < 0.001}
  147. } {}
  148. test {GEOADD update with XX option} {
  149. assert_equal 0 [r geoadd nyc XX -83.9454966 40.747533 "lic market"]
  150. lassign [lindex [r geopos nyc "lic market"] 0] x1 y1
  151. assert {abs($x1) - 83.945 < 0.001}
  152. assert {abs($y1) - 40.747 < 0.001}
  153. } {}
  154. test {GEOADD update with CH NX option} {
  155. r geoadd nyc CH NX -73.9454966 40.747533 "lic market"
  156. } {0}
  157. test {GEOADD update with CH XX option} {
  158. r geoadd nyc CH XX -73.9454966 40.747533 "lic market"
  159. } {1}
  160. test {GEOADD update with XX NX option will return syntax error} {
  161. catch {
  162. r geoadd nyc xx nx -73.9454966 40.747533 "lic market"
  163. } err
  164. set err
  165. } {ERR*syntax*}
  166. test {GEOADD update with invalid option} {
  167. catch {
  168. r geoadd nyc ch xx foo -73.9454966 40.747533 "lic market"
  169. } err
  170. set err
  171. } {ERR*syntax*}
  172. test {GEOADD invalid coordinates} {
  173. catch {
  174. r geoadd nyc -73.9454966 40.747533 "lic market" \
  175. foo bar "luck market"
  176. } err
  177. set err
  178. } {*valid*}
  179. test {GEOADD multi add} {
  180. 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
  181. } {6}
  182. test {Check geoset values} {
  183. r zrange nyc 0 -1 withscores
  184. } {{wtc one} 1791873972053020 {union square} 1791875485187452 {central park n/q/r} 1791875761332224 4545 1791875796750882 {lic market} 1791875804419201 q4 1791875830079666 jfk 1791895905559723}
  185. test {GEORADIUS simple (sorted)} {
  186. r georadius nyc -73.9798091 40.7598464 3 km asc
  187. } {{central park n/q/r} 4545 {union square}}
  188. test {GEOSEARCH simple (sorted)} {
  189. r geosearch nyc fromlonlat -73.9798091 40.7598464 bybox 6 6 km asc
  190. } {{central park n/q/r} 4545 {union square} {lic market}}
  191. test {GEOSEARCH FROMLONLAT and FROMMEMBER cannot exist at the same time} {
  192. catch {r geosearch nyc fromlonlat -73.9798091 40.7598464 frommember xxx bybox 6 6 km asc} e
  193. set e
  194. } {ERR*syntax*}
  195. test {GEOSEARCH FROMLONLAT and FROMMEMBER one must exist} {
  196. catch {r geosearch nyc bybox 3 3 km asc desc withhash withdist withcoord} e
  197. set e
  198. } {ERR*exactly one of FROMMEMBER or FROMLONLAT*}
  199. test {GEOSEARCH BYRADIUS and BYBOX cannot exist at the same time} {
  200. catch {r geosearch nyc fromlonlat -73.9798091 40.7598464 byradius 3 km bybox 3 3 km asc} e
  201. set e
  202. } {ERR*syntax*}
  203. test {GEOSEARCH BYRADIUS and BYBOX one must exist} {
  204. catch {r geosearch nyc fromlonlat -73.9798091 40.7598464 asc desc withhash withdist withcoord} e
  205. set e
  206. } {ERR*exactly one of BYRADIUS and BYBOX*}
  207. test {GEOSEARCH with STOREDIST option} {
  208. catch {r geosearch nyc fromlonlat -73.9798091 40.7598464 bybox 6 6 km asc storedist} e
  209. set e
  210. } {ERR*syntax*}
  211. test {GEORADIUS withdist (sorted)} {
  212. r georadius nyc -73.9798091 40.7598464 3 km withdist asc
  213. } {{{central park n/q/r} 0.7750} {4545 2.3651} {{union square} 2.7697}}
  214. test {GEOSEARCH withdist (sorted)} {
  215. r geosearch nyc fromlonlat -73.9798091 40.7598464 bybox 6 6 km withdist asc
  216. } {{{central park n/q/r} 0.7750} {4545 2.3651} {{union square} 2.7697} {{lic market} 3.1991}}
  217. test {GEORADIUS with COUNT} {
  218. r georadius nyc -73.9798091 40.7598464 10 km COUNT 3
  219. } {{central park n/q/r} 4545 {union square}}
  220. test {GEORADIUS with ANY not sorted by default} {
  221. r georadius nyc -73.9798091 40.7598464 10 km COUNT 3 ANY
  222. } {{wtc one} {union square} {central park n/q/r}}
  223. test {GEORADIUS with ANY sorted by ASC} {
  224. r georadius nyc -73.9798091 40.7598464 10 km COUNT 3 ANY ASC
  225. } {{central park n/q/r} {union square} {wtc one}}
  226. test {GEORADIUS with ANY but no COUNT} {
  227. catch {r georadius nyc -73.9798091 40.7598464 10 km ANY ASC} e
  228. set e
  229. } {ERR*ANY*requires*COUNT*}
  230. test {GEORADIUS with COUNT but missing integer argument} {
  231. catch {r georadius nyc -73.9798091 40.7598464 10 km COUNT} e
  232. set e
  233. } {ERR*syntax*}
  234. test {GEORADIUS with COUNT DESC} {
  235. r georadius nyc -73.9798091 40.7598464 10 km COUNT 2 DESC
  236. } {{wtc one} q4}
  237. test {GEORADIUS HUGE, issue #2767} {
  238. r geoadd users -47.271613776683807 -54.534504198047678 user_000000
  239. llength [r GEORADIUS users 0 0 50000 km WITHCOORD]
  240. } {1}
  241. test {GEORADIUSBYMEMBER simple (sorted)} {
  242. r georadiusbymember nyc "wtc one" 7 km
  243. } {{wtc one} {union square} {central park n/q/r} 4545 {lic market}}
  244. test {GEOSEARCH FROMMEMBER simple (sorted)} {
  245. r geosearch nyc frommember "wtc one" bybox 14 14 km
  246. } {{wtc one} {union square} {central park n/q/r} 4545 {lic market} q4}
  247. test {GEOSEARCH vs GEORADIUS} {
  248. r del Sicily
  249. r geoadd Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
  250. r geoadd Sicily 12.758489 38.788135 "edge1" 17.241510 38.788135 "eage2"
  251. set ret1 [r georadius Sicily 15 37 200 km asc]
  252. assert_equal $ret1 {Catania Palermo}
  253. set ret2 [r geosearch Sicily fromlonlat 15 37 bybox 400 400 km asc]
  254. assert_equal $ret2 {Catania Palermo eage2 edge1}
  255. }
  256. test {GEOSEARCH non square, long and narrow} {
  257. r del Sicily
  258. r geoadd Sicily 12.75 36.995 "test1"
  259. r geoadd Sicily 12.75 36.50 "test2"
  260. r geoadd Sicily 13.00 36.50 "test3"
  261. # box height=2km width=400km
  262. set ret1 [r geosearch Sicily fromlonlat 15 37 bybox 400 2 km]
  263. assert_equal $ret1 {test1}
  264. # Add a western Hemisphere point
  265. r geoadd Sicily -1 37.00 "test3"
  266. set ret2 [r geosearch Sicily fromlonlat 15 37 bybox 3000 2 km asc]
  267. assert_equal $ret2 {test1 test3}
  268. }
  269. test {GEOSEARCH corner point test} {
  270. r del Sicily
  271. r geoadd Sicily 12.758489 38.788135 edge1 17.241510 38.788135 edge2 17.250000 35.202000 edge3 12.750000 35.202000 edge4 12.748489955781654 37 edge5 15 38.798135872540925 edge6 17.251510044218346 37 edge7 15 35.201864127459075 edge8 12.692799634687903 38.798135872540925 corner1 12.692799634687903 38.798135872540925 corner2 17.200560937451133 35.201864127459075 corner3 12.799439062548865 35.201864127459075 corner4
  272. set ret [lsort [r geosearch Sicily fromlonlat 15 37 bybox 400 400 km asc]]
  273. assert_equal $ret {edge1 edge2 edge5 edge7}
  274. }
  275. test {GEORADIUSBYMEMBER withdist (sorted)} {
  276. r georadiusbymember nyc "wtc one" 7 km withdist
  277. } {{{wtc one} 0.0000} {{union square} 3.2544} {{central park n/q/r} 6.7000} {4545 6.1975} {{lic market} 6.8969}}
  278. test {GEOHASH is able to return geohash strings} {
  279. # Example from Wikipedia.
  280. r del points
  281. r geoadd points -5.6 42.6 test
  282. lindex [r geohash points test] 0
  283. } {ezs42e44yx0}
  284. test {GEOPOS simple} {
  285. r del points
  286. r geoadd points 10 20 a 30 40 b
  287. lassign [lindex [r geopos points a b] 0] x1 y1
  288. lassign [lindex [r geopos points a b] 1] x2 y2
  289. assert {abs($x1 - 10) < 0.001}
  290. assert {abs($y1 - 20) < 0.001}
  291. assert {abs($x2 - 30) < 0.001}
  292. assert {abs($y2 - 40) < 0.001}
  293. }
  294. test {GEOPOS missing element} {
  295. r del points
  296. r geoadd points 10 20 a 30 40 b
  297. lindex [r geopos points a x b] 1
  298. } {}
  299. test {GEODIST simple & unit} {
  300. r del points
  301. r geoadd points 13.361389 38.115556 "Palermo" \
  302. 15.087269 37.502669 "Catania"
  303. set m [r geodist points Palermo Catania]
  304. assert {$m > 166274 && $m < 166275}
  305. set km [r geodist points Palermo Catania km]
  306. assert {$km > 166.2 && $km < 166.3}
  307. }
  308. test {GEODIST missing elements} {
  309. r del points
  310. r geoadd points 13.361389 38.115556 "Palermo" \
  311. 15.087269 37.502669 "Catania"
  312. set m [r geodist points Palermo Agrigento]
  313. assert {$m eq {}}
  314. set m [r geodist points Ragusa Agrigento]
  315. assert {$m eq {}}
  316. set m [r geodist empty_key Palermo Catania]
  317. assert {$m eq {}}
  318. }
  319. test {GEORADIUS STORE option: syntax error} {
  320. r del points{t}
  321. r geoadd points{t} 13.361389 38.115556 "Palermo" \
  322. 15.087269 37.502669 "Catania"
  323. catch {r georadius points{t} 13.361389 38.115556 50 km store} e
  324. set e
  325. } {*ERR*syntax*}
  326. test {GEOSEARCHSTORE STORE option: syntax error} {
  327. catch {r geosearchstore abc{t} points{t} fromlonlat 13.361389 38.115556 byradius 50 km store abc{t}} e
  328. set e
  329. } {*ERR*syntax*}
  330. test {GEORANGE STORE option: incompatible options} {
  331. r del points{t}
  332. r geoadd points{t} 13.361389 38.115556 "Palermo" \
  333. 15.087269 37.502669 "Catania"
  334. catch {r georadius points{t} 13.361389 38.115556 50 km store points2{t} withdist} e
  335. assert_match {*ERR*} $e
  336. catch {r georadius points{t} 13.361389 38.115556 50 km store points2{t} withhash} e
  337. assert_match {*ERR*} $e
  338. catch {r georadius points{t} 13.361389 38.115556 50 km store points2{t} withcoords} e
  339. assert_match {*ERR*} $e
  340. }
  341. test {GEORANGE STORE option: plain usage} {
  342. r del points{t}
  343. r geoadd points{t} 13.361389 38.115556 "Palermo" \
  344. 15.087269 37.502669 "Catania"
  345. r georadius points{t} 13.361389 38.115556 500 km store points2{t}
  346. assert_equal [r zrange points{t} 0 -1] [r zrange points2{t} 0 -1]
  347. }
  348. test {GEORADIUSBYMEMBER STORE/STOREDIST option: plain usage} {
  349. r del points{t}
  350. r geoadd points{t} 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
  351. r georadiusbymember points{t} Palermo 500 km store points2{t}
  352. assert_equal {Palermo Catania} [r zrange points2{t} 0 -1]
  353. r georadiusbymember points{t} Catania 500 km storedist points2{t}
  354. assert_equal {Catania Palermo} [r zrange points2{t} 0 -1]
  355. set res [r zrange points2{t} 0 -1 withscores]
  356. assert {[lindex $res 1] < 1}
  357. assert {[lindex $res 3] > 166}
  358. }
  359. test {GEOSEARCHSTORE STORE option: plain usage} {
  360. r geosearchstore points2{t} points{t} fromlonlat 13.361389 38.115556 byradius 500 km
  361. assert_equal [r zrange points{t} 0 -1] [r zrange points2{t} 0 -1]
  362. }
  363. test {GEORANGE STOREDIST option: plain usage} {
  364. r del points{t}
  365. r geoadd points{t} 13.361389 38.115556 "Palermo" \
  366. 15.087269 37.502669 "Catania"
  367. r georadius points{t} 13.361389 38.115556 500 km storedist points2{t}
  368. set res [r zrange points2{t} 0 -1 withscores]
  369. assert {[lindex $res 1] < 1}
  370. assert {[lindex $res 3] > 166}
  371. assert {[lindex $res 3] < 167}
  372. }
  373. test {GEOSEARCHSTORE STOREDIST option: plain usage} {
  374. r geosearchstore points2{t} points{t} fromlonlat 13.361389 38.115556 byradius 500 km storedist
  375. set res [r zrange points2{t} 0 -1 withscores]
  376. assert {[lindex $res 1] < 1}
  377. assert {[lindex $res 3] > 166}
  378. assert {[lindex $res 3] < 167}
  379. }
  380. test {GEORANGE STOREDIST option: COUNT ASC and DESC} {
  381. r del points{t}
  382. r geoadd points{t} 13.361389 38.115556 "Palermo" \
  383. 15.087269 37.502669 "Catania"
  384. r georadius points{t} 13.361389 38.115556 500 km storedist points2{t} asc count 1
  385. assert {[r zcard points2{t}] == 1}
  386. set res [r zrange points2{t} 0 -1 withscores]
  387. assert {[lindex $res 0] eq "Palermo"}
  388. r georadius points{t} 13.361389 38.115556 500 km storedist points2{t} desc count 1
  389. assert {[r zcard points2{t}] == 1}
  390. set res [r zrange points2{t} 0 -1 withscores]
  391. assert {[lindex $res 0] eq "Catania"}
  392. }
  393. test {GEOSEARCH the box spans -180° or 180°} {
  394. r del points
  395. r geoadd points 179.5 36 point1
  396. r geoadd points -179.5 36 point2
  397. assert_equal {point1 point2} [r geosearch points fromlonlat 179 37 bybox 400 400 km asc]
  398. assert_equal {point2 point1} [r geosearch points fromlonlat -179 37 bybox 400 400 km asc]
  399. }
  400. foreach {type} {byradius bybox} {
  401. test "GEOSEARCH fuzzy test - $type" {
  402. if {$::accurate} { set attempt 300 } else { set attempt 30 }
  403. while {[incr attempt -1]} {
  404. set rv [lindex $regression_vectors $rv_idx]
  405. incr rv_idx
  406. set radius_km 0; set width_km 0; set height_km 0
  407. unset -nocomplain debuginfo
  408. set srand_seed [clock milliseconds]
  409. if {$rv ne {}} {set srand_seed [lindex $rv 0]}
  410. lappend debuginfo "srand_seed is $srand_seed"
  411. expr {srand($srand_seed)} ; # If you need a reproducible run
  412. r del mypoints
  413. if {[randomInt 10] == 0} {
  414. # From time to time use very big radiuses
  415. if {$type == "byradius"} {
  416. set radius_km [expr {[randomInt 5000]+10}]
  417. } elseif {$type == "bybox"} {
  418. set width_km [expr {[randomInt 5000]+10}]
  419. set height_km [expr {[randomInt 5000]+10}]
  420. }
  421. } else {
  422. # Normally use a few - ~200km radiuses to stress
  423. # test the code the most in edge cases.
  424. if {$type == "byradius"} {
  425. set radius_km [expr {[randomInt 200]+10}]
  426. } elseif {$type == "bybox"} {
  427. set width_km [expr {[randomInt 200]+10}]
  428. set height_km [expr {[randomInt 200]+10}]
  429. }
  430. }
  431. if {$rv ne {}} {
  432. set radius_km [lindex $rv 1]
  433. set width_km [lindex $rv 1]
  434. set height_km [lindex $rv 1]
  435. }
  436. geo_random_point search_lon search_lat
  437. if {$rv ne {}} {
  438. set search_lon [lindex $rv 2]
  439. set search_lat [lindex $rv 3]
  440. }
  441. lappend debuginfo "Search area: $search_lon,$search_lat $radius_km $width_km $height_km km"
  442. set tcl_result {}
  443. set argv {}
  444. for {set j 0} {$j < 20000} {incr j} {
  445. geo_random_point lon lat
  446. lappend argv $lon $lat "place:$j"
  447. if {$type == "byradius"} {
  448. if {[pointInCircle $radius_km $lon $lat $search_lon $search_lat]} {
  449. lappend tcl_result "place:$j"
  450. }
  451. } elseif {$type == "bybox"} {
  452. if {[pointInRectangle $width_km $height_km $lon $lat $search_lon $search_lat 1]} {
  453. lappend tcl_result "place:$j"
  454. }
  455. }
  456. lappend debuginfo "place:$j $lon $lat"
  457. }
  458. r geoadd mypoints {*}$argv
  459. if {$type == "byradius"} {
  460. set res [lsort [r geosearch mypoints fromlonlat $search_lon $search_lat byradius $radius_km km]]
  461. } elseif {$type == "bybox"} {
  462. set res [lsort [r geosearch mypoints fromlonlat $search_lon $search_lat bybox $width_km $height_km km]]
  463. }
  464. set res2 [lsort $tcl_result]
  465. set test_result OK
  466. if {$res != $res2} {
  467. set rounding_errors 0
  468. set diff [compare_lists $res $res2]
  469. foreach place $diff {
  470. lassign [lindex [r geopos mypoints $place] 0] lon lat
  471. set mydist [geo_distance $lon $lat $search_lon $search_lat]
  472. set mydist [expr $mydist/1000]
  473. if {$type == "byradius"} {
  474. if {($mydist / $radius_km) > 0.999} {
  475. incr rounding_errors
  476. continue
  477. }
  478. if {$mydist < [expr {$radius_km*1000}]} {
  479. # This is a false positive for redis since given the
  480. # same points the higher precision calculation provided
  481. # by TCL shows the point within range
  482. incr rounding_errors
  483. continue
  484. }
  485. } elseif {$type == "bybox"} {
  486. # we add 0.1% error for floating point calculation error
  487. if {[pointInRectangle $width_km $height_km $lon $lat $search_lon $search_lat 1.001]} {
  488. incr rounding_errors
  489. continue
  490. }
  491. }
  492. }
  493. # Make sure this is a real error and not a rounidng issue.
  494. if {[llength $diff] == $rounding_errors} {
  495. set res $res2; # Error silenced
  496. }
  497. }
  498. if {$res != $res2} {
  499. set diff [compare_lists $res $res2]
  500. puts "*** Possible problem in GEO radius query ***"
  501. puts "Redis: $res"
  502. puts "Tcl : $res2"
  503. puts "Diff : $diff"
  504. puts [join $debuginfo "\n"]
  505. foreach place $diff {
  506. if {[lsearch -exact $res2 $place] != -1} {
  507. set where "(only in Tcl)"
  508. } else {
  509. set where "(only in Redis)"
  510. }
  511. lassign [lindex [r geopos mypoints $place] 0] lon lat
  512. set mydist [geo_distance $lon $lat $search_lon $search_lat]
  513. set mydist [expr $mydist/1000]
  514. puts "$place -> [r geopos mypoints $place] $mydist $where"
  515. }
  516. set test_result FAIL
  517. }
  518. unset -nocomplain debuginfo
  519. if {$test_result ne {OK}} break
  520. }
  521. set test_result
  522. } {OK}
  523. }
  524. test {GEOSEARCH box edges fuzzy test} {
  525. if {$::accurate} { set attempt 300 } else { set attempt 30 }
  526. while {[incr attempt -1]} {
  527. unset -nocomplain debuginfo
  528. set srand_seed [clock milliseconds]
  529. lappend debuginfo "srand_seed is $srand_seed"
  530. expr {srand($srand_seed)} ; # If you need a reproducible run
  531. r del mypoints
  532. geo_random_point search_lon search_lat
  533. set width_m [expr {[randomInt 10000]+10}]
  534. set height_m [expr {[randomInt 10000]+10}]
  535. set lat_delta [geo_raddeg [expr {$height_m/2/6372797.560856}]]
  536. set long_delta_top [geo_raddeg [expr {$width_m/2/6372797.560856/cos([geo_degrad [expr {$search_lat+$lat_delta}]])}]]
  537. set long_delta_middle [geo_raddeg [expr {$width_m/2/6372797.560856/cos([geo_degrad $search_lat])}]]
  538. set long_delta_bottom [geo_raddeg [expr {$width_m/2/6372797.560856/cos([geo_degrad [expr {$search_lat-$lat_delta}]])}]]
  539. # Total of 8 points are generated, which are located at each vertex and the center of each side
  540. set points(north) [list $search_lon [expr {$search_lat+$lat_delta}]]
  541. set points(south) [list $search_lon [expr {$search_lat-$lat_delta}]]
  542. set points(east) [list [expr {$search_lon+$long_delta_middle}] $search_lat]
  543. set points(west) [list [expr {$search_lon-$long_delta_middle}] $search_lat]
  544. set points(north_east) [list [expr {$search_lon+$long_delta_top}] [expr {$search_lat+$lat_delta}]]
  545. set points(north_west) [list [expr {$search_lon-$long_delta_top}] [expr {$search_lat+$lat_delta}]]
  546. set points(south_east) [list [expr {$search_lon+$long_delta_bottom}] [expr {$search_lat-$lat_delta}]]
  547. set points(south_west) [list [expr {$search_lon-$long_delta_bottom}] [expr {$search_lat-$lat_delta}]]
  548. lappend debuginfo "Search area: geosearch mypoints fromlonlat $search_lon $search_lat bybox $width_m $height_m m"
  549. set tcl_result {}
  550. foreach name [array names points] {
  551. set x [lindex $points($name) 0]
  552. set y [lindex $points($name) 1]
  553. # If longitude crosses -180° or 180°, we need to convert it.
  554. # latitude doesn't have this problem, because it's scope is -70~70, see geo_random_point
  555. if {$x > 180} {
  556. set x [expr {$x-360}]
  557. } elseif {$x < -180} {
  558. set x [expr {$x+360}]
  559. }
  560. r geoadd mypoints $x $y place:$name
  561. lappend tcl_result "place:$name"
  562. lappend debuginfo "geoadd mypoints $x $y place:$name"
  563. }
  564. set res2 [lsort $tcl_result]
  565. # make the box larger by two meter in each direction to put the coordinate slightly inside the box.
  566. set height_new [expr {$height_m+4}]
  567. set width_new [expr {$width_m+4}]
  568. set res [lsort [r geosearch mypoints fromlonlat $search_lon $search_lat bybox $width_new $height_new m]]
  569. if {$res != $res2} {
  570. set diff [compare_lists $res $res2]
  571. lappend debuginfo "res: $res, res2: $res2, diff: $diff"
  572. fail "place should be found, debuginfo: $debuginfo, height_new: $height_new width_new: $width_new"
  573. }
  574. # The width decreases and the height increases. Only north and south are found
  575. set width_new [expr {$width_m-4}]
  576. set height_new [expr {$height_m+4}]
  577. set res [lsort [r geosearch mypoints fromlonlat $search_lon $search_lat bybox $width_new $height_new m]]
  578. if {$res != {place:north place:south}} {
  579. lappend debuginfo "res: $res"
  580. fail "place should not be found, debuginfo: $debuginfo, height_new: $height_new width_new: $width_new"
  581. }
  582. # The width increases and the height decreases. Only ease and west are found
  583. set width_new [expr {$width_m+4}]
  584. set height_new [expr {$height_m-4}]
  585. set res [lsort [r geosearch mypoints fromlonlat $search_lon $search_lat bybox $width_new $height_new m]]
  586. if {$res != {place:east place:west}} {
  587. lappend debuginfo "res: $res"
  588. fail "place should not be found, debuginfo: $debuginfo, height_new: $height_new width_new: $width_new"
  589. }
  590. # make the box smaller by two meter in each direction to put the coordinate slightly outside the box.
  591. set height_new [expr {$height_m-4}]
  592. set width_new [expr {$width_m-4}]
  593. set res [r geosearch mypoints fromlonlat $search_lon $search_lat bybox $width_new $height_new m]
  594. if {$res != ""} {
  595. lappend debuginfo "res: $res"
  596. fail "place should not be found, debuginfo: $debuginfo, height_new: $height_new width_new: $width_new"
  597. }
  598. unset -nocomplain debuginfo
  599. }
  600. }
  601. }