本文根据北斗格网编码标注编写的测试代码, 北斗国标描述网址 http://c.gb688.cn/bzgk/gb/showGb?type=online&hcno=77B7EA113926D3247F9688324D4A91C8
我将北斗编码整理成一张图, 如下(代码在文末附上):
#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <iosfwd>
#include <string>
#include <map>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <stdio.h>
#include <math.h>std::string GBBeiDouCode(int level, double x, double y, double z)
{//if(y >=88)std::vector<std::string> code;std::vector<std::string> hcode;LocType ltype = XYLocType(x, y);std::pair<int, int> sym = LocTypeSym(ltype);int pre_r, pre_c;double posLon = -180.00000000, posLat = 0.00000000;if (x >= 180.00000)x = x - D3600_2_8888 / 100;if (x <= -180.000000)x = x - D3600_2_8888 / -100;//行列号按照左闭右开,最后180按照闭算到59for (int i = 1; i <= level; i++){if (i == 1) {if (y >= 0)code.push_back("N");elsecode.push_back("S");int nr = floor((x - (posLon)) / 6.00000000); //0-59int nc = floor(fabs(y - posLat) / 4.00000000);//0-21std::string nrc;if (nr < 10){nrc = nrc.append("0");int tmpnr = nr == 0 ? 1 : nr;nrc = nrc.append(std::to_string(tmpnr));}else{nrc = std::to_string(nr > 59 ? 60 : nr + 1);}if (nc > 21)nc = 21;char nccc = (char)(nc + 65);char f[2] = { nccc,0 };code.emplace_back();code[code.size() - 1].append(nrc);code[code.size() - 1].append(f);code[code.size() - 1].resize(3);nr = nr > 59 ? 59 : nr;nc = nc > 21 ? 21 : nc;pre_r = nr;pre_c = nc;continue;}else if (i == 2){if (x > 0)posLon = posLon + (pre_r) * 6;else{/* if (x <= -180.00000000000)posLon = posLon + (pre_r + 1) * 6;else*/posLon = posLon + (pre_r + 1) * 6;}if (y > 0)posLat = posLat + (pre_c) * 4;elseposLat = posLat - (pre_c) * 4;int nc = (int)floor(fabs(y - posLat) / 0.5);//0-11int nr = (int)floor(fabs(x - posLon) / 0.5);//0-7nr = nr > 11 ? 11 : nr;nc = nc > 7 ? 7 : nc;char nrc = (char)(nr > 9 ? nr + 55 : nr + 48);char ncc = (char)(nc + 48);pre_r = nr;pre_c = nc;code.emplace_back();code[code.size() - 1].resize(2);code[code.size() - 1][0] = nrc;code[code.size() - 1][1] = ncc;continue;}else if (i == 3){if (x > 0)posLon = posLon + (pre_r) * 0.5;elseposLon = posLon - pre_r * 0.5;if (y > 0)posLat = posLat + (pre_c) * 0.5;elseposLat = posLat - (pre_c) * 0.5;int nc = (int)floor(fabs(y - posLat) / D60_10);//0-2 int nr = (int)floor(fabs(x - posLon) / D60_15); //0-1nr = nr > 1 ? 1 : nr;nc = nc > 2 ? 2 : nc;pre_r = nr;pre_c = nc;int ncc = nc * 2 + nr;char nccc = (char)(ncc + 48);code.emplace_back();code[code.size() - 1].resize(1);code[code.size() - 1][0] = nccc;continue;}else if (i == 4){if (x > 0)posLon = posLon + (pre_r)* D60_15;elseposLon = posLon - (pre_r)* D60_15;if (y > 0)posLat = posLat + (pre_c)* D60_10;elseposLat = posLat - (pre_c)* D60_10;int nc = (int)floor(fabs(y - posLat) / D60_1);int nr = (int)floor(fabs(x - posLon) / D60_1);nr = nr > 14 ? 14 : nr;nc = nc > 9 ? 9 : nc;pre_r = nr;pre_c = nc;char nrc = (char)(nr > 9 ? nr + 55 : nr + 48);char ncc = (char)(nc + 48);code.emplace_back();code[code.size() - 1].resize(2);code[code.size() - 1][0] = nrc;code[code.size() - 1][1] = ncc;continue;}else if (i == 5){if (x > 0)posLon = posLon + (pre_r) * (D60_1);elseposLon = posLon - (pre_r) * (D60_1);if (y > 0)posLat = posLat + (pre_c) * (D60_1);elseposLat = posLat - (pre_c) * (D60_1);int nc = (int)floor(fabs(y - posLat) / D3600_4);//0-14int nr = (int)floor(fabs(x - posLon) / D3600_4);//0-14nr = nr > 14 ? 14 : nr;nc = nc > 14 ? 14 : nc;pre_r = nr;pre_c = nc;char nrc = (char)(nr > 9 ? nr + 55 : nr + 48);char ncc = (char)(nc > 9 ? nc + 55 : nc + 48);code.emplace_back();code[code.size() - 1].resize(2);code[code.size() - 1][0] = nrc;code[code.size() - 1][1] = ncc;continue;}else if (i == 6){if (x > 0)posLon = posLon + (pre_r)* D3600_4;elseposLon = posLon - (pre_r)* D3600_4;if (y > 0)posLat = posLat + (pre_c)* D3600_4;elseposLat = posLat - (pre_c)* D3600_4;int nc = (int)floor(fabs(y - posLat) / D3600_2);//0-1int nr = (int)floor(fabs(x - posLon) / D3600_2);//0-1nr = nr > 1 ? 1 : nr;nc = nc > 1 ? 1 : nc;pre_r = nr;pre_c = nc;int ncc = nc * 2 + nr;char nccc = (char)(ncc + 48);code.emplace_back();code[code.size() - 1].resize(1);code[code.size() - 1][0] = nccc;continue;}else if (i >= 7 && i <= 10){int d = i - 7 + 1;double dd2 = D3600_2 / pow(8, d);double dd1 = D3600_2 / pow(8, d - 1);if (x > 0)posLon = posLon + (pre_r)* dd1;elseposLon = posLon - (pre_r)* dd1;if (y > 0)posLat = posLat + (pre_c)* dd1;elseposLat = posLat - (pre_c)* dd1;int nc = (int)floor(fabs(y - posLat) / dd2);int nr = (int)floor(fabs(x - posLon) / dd2);nr = nr > 7 ? 7 : nr;nc = nc > 7 ? 7 : nc;pre_r = nr;pre_c = nc;char nrc = (char)(nr + 48);char ncc = (char)(nc + 48);code.emplace_back();code[code.size() - 1].resize(2);code[code.size() - 1][0] = nrc;code[code.size() - 1][1] = ncc;continue;}}//n0double r0 = 6378137.000000000;// a=1+sta0 b=(h+r0)/r0//n=sta0/sta * logb /log a b //a 为底, b为指double PI = 3.1415926535897932;double sta0 = PI / 180.00000000;double sta = g_zdel[level-1] * 0.017453292519943295;double rn = (r0 + z) / r0;double fg = (sta0 / sta) * (log(rn) / log(1 + sta0));int ffg = floor(fg);std::vector<std::pair<int, int>>g_levelStartEndLocation ={ { 32,32 }, { 26, 31 }, { 23, 25 }, { 22, 22 }, { 18, 21 },{ 14, 17 }, { 13, 13 }, { 10, 12 }, { 7, 9 } , { 4, 6 } , { 1, 3 } };// 1 6 3 1 4 4 1 3 3 3 3 int left = 0;for (int j = 0; j <= level; ++j){int left = 32 - g_levelStartEndLocation[j].second;int right = g_levelStartEndLocation[j].first + 32 - g_levelStartEndLocation[j].second - 1;int nVal = (((unsigned int)ffg << left) >> right);if (j == 3 || j == 6) {if (nVal > 1)nVal = 1;}else if (j == 4 || j == 5) {if (nVal > 14)nVal = 14;}else if (j == 2 || j >= 7){if (nVal > 7)nVal = 7;}else if (j == 1){if (nVal > 63)nVal = 63;}if (j == 4 || j == 5) {std::string s;char nrc = (char)(nVal > 9 ? nVal + 55 : nVal + 48);s.resize(1);s[0] = nrc;hcode.push_back(s);}else if (j == 1 && nVal < 10) {std::string f = "0";f.append(std::to_string(nVal));hcode.push_back(f.c_str());}else {hcode.push_back(std::to_string(nVal).c_str());}}std::stringbuf buf;for (int i = 0; i < code.size(); i++){buf.sputn(code[i].c_str(), code[i].size());buf.sputn(hcode[i].c_str(), hcode[i].size());}return buf.str();}
反算代码后面再写,