我国地形图有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;
}