如何在Mercator地图(JPEG)上从x,y获取经度和纬度? [英] How can I get latitude, longitude from x, y on a Mercator map (JPEG)?
问题描述
我有一个Mercator投影图作为JPEG,我想知道如何将给定的x,y坐标与其纬度和经度相关联.我看过Gudermannian函数,但老实说,我不明白如何使用该函数并将其应用.即,它期望什么输入?我发现(JavaScript)的实现似乎介于-PI和PI之间,但是像素的y值与该范围之间有什么关系?
I have a Mercator projection map as a JPEG and I would like to know how to relate a given x, y coordinate to its latitude and longitude. I've looked at the Gudermannian function but I honestly don't understand how to take that function and apply it. Namely, what input is it expecting? The implementation I found (JavaScript) seems to take a range between -PI and PI, but what's the correlation between my y-value in pixels and that range?
此外,我发现此函数需要纬度并返回Google Maps的图块,该图块也使用Mercator.看来,如果我知道如何逆转此函数,我将很接近得到答案的答案.
Also, I found this function which takes a latitude and returns the tile for Google Maps, which also uses Mercator. It would seem that if I knew how to inverse this function, I'd be pretty close to having my answer.
/*<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);
}
推荐答案
以下是适合您的代码...如果您需要更多说明,请告诉我.
Here is some code for you... Let me know if you need more explanation.
/// <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;
}
这篇关于如何在Mercator地图(JPEG)上从x,y获取经度和纬度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!