这篇文章主要提供GeoHash算法的C#实现,并提供源代码。
首先,GeoHash算法的原理我觉得大家必须要清除,可以参考这篇文章:GeoHash算法原理。
弄清楚原理之后,接下来就是实现部分,本文采用C#语言。在实现之前,我想先说一下整体思路:
1.采用静态类的方式,以达到密封,不可实例化的目的;
2.提供三个公有方法:编码,解码,计算距离
代码部分参考Java的GeoHash实现,并加上了一些自己的东西,这里贴上Java实现的地址:Java实现GeoHash。
C#代码如下:
using System;
using System.Linq;
using System.Text;
namespace XXX
{
/// <summary>
/// 空间索引编码辅助类
/// </summary>
public static class SpatialIndexCodeHelp
{
//编码字符
private static char[] CHARS = {'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'};
//地球半径常量
private const double EARTH_RADIUS = 6378137;
//二进制长度常量,20对应的编码长度为8,定位精确到米级
private const int Binary_Length = 20;
#region 私有方法
/// <summary>
/// 经纬度转换成二进制编码
/// </summary>
/// <param name="value">经度度</param>
/// <param name="min">经纬度最小值</param>
/// <param name="max">经纬度最大值</param>
/// <param name="length">二进制编码长度</param>
/// <returns></returns>
private static bool[] getHashArray(double value, double min, double max, int length)
{
if (value < min || value > max)
{
return null;
}
if (length < 1)
{
return null;
}
bool[] result = new bool[length];
for (int i = 0; i < length; i++)
{
double mid = (min + max) / 2;
if (value > mid)
{
result[i] = true;
min = mid;
}
else
{
result[i] = false;
max = mid;
}
}
return result;
}
/// <summary>
/// 合并经度和纬度的二进制编码
/// </summary>
/// <param name="latArray">纬度二进制编码</param>
/// <param name="lngArray">经度二进制编码</param>
/// <returns></returns>
private static bool[] getMergeArray(bool[] latArray, bool[] lngArray)
{
if (latArray == null || lngArray == null)
{
return null;
}
bool[] result = new bool[lngArray.Length + latArray.Length];
for (int i = 0; i < lngArray.Length; i++)
{
result[2 * i] = lngArray[i];
}
for (int i = 0; i < latArray.Length; i++)
{
result[2 * i + 1] = latArray[i];
}
return result;
}
/// <summary>
/// 二进制节转字符
/// </summary>
/// <param name="base32">二进制节数组</param>
/// <returns></returns>
private static char getBase32Char(bool[] base32)
{
if (base32 == null || base32.Length != 5)
{
return ' ';
}
int num = 0;
foreach (bool item in base32)
{
num <<= 1;
if (item)
{
num += 1;
}
}
return CHARS[num % CHARS.Length];
}
/// <summary>
/// 经纬度转化成弧度
/// </summary>
/// <param name="d">经纬度</param>
/// <returns></returns>
private static double Rad(double d)
{
return (double)d * Math.PI / 180d;
}
/// <summary>
/// 解码细化区间
/// </summary>
/// <param name="interval">待细化的数组</param>
/// <param name="cd">字符下标</param>
/// <param name="mask">分隔索引</param>
private static void refineInterval(ref double[] interval, int cd, int mask)
{
if ((cd & mask) != 0)
{
interval[0] = (interval[0] + interval[1]) / 2;
}
else
{
interval[1] = (interval[0] + interval[1]) / 2;
}
}
#endregion
/// <summary>
/// 对经纬度编码
/// </summary>
/// <param name="lat">纬度</param>
/// <param name="lng">经度</param>
/// <returns></returns>
public static string EnCode(double lat, double lng)
{
bool[] latArray = getHashArray(lat, -90, 90, Binary_Length);
bool[] lngArray = getHashArray(lng, -180, 180, Binary_Length);
bool[] bools = getMergeArray(latArray, lngArray);
if (bools == null)
{
return null;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bools.Length; i = i + 5)
{
bool[] base32 = new bool[5];
for (int j = 0; j < 5; j++)
{
base32[j] = bools[i + j];
}
char cha = getBase32Char(base32);
if (' ' == cha)
{
return null;
}
sb.Append(cha);
}
return sb.ToString();
}
/// <summary>
/// 对索引编码解码
/// </summary>
/// <param name="geoCode">索引编码</param>
/// <returns></returns>
public static double[] DeCode(string geoCode)
{
bool even = true;
double[] lat = { -90.0, 90.0 };
double[] lon = { -180.0, 180.0 };
foreach (char c in geoCode)
{
int cd = CHARS.ToList().IndexOf(c);
for (int j = 0; j < 5; j++)
{
int mask = int.Parse(Math.Pow(2, 4 - j).ToString());
if (even)
{
refineInterval(ref lon, cd, mask);
}
else
{
refineInterval(ref lat, cd, mask);
}
even = !even;
}
}
return new[] { (lat[0] + lat[1]) / 2, (lon[0] + lon[1]) / 2 };
}
/// <summary>
/// 计算两点位置的距离,返回两点的距离,单位米
/// </summary>
/// <param name="lat1">第一点纬度</param>
/// <param name="lng1">第一点经度</param>
/// <param name="lat2">第二点纬度</param>
/// <param name="lng2">第二点经度</param>
/// <returns></returns>
public static double GetDistance(double lat1, double lng1, double lat2, double lng2)
{
double radLat1 = Rad(lat1);
double radLng1 = Rad(lng1);
double radLat2 = Rad(lat2);
double radLng2 = Rad(lng2);
double a = radLat1 - radLat2;
double b = radLng1 - radLng2;
double result = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2))) * EARTH_RADIUS;
return result;
}
}
}
自己修改命名空间就可以用了,很方便。
伸手党也可以直接下载这里的类文件:C#实现GeoHash类文件