2015-06-23 09:02:37 -04:00
# Helper functins to simulate search-in-radius in the Tcl side in order to
# verify the Redis implementation with a fuzzy test.
proc geo_degrad deg { expr { $deg * atan ( 1 ) * 8 / 360 } }
2015-06-25 12:05:45 -04:00
proc geo_distance { lon1d lat1d lon2d lat2d} {
2015-06-23 09:02:37 -04:00
set lon1r [ geo_degrad $lon1d ]
2015-06-25 12:05:45 -04:00
set lat1r [ geo_degrad $lat1d ]
2015-06-23 09:02:37 -04:00
set lon2r [ geo_degrad $lon2d ]
2015-06-25 12:05:45 -04:00
set lat2r [ geo_degrad $lat2d ]
2015-06-23 09:02:37 -04:00
set v [ expr { sin ( ( $lon2r - $lon1r ) / 2 ) } ]
2015-06-25 12:05:45 -04:00
set u [ expr { sin ( ( $lat2r - $lat1r ) / 2 ) } ]
2015-06-23 09:02:37 -04:00
expr { 2.0 * 6372797.560856 * \
asin ( sqrt ( $u * $u + cos ( $lat1r ) * cos( $lat2r ) * $v * $v ) ) }
}
2015-06-25 12:05:45 -04:00
proc geo_random_point { lonvar latvar} {
2015-06-23 09:02:37 -04:00
upvar 1 $lonvar lon
2015-06-25 12:05:45 -04:00
upvar 1 $latvar lat
2015-06-23 09:02:37 -04:00
# Note that the actual latitude limit should be -85 to +85, we restrict
# the test to -70 to +70 since in this range the algorithm is more precise
# while outside this range occasionally some element may be missing.
set lon [ expr { -180 + rand( ) * 360 } ]
2015-06-25 12:05:45 -04:00
set lat [ expr { -70 + rand( ) * 140 } ]
2015-06-23 09:02:37 -04:00
}
2014-05-12 14:38:17 -04:00
start_server { tags { " g e o " } } {
test { GEOADD create} {
2015-06-25 12:05:45 -04:00
r geoadd nyc - 73.9454966 40.747533 " l i c m a r k e t "
2014-05-12 14:38:17 -04:00
} { 1 }
test { GEOADD update} {
2015-06-25 12:05:45 -04:00
r geoadd nyc - 73.9454966 40.747533 " l i c m a r k e t "
2014-05-12 14:38:17 -04:00
} { 0 }
2015-06-23 04:19:40 -04:00
test { GEOADD invalid coordinates} {
catch {
2015-06-25 12:05:45 -04:00
r geoadd nyc - 73.9454966 40.747533 " l i c m a r k e t " \
2015-06-23 04:19:40 -04:00
foo bar " l u c k m a r k e t "
} err
set err
} { * valid * }
2014-05-12 14:38:17 -04:00
test { GEOADD multi add} {
2015-06-25 12:05:45 -04:00
r geoadd nyc - 73.9733487 40.7648057 " c e n t r a l p a r k n / q / r " - 73.9903085 40.7362513 " u n i o n s q u a r e " - 74.0131604 40.7126674 " w t c o n e " - 73.7858139 40.6428986 " j f k " - 73.9375699 40.7498929 " q 4 " - 73.9564142 40.7480973 4545
2014-05-12 14:38:17 -04:00
} { 6 }
test { Check geoset values} {
r zrange nyc 0 - 1 withscores
} { { wtc one} 1791873972053020 { union square} 1791875485187452 { central park n/ q/ r} 1791875761332224 4545 1791875796750882 { lic market} 1791875804419201 q4 1791875830079666 jfk 1791895905559723 }
test { GEORADIUS simple ( sorted ) } {
2015-06-27 03:43:47 -04:00
r georadius nyc - 73.9798091 40.7598464 3 km asc
2014-05-12 14:38:17 -04:00
} { { central park n/ q/ r} 4545 { union square} }
2015-06-27 03:43:47 -04:00
test { GEORADIUS withdist ( sorted ) } {
r georadius nyc - 73.9798091 40.7598464 3 km withdist asc
2015-06-22 09:00:37 -04:00
} { { { central park n/ q/ r} 0.7750 } { 4545 2.3651 } { { union square} 2.7697 } }
2014-05-12 14:38:17 -04:00
2015-06-29 03:52:23 -04:00
test { GEORADIUS with COUNT} {
r georadius nyc - 73.9798091 40.7598464 10 km COUNT 3
} { { central park n/ q/ r} 4545 { union square} }
test { GEORADIUS with COUNT DESC} {
r georadius nyc - 73.9798091 40.7598464 10 km COUNT 2 DESC
} { { wtc one} q4 }
2014-05-12 14:38:17 -04:00
test { GEORADIUSBYMEMBER simple ( sorted ) } {
r georadiusbymember nyc " w t c o n e " 7 km
} { { wtc one} { union square} { central park n/ q/ r} 4545 { lic market} }
2015-06-27 03:43:47 -04:00
test { GEORADIUSBYMEMBER withdist ( sorted ) } {
2014-05-12 14:38:17 -04:00
r georadiusbymember nyc " w t c o n e " 7 km withdist
2015-06-22 09:00:37 -04:00
} { { { wtc one} 0.0000 } { { union square} 3.2544 } { { central park n/ q/ r} 6.7000 } { 4545 6.1975 } { { lic market} 6.8969 } }
2014-05-12 14:38:17 -04:00
test { GEOENCODE simple} {
2015-06-25 12:05:45 -04:00
r geoencode 1.8063239 41.2358883
} { 3471579339700058 { 1.8063229322433472 41.235888125243704 } \
{ 1.806328296661377 41.235890659964866 } \
2015-06-29 03:46:58 -04:00
{ 1.8063256144523621 41.235889392604285 } \
{ 3471579339700058 3471579339700059 } }
2014-05-12 14:38:17 -04:00
test { GEODECODE simple} {
r geodecode 3471579339700058
2015-06-25 12:05:45 -04:00
} { { 1.8063229322433472 41.235888125243704 } \
{ 1.806328296661377 41.235890659964866 } \
{ 1.8063256144523621 41.235889392604285 } }
2015-06-23 09:02:37 -04:00
2015-06-24 10:34:17 -04:00
test { GEOHASH is able to return geohash strings} {
# Example from Wikipedia.
r del points
2015-06-25 12:05:45 -04:00
r geoadd points - 5.6 42.6 test
2015-06-24 10:34:17 -04:00
lindex [ r geohash points test] 0
} { ezs42e44yx0 }
2015-06-29 04:47:07 -04:00
test { GEOPOS simple} {
r del points
r geoadd points 10 20 a 30 40 b
lassign [ lindex [ r geopos points a b] 0 ] x1 y1
lassign [ lindex [ r geopos points a b] 1 ] x2 y2
assert { abs ( $x1 - 10 ) < 0.001 }
assert { abs ( $y1 - 20 ) < 0.001 }
assert { abs ( $x2 - 30 ) < 0.001 }
assert { abs ( $y2 - 40 ) < 0.001 }
}
test { GEOPOS missing element} {
r del points
r geoadd points 10 20 a 30 40 b
lindex [ r geopos points a x b] 1
} { }
2015-06-29 06:44:31 -04:00
test { GEODIST simple & unit} {
r del points
r geoadd points 13.361389 38.115556 " P a l e r m o " \
15.087269 37.502669 " C a t a n i a "
set m [ r geodist points Palermo Catania]
assert { $m > 166274 && $m < 166275 }
set km [ r geodist points Palermo Catania km]
assert { $km > 166.2 && $km < 166.3 }
}
test { GEODIST missing elements} {
r del points
r geoadd points 13.361389 38.115556 " P a l e r m o " \
15.087269 37.502669 " C a t a n i a "
set m [ r geodist points Palermo Agrigento]
assert { $m eq { } }
set m [ r geodist points Ragusa Agrigento]
assert { $m eq { } }
set m [ r geodist empty_key Palermo Catania]
assert { $m eq { } }
}
2015-06-23 09:02:37 -04:00
test { GEOADD + GEORANGE randomized test} {
set attempt 10
while { [ incr attempt - 1 ] } {
unset - nocomplain debuginfo
set srand_seed [ randomInt 1000000 ]
lappend debuginfo " s r a n d _ s e e d i s $ s r a n d _ s e e d "
expr { srand ( $srand_seed ) } ; # If you need a reproducible run
r del mypoints
set radius_km [ expr { [ randomInt 200 ] + 10 } ]
set radius_m [ expr { $radius_km * 1000 } ]
2015-06-25 12:05:45 -04:00
geo_random_point search_lon search_lat
lappend debuginfo " S e a r c h a r e a : $ s e a r c h _ l o n , $ s e a r c h _ l a t $ r a d i u s _ k m k m "
2015-06-23 09:02:37 -04:00
set tcl_result { }
set argv { }
for { set j 0 } { $j < 20000 } { incr j} {
2015-06-25 12:05:45 -04:00
geo_random_point lon lat
lappend argv $lon $lat " p l a c e : $ j "
if { [ geo_distance $lon $lat $search_lon $search_lat ] < $radius_m } {
2015-06-23 09:02:37 -04:00
lappend tcl_result " p l a c e : $ j "
2015-06-25 12:05:45 -04:00
lappend debuginfo " p l a c e : $ j $ l o n $ l a t [ e x p r { [ g e o _ d i s t a n c e $ l o n $ l a t $ s e a r c h _ l o n $ s e a r c h _ l a t ] / 1 0 0 0 } ] k m "
2015-06-23 09:02:37 -04:00
}
}
r geoadd mypoints { * } $argv
2015-06-25 12:05:45 -04:00
set res [ lsort [ r georadius mypoints $search_lon $search_lat $radius_km km] ]
2015-06-23 09:02:37 -04:00
set res2 [ lsort $tcl_result ]
set test_result OK
if { $res != $res2 } {
puts " R e d i s : $ r e s "
puts " T c l : $ r e s 2 "
puts [ join $debuginfo " \n " ]
set test_result FAIL
}
unset - nocomplain debuginfo
if { $test_result ne { OK } } break
}
set test_result
} { OK }
2014-05-12 14:38:17 -04:00
}