ercercerc


I apologize if this question has been asked before - seems it should have - but I couldn't find an answer.

Is there a correct way to translate a pixel x,y coordinate on a given VE map tile to the corresponding latitude and longitude

This link gives the opposite process: http://msdn2.microsoft.com/en-us/library/bb259689.aspx

Enrique




Re: Converting from x,y to lat.lon

Derrick


Do you mean from the mapcontrol

http://msdn2.microsoft.com/en-us/library/bb429564.aspx






Re: Converting from x,y to lat.lon

ercercerc

Sorry, my question was not complete.

I am coding in C#, retrieving VE map tiles to display within my application. I got the process working with a lot of help from this article: http://msdn2.microsoft.com/en-us/library/bb259689.aspx.

The article also shows how to calculate x/y from a lat/lon, that works well.

I need to calculate the lat/lon from an x/y pair. I can simply reverse the equation/process above but I wanted to see if there was a more educated/sanctioned way to do this.

Enrique






Re: Converting from x,y to lat.lon

SoulSolutions

Yes, I just reversed it. Always good to test it in a simple console app to make sure you get it correct:

Code Snippet

using System;

namespace LatToYConverter

{

internal class Program

{

private static void Main(string[] args)

{

int y = LatitudeToYAtZoom(43.1, 19);

Console.WriteLine(YToLatitudeAtZoom(y, 19));

}

private const double earthRadius = 6378137; //The radius of the earth - should never change!

private const double earthCircum = earthRadius*2.0*Math.PI; //calulated circumference of the earth

private const double earthHalfCirc = earthCircum/2; //calulated half circumference of the earth

private const int pixelsPerTile = 256;

public static int LatitudeToYAtZoom(double lat, int zoom)

{

int y;

double arc = earthCircum/((1 << zoom)*pixelsPerTile);

double sinLat = Math.Sin(DegToRad(lat));

double metersY = earthRadius/2*Math.Log((1 + sinLat)/(1 - sinLat));

y = (int) Math.Round((earthHalfCirc - metersY)/arc);

return y;

}

public static double YToLatitudeAtZoom(int y, int zoom)

{

double arc = earthCircum/((1 << zoom)*pixelsPerTile);

double metersY = earthHalfCirc - (y*arc);

double a = Math.Exp(metersY*2/earthRadius);

double result = RadToDeg(Math.Asin((a - 1)/(a + 1)));

return result;

}

private static double DegToRad(double d)

{

return d*Math.PI/180.0;

}

public static double RadToDeg(double d)

{

return d/Math.PI*180.0;

}

}

}

John






Re: Converting from x,y to lat.lon

ercercerc

Thanks much, this works.