prismspecs
prismspecs

Reputation: 1489

Get latitude and longitude from X and Y coordinates

It seems that there is a wealth of knowledge on converting from Longitude and Latitude to X and Y coordinates, but not the reverse.

Here is a function of mine based on Kavrayskiy's math

  float xp = kavraX(radians(pv.x), radians(pv.y))*FACTOR;
  float yp = kavraY(radians(pv.x), radians(pv.y))*FACTOR;

// mapping -- this gives you screen X and Y coords from LAT and LONG
float kavraX (float latitude, float longitude) // Kavra for Kavrayskiy 
// formula from http://en.wikipedia.org/wiki/Kavrayskiy_VII_projection
{
  return ((3 * longitude) / TWO_PI)*sqrt(pow(PI, 2)/3 - pow(latitude, 2));
} 

float kavraY (float latitude, float longitude) 
{
  return latitude*-1;
} 

pv.x in this case could simply be 34 (for LA) ad pv.y would be -118 in that case. I'm having a hard time turning the equation around, though. Any ideas?

Upvotes: 1

Views: 2598

Answers (1)

prismspecs
prismspecs

Reputation: 1489

Ok based on the math @ Wikipedia, I managed to reverse the equation

  // find latitude from Y coord
    // height / 2 to make middle of map ZERO, *-1 to flip it, so south of equator is negative.
    // divide by FACTOR to make it fit within bounds of larger map
  float reMapY = ((mouseY - (height/2))*-1)/FACTOR;
  println(degrees(reMapY));

  // I have no idea what I'm doing
  float temp = sqrt((pow(PI,2)/3 - pow(reMapY,2)));
  float reMapX = (mouseX - (width/2))/FACTOR;
  float temp2 = ((reMapX / temp) * TWO_PI) / 3;
  println(degrees(temp2));

Just remember that FACTOR is something inherent in my design, because of the size of my map. I believe it should be width = 5.47 * FACTOR.

Upvotes: 2

Related Questions