新旧图幅号转换公式及示例代码(C#)

2 篇文章 0 订阅

转载地址:http://blog.csdn.net/rrrrssss00/article/details/6218454

新旧图幅号转换的公式如下图所示:

 

共有50W,25W,10W,5W,2.5W,1W几种比例尺

下面的公式中,字母的下标表示对应的比例尺

H代表新图幅号中的行号,例如:J49E016003中的016,共三位,不足三位的在前面补0

L代表新图幅号中的列号,例如:J49E016003中的003,共三位,不足三位的在前面补0

X代表旧图幅号中的地图代码值,有A,B,C,D的一律转为1,2,3,4

其中[]表示取整,()表示取模

 

旧->新

 

新->旧:

 

下面是一段将新图幅号转为旧图幅号的示例代码(C#,100W,50W,25W,10W,5W几种比例尺)

[c-sharp]  view plain  copy
  1. //新图幅号转到旧图幅号  
  2.         private string SheetNoConvert(string newNo)  
  3.         {  
  4.             if (newNo.Length != 3 && newNo.Length != 10) return null;  
  5.             string bigRowNo = newNo[0].ToString();  
  6.             string bigColNo = newNo.Substring(1, 2);  
  7.             if (newNo.Length == 3)  //100W  
  8.                 return bigRowNo + "-" + bigColNo;  
  9.             else  
  10.             {  
  11.                 char scaleChar = newNo[3];      //新图幅号第三位,标识是哪种比例尺  
  12.                 int H, L;                       //新图幅号的行列代码,H为行代码,L为列代码  
  13.                 //提取行代码和列代码,若非数字,则表示图幅号格式不正确  
  14.                 if (!int.TryParse(newNo.Substring(4, 3), out H) || !int.TryParse(newNo.Substring(7, 3), out L))  
  15.                     return null;  
  16.                 if (newNo[3] == 'B')   //50W  
  17.                 {  
  18.                     #region 50W  
  19.                     if (H > 2 || L > 2)  
  20.                         return null;  
  21.                     int X = (H - 1) * 2 + (L - 1) + 1;      //旧图幅号对应的地图代码  
  22.                     string code = "";  
  23.                     switch (X)  
  24.                     {  
  25.                         case 1:  
  26.                             code = "A";  
  27.                             break;  
  28.                         case 2:  
  29.                             code = "B";  
  30.                             break;  
  31.                         case 3:  
  32.                             code = "C";  
  33.                             break;  
  34.                         case 4:  
  35.                             code = "D";  
  36.                             break;  
  37.                         default:  
  38.                             break;  
  39.                     }  
  40.                     return bigRowNo + "-" + bigColNo + "-" + code;  
  41.                     #endregion  
  42.                 }  
  43.                 else if (newNo[3] == 'C')//25W  
  44.                 {  
  45.                     #region 25W  
  46.                     if (H > 4 || L > 4)  
  47.                         return null;  
  48.                     int X = (H - 1) * 4 + (L - 1) + 1;      //旧图幅号对应的地图代码  
  49.                     string code = "";  
  50.                     if (X > 9)  
  51.                         code = X.ToString();  
  52.                     else code = "0" + X;  
  53.                     return bigRowNo + "-" + bigColNo + "-[" + code + "]";  
  54.                     #endregion  
  55.                 }  
  56.                 else if (newNo[3] == 'D')   //10W  
  57.                 {  
  58.                     #region 10W  
  59.                     if (H > 12 || L > 12)  
  60.                         return null;  
  61.                     int X = (H - 1) * 12 + (L - 1) + 1;         //旧图幅号对应的地图代码  
  62.                     string code = "";  
  63.                     if (X > 99)  
  64.                         code = X.ToString();  
  65.                     else if (X > 9) code = "0" + X;  
  66.                     else code = "00" + X;  
  67.                     return bigRowNo + "-" + bigColNo + "-" + code + "";  
  68.                     #endregion  
  69.                 }  
  70.                 else if (newNo[3] == 'E')   //5W  
  71.                 {  
  72.                     #region 5W  
  73.                     if (H > 24 || L > 24)  
  74.                         return null;  
  75.                     int H10 = (H - 1) / 2 + 1;  //10W地形图对应的行号  
  76.                     int L10 = (L - 1) / 2 + 1;  //10W地形图对应的列号  
  77.                     int X10 = (H10 - 1) * 12 + (L10 - 1) + 1;         //10W旧图幅号对应的地图代码  
  78.                     string code = "";  
  79.                     if (X10 > 99)  
  80.                         code = X10.ToString();  
  81.                     else if (X10 > 9) code = "0" + X10;  
  82.                     else code = "00" + X10;  
  83.                     int X = (H - 2 * H10 + 1) * 2 + (L - 2 * L10 + 1) + 1;         //旧图幅号对应的地图代码  
  84.                     switch (X)  
  85.                     {  
  86.                         case 1:  
  87.                             code += "-A";  
  88.                             break;  
  89.                         case 2:  
  90.                             code += "-B";  
  91.                             break;  
  92.                         case 3:  
  93.                             code += "-C";  
  94.                             break;  
  95.                         case 4:  
  96.                             code += "-D";  
  97.                             break;  
  98.                         default:  
  99.                             break;  
  100.                     }  
  101.                     return bigRowNo + "-" + bigColNo + "-" + code + "";  
  102.                     #endregion  
  103.                 }  
  104.                 else return null;       //图幅号格式不正确  
  105.             }  

 

下面是一段将旧图幅号转为新图幅号的示例代码(C#,100W,50W,25W,10W,5W几种比例尺)

[c-sharp]  view plain  copy
  1. //旧图幅号转到新图幅号  
  2.         private string SheetNoConvert2(string old_number)  
  3.         {  
  4.             String[] temp1 = old_number.Split('-');  
  5.             if (temp1.Length == 1) return "";  
  6.             else if (temp1.Length == 2)//   I-45     100万比例尺的图幅号  
  7.                 return temp1[0] + temp1[1];  
  8.             else if (temp1.Length == 4)         //5W地形图 I-48-060-C  
  9.             {  
  10.                 #region 5w  
  11.                 int x10 = -1;  
  12.                 if (!int.TryParse(temp1[1], out x10))  
  13.                     return "";  
  14.                 if (!int.TryParse(temp1[2], out x10))  
  15.                     return "";  
  16.                 int x5 = -1;  
  17.                 if (temp1[3] == "A")  
  18.                     x5 = 1;  
  19.                 else if (temp1[3] == "B")  
  20.                     x5 = 2;  
  21.                 else if (temp1[3] == "C")  
  22.                     x5 = 3;  
  23.                 else if (temp1[3] == "D")  
  24.                     x5 = 4;  
  25.                 else return "";  
  26.                 int h10 = (x10 - 1) / 12 + 1;  
  27.                 int l10 = (x10 + 11) % 12 + 1;  
  28.                 int h5 = 2 * h10 + (x5 - 1) / 2 - 1;  
  29.                 int l5 = 2 * l10 + (x5 + 1) % 2 - 1;  
  30.                 string h5s = "";  
  31.                 if (h5 < 10)  
  32.                     h5s = "00" + h5;  
  33.                 else if (h5 < 100)  
  34.                     h5s = "0" + h5;  
  35.                 else h5s = h5.ToString();  
  36.                 string l5s = "";  
  37.                 if (l5 < 10)  
  38.                     l5s = "00" + l5;  
  39.                 else if (l5 < 100)  
  40.                     l5s = "0" + l5;  
  41.                 else l5s = l5.ToString();  
  42.                 return temp1[0] + temp1[1] + "E" + h5s + l5s;  
  43.                 #endregion  
  44.             }  
  45.             else if (temp1.Length == 3)  
  46.             {  
  47.                 if (temp1[2].Length == 1)        //50W地形图 H-45-B  
  48.                 {  
  49.                     #region 50w  
  50.                     int x50 = -1;  
  51.                     if (temp1[2] == "A")  
  52.                         x50 = 1;  
  53.                     else if (temp1[2] == "B")  
  54.                         x50 = 2;  
  55.                     else if (temp1[2] == "C")  
  56.                         x50 = 3;  
  57.                     else if (temp1[2] == "D")  
  58.                         x50 = 4;  
  59.                     else return "";  
  60.                     int h50 = (x50 - 1) / 2 + 1;  
  61.                     int l50 = (x50 + 1) % 2 + 1;  
  62.                     string h50s = "";  
  63.                     if (h50 < 10)  
  64.                         h50s = "00" + h50;  
  65.                     else if (h50 < 100)  
  66.                         h50s = "0" + h50;  
  67.                     else h50s = h50.ToString();  
  68.                     string l50s = "";  
  69.                     if (l50 < 10)  
  70.                         l50s = "00" + l50;  
  71.                     else if (l50 < 100)  
  72.                         l50s = "0" + l50;  
  73.                     else l50s = l50.ToString();  
  74.                     return temp1[0] + temp1[1] + "B" + h50s + l50s;  
  75.                     #endregion  
  76.                 }  
  77.                 else if (temp1[2].Length == 4)          //25W地形图 H-47-[14]  
  78.                 {  
  79.                     #region 25w  
  80.                     if (temp1[2][0] != '[' || temp1[2][3] != ']')  
  81.                         return "";  
  82.                     int x25 = -1;  
  83.                     if (!int.TryParse(temp1[2].Substring(1, 2), out x25))  
  84.                         return "";  
  85.                     int h25 = (x25 - 1) / 4 + 1;  
  86.                     int l25 = (x25 + 3) % 4 + 1;  
  87.                     string h25s = "";  
  88.                     if (h25 < 10)  
  89.                         h25s = "00" + h25;  
  90.                     else if (h25 < 100)  
  91.                         h25s = "0" + h25;  
  92.                     else h25s = h25.ToString();  
  93.                     string l25s = "";  
  94.                     if (l25 < 10)  
  95.                         l25s = "00" + l25;  
  96.                     else if (l25 < 100)  
  97.                         l25s = "0" + l25;  
  98.                     else l25s = l25.ToString();  
  99.                     return temp1[0] + temp1[1] + "C" + h25s + l25s;  
  100.                     #endregion  
  101.                 }  
  102.                 else if (temp1[2].Length == 3)          //10W地形图 K-50-041  
  103.                 {  
  104.                     #region 10w  
  105.                     int x10 = -1;  
  106.                     if (!int.TryParse(temp1[2], out x10))  
  107.                         return "";  
  108.                     int h10 = (x10 - 1) / 12 + 1;  
  109.                     int l10 = (x10 + 11) % 12 + 1;  
  110.                     string h10s = "";  
  111.                     if (h10 < 10)  
  112.                         h10s = "00" + h10;  
  113.                     else if (h10 < 100)  
  114.                         h10s = "0" + h10;  
  115.                     else h10s = h10.ToString();  
  116.                     string l10s = "";  
  117.                     if (l10 < 10)  
  118.                         l10s = "00" + l10;  
  119.                     else if (l10 < 100)  
  120.                         l10s = "0" + l10;  
  121.                     else l10s = l10.ToString();  
  122.                     return temp1[0] + temp1[1] + "D" + h10s + l10s;  
  123.                     #endregion  
  124.                 }  
  125.                 else return "";  
  126.             }  
  127.             else return "";  
  128.         }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值