天地图影像底图之墨卡托投影下载
提示:仅适合墨卡托,你可以先看效果
前言
要求在电脑离线状态下使用天地图,那么你就不得不下载天地图的瓦片数据了。
一、下载需要
- 天地图Token(
提示:个人和企业区别很大
) - IDEA工具
二、下载步骤
1.直接完整代码,里面有注释
代码如下(示例):
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
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;
/**
* 天地图瓦片下载
*
* @date 2023.3.10
*/
public class TianDiTuDownload {
//影像 - 墨卡托
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[] servers = {"t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"};
public static void main(String[] args) {
String basePath = "D:/tianditu";
//注意天地图API访问次数限制
String tk = "98ae942bdcf11b93668******e48affb6";
String[] urlArr = {img_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 = basePath + "/" + layerName + "/" + z + "/" + x + "/" + y + ".png";
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;
}
}
三、效果
(示例)1:层级
(示例)2:ROW
(示例)3:COL
(示例)4:最终效果展示
我这个加了标记,你们应该是没有的
总结
希望可以帮到大家,有错误望指正,共同进步。