Native/C

GPS 거리 계산

aucd29 2013. 10. 2. 18:53
[code]
// DATE        :
// CODER    : fuhaaa
// MODIFY : aucd29(ccd@apsat.co.kr)
// URL        : http://www.apsat.co.kr
//
// Copyright Asia pasific satellite industries Co., Ltd.
//

#define EARTH_R        6371 /* earth's mean radius in km */
#define PI            3.141592654f
#define PIRADIAN    (PI/180)

double GpsAtof(char* pData)
{
    char* pJump;
    double dSecond = 0.0, dFirst = 0.0;

    pJump = strstr(pData, ".");
    pJump -= 2;
    dSecond = atof(pJump) / 60;
    *pJump = 0x00;
    dFirst = atof(pData);

    return (dFirst + dSecond);
}

double PosConverter(char* pszLat1, char* pszLat2, char* pszLot1, char* pszLot2)
{
    /*
    Haversine formula:

    R = earth’s radius (mean radius = 6,371km)
    Δlat = lat2 - lat1
    Δlong = long2 - long1
    a     = sin²(Δlat/2) + cos(lat1).cos(lat2).sin²(Δlong/2)
    c     = 2 * atan2(√a, √(1-a))
    d     = R * c
    */

    // position1 = fLat1, fLong1
    // position2 = fLat2, fLong2

    // NMEA ddmm.mmmm or dddmm.mmmm
    // ex> 3734.1111 => dd.dddddd...... 37 + (34.1111 /60) = result => fLat1 or fLat2

    double a=0.0, c=0.0, d=0.0;
    double dLat = 0.0;
    double dLong = 0.0;
    double dLat1 = 0.0;
    double dLat2 = 0.0;
    double dLot1 = 0.0;
    double dLot2 = 0.0;

    dLat1 = GpsAtof(pszLat1);
    dLat2 = GpsAtof(pszLat2);
    dLot1 = GpsAtof(pszLot1);
    dLot2 = GpsAtof(pszLot2);

    dLat = (dLat1 - dLat2);
    dLong = (dLot1 - dLot2);

    dLat1 *= PIRADIAN;
    dLat2 *= PIRADIAN;

    dLat *= PIRADIAN;
    dLong *= PIRADIAN;

    a = (sin(dLat/2.0) * sin(dLat/2.0)) +
        (cos(dLat1) * cos(dLat2)) * (sin(dLong/2.0) * sin(dLong/2.0));

    c = 2 * atan2(sqrt(a), sqrt(1 - a));
    d = EARTH_R * c;

    return d;
}


int main(int argc, char* argv[])
{
    double x = 37.0 + (28.6316 / 60.0);
    double y = 126 + (52.8609 / 60.0);
    double x2 = x+1;
    double y2 = y;

    char xx[20] = {0, };
    char xy[20] = {0, };
    char yx[20] = {0, };
    char yy[20] = {0, };

    strcpy(xx, "3728.6316");
    strcpy(xy, "3724.6316");

    strcpy(yx, "12652.8609");
    strcpy(yy, "12652.8609");

    printf("res : %lf\n", PosConverter(xx, xy, yx, yy));

    return 0;
}
[/code]