Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert lat long to X Y coordinates C++ [duplicate]

Tags:

c++

I have a vector full of Lat Long coordinates in this format

82.0000000, -180.0000000

I am trying to convert them into an X and a Y coordinate to plot them to a map using this code, which as far as i can see is right...

X:

double testClass::getX(double lon)
{
    // Convert long to X coordinate (2043 = map width)
    double x =  lon;
    // Scale
    x =         x * 2043.0 / 360.0;
    // Center
    x +=        2043.0/2.0;
    return x;
}

Y:

double testClass::getY(double lat)
{
    // Convert lat to Y coordinate (1730 = map height)
    double y =  -1 * lat;
    // Scale
    y =         y * 1730.0 / 180.0;
    // Center
    y +=        1730.0/2.0;
    return y;
}

However when plotted on my map i can see the points do resemble a world map but they are all off by x amount and i think its something to do with my scaling

any ideas?

like image 302
AngryDuck Avatar asked Apr 18 '13 10:04

AngryDuck


1 Answers

Ok i found the answer to this

double testClass::getX(double lon, int width)
{
    // width is map width
    double x = fmod((width*(180+lon)/360), (width +(width/2)));

    return x;
}

double testClass::getY(double lat, int height, int width)
{
    // height and width are map height and width
    double PI = 3.14159265359;
    double latRad = lat*PI/180;

    // get y value
    double mercN = log(tan((PI/4)+(latRad/2)));
    double y     = (height/2)-(width*mercN/(2*PI));
    return y;
}

so yea this works perfectly when using a mercator map

like image 51
AngryDuck Avatar answered Sep 22 '22 12:09

AngryDuck