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