MCPcopy Create free account
hub / github.com/F-Stack/f-stack / geohashCommand

Function geohashCommand

app/redis-6.2.6/src/geo.c:871–927  ·  view source on GitHub ↗

GEOHASH key ele1 ele2 ... eleN * * Returns an array with an 11 characters geohash representation of the * position of the specified elements. */

Source from the content-addressed store, hash-verified

869 * Returns an array with an 11 characters geohash representation of the
870 * position of the specified elements. */
871void geohashCommand(client *c) {
872 char *geoalphabet= "0123456789bcdefghjkmnpqrstuvwxyz";
873 int j;
874
875 /* Look up the requested zset */
876 robj *zobj = lookupKeyRead(c->db, c->argv[1]);
877 if (checkType(c, zobj, OBJ_ZSET)) return;
878
879 /* Geohash elements one after the other, using a null bulk reply for
880 * missing elements. */
881 addReplyArrayLen(c,c->argc-2);
882 for (j = 2; j < c->argc; j++) {
883 double score;
884 if (!zobj || zsetScore(zobj, c->argv[j]->ptr, &score) == C_ERR) {
885 addReplyNull(c);
886 } else {
887 /* The internal format we use for geocoding is a bit different
888 * than the standard, since we use as initial latitude range
889 * -85,85, while the normal geohashing algorithm uses -90,90.
890 * So we have to decode our position and re-encode using the
891 * standard ranges in order to output a valid geohash string. */
892
893 /* Decode... */
894 double xy[2];
895 if (!decodeGeohash(score,xy)) {
896 addReplyNull(c);
897 continue;
898 }
899
900 /* Re-encode */
901 GeoHashRange r[2];
902 GeoHashBits hash;
903 r[0].min = -180;
904 r[0].max = 180;
905 r[1].min = -90;
906 r[1].max = 90;
907 geohashEncode(&r[0],&r[1],xy[0],xy[1],26,&hash);
908
909 char buf[12];
910 int i;
911 for (i = 0; i < 11; i++) {
912 int idx;
913 if (i == 10) {
914 /* We have just 52 bits, but the API used to output
915 * an 11 bytes geohash. For compatibility we assume
916 * zero. */
917 idx = 0;
918 } else {
919 idx = (hash.bits >> (52-((i+1)*5))) & 0x1f;
920 }
921 buf[i] = geoalphabet[idx];
922 }
923 buf[11] = '\0';
924 addReplyBulkCBuffer(c,buf,11);
925 }
926 }
927}
928

Callers

nothing calls this directly

Calls 8

lookupKeyReadFunction · 0.85
checkTypeFunction · 0.85
addReplyArrayLenFunction · 0.85
zsetScoreFunction · 0.85
addReplyNullFunction · 0.85
decodeGeohashFunction · 0.85
geohashEncodeFunction · 0.85
addReplyBulkCBufferFunction · 0.85

Tested by

no test coverage detected