java 规则计算公式_使用Java实现的新版地图图幅编号计算公式(新版图幅规则(GB/T 13989—2012))...

这是一个Java类,用于根据GB/T 13989—2012标准计算地图图幅编号。方法接收经度、纬度和缩放比例作为参数,通过计算得出图幅编号。同时,类中包含将新图幅编号转换为旧图幅编号的函数。
摘要由CSDN通过智能技术生成

1 packagecom.jucheap.utils;2

3 importcom.jucheap.base.CoordinateDTO;4

5 /**

6 *

7 * 地图图幅编号计算8 *

9 *10 * @Author jucheap11 * @Email jucheap@jucheap.com12 * @Date 2020/8/2213 */

14 public classMapNumberUtil {15

16 /**

17 * 新版图幅规则(GB/T 13989—2012)18 *@paramlongitude 经度19 *@paramlatitude 纬度20 *@paramscaleNumber 缩放比例(1000000/500000/100000/50000)21 *@return图幅编号22 */

23 public staticString toMapNumber(CoordinateDTO longitude, CoordinateDTO latitude, Integer scaleNumber) {24 //经/纬差值计算

25 Double lngDiff =getLongitudeDiff(scaleNumber);26 Double latDiff =getLatitudeDiff(scaleNumber);27 //度分秒转换,统一成度

28 Double lng =longitude.toDegreeNumber();29 Double lat =latitude.toDegreeNumber();30 StringBuilder sb = new StringBuilder(512);31 String[] codes =toWords();32 Double word_row = Double.valueOf(1) + lat / 4;33 Double word_col = Double.valueOf(31) + lng / 6;34 sb.append(String.format("%s%s%s", codes[word_row.intValue() - 1], word_col.intValue(), toScaleCode(scaleNumber)));35 Integer number_row = Double.valueOf(4.0 / latDiff).intValue() - Double.valueOf((lat % 4.0) /latDiff).intValue();36 Double number_col = lng % 6.0 / lngDiff + 1;37 sb.append(String.format("%03d", number_row));38 sb.append(String.format("%03d", number_col.intValue()));39 returnsb.toString();40 }41

42 /**

43 * 生成26个大写字母44 *@return26个大写字母数组45 */

46 private staticString[] toWords() {47 String[] codes = new String[26];48 for (int i = 1; i <= 26; i++) {49 codes[i - 1] =toUpperCode(i);50 }51 returncodes;52 }53

54 /**

55 * 转换成单个的大写字母56 *@paramvalue57 *@return

58 */

59 private staticString toUpperCode(Integer value) {60 return String.valueOf(Character.toUpperCase((char) (96 +value)));61 }62

63 /**

64 * 获取缩放比例代码65 *66 *@paramscaleNumber 缩放比例(如50000,代表1:50000)67 *@return缩放比例代码68 */

69 private static String toScaleCode(intscaleNumber) {70 switch(scaleNumber) {71 case 500000:72 return "B";73 case 250000:74 return "C";75 case 100000:76 return "D";77 case 50000:78 return "E";79 case 25000:80 return "F";81 case 10000:82 return "G";83 case 5000:84 return "H";85 case 2000:86 return "I";87 case 1000:88 return "J";89 case 500:90 return "K";91 default:92 return "";93 }94 }95

96 /**

97 * 获取经差值98 *99 *@paramscaleNumber 缩放比例(如50000,代表1:50000)100 *@return经差值101 */

102 private static Double getLongitudeDiff(intscaleNumber) {103 switch(scaleNumber) {104 case 1000000:105 return 6.0;106 case 500000:107 return 3.0;108 case 250000:109 return 1.5;110 case 100000:111 return 0.5;112 case 50000:113 return 15.0 / 60.0;114 case 25000:115 return 7.0 / 60.0 + 30.0 / 3600.0;116 case 10000:117 return 3.0 / 60.0 + 45.0 / 3600.0;118 case 5000:119 return 1.0 / 60.0 + 52.5 / 3600.0;120 case 2000:121 return 37.5 / 3600.0;122 case 1000:123 return 18.75 / 3600.0;124 case 500:125 return 9.375 / 3600.0;126 default:127 return 0.0;128 }129 }130

131 /**

132 * 获取纬差值133 *134 *@paramscaleNumber 缩放比例(如50000,代表1:50000)135 *@return纬差值136 */

137 private static Double getLatitudeDiff(intscaleNumber) {138 switch(scaleNumber) {139 case 1000000:140 return 4.0;141 case 500000:142 return 2.0;143 case 250000:144 return 1.0;145 case 100000:146 return 20.0 / 60.0;147 case 50000:148 return 10.0 / 60.0;149 case 25000:150 return 5.0 / 60.0;151 case 10000:152 return 2.0 / 60.0 + 30.0 / 3600.0;153 case 5000:154 return 1.0 / 60.0 + 15.0 / 3600.0;155 case 2000:156 return 25.0 / 3600.0;157 case 1000:158 return 12.5 / 3600.0;159 case 500:160 return 6.25 / 3600.0;161 default:162 return 0.0;163 }164 }165

166 /**

167 * 新的图幅编号转换成旧的图幅编号168 *@paramnewMapNumber 新图幅编号169 *@return旧图幅编号170 */

171 public staticString toOldMapNumber(String newMapNumber) {172 if (newMapNumber.length() != 3 && newMapNumber.length() != 10) {173 return "";174 }175 char[] newMapNumberChars =newMapNumber.toCharArray();176 String bigRowNo = String.valueOf(newMapNumberChars[0] - 'A' + 1);177 String bigColNo = newMapNumber.substring(1, 3);178 if (newMapNumber.length() == 3) { //100W

179 return String.format("%s-%s", bigRowNo, bigColNo);180 } else{181 //新图幅号的行列代码,col_number为行代码,col_number为列代码

182 Integer row_number = Integer.valueOf(newMapNumber.substring(4, 7));183 Integer col_number = Integer.valueOf(newMapNumber.substring(7, 10));184 //提取行代码和列代码,若非数字,则表示图幅号格式不正确

185 char mapScaleNumber = newMapNumberChars[3];186 if (mapScaleNumber == 'B') { //50W187 //region 50W

188 if (row_number > 2 || col_number > 2){189 return "";190 }191 //旧图幅号对应的地图代码

192 int X = (row_number - 1) * 2 + (col_number - 1) + 1;193 return String.format("%s-%s-%s", bigRowNo, bigColNo, toUpperCode(X));194 //endregion

195 } else if (mapScaleNumber == 'C') {//25W196 //region 25W

197 if (row_number > 4 || col_number > 4) {198 return "";199 }200 //旧图幅号对应的地图代码

201 int X = (row_number - 1) * 4 + (col_number - 1) + 1;202 //String code = String.format("%02d", X);

203 String code =String.valueOf(X);204 return String.format("%s-%s-[%s]", bigRowNo, bigColNo, code);205 //endregion

206 } else if (mapScaleNumber == 'D') {//10W207 //region 10W

208 if (row_number > 12 || col_number > 12){209 return "";210 }211 //旧图幅号对应的地图代码

212 int X = (row_number - 1) * 12 + (col_number - 1) + 1;213 //String code = String.format("%03d", X);

214 String code =String.valueOf(X);215 return String.format("%s-%s-%s", bigRowNo, bigColNo, code);216 //endregion

217 } else if (mapScaleNumber == 'E') { //5W218 //region 5W

219 if (row_number > 24 || col_number > 24) {220 return "";221 }222 //10W地形图对应的行号

223 int H10 = (row_number - 1) / 2 + 1;224 //10W地形图对应的列号

225 int L10 = (col_number - 1) / 2 + 1;226 //10W旧图幅号对应的地图代码

227 int X10 = (H10 - 1) * 12 + (L10 - 1) + 1;228 //String code = String.format("%03d", X10);

229 String code =String.valueOf(X10);230 //旧图幅号对应的地图代码

231 int X = (row_number - 2 * H10 + 1) * 2 + (col_number - 2 * L10 + 1) + 1;232 return String.format("%s-%s-%s-%s", bigRowNo, bigColNo, code, toUpperCode(X));233 //endregion

234 } else if (mapScaleNumber == 'F') {236 //5W地形图对应的行号

237 int H5 = (row_number - 1) / 2 + 1;238 //5W地形图对应的列号

239 int L5 = (col_number - 1) / 2 + 1;240

241 //10W地形图对应的行号

242 int H10 = (H5 - 1) / 2 + 1;243 //10W地形图对应的列号

244 int L10 = (L5 - 1) / 2 + 1;245

246 Integer X2 = (row_number - 2 * H5 + 1) * 2 + (col_number - 2 * L5 + 1) + 1;247 Integer X5 = (H5 - 2 * H10 + 1) * 2 + (L5 -2 * L10 + 1) + 1;248 Integer X10 = (H10 - 1) * 12 + (L10 - 1) + 1;249

250 return String.format("%s-%s-%s-%s-%s", bigRowNo, bigColNo, X10, toUpperCode(X5), X2);251 }252 //图幅号格式不正确

253 return "";254 }255 }256 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值