下载墨卡托坐标天地图方法

一、使用idea新建一个项目:

二、配置依赖:

<dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.14</version>
  </dependency>

三、创建下载类:

package org.tianditu;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TianDiTuDownload {
    /**
     *  墨卡托坐标地形图
     *  vec_w - 电子地图;
     *  img_w - 影像地图;
     *  ter_w - 地形地图;
     *  cva_w - 地图标注
     * */
    // 电子地图
    public static String vec_w = "http://{server}.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk={tk}";
    // 影像地图
    public static String img_w = "http://{server}.tianditu.gov.cn/img_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk={tk}";
    // 地形地图
    public static String ter_w = "http://{server}.tianditu.gov.cn/ter_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk={tk}";
    // 地图标注
    public static String cva_w = "http://{server}.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk={tk}";




    public static String[] servers = {"t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"};

    public static void main(String[] args) {
        // 下载地址
        String basePath = "D:/tianditu";
        //注意天地图API访问次数限制
        //String tk = "cef191b507ff5cb6988***11ca0";
        String tk = "352d4b1313777a8643***6a28d17e5";
        // 要下载的图层
        String[] urlArr = {cva_w};
        int minZoom = 1;  //下载开始层级
        int maxZoom = 9;  //下载结束层级
        //坐标祖国
        double startLat = 53.58;//开始纬度(从北到南)
        double endLat = 2.7;//结束纬度(从北到南)
        double startLon = 73.2;//开始经度(从西到东)
        double endLon = 135.15;//结束经度(从西到东)

        ExecutorService exe = Executors.newFixedThreadPool(6);
        //等经纬度第一层是1x2,纬度数量是2^0,经度数量是2^1
        //墨卡托投影第一层是2x2,纬度数量是2^1,经度数量是2^1
        for (int i = 0; i < urlArr.length; i++) {
            String url = urlArr[i].replace("{tk}", tk);
            System.out.println(url);
            String layerName = url.split("tianditu.gov.cn/")[1].split("/wmts?")[0];
            for (int z = minZoom; z <= maxZoom; z++) {
                double deg = 360.0 / Math.pow(2, z) / 256;//一个像素点代表多少度
                int startX = (int) ((startLon + 180) / deg / 256);
                int endX = (int) ((endLon + 180) / deg / 256);
                int startY = (((int) Math.pow(2, z) * 256 / 2) - (int) ((Math.log(Math.tan((90 + startLat) * Math.PI / 360)) / (Math.PI / 180)) / (360 / Math.pow(2, z) / 256) + 0.5)) / 256;
                int endY = (((int) Math.pow(2, z) * 256 / 2) - (int) ((Math.log(Math.tan((90 + endLat) * Math.PI / 360)) / (Math.PI / 180)) / (360 / Math.pow(2, z) / 256) + 0.5)) / 256;
                for (int x = startX; x <= endX; x++) {
                    for (int y = startY; y <= endY; y++) {
                        final String newUrl = url.replace("{server}", servers[(int) (Math.random() * servers.length)]).replace("{z}", z + "").replace("{x}", x + "").replace("{y}", y + "");
                        //System.out.println(newUrl);
                        final String filePath;
                        if("vec_w".equals(layerName)){
                            filePath = basePath + "/" + layerName + "/" + z + "/" + x + "/" + y + ".png";
                        }else {
                            filePath = basePath + "/" + layerName + "/" + z + "/" + x + "/" + y + ".jpg";
                        }

                        exe.execute(new Runnable() {
                            @Override
                            public void run() {
                                File file = new File(filePath);
                                if (!file.exists()) {
                                    if (!file.getParentFile().exists()) {
                                        file.getParentFile().mkdirs();
                                    }
                                    boolean loop = true;
                                    int count = 0;
                                    while (loop && count < 5) {//下载出错进行重试,最多5次
                                        count++;
                                        try {
                                            InputStream in = getFileInputStream(newUrl);
                                            OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
                                            byte[] b = new byte[8192];
                                            int len = 0;
                                            while ((len = in.read(b)) > -1) {
                                                out.write(b, 0, len);
                                                out.flush();
                                            }
                                            out.close();
                                            in.close();
                                            loop = false;
                                        } catch (Exception e) {
                                            loop = true;
                                        }
                                    }
                                    if (loop) {
                                        System.out.println("下载失败:" + newUrl);
                                    }
                                }
                            }
                        });
                    }
                }
            }
        }

        exe.shutdown();
        while (true) {
        try {
            Thread.sleep(1000L);//主线程休眠1秒,等待线程池运行结束,同时避免一直死循环造成CPU浪费
        } catch (InterruptedException e) {
        }
        if (exe.isTerminated()) {//线程池所有线程都结束运行
            break;
        }
    }
}

    //获取文件下载流
    public static InputStream getFileInputStream(String url) throws Exception {
        InputStream is = null;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet request = new HttpGet(url);
        request.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
        HttpResponse response = httpclient.execute(request);
        response.setHeader("Content-Type", "application/octet-stream");
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        }
        return is;
    }

}

 四、使用:

(*.png格式,uniapp离线加载所用)

如土所示表示下载完成

注意:级别越高下载的数据越慢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值