C# 根据地图图号判断比例尺

我国地形图有7种基本比例尺,根据地图图号判断其属于哪个合法的比例尺。

using System.Text.RegularExpressions;

// 判断合法的图号名称,并且返回比例尺       
        public static long getMapScaleByMapID(string strS)
        {
            long scale = 0;

            if (strS.Length < 5 || strS.Length > 11)
                return scale;

            string temp = strS.Substring(0, 1);

            if (temp != "S"&& temp != "N")
                return scale;

            int index100B, index100L, index50, index25, index10, index5, index2d5, index1;
            string sNum = strS.Substring(1, strS.Length - 1);

            string s100_B = sNum.Substring(0, 2);
            string s100_L = sNum.Substring(2, 2);

            // 具有合法 百万图号
            if (!IsNumberic(s100_B, out index100B))
                return scale;
            if(index100B<1|| index100B>22)
                return scale;
            if (!IsNumberic(s100_L, out index100L))
                return scale;
            if (index100L < 1 || index100L > 60)
                return scale;

            // 进一步 判断合法 相应比例尺图号
            int nBit = sNum.Length; // 长度

            if (nBit==4)// 100万
            {
                scale = 1000000;
            }
            else if(nBit == 5)// 50万
            {
                temp = sNum.Substring(4, 1);
                if (!IsNumberic(temp, out index50))
                    return scale;
                if (index50 < 1 || index50 > 4)
                    return scale;
                scale = 500000;
            }
            else if(nBit == 6)// 25万
            {
                temp = sNum.Substring(4, 2);
                if (!IsNumberic(temp, out index25))
                    return scale;
                if (index25 < 1 || index25 > 16)
                    return scale;
                scale = 250000;
            }
            
            if (nBit >= 7)// Maybe 10万
            {
                temp = sNum.Substring(4, 3);
                if (!IsNumberic(temp, out index10))
                    return scale;
                if (index10 < 1 || index10 > 144)
                    return scale;
                if (nBit == 7)
                    scale = 100000;
            }

            if(nBit >= 8)// Maybe 5万
            {
                temp = sNum.Substring(7, 1);
                if (!IsNumberic(temp, out index5))
                    return scale;
                if (index5 < 1 || index5 > 4)
                    return scale;
                if (nBit == 8) 
                    scale = 50000;
            }

            if(nBit >= 9)// Maybe 2.5万
            {
                temp = sNum.Substring(8, 1);
                if (!IsNumberic(temp, out index2d5))
                    return scale;
                if (index2d5 < 1 || index2d5 > 4)
                    return scale;
                if (nBit == 9)
                    scale = 25000;
            }
            if (nBit == 10)// 1万
            {
                temp = sNum.Substring(9, 1);
                if (!IsNumberic(temp, out index1))
                    return scale;
                if (index1 < 1 || index1 > 4)
                    return scale;
                scale = 10000;
            }     

            return scale;
        }

public static bool IsNumberic(string strS, out int result)
        {   
            Regex rx = new Regex(@"^[1-9]\d*|0$");// 非负整数
            result = -1;            

            strS = strS.TrimStart('0');
            if (rx.IsMatch(strS))
            {
                result = int.Parse(strS);
                return true;
            }
            else
                return false;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starhuhu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值