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 } }
proc geo_distance { lat1d lon1d lat2d lon2d} {
set lat1r [ geo_degrad $lat1d ]
set lon1r [ geo_degrad $lon1d ]
set lat2r [ geo_degrad $lat2d ]
set lon2r [ geo_degrad $lon2d ]
set u [ expr { sin ( ( $lat2r - $lat1r ) / 2 ) } ]
set v [ expr { sin ( ( $lon2r - $lon1r ) / 2 ) } ]
expr { 2.0 * 6372797.560856 * \
asin ( sqrt ( $u * $u + cos ( $lat1r ) * cos( $lat2r ) * $v * $v ) ) }
}
proc geo_random_point { latvar lonvar} {
upvar 1 $latvar lat
upvar 1 $lonvar lon
# 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 lat [ expr { -70 + rand( ) * 140 } ]
set lon [ expr { -180 + rand( ) * 360 } ]
}
2014-05-12 14:38:17 -04:00
start_server { tags { " g e o " } } {
test { GEOADD create} {
r geoadd nyc 40.747533 - 73.9454966 " l i c m a r k e t "
} { 1 }
test { GEOADD update} {
r geoadd nyc 40.747533 - 73.9454966 " l i c m a r k e t "
} { 0 }
2015-06-23 04:19:40 -04:00
test { GEOADD invalid coordinates} {
catch {
r geoadd nyc 40.747533 - 73.9454966 " l i c m a r k e t " \
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} {
r geoadd nyc 40.7648057 - 73.9733487 " c e n t r a l p a r k n / q / r " 40.7362513 - 73.9903085 " u n i o n s q u a r e " 40.7126674 - 74.0131604 " w t c o n e " 40.6428986 - 73.7858139 " j f k " 40.7498929 - 73.9375699 " q 4 " 40.7480973 - 73.9564142 4545
} { 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 ) } {
r georadius nyc 40.7598464 - 73.9798091 3 km ascending
} { { central park n/ q/ r} 4545 { union square} }
test { GEORADIUS withdistance ( sorted ) } {
r georadius nyc 40.7598464 - 73.9798091 3 km withdistance ascending
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
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} }
test { GEORADIUSBYMEMBER withdistance ( sorted ) } {
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} {
r geoencode 41.2358883 1.8063239
} { 3471579339700058 { 41.235888125243704 1.8063229322433472 } \
{ 41.235890659964866 1.806328296661377 } \
{ 41.235889392604285 1.8063256144523621 } }
test { GEODECODE simple} {
r geodecode 3471579339700058
} { { 41.235888125243704 1.8063229322433472 } \
{ 41.235890659964866 1.806328296661377 } \
{ 41.235889392604285 1.8063256144523621 } }
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 } ]
geo_random_point search_lat search_lon
lappend debuginfo " S e a r c h a r e a : $ s e a r c h _ l a t , $ s e a r c h _ l o n $ r a d i u s _ k m k m "
set tcl_result { }
set argv { }
for { set j 0 } { $j < 20000 } { incr j} {
geo_random_point lat lon
lappend argv $lat $lon " p l a c e : $ j "
if { [ geo_distance $lat $lon $search_lat $search_lon ] < $radius_m } {
lappend tcl_result " p l a c e : $ j "
lappend debuginfo " p l a c e : $ j $ l a t $ l o n [ e x p r { [ g e o _ d i s t a n c e $ l a t $ l o n $ s e a r c h _ l a t $ s e a r c h _ l o n ] / 1 0 0 0 } ] k m "
}
}
r geoadd mypoints { * } $argv
set res [ lsort [ r georadius mypoints $search_lat $search_lon $radius_km km] ]
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
}