經(jīng)緯度轉(zhuǎn)凱立德 K 碼
由于GPS信號(hào)位置均是經(jīng)緯度球面坐標(biāo),國家從安全角度考慮不允許直接引用,因此凱立德為了方便導(dǎo)航者易于導(dǎo)航,就把經(jīng)緯度坐標(biāo)網(wǎng)格化,每一個(gè)網(wǎng)格單位代表縱橫向0.1秒。
K碼的具體格式如下:
1. K碼是9位碼;
2. K碼將地圖分成了四塊進(jìn)行編碼,中心點(diǎn)在內(nèi)蒙的阿拉善左旗境內(nèi);經(jīng)緯度是東經(jīng)105度0分0秒;北緯40度0分0秒
3. 以該點(diǎn)為中心分別在東西方向和南北方向畫一條線當(dāng)橫縱(XY)坐標(biāo)軸,那么第一象限(即東北方向的那塊)的K碼的第1位是5,第2、3、4象限的K碼的第一位分別是6、7、8。
4. K碼的第2-5位表示東西方向上的坐標(biāo),第6-9位代表南北方向上的坐標(biāo)。
5. K碼是一個(gè)凱立德特有的34進(jìn)制數(shù),(在KLD K碼輸入界面有說明:26個(gè)字母加10個(gè)阿拉伯?dāng)?shù)字,再去掉不用的L和O共34個(gè)字符),這個(gè)34進(jìn)制數(shù)從左向右從低位向高位排列
6. 中心點(diǎn)的K碼有4個(gè),分別是500000000、6uy1y0000、7uy1yuy1y、80000uy1y。分別代表原點(diǎn)或中心點(diǎn)四個(gè)象限的K碼。及X、Y軸正方向起始坐標(biāo)0秒或代碼為0000,X、Y軸負(fù)方向最大坐標(biāo)為1260000秒或代碼為uy1y
VC++的代碼如下:
KCode.h
#ifndef?_KLD_K_CODE_CALC_H_ #define?_KLD_K_CODE_CALC_H_ typedef?struct{ int?iIndex; TCHAR?cKCode; }MyKCode_T; #define?KLD_KCODE_SYSTEM 34 #define?KOCDE_MAX_LENGTH 4 #define?KCODE_CENTER_LAT (40.0) #define?KCODE_CENTER_LON (105.0) #define?KCODE_X_Y_MAX_NAGATIVE 1260000 BOOL?ConvertDuFenMiaoToDu(double?dLatLonDu,double?dLatLonFen,double?dLatLonMiao,double?&dLatLon); BOOL?ConvertTo34System(DWORD?dwNumber,TCHAR?*pKCode,BOOL?bNegative); BOOL?ConvertTo34System2(DWORD?dwNumber,TCHAR?*pKCode,BOOL?bNegative); //?與?ConvertTo34System?功能相同,采用對照表的方式實(shí)現(xiàn) BOOL?KCodeToZeroPoint(double?dLat,double?dLon,TCHAR?*pKCode,int?iKCodeSize); /* 調(diào)用示例: double?dLon?=?0.0; double?dLat?=?0.0; TCHAR?cTmpBuffer[2?*?KOCDE_MAX_LENGTH?+?2]; ZeroMemory(cTmpBuffer,sizeof(TCHAR)?*?(2?*?KOCDE_MAX_LENGTH?+?2)); ConvertDuFenMiaoToDu(101.0,7.0,26.33,dLon); ConvertDuFenMiaoToDu(29.0,39.0,51.76,dLat); //?ConvertDuFenMiaoToDu(40.0,0.0,0.0,dLat); //?坐標(biāo)軸上的點(diǎn)測試 KCodeToZeroPoint(dLat,dLon,cTmpBuffer,2?*?KOCDE_MAX_LENGTH?+?2); */ #endif
源代碼:
KCode.cpp
#include?"stdafx.h" #include?"KCode.h" MyKCode_T?gMyKCode[KLD_KCODE_SYSTEM]?=? { {0,?'0'}, {1,?'1'}, {2,?'2'}, {3,?'3'}, {4,?'4'}, {5,?'5'}, {6,?'6'}, {7,?'7'}, {8,?'8'}, {9,?'9'}, {10,'a'}, {11,'b'}, {12,'c'}, {13,'d'}, {14,'e'}, {15,'f'}, {16,'g'}, {17,'h'}, {18,'i'}, {19,'j'}, {20,'k'}, //?no?L {21,'m'}, {22,'n'}, //?no?character?O {23,'p'}, {24,'q'}, {25,'r'}, {26,'s'}, {27,'t'}, {28,'u'}, {29,'v'}, {30,'w'}, {31,'x'}, {32,'y'}, {33,'z'}, }; BOOL?ConvertDuFenMiaoToDu(double?dLatLonDu,double?dLatLonFen,double?dLatLonMiao,double?&dLatLon) { BOOL?bRet?=?TRUE; //?輸入有效性判斷 { } dLatLon?=?dLatLonDu?+?(dLatLonFen?+?(dLatLonMiao?/?60.0))?/?60.0; return?bRet; } BOOL?ConvertTo34System2(DWORD?dwNumber,TCHAR?*pKCode,BOOL?bNegative) { BOOL?bRet?=?TRUE; DWORD?dwDivideBy34?=?dwNumber; int?iModeOf34?=?0; int?iCount?=?0; int?i?=?0; //?dwDivideBy34?=?1260000; do { iModeOf34?=?dwDivideBy34?%?34; dwDivideBy34?=?dwDivideBy34?/?34; for(i?=?0;i?<?KLD_KCODE_SYSTEM;i++) { if(iModeOf34?==?gMyKCode[i].iIndex) { pKCode[iCount]?=?gMyKCode[i].cKCode; iCount++; if(1?==?iCount?&&?bNegative) { pKCode[0]--; } break; } } }while(dwDivideBy34?>?0); return?bRet; } BOOL?ConvertTo34System(DWORD?dwNumber,TCHAR?*pKCode,BOOL?bNegative) { BOOL?bRet?=?TRUE; DWORD?dwDivideBy34?=?dwNumber; int?iModeOf34?=?0; int?iCount?=?0; do { iModeOf34?=?dwDivideBy34?%?34; dwDivideBy34?=?dwDivideBy34?/?34; if(iModeOf34?=?10?&&?iModeOf34?=?21?&&?iModeOf34?=?23?&&?iModeOf34??0); return?bRet; } /* ?*?說明:?iKCodeSize?的大小包含最后的?NULL */ BOOL?KCodeToZeroPoint(double?dLat,double?dLon,TCHAR?*pKCode,int?iKCodeSize) { BOOL?bRet?=?TRUE; DWORD?dwTmpLat?=?0; DWORD?dwTmpLon?=?0; BOOL?bIsNegative?=?FALSE; TCHAR?pKCodeLat[KOCDE_MAX_LENGTH?+?1]; TCHAR?pKCodeLon[KOCDE_MAX_LENGTH?+?1]; //?輸入有效性判斷 { if(iKCodeSize?<?(2?*?KOCDE_MAX_LENGTH?+?1)) { return?bRet; } if(NULL?==?pKCode) { bRet?=?FALSE; return?bRet; } } ZeroMemory(pKCodeLat,sizeof(TCHAR)?*?(KOCDE_MAX_LENGTH?+?1)); ZeroMemory(pKCodeLon,sizeof(TCHAR)?*?(KOCDE_MAX_LENGTH?+?1)); if(NULL?!=?pKCodeLat) { double?dDiff?=?(dLat?-?KCODE_CENTER_LAT); if(dDiff?<?0) { bIsNegative?=?TRUE; dDiff?=?-dDiff; dwTmpLat?=?(DWORD)((dDiff?*?3600)?*?10); //?以?0.1?秒為單位 dwTmpLat?=?KCODE_X_Y_MAX_NAGATIVE?-?dwTmpLat; } else { bIsNegative?=?FALSE; dwTmpLat?=?(DWORD)((dDiff?*?3600)?*?10); //?以?0.1?秒為單位 } ConvertTo34System(dwTmpLat,pKCodeLat,bIsNegative); } if(NULL?!=?pKCodeLon) { double?dDiff?=?(dLon?-?KCODE_CENTER_LON); if(dDiff?<?0) { bIsNegative?=?TRUE; dDiff?=?-dDiff; dwTmpLon?=?(DWORD)((dDiff?*?3600)?*?10); //?以?0.1?秒為單位 dwTmpLon?=?KCODE_X_Y_MAX_NAGATIVE?-?dwTmpLon; } else { bIsNegative?=?FALSE; dwTmpLon?=?(DWORD)((dDiff?*?3600)?*?10); //?以?0.1?秒為單位 } ConvertTo34System(dwTmpLon,pKCodeLon,bIsNegative); } if(KCODE_CENTER_LAT?==?dLat?||?KCODE_CENTER_LON?==?dLon) { if(KCODE_CENTER_LAT?==?dLat?&&?KCODE_CENTER_LON?==?dLon) { pKCode[0]?=?'5'; } else?if(KCODE_CENTER_LAT?==?dLat) { if(dLon?>?KCODE_CENTER_LON) { pKCode[0]?=?'6'; } else { pKCode[0]?=?'8'; } } else?if(KCODE_CENTER_LON?==?dLon) { if(dLat?>?KCODE_CENTER_LAT) { pKCode[0]?=?'5'; } else { pKCode[0]?=?'7'; } } } else { if(dLat?>?KCODE_CENTER_LAT) { if(dLon?>?KCODE_CENTER_LON) { pKCode[0]?=?'5'; } else { pKCode[0]?=?'6'; } } else { if(dLon?>?KCODE_CENTER_LON) { pKCode[0]?=?'8'; } else { pKCode[0]?=?'7'; } } } wcscat_s(pKCode,iKCodeSize,pKCodeLon); wcscat_s(pKCode,iKCodeSize,pKCodeLat); printf("K?Code?is:?%srn",pKCode); return?bRet; }