For Geohash analysis, please refer to here:
Http://www.open-open.com/lib/view/open1417940079964.html
Java implementation Geohash, the code is commented.
ImportJava.util.BitSet;ImportJava.util.HashMap;/*** Geographical Knowledge Supplement: Latitude (latitude) [ -90, 90],longitude (longitude) [ -180,] * *@authorFengkang 2014-10-03 **/ Public classGeohash {Private Static intNumbits = 6 * 5;//maximum number of partitions, also the maximum length of the generated bits Final Static Char[] digits = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' H ', ' J ', ' K ', ' m ', ' n ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z ' }; //decode for decoding purposes Final StaticHashmap<character, integer> lookup =NewHashmap<>(); Static { inti = 0; for(Charch:digits) {lookup.put (ch, i++); } } /*** The number of LAT coding intervals in the [floor, Ceil] interval is numbits times * *@paramlat *-to be coded by/latitude *@paramFloor *@paramCeil *@returnBinary encoding*/ PrivateBitSet GetBits (DoubleLatDoubleFloorDoubleceil) {BitSet buffer=NewBitSet (numbits); for(inti = 0; i < numbits; ++i) {DoubleMid = (floor + ceil)/2; if(Lat >=mid) {buffer.set (i); Floor=mid; } Else{ceil=mid; } } returnbuffer; } /*** bits in long type I, each 5 bits mapped to a digits[] in the code * here is the implementation of reference to the implementation of ToString in Long.java * *@paramI *@return */ Private StaticString Base32 (Longi) {Char[] buf =New Char[65];//Buf[0] Standby, if negative, store '-' intCharpos = 64; BooleanNegative = (I < 0); if(!negative) {i= -i; } while(I <-32) {Buf[charpos--] = digits[(int) (-(I% 32))]; I/= 32; } Buf[charpos]= digits[(int) (-i)]; if(negative) {buf[--charpos] = '-'; } return NewString (buf, Charpos, (65-charpos)); } PublicString Encode (DoubleLatDoubleLon) {BitSet latbits= GetBits (lat,-90, 90); BitSet lonbits= GetBits (lon,-180, 180); /*! Longitude and latitude staggered combination coding*/StringBuilder Buffer=NewStringBuilder (); for(inti = 0; i < numbits; ++i) {buffer.append (Lonbits.get (i) )? ' 1 ': ' 0 '); Buffer.append ((Latbits.get (i))? ' 1 ': ' 0 '); } System.out.println (Buffer.tostring ()); //converts a 2 binary string to a 10 binary long type returnBase32 (Long.parselong (Buffer.tostring (), 2)); } /*** Division [Floor, Ceil] is divided according to BS * 0 indicates the interval [floor, (floor + ceil)/2] * 1 is located in the interval [(Floor + ceil)/2, Ceil] * * @paramBS * Binary sequence *@paramFloor * Initial interval boundary [floor, Ceil] *@paramCeil *@returninterval boundary, closest to the interval boundary of a BS sequence*/ Private DoubleDecode (BitSet BS,DoubleFloorDoubleceil) { DoubleMID = 0; for(inti = 0; I < bs.length (); ++i) {mid= (floor + ceil)/2; if(Bs.get (i)) { floor= Mid;//Right Interval}Else{ceil= Mid;//left Interval } } returnmid; } Public Double[] Decode (String geohash) {StringBuilder buffer=NewStringBuilder (); for(CharCh:geohash.toCharArray ()) { inti = Lookup.get (ch) + 32;//guarantees that the converted binary string is 5 bits//+32 Convert to 1#####, then intercept from subscript 1, i.e. #####Buffer.append (Integer.tostring (i, 2). SUBSTRING (1)); } //System.out.println (buffer);BitSet Lonset=NewBitSet (); BitSet Latset=NewBitSet (); //even bits intj = 0; for(inti = 0; I < numbits * 2; i + = 2) { BooleanIsSet =false; if(I <buffer.length ()) {IsSet= (Buffer.charat (i) = = ' 1 '); } lonset.set (J++, IsSet); } //Odd Bitsj = 0; for(inti = 1; I < numbits * 2; i + = 2) { BooleanIsSet =false; if(I <buffer.length ()) {IsSet= (Buffer.charat (i) = = ' 1 '); } latset.set (J++, IsSet); } DoubleLon = Decode (Lonset,-180, 180); DoubleLat = decode (latset,-90, 90); return New Double[] {lat, lon}; } Public Static voidMain (string[] args)throwsException {geohash Geohash=NewGeohash (); String encodes= Geohash.encode (45, 125); SYSTEM.OUT.PRINTLN (encodes); Double[] decodes =Geohash.decode (encodes); System.out.println (decodes[0]); System.out.println (decodes[1]); /*** Output Results 111100101000000010101000000010101000000010101000000010101000 * yb0bh2n0p058 45.0 124.999999925 4942*/ }}
Note: The code is not written by me, I just add some comments!
Geohash Parsing and Java implementation