简易版谷歌系瓦片下载(天地图为例)

问题来源

项目需要瓦片作为离线底图,所以便写个简易的方法。大神们可以在这个基础上改进。

代码

package tile;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class test {

	/**
     * 远程文件下载
     * @param url 下载地址
     * @param file 保存文件地址
     */
    public static boolean download(URL url, File file) throws IOException {
        boolean flag = true;
        DataOutputStream dos = null;
        DataInputStream dis = null;
        try {
            if(!file.getParentFile().exists()) file.getParentFile().mkdirs();
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            dos = new DataOutputStream(new FileOutputStream(file));
            dis = new DataInputStream(conn.getInputStream());
            byte[] data = new byte[2048];
            int i = 0;
            while ((i = dis.read(data)) != -1) {
                dos.write(data, 0, i);
            }
            dos.flush();
        } catch (IOException e) {
            flag = false;
            throw e;
        } finally {
            if(dis != null) dis.close();
            if(dos != null) dos.close();
        }
        return flag;
    }

    /**
     * 计算分辨率
     * @param maxLevel 最大级别
     */
    public static double[] getResolutions(int maxLevel){
    	double max = 360.0/256.0;
    	double[] resolutions = new double[maxLevel+1];
    	for(int z=0;z<=maxLevel;z++) resolutions[z] = max/Math.pow(2, z);
    	return resolutions;
    }
    
	public static void main(String[] arg) throws IOException{
    	double[] re = getResolutions(18);
    	double[] extent = {106.54,21.59,108.26,23.20};
    	
    	for(int z=0;z<18;z++){
    	    int totalX = (int)Math.ceil(360.0/(re[z]*256.0)); //列数向上取整
    		int totalY = (int)Math.ceil(180.0/(re[z]*256.0)); //行数向上取整 */	
    		
    		//起始结束列
            int sX = (int)Math.floor(((extent[0] + 180) / 360) * totalX);
            int eX = (int)Math.floor(((extent[2] + 180) / 360) * totalX);

            //起始结束行
            int sY = (int)Math.floor(((90 - extent[3]) / 180) * totalY);
            int eY = (int)Math.floor(((90 - extent[1]) / 180) * totalY);
    		
    		for(int y=sY;y<=eY;y++){
    			for(int x=sX;x<=eX;x++){
    				String urlstr = "http://t3.tianditu.com/DataServer?T=cva_c&x="+x+"&y="+y+"&l="+z; //天地图服务器t0-t8间选一个
    				System.out.println(urlstr);
    				String path = "D:/打包/"+z+File.separator+y+File.separator+x+".png";
    				File file = new File(path);
    				URL url = new URL(urlstr);
    				download(url,file);
    			}
    		}
    		
    		
    	}
	}
}

注意

所谓谷歌系,指的是原点在左上角,满足长宽比1:2的地图。若是如OSM等正方形,请自行改变。

转载自:https://blog.csdn.net/u013323965/article/details/53154046

You may also like...