|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace Algorithms.Other |
| 8 | +{ |
| 9 | + public static class Geohash |
| 10 | + { |
| 11 | + private const string Base32Characters = "0123456789bcdefghjkmnpqrstuvwxyz"; // Convert latitude and longitude coordinates into a concise string |
| 12 | + private const int GeohashLength = 12; // ± 1.86 cm |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Encodes the provided latitude and longitude coordinates into a Geohash string. |
| 16 | + /// Geohashing is a method to encode geographic coordinates (latitude, longitude). |
| 17 | + /// into a short string of letters and digits. Each character in the resulting Geohash . |
| 18 | + /// string adds more precision to the location. The longer the Geohash, the smaller the area. |
| 19 | + /// </summary> |
| 20 | + /// <param name="latitude">The latitude of the location to encode. It must be a value between -90 and 90.</param> |
| 21 | + /// <param name="longitude">The longitude of the location to encode. It must be a value between -180 and 180.</param> |
| 22 | + /// <returns> |
| 23 | + /// A Geohash string of length 12 representing the location with high precision. |
| 24 | + /// A longer Geohash provides higher precision in terms of geographic area. |
| 25 | + /// and a 12-character Geohash can be accurate down to around 1.86 cm. |
| 26 | + /// </returns> |
| 27 | + public static string Encode(double latitude, double longitude) |
| 28 | + { |
| 29 | + double[] latitudeRange = new[] { -90.0, 90.0 }; |
| 30 | + double[] longitudeRange = new[] { -180.0, 180.0 }; |
| 31 | + bool isEncodingLongitude = true; |
| 32 | + int currentBit = 0; |
| 33 | + int base32Index = 0; |
| 34 | + StringBuilder geohashResult = new StringBuilder(); |
| 35 | + |
| 36 | + while (geohashResult.Length < GeohashLength) |
| 37 | + { |
| 38 | + double midpoint; |
| 39 | + |
| 40 | + if (isEncodingLongitude) |
| 41 | + { |
| 42 | + midpoint = (longitudeRange[0] + longitudeRange[1]) / 2; |
| 43 | + if (longitude > midpoint) |
| 44 | + { |
| 45 | + base32Index |= 1 << (4 - currentBit); |
| 46 | + longitudeRange[0] = midpoint; |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + longitudeRange[1] = midpoint; |
| 51 | + } |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + midpoint = (latitudeRange[0] + latitudeRange[1]) / 2; |
| 56 | + if (latitude > midpoint) |
| 57 | + { |
| 58 | + base32Index |= 1 << (4 - currentBit); |
| 59 | + latitudeRange[0] = midpoint; |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + latitudeRange[1] = midpoint; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + isEncodingLongitude = !isEncodingLongitude; |
| 68 | + |
| 69 | + if (currentBit < 4) |
| 70 | + { |
| 71 | + currentBit++; |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + geohashResult.Append(Base32Characters[base32Index]); |
| 76 | + currentBit = 0; |
| 77 | + base32Index = 0; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return geohashResult.ToString(); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments