//4个数组为从1-18级逐级对应,如发现转换对应不上可以逐级自己修改对应的X,Y编号,此套编码对应四川甘孜州地区.
离线地图说明请参考: http://blog.csdn.net/leiyong0326/article/details/42527991
package com.ly.baidu.tools;
import java.io.File;
public class GoogleToBaidu {
static int[] baiduX = { 0, 0, 1, 3, 6, 12, 24, 49, 98, 197, 395, 790, 1581,
3163, 6327, 12654, 25308, 50617 };
static int[] baiduY = { 0, 0, 0, 1, 2, 4, 9, 18, 36, 73, 147, 294, 589,
1178, 2356, 4712, 9425, 18851 };
static int[] googleX = { 0, 1, 3, 7, 13, 26, 52, 106, 212, 425, 851, 1702,
3405, 6811, 13623, 27246, 54492, 107917 };
static int[] googleY = { 0, 0, 1, 2, 5, 12, 23, 47, 95, 190, 380, 761,
1522, 3045, 6091, 12183, 24366, 47261 };
public static void main(String[] args) {
for (int zoom = 1; zoom < 18; zoom++) {
//底片瓦片转换
File dir = new File("satellite"+File.separator + (zoom - 1));
execute(dir,zoom);
//路段层瓦片转换
File dir2 = new File("overlay"+File.separator + (zoom - 1));
execute(dir2,zoom);
System.out.println("current execute zoom:"+zoom);
}
}
/**
* 转换瓦片
* @param dir
* @param zoom
*/
private static void execute(File dir, int zoom){
if (dir.isDirectory()) {
File[] xfs = dir.listFiles();
for (int i = 0; i < xfs.length; i++) {
File xf = xfs[i];
if (xf.isDirectory()) {
File[] yfs = xf.listFiles();//Y轴为瓦片文件,先替换文件再替换文件夹
for (int j = 0; j < yfs.length; j++) {
File yf = yfs[j];
String yName = yf.getName();
String path = yf.getParent();
String fileType = yName.replaceAll("\\d", "");
if (yf.isFile()
&& yName.matches("^\\d+\\.(png|jpg){1}$")) {//只有png和jpg后缀的文件才替换
int newY = googleToBaiduY(Integer.valueOf(yName
.replaceAll("\\D", "")), zoom);
File yNew = new File(path + File.separator
+ newY + fileType);
yf.renameTo(yNew);
// System.out.println(yName + "to"
// + yNew.getName());
}
}
int nX = googleToBaiduX(Integer.valueOf(xf.getName()), zoom);
File newX = new File(xf.getParent() + File.separator
+ nX);
xf.renameTo(newX);//X轴为文件夹
// System.out.println(xf.getName()+"to"+newX.getName());
}
}
}
}
private static int googleToBaiduX(int x, int z) {
int b = baiduX[z - 1];// 395
int g = googleX[z - 1];// 11:843,12:1685
// int gx = g + (x-b);// --- 1587+
int gx = x - g + b;// --- 1587+
// 谷歌瓦片行编号=[谷歌参照瓦片行编号+(百度行编号 – 百度参照瓦片行编号)]
return gx;
}
private static int googleToBaiduY(int y, int z) {
int b = baiduY[z - 1];// 147
int g = googleY[z - 1];// 10:
// int gy = g - (y-b);//
int gy = g + b - y;//
// 谷歌瓦片列编号=[谷歌参照瓦片列编号- (百度列编号 – 百度参照瓦片列编号)] //向上,列为递减
return gy;
}
}