经纬度转凯立德 K 码
扫描二维码
随时随地手机看文章
由于GPS信号位置均是经纬度球面坐标,国家从安全角度考虑不允许直接引用,因此凯立德为了方便导航者易于导航,就把经纬度坐标网格化,每一个网格单位代表纵横向0.1秒。
K码的具体格式如下:
1. K码是9位码;
2. K码将地图分成了四块进行编码,中心点在内蒙的阿拉善左旗境内;经纬度是东经105度0分0秒;北纬40度0分0秒
3. 以该点为中心分别在东西方向和南北方向画一条线当横纵(XY)坐标轴,那么第一象限(即东北方向的那块)的K码的第1位是5,第2、3、4象限的K码的第一位分别是6、7、8。
4. K码的第2-5位表示东西方向上的坐标,第6-9位代表南北方向上的坐标。
5. K码是一个凯立德特有的34进制数,(在KLD K码输入界面有说明:26个字母加10个阿拉伯数字,再去掉不用的L和O共34个字符),这个34进制数从左向右从低位向高位排列
6. 中心点的K码有4个,分别是500000000、6uy1y0000、7uy1yuy1y、80000uy1y。分别代表原点或中心点四个象限的K码。及X、Y轴正方向起始坐标0秒或代码为0000,X、Y轴负方向最大坐标为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 功能相同,采用对照表的方式实现 BOOL KCodeToZeroPoint(double dLat,double dLon,TCHAR *pKCode,int iKCodeSize); /* 调用示例: 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); // 坐标轴上的点测试 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; }