Skip to content

Commit 013a6b9

Browse files
authored
Add Geohashing (#486)
1 parent 56ca719 commit 013a6b9

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using Algorithms.Other;
2+
using NUnit.Framework;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Algorithms.Tests.Other
10+
{
11+
[TestFixture]
12+
public class GeohashTests
13+
{
14+
[Test]
15+
public void Encode_ShouldReturnCorrectGeohash_ForHoChiMinhCity()
16+
{
17+
double latitude = 10.8231;
18+
double longitude = 106.6297;
19+
string result = Geohash.Encode(latitude, longitude);
20+
Assert.That(result, Is.EqualTo("w3gvd6m3hh54"));
21+
}
22+
23+
[Test]
24+
public void Encode_ShouldReturnCorrectGeohash_ForHanoi()
25+
{
26+
double latitude = 21.0285;
27+
double longitude = 105.8542;
28+
string result = Geohash.Encode(latitude, longitude);
29+
Assert.That(result, Is.EqualTo("w7er8u0evss2"));
30+
}
31+
32+
[Test]
33+
public void Encode_ShouldReturnCorrectGeohash_ForDaNang()
34+
{
35+
double latitude = 16.0544;
36+
double longitude = 108.2022;
37+
string result = Geohash.Encode(latitude, longitude);
38+
Assert.That(result, Is.EqualTo("w6ugq4w7wj04"));
39+
}
40+
41+
[Test]
42+
public void Encode_ShouldReturnCorrectGeohash_ForNhaTrang()
43+
{
44+
double latitude = 12.2388;
45+
double longitude = 109.1967;
46+
string result = Geohash.Encode(latitude, longitude);
47+
Assert.That(result, Is.EqualTo("w6jtsu485t8v"));
48+
}
49+
50+
[Test]
51+
public void Encode_ShouldReturnCorrectGeohash_ForVungTau()
52+
{
53+
double latitude = 10.3460;
54+
double longitude = 107.0843;
55+
string result = Geohash.Encode(latitude, longitude);
56+
Assert.That(result, Is.EqualTo("w3u4ug2mv41m"));
57+
}
58+
}
59+
}

Algorithms/Other/Geohash.cs

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ find more than one implementation for the same objective but using different alg
222222
* [Welford's Variance](./Algorithms/Other/WelfordsVariance.cs)
223223
* [Julian Easter](./Algorithms/Other/JulianEaster.cs)
224224
* [Pollard's Rho](./Algorithms/Other/PollardsRhoFactorizing.cs)
225+
* [GeoLocation Hash](./Algorithms/Other/Geohash.cs)
225226
* [Problems](./Algorithms/Problems)
226227
* [Stable Marriage](./Algorithms/Problems/StableMarriage)
227228
* [Gale-Shapley](./Algorithms/Problems/StableMarriage/GaleShapley.cs)

0 commit comments

Comments
 (0)