网上好多新旧图幅转换的代码或资料,但大部分不完整且有错误,现将整理后的代码供大家分享,如有问题,请大家指正。
1.旧图幅号转新图幅号
//旧图幅号转到新图幅号
string SheetNoConvert_Old2New(string old_number)
{
vector<string> temp1;
boost::split(temp1,old_number,boost::is_any_of(_T("-")));
if (temp1.size() == 1)
return "";
else if (temp1.size() == 2)// I-45 100万比例尺的图幅号
return temp1[0] + temp1[1];
else if (temp1.size() == 4) //5W地形图 I-48-060-C
{
if (temp1[3].length() == 1) //5W地形图 I-48-060-C
{
#pragma region 5
int x10 = -1;
x10 = atoi(temp1[1].c_str());
x10 = atoi(temp1[2].c_str());
int x5 = -1;
if (temp1[3] == "A")
x5 = 1;
else if (temp1[3] == "B")
x5 = 2;
else if (temp1[3] == "C")
x5 = 3;
else if (temp1[3] == "D")
x5 = 4;
else return "";
int h10 = (x10 - 1) / 12 + 1;
int l10 = (x10 + 11) % 12 + 1;
int h5 = 2 * h10 + (x5 - 1) / 2 - 1;
int l5 = 2 * l10 + (x5 + 1) % 2 - 1;
string h5s = "";
if (h5 < 10)
{
char stemp[4];
sprintf(stemp,"00%d",h5);
h5s = stemp;
}
else if (h5 < 100)
{
char stemp[4];
sprintf(stemp,"0%d",h5);
h5s = stemp;
}
else
{
char stemp[4];
sprintf(stemp,"%d",h5);
h5s = stemp;
}
string l5s = "";
if (l5 < 10)
{
char stemp[4];
sprintf(stemp,"00%d",l5);
l5s = stemp;
}
else if (l5 < 100)
{
char stemp[4];
sprintf(stemp,"0%d",l5);
l5s = stemp;
}
else
{
char stemp[4];
sprintf(stemp,"%d",l5);
l5s = stemp;
}
return temp1[0] + temp1[1] + "E" + h5s + l5s;
#pragma endregion 5
}
if (temp1[3].length()>2 && temp1[3].find("(") != -1) //1W地形图 J-50-87-(40)
{
#pragma region 1
int x10 = -1, x1 = -1;
x10 = atoi(temp1[2].c_str());
if (temp1[3][0] != '(' || temp1[3][temp1[3].length()-1] != ')')
return "";
string s = temp1[3].substr(1,temp1[3].length()-2);
x1 = atoi(s.c_str());
int h10 = (x10 - 1) / 12 + 1;
int l10 = (x10 + 11) % 12 + 1;
int h1 = 8 * (h10 - 1) + (x1 - 1) / 8 + 1;
int l1 = 8 * (l10 - 1) + (x1 + 7) % 8 + 1;
string h1s = "";
if (h1 < 10)
{
char stemp[4];
sprintf(stemp,"00%d",h1);
h1s = stemp;
}
else if (h1 < 100)
{
char stemp[4];
sprintf(stemp,"0%d",h1);
h1s = stemp;
}
else
{
char stemp[4];
sprintf(stemp,"%d",h1);
h1s = stemp;
}
string l1s = "";
if (l1 < 10)
{
char stemp[4];
sprintf(stemp,"00%d",l1);
l1s = stemp;
}
else if (l1 < 100)
{
char stemp[4];
sprintf(stemp,"0%d",l1);
l1s = stemp;
}
else
{
char stemp[4];
sprintf(stemp,"%d",l1);
l1s = stemp;
}
return temp1[0] + temp1[1] + "G" + h1s + l1s;
#pragma endregion 1
}
}
else if (temp1.size() == 3)
{
if (temp1[2].length() == 1 && (temp1[2] == "A"||temp1[2] == "B"||temp1[2] == "C"||temp1[2] == "D")) //50W地形图 H-45-B
{
#pragma region 50
int x50 = -1;
if (temp1[2] == "A")
x50 = 1;
else if (temp1[2] == "B")
x50 = 2;
else if (temp1[2] == "C")
x50 = 3;
else if (temp1[2] == "D")
x50 = 4;
else return "";
int h50 = (x50 - 1) / 2 + 1;
int l50 = (x50 + 1) % 2 + 1;
string h50s = "";
if (h50 < 10)
{
char stemp[4];
sprintf(stemp,"00%d",h50);
h50s = stemp;
}
else if (h50 < 100)
{
char stemp[4];
sprintf(stemp,"0%d",h50);
h50s = stemp;
}
else
{
char stemp[4];
sprintf(stemp,"%d",h50);
h50s = stemp;
}
string l50s = "";
if (l50 < 10)
{
char stemp[4];
sprintf(stemp,"00%d",l50);
l50s = stemp;
}
else if (l50 < 100)
{
char stemp[4];
sprintf(stemp,"0%d",l50);
l50s = stemp;
}
else
{
char stemp[4];
sprintf(stemp,"%d",l50);
l50s = stemp;
}
return temp1[0] + temp1[1] + "B" + h50s + l50s;
#pragma endregion 50
}
else if (temp1[2].length()>2 && temp1[2].find("[") != -1) //25W地形图 H-47-[14]
{
#pragma region 25
if (temp1[2][0] != '[' || temp1[2][temp1[2].length()-1] != ']')
return "";
int x25 = -1;
tString s = temp1[2].substr(1,temp1[2].length()-2);
x25 = atoi(s.c_str());
int h25 = (x25 - 1) / 4 + 1;
int l25 = (x25 + 3) % 4 + 1;
string h25s = "";
if (h25 < 10)
{
char stemp[4];
sprintf(stemp,"00%d",h25);
h25s = stemp;
}
else if (h25 < 100)
{
char stemp[4];
sprintf(stemp,"0%d",h25);
h25s = stemp;
}
else
{
char stemp[4];
sprintf(stemp,"%d",h25);
h25s = stemp;
}
string l25s = "";
if (l25 < 10)
{
char stemp[4];
sprintf(stemp,"00%d",l25);
l25s = stemp;
}
else if (l25 < 100)
{
char stemp[4];
sprintf(stemp,"0%d",l25);
l25s = stemp;
}
else
{
char stemp[4];
sprintf(stemp,"%d",l25);
l25s = stemp;
}
return temp1[0] + temp1[1] + "C" + h25s + l25s;
#pragma endregion 25
}
else //10W地形图 K-50-041
{
#pragma region 10
int x10 = -1;
x10 = atoi(temp1[2].c_str());
int h10 = (x10 - 1) / 12 + 1;
int l10 = (x10 + 11) % 12 + 1;
string h10s = "";
if (h10 < 10)
{
char stemp[4];
sprintf(stemp,"00%d",h10);
h10s = stemp;
}
else if (h10 < 100)
{
char stemp[4];
sprintf(stemp,"0%d",h10);
h10s = stemp;
}
else
{
char stemp[4];
sprintf(stemp,"%d",h10);
h10s = stemp;
}
string l10s = "";
if (l10 < 10)
{
char stemp[4];
sprintf(stemp,"00%d",l10);
l10s= stemp;
}
else if (l10 < 100)
{
char stemp[4];
sprintf(stemp,"0%d",l10);
l10s= stemp;
}
else
{
char stemp[4];
sprintf(stemp,"%d",l10);
l10s= stemp;
}
return temp1[0] + temp1[1] + "D" + h10s + l10s;
#pragma endregion 10
}
}
else if (temp1.size() == 5)//1:2.5W后面加1、2、3、4:J-50-87-B-3
{
if (temp1[3].find("(") != -1)//1:5000:J-50-87-(45)-a
{
#pragma region 5000
int x10 = -1, x1 = -1,x5000 = -1,y5000 = -1;
x10 = atoi(temp1[2].c_str());
if (temp1[3][0] != '(' || temp1[3][temp1[3].length()-1] != ')')
return "";
string s1 = temp1[3].substr(1,temp1[3].length()-2);
x1 = atoi(s1.c_str());
if(temp1[4] == "a")
{
x5000 = 1;
y5000 = 1;
}
else if (temp1[4] == "b")
{
x5000 = 1;
y5000 = 2;
}
else if (temp1[4] == "c")
{
x5000 = 2;
y5000 = 1;
}
else if (temp1[4] == "d")
{
x5000 = 2;
y5000 = 2;
}
int h10 = (x10 - 1) / 12 + 1;
int l10 = (x10 + 11) % 12 + 1;
int h1 = 8 * (h10 - 1) + (x1 - 1) / 8 + 1;
int l1 = 8 * (l10 - 1) + (x1 + 7) % 8 + 1;
int h5000 = 2 * (h1 - 1) + x5000;
int l5000 = 2 * (l1 - 1) + y5000;
string h5000s = "";
if (h5000 < 10)
{
char stemp[4];
sprintf(stemp,"00%d",h5000);
h5000s = stemp;
}
else if (h5000 < 100)
{
char stemp[4];
sprintf(stemp,"0%d",h5000);
h5000s = stemp;
}
else
{
char stemp[4];
sprintf(stemp,"%d",h5000);
h5000s = stemp;
}
string l5000s = "";
if (l5000 < 10)
{
char stemp[4];
sprintf(stemp,"00%d",l5000);
l5000s = stemp;
}
else if (l1 < 100)
{
char stemp[4];
sprintf(stemp,"0%d",l5000);
l5000s = stemp;
}
else
{
char stemp[4];
sprintf(stemp,"%d",l5000);
l5000s = stemp;
}
return temp1[0] + temp1[1] + "G" + h5000s + l5000s;
#pragma endregion 5000
}
else//1:2.5W后面加1、2、3、4:J-50-87-B-3
{
#pragma region 2.5
int x10 = -1, x5 = -1, x2 = -1;
x10 = atoi(temp1[2].c_str());
int h10 = (x10 - 1) / 12 + 1;
int l10 = (x10 + 11) % 12 + 1;
if (temp1[3] == "A")
x5 = 1;
else if (temp1[3] == "B")
x5 = 2;
else if (temp1[3] == "C")
x5 = 3;
else if (temp1[3] == "D")
x5 = 4;
else return "";
x2 = atoi(temp1[4].c_str());
int h5 = 2 * h10 + (x5 - 1) / 2 - 1;
int l5 = 2 * l10 + (x5 + 1) % 2 - 1;
int h2 = 2 * h5 + (x2 - 1) / 2 - 1;
int l2 = 2 * l5 + (x2 + 1) % 2 - 1;
string h2s = "";
if (h2 < 10)
{
char stemp[4];
sprintf(stemp,"00%d",h2);
h2s = stemp;
}
else if (h2 < 100)
{
char stemp[4];
sprintf(stemp,"0%d",h2);
h2s = stemp;
}
else
{
char stemp[4];
sprintf(stemp,"%d",h2);
h2s = stemp;
}
string l2s = "";
if (l2 < 10)
{
char stemp[4];
sprintf(stemp,"00%d",l2);
l2s = stemp;
}
else if (l2 < 100)
{
char stemp[4];
sprintf(stemp,"0%d",l2);
l2s = stemp;
}
else
{
char stemp[4];
sprintf(stemp,"%d",l2);
l2s = stemp;
}
return temp1[0] + temp1[1] + "F" + h2s + l2s;
#pragma endregion 2.5
}
}
else return "";
}
2.新图幅号转旧图幅号
//新图幅号转到旧图幅号
string SheetNoConvert_New2Old(string newMapsub)
{
string oldMapsub = "";
if (newMapsub.size()!=3&&newMapsub.size()!= 10)
{
smartlog<<"新图幅号不正确!";
return "";
}
string bigRowNo = newMapsub.substr(0,1);
string bigColNo = newMapsub.substr(1,2);
if (newMapsub.length() == 3)//100W
{
oldMapsub = bigRowNo+"-"+bigColNo;
}
else
{
string scaleChar = newMapsub.substr(3,1);//比例尺
int H,L;//新图幅号的行列代码,H:行;L:列;
//提取行列号代码,若不是数字,表示图幅号不正确。
string strH = newMapsub.substr(4,3);
string strL = newMapsub.substr(7,3);
H = atoi(strH.c_str());
L = atoi(strL.c_str());
if (scaleChar == "B")//50W
{
#pragma region 50
if (H>2||L>2)
{
smartlog<<"图幅号格式不正确";
return "";
}
int X = (H-1)*2+L;//旧图幅号对应的地图代码
string code = "";
switch(X)
{
case 1:
code = "A";
break;
case 2:
code = "B";
break;
case 3:
code = "C";
break;
case 4:
code = "D";
break;
default:
break;
}
oldMapsub = bigRowNo +"-"+bigColNo+"-"+code;
#pragma endregion 50
}
else if (scaleChar == "C")//25W
{
#pragma region 25
if (H>4||L>4)
{
smartlog<<"图幅号不正确!";
return "";
}
int X = (H-1)*4 + L;//旧图幅号对应的地图代码
string code= "";
// if (X>9)
// {
char stemp[3];
sprintf(stemp,"%d",X);
code = stemp;
// }
// else
// {
// //code = "0"+X;
// char stemp[3];
// sprintf(stemp,"0%d",X);
// code = stemp;
// }
oldMapsub = bigRowNo+"-"+bigColNo+"-["+code+"]";
#pragma endregion 25
}
else if (scaleChar == "D")
{
#pragma region 10
if (H > 12 || L > 12)
return false;
int X = (H - 1) * 12 + L; //旧图幅号对应的地图代码
string code = "";
// if (X > 99)
// {
char stemp[4];
sprintf(stemp,"%d",X);
code = stemp;
// }
// else if (X > 9)
// {
// //code = "0" + X;
// char stemp[4];
// sprintf(stemp,"0%d",X);
// code = stemp;
// }
// else
// {
// //code = "00" + X;
// char stemp[4];
// sprintf(stemp,"00%d",X);
// code = stemp;
// }
oldMapsub = bigRowNo + "-" + bigColNo + "-" + code + "";
#pragma endregion 10
}
else if (scaleChar == "E")
{
#pragma region 5
if (H > 24 || L > 24)
return false;
int H10 = (H - 1) / 2 + 1; //10W地形图对应的行号
int L10 = (L - 1) / 2 + 1; //10W地形图对应的列号
int X10 = (H10 - 1) * 12 + (L10 - 1) + 1; //10W旧图幅号对应的地图代码
string code = "";
// if (X10 > 99)
// {
//code = X10;
char stemp[4];
sprintf(stemp,"%d",X10);
code = stemp;
// }
// else if (X10 > 9)
// {
// //code = "0" + X10;
// char stemp[4];
// sprintf(stemp,"0%d",X10);
// code = stemp;
// }
// else
// {
// //code = "00" + X10;
// char stemp[4];
// sprintf(stemp,"00%d",X10);
// code = stemp;
// }
int X = (H - 2 * H10 + 1) * 2 + (L - 2 * L10 + 2); //旧图幅号对应的地图代码
switch (X)
{
case 1:
code += "-A";
break;
case 2:
code += "-B";
break;
case 3:
code += "-C";
break;
case 4:
code += "-D";
break;
default:
break;
}
oldMapsub = bigRowNo + "-" + bigColNo + "-" + code + "";
#pragma endregion 5
}
else if (scaleChar == "F")//2.5W
{
#pragma region 2.5
int H10 = (H - 1) / 2 + 1; //10W地形图对应的行号
int L10 = (L - 1) / 2 + 1; //10W地形图对应的列号
int X10 = (H10 - 1) * 12 + (L10 - 1) + 1; //10W旧图幅号对应的地图代码
string code = "";
// if (X10 > 99)
// {
char stemp[4];
sprintf(stemp,"%d",X10);
code = stemp;
// }
// else if (X10 > 9)
// {
// //code = "0" + X10;
// char stemp[4];
// sprintf(stemp,"0%d",X10);
// code = stemp;
// }
// else
// {
// //code = "00" + X10;
// char stemp[4];
// sprintf(stemp,"00%d",X10);
// code = stemp;
// }
int X = (H - 2 * H10 + 1) * 2 + (L - 2 * L10 + 1) + 1; //5W旧图幅号对应的地图代码
switch (X)
{
case 1:
code += "-A";
break;
case 2:
code += "-B";
break;
case 3:
code += "-C";
break;
case 4:
code += "-D";
break;
default:
break;
}
int H5 = (H - 1) / 2 + 1; //5W地形图对应的行号
int L5 = (L - 1) / 2 + 1; //5W地形图对应的列号
// int X5 = (H5 - 1) * 12 + (L5 - 1) + 1; //5W旧图幅号对应的地图代码
int X1 = (H - 2 * H5 + 1) * 2 + (L - 2 * L5 + 1) + 1; //2.5W旧图幅号对应的地图代码
// if (X1 > 99)
// {
//code += X1;
//char stemp[4];
sprintf(stemp,"-%d",X1);
code += stemp;
// }
// else if (X1 > 9)
// {
// //code += "-0" + X1;
// char stemp[4];
// sprintf(stemp,"-0%d",X1);
// code += stemp;
// }
// else
// {
// //code += "-00" + X1;
// char stemp[4];
// sprintf(stemp,"-00%d",X1);
// code += stemp;
// }
oldMapsub = bigRowNo + "-" + bigColNo + "-" + code;
#pragma endregion 2.5
}
else if (scaleChar == "G")//1W
{
#pragma region 1
int H10 = (H - 1) / 8 + 1; //10W地形图对应的行号
int L10 = (L - 1) / 8 + 1; //10W地形图对应的列号
int X10 = (H10 - 1) * 12 + (L10 - 1) + 1; //10W旧图幅号对应的地图代码
string code = "";
// if (X10 > 99)
// {
//code = X10;
char stemp[4];
sprintf(stemp,"%d",X10);
code = stemp;
// }
// else if (X10 > 9)
// {
// //code = "0" + X10;
// char stemp[4];
// sprintf(stemp,"0%d",X10);
// code = stemp;
// }
// else
// {
// //code = "00" + X10;
// char stemp[4];
// sprintf(stemp,"00%d",X10);
// code = stemp;
// }
//if (H > 2 || L > 2)
// return null;
int X = (H - 8 *(H10 - 1) - 1) * 8 + (L - 8 * (L10 - 1)); //旧图幅号对应的地图代码
// if (X > 9)
// {
//code += "-(" + X;
// char stemp[3];
sprintf(stemp,"%d",X);
code += "-(" ;
code += stemp;
// }
// else
// {
// //code += "-(0" + X;
// char stemp[3];
// sprintf(stemp,"0%d",X);
// code += "-(";
// code += stemp;
// }
oldMapsub = bigRowNo + "-" + bigColNo + "-" + code + ")";
#pragma endregion 1
}
else if (scaleChar == "H")//5000
{
#pragma region 5000
int H10 = (H - 1) / 16 + 1; //10W地形图对应的行号
int L10 = (L - 1) / 16 + 1; //10W地形图对应的列号
int X10 = (H10 - 1) * 12 + (L10 - 1) + 1; //10W旧图幅号对应的地图代码
string code = "";
// if (X10 > 99)
// {
//code = X10;
char stemp[4];
sprintf(stemp,"%d",X10);
code = stemp;
// }
// else if (X10 > 9)
// {
// //code = "0" + X10;
// char stemp[4];
// sprintf(stemp,"0%d",X10);
// code = stemp;
// }
// else
// {
// //code = "00" + X10;
// char stemp[4];
// sprintf(stemp,"00%d",X10);
// code = stemp;
// }
//if (H > 2 || L > 2)
// return null;
int H1 = (H - (H10 - 1)*16 - 1)/2 + 1; //1W地形图对应的行号
int L1 = (L - (L10 - 1)*16 - 1)/2 + 1; //1W地形图对应的列号
int X1 = (H1 - 1)*8 + L1; //1W旧图幅号对应的地图代码
// if (X1 > 9)
// {
//code += "-(" + X;
// char stemp[3];
sprintf(stemp,"%d",X1);
code += "-(";
code += stemp;
code += ")";
// }
// else
// {
// //code += "-(0" + X;
// char stemp[3];
// sprintf(stemp,"0%d",X1);
// code += "-(";
// code += stemp;
// code += ")";
// }
int H5000 = (H - (H10 - 1)*16 - (H1 - 1)*2); //5K地形图对应的行号
int L5000 = (L - (L10 - 1)*16 - (L1 - 1)*2); //5K地形图对应的列号
int X5000 = (H5000 - 1)*2 + L5000; //1W旧图幅号对应的地图代码
switch (X5000)
{
case 1:
code += "-a";
break;
case 2:
code += "-b";
break;
case 3:
code += "-c";
break;
case 4:
code += "-d";
break;
default:
break;
}
oldMapsub = bigRowNo + "-" + bigColNo + "-" + code + "";
#pragma endregion 5000
}
else
{
smartlog<<"图幅号格式不正确";
return "";
}
}
return oldMapsub;
}
3.测试用例
string mapnew = SheetNoConvert_Old2New("J-50");//100W
mapnew = SheetNoConvert_Old2New("J-50-A");//50W
mapnew = SheetNoConvert_Old2New("J-50-[3]");//25W
mapnew = SheetNoConvert_Old2New("J-50-87");//10W
mapnew = SheetNoConvert_Old2New("J-50-87-B");//5W
mapnew = SheetNoConvert_Old2New("J-50-87-B-3");//2.5W
mapnew = SheetNoConvert_Old2New("J-50-87-(4)");//1W
mapnew = SheetNoConvert_Old2New("J-50-87-(4)-a");//5000
string mapold = SheetNoConvert_New2Old("H48");//100W
mapold = SheetNoConvert_New2Old("H48B001001");//50W
mapold = SheetNoConvert_New2Old("H48C002001");//25W
mapold = SheetNoConvert_New2Old("H48D005002");//10W
mapold = SheetNoConvert_New2Old("H48E009003");//5W.
mapold = SheetNoConvert_New2Old("H48F017006");//2.5W
mapold = SheetNoConvert_New2Old("H48G034011");//1W
mapold = SheetNoConvert_New2Old("H48H067021");//5K