• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
bobird的学习笔记
博客园    首页    新随笔    联系   管理    订阅  订阅
新旧图幅号转换公式及示例代码(C#)

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

 

共有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几种比例尺)

//新图幅号转到旧图幅号
        private string SheetNoConvert(string newNo)
        {
            if (newNo.Length != 3 && newNo.Length != 10) return null;
            string bigRowNo = newNo[0].ToString();
            string bigColNo = newNo.Substring(1, 2);
            if (newNo.Length == 3)  //100W
                return bigRowNo + "-" + bigColNo;
            else
            {
                char scaleChar = newNo[3];      //新图幅号第三位,标识是哪种比例尺
                int H, L;                       //新图幅号的行列代码,H为行代码,L为列代码
                //提取行代码和列代码,若非数字,则表示图幅号格式不正确
                if (!int.TryParse(newNo.Substring(4, 3), out H) || !int.TryParse(newNo.Substring(7, 3), out L))
                    return null;
                if (newNo[3] == 'B')   //50W
                {
                    #region 50W
                    if (H > 2 || L > 2)
                        return null;
                    int X = (H - 1) * 2 + (L - 1) + 1;      //旧图幅号对应的地图代码
                    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;
                    }
                    return bigRowNo + "-" + bigColNo + "-" + code;
                    #endregion
                }
                else if (newNo[3] == 'C')//25W
                {
                    #region 25W
                    if (H > 4 || L > 4)
                        return null;
                    int X = (H - 1) * 4 + (L - 1) + 1;      //旧图幅号对应的地图代码
                    string code = "";
                    if (X > 9)
                        code = X.ToString();
                    else code = "0" + X;
                    return bigRowNo + "-" + bigColNo + "-[" + code + "]";
                    #endregion
                }
                else if (newNo[3] == 'D')   //10W
                {
                    #region 10W
                    if (H > 12 || L > 12)
                        return null;
                    int X = (H - 1) * 12 + (L - 1) + 1;         //旧图幅号对应的地图代码
                    string code = "";
                    if (X > 99)
                        code = X.ToString();
                    else if (X > 9) code = "0" + X;
                    else code = "00" + X;
                    return bigRowNo + "-" + bigColNo + "-" + code + "";
                    #endregion
                }
                else if (newNo[3] == 'E')   //5W
                {
                    #region 5W
                    if (H > 24 || L > 24)
                        return null;
                    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.ToString();
                    else if (X10 > 9) code = "0" + X10;
                    else code = "00" + X10;
                    int X = (H - 2 * H10 + 1) * 2 + (L - 2 * L10 + 1) + 1;         //旧图幅号对应的地图代码
                    switch (X)
                    {
                        case 1:
                            code += "-A";
                            break;
                        case 2:
                            code += "-B";
                            break;
                        case 3:
                            code += "-C";
                            break;
                        case 4:
                            code += "-D";
                            break;
                        default:
                            break;
                    }
                    return bigRowNo + "-" + bigColNo + "-" + code + "";
                    #endregion
                }
                else return null;       //图幅号格式不正确
            }

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

//旧图幅号转到新图幅号
        private string SheetNoConvert2(string old_number)
        {
            String[] temp1 = old_number.Split('-');
            if (temp1.Length == 1) return "";
            else if (temp1.Length == 2)//   I-45     100万比例尺的图幅号
                return temp1[0] + temp1[1];
            else if (temp1.Length == 4)         //5W地形图 I-48-060-C
            {
                #region 5w
                int x10 = -1;
                if (!int.TryParse(temp1[1], out x10))
                    return "";
                if (!int.TryParse(temp1[2], out x10))
                    return "";
                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)
                    h5s = "00" + h5;
                else if (h5 < 100)
                    h5s = "0" + h5;
                else h5s = h5.ToString();
                string l5s = "";
                if (l5 < 10)
                    l5s = "00" + l5;
                else if (l5 < 100)
                    l5s = "0" + l5;
                else l5s = l5.ToString();
                return temp1[0] + temp1[1] + "E" + h5s + l5s;
                #endregion
            }
            else if (temp1.Length == 3)
            {
                if (temp1[2].Length == 1)        //50W地形图 H-45-B
                {
                    #region 50w
                    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)
                        h50s = "00" + h50;
                    else if (h50 < 100)
                        h50s = "0" + h50;
                    else h50s = h50.ToString();
                    string l50s = "";
                    if (l50 < 10)
                        l50s = "00" + l50;
                    else if (l50 < 100)
                        l50s = "0" + l50;
                    else l50s = l50.ToString();
                    return temp1[0] + temp1[1] + "B" + h50s + l50s;
                    #endregion
                }
                else if (temp1[2].Length == 4)          //25W地形图 H-47-[14]
                {
                    #region 25w
                    if (temp1[2][0] != '[' || temp1[2][3] != ']')
                        return "";
                    int x25 = -1;
                    if (!int.TryParse(temp1[2].Substring(1, 2), out x25))
                        return "";
                    int h25 = (x25 - 1) / 4 + 1;
                    int l25 = (x25 + 3) % 4 + 1;
                    string h25s = "";
                    if (h25 < 10)
                        h25s = "00" + h25;
                    else if (h25 < 100)
                        h25s = "0" + h25;
                    else h25s = h25.ToString();
                    string l25s = "";
                    if (l25 < 10)
                        l25s = "00" + l25;
                    else if (l25 < 100)
                        l25s = "0" + l25;
                    else l25s = l25.ToString();
                    return temp1[0] + temp1[1] + "C" + h25s + l25s;
                    #endregion
                }
                else if (temp1[2].Length == 3)          //10W地形图 K-50-041
                {
                    #region 10w
                    int x10 = -1;
                    if (!int.TryParse(temp1[2], out x10))
                        return "";
                    int h10 = (x10 - 1) / 12 + 1;
                    int l10 = (x10 + 11) % 12 + 1;
                    string h10s = "";
                    if (h10 < 10)
                        h10s = "00" + h10;
                    else if (h10 < 100)
                        h10s = "0" + h10;
                    else h10s = h10.ToString();
                    string l10s = "";
                    if (l10 < 10)
                        l10s = "00" + l10;
                    else if (l10 < 100)
                        l10s = "0" + l10;
                    else l10s = l10.ToString();
                    return temp1[0] + temp1[1] + "D" + h10s + l10s;
                    #endregion
                }
                else return "";
            }
            else return "";
        }

 

posted on 2013-08-01 09:57  bobird  阅读(1133)  评论(1)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3