Mat*_*ing 8 mapping math maps latitude-longitude
我有一个墨卡托投影图作为JPEG,我想知道如何将给定的x,y坐标与其纬度和经度相关联.我看过Gudermannian函数,但老实说我不明白如何使用该函数并应用它.即,它期待什么输入?我发现的实现(JavaScript)似乎在-PI和PI之间取得了一个范围,但是我的y值(以像素为单位)与该范围之间的相关性是什么?
此外,我发现这个功能需要一个纬度并返回谷歌地图的瓷砖,谷歌地图也使用墨卡托.似乎如果我知道如何反转这个功能,我会非常接近我的答案.
/*<summary>Get the vertical tile number from a latitude
using Mercator projection formula</summary>*/
private int getMercatorLatitude(double lati)
{
double maxlat = Math.PI;
double lat = lati;
if (lat > 90) lat = lat - 180;
if (lat < -90) lat = lat + 180;
// conversion degre=>radians
double phi = Math.PI * lat / 180;
double res;
//double temp = Math.Tan(Math.PI / 4 - phi / 2);
//res = Math.Log(temp);
res = 0.5 * Math.Log((1 + Math.Sin(phi)) / (1 - Math.Sin(phi)));
double maxTileY = Math.Pow(2, zoom);
int result = (int)(((1 - res / maxlat) / 2) * (maxTileY));
return (result);
}
Run Code Online (Sandbox Code Playgroud)
这里有一些代码...如果您需要更多解释,请告诉我.
/// <summary>
/// Calculates the Y-value (inverse Gudermannian function) for a latitude.
/// <para><see cref="http://en.wikipedia.org/wiki/Gudermannian_function"/></para>
/// </summary>
/// <param name="latitude">The latitude in degrees to use for calculating the Y-value.</param>
/// <returns>The Y-value for the given latitude.</returns>
public static double GudermannianInv(double latitude)
{
double sign = Math.Sign(latitude);
double sin = Math.Sin(latitude * RADIANS_PER_DEGREE * sign);
return sign * (Math.Log((1.0 + sin) / (1.0 - sin)) / 2.0);
}
/// <summary>
/// Returns the Latitude in degrees for a given Y.
/// </summary>
/// <param name="y">Y is in the range of +PI to -PI.</param>
/// <returns>Latitude in degrees.</returns>
public static double Gudermannian(double y)
{
return Math.Atan(Math.Sinh(y)) * DEGREES_PER_RADIAN;
}
Run Code Online (Sandbox Code Playgroud)