实际加载效果如图:
1、下载离线地图瓦片方式(多种任选其一,个人倾向于Qgis工具):
方式1、采用第三方下载工具如:91卫图、水经注、全能电子地图下载器、bigemap等等。(这些有的下载层级不够,有的需要付费,结合个人情况选择)
方式2、爬虫技术实现,网上贴的相关代码如:Java为例
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;public class map{//矢量(行政) - 等经纬度public static String vec_c = "http://{server}.tianditu.gov.cn/vec_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=c&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk={tk}";//矢量(行政) - 墨卡托public static String vec_w = "http://{server}.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk={tk}";//矢量注记(行政) - 等经纬度public static String cva_c = "http://{server}.tianditu.gov.cn/cva_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=c&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=cva&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk={tk}";//影像 - 等经纬度public static String img_c = "http://{server}.tianditu.gov.cn/img_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=c&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 cia_c = "http://{server}.tianditu.gov.cn/cia_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cia&STYLE=default&TILEMATRIXSET=c&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk={tk}";//影像注记 - 墨卡托public static String cia_w = "http://{server}.tianditu.gov.cn/cia_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cia&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk={tk}";//地形 - 等经纬度public static String ter_c = "http://{server}.tianditu.gov.cn/ter_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=ter&STYLE=default&TILEMATRIXSET=c&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=ter&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk={tk}";//地形注记 - 等经纬度public static String cta_c = "http://{server}.tianditu.gov.cn/cta_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cta&STYLE=default&TILEMATRIXSET=c&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk={tk}";//地形注记 - 墨卡托public static String cta_w = "http://{server}.tianditu.gov.cn/cta_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cta&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 = "E:/天地图";//这里放你的天地图开发者秘钥,注意天地图API访问次数限制String tk = "75f0434f240669f4a2df635927XXXXXXX";String[] urlArr = {img_w};//要下载的图层int minZoom = 17;int maxZoom = 18;double startLat = 34.29;//开始纬度(从北到南)double endLat = 33.67;//结束纬度(从北到南)double startLon = 107.99;//开始经度(从西到东)double endLon = 109.14;//结束经度(从西到东)ExecutorService exe = Executors.newFixedThreadPool(6);//等经纬度第一层是1x2,纬度数量是2^0,经度数量是2^1//墨卡托投影第一层是2x2,纬度数量是2^1,经度数量是2^1for(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];System.out.println(layerName);if(layerName.endsWith("c")){//等经纬度for(int z=minZoom; z<=maxZoom; z++){if(z > 17){//天地图10级以上只有中国区域startLat = startLat > 34.29 ? 34.29 : startLat;//开始纬度(从北到南)endLat = endLat < 33.67 ? 33.67 : endLat;//结束纬度(从北到南)startLon = startLon < 107.99 ? 107.99 : startLon;//开始经度(从西到东)endLon = endLon > 109.14 ? 109.14 : endLon;//结束经度(从西到东)}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)((90 - startLat) / deg / 256);int endY = (int)((90 - endLat) / deg / 256);for(int y=startY; y<=endY; y++){for(int x=startX; x<=endX; x++){final String newUrl = url.replace("{server}", servers[(int) (Math.random()*servers.length)]).replace("{z}", z+"").replace("{x}", x+"").replace("{y}", y+"");final String filePath = basePath + "/" + layerName + "/" + z + "/" + x + "/" + y + ".png";exe.execute(new Runnable() {@Overridepublic 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);}}}});}}}}else{//墨卡托/*** earthRadius = 6378137;//地球赤道半径6378137米* aa = earthRadius * (Math.PI - 0);//赤道周长的一半,20037508.3427892* 85.05112877980659 = (Math.atan(Math.exp(aa / earthRadius))-Math.PI/4)*2 * 180 / Math.PI;//墨卡托最大有效纬度*/if(startLat > 85.051128){System.out.println("墨卡托投影起始纬度最大为:85.051128,下载区域将被限定。");startLat = 85.051128;}if(endLat <- 85.051128){System.out.println("墨卡托投影终止纬度最小为:-85.051128,下载区域将被限定。");endLat = -85.051128;}for(int z=minZoom; z<=maxZoom; z++){if(z > 17){//天地图10级以上只有中国区域startLat = startLat > 34.29 ? 34.29 : startLat;//开始纬度(从北到南)endLat = endLat < 33.67 ? 33.67 : endLat;//结束纬度(从北到南)startLon = startLon < 107.99 ? 107.99 : startLon;//开始经度(从西到东)endLon = endLon > 109.14 ? 109.14 : endLon;//结束经度(从西到东)}double deg = 360.0 / Math.pow(2, z) / 256;//一个像素点代表多少度int startX = (int)((startLon + 180) / deg / 256);int endX = (int)((endLon + 180) / deg / 256);/*** 这里是基于等经纬度坐标转墨卡托坐标的公式得到的算法。()* 先算出当前地图级别下纬度方向用该有多少个像素点(一个瓦片是256个点,一共有Math.pow(2, z)个瓦片)* 再算出指定纬度坐标与赤道的距离(北半球为正南半球为负)(这里抵消了带入赤道周长的计算,因为它是一个固定值,把赤道周长定为1就抵消了):Math.log(Math.tan((90 + startLat) * Math.PI / 360)) / (Math.PI / 180)* 就可以得到纬度坐标距赤道有多少个点:(Math.log(Math.tan((90 + startLat) * Math.PI / 360)) / (Math.PI / 180)) / (360/Math.pow(2, z)/256) + 0.5)* 然后就可以计算出其瓦片坐标。*/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 y=startY; y<=endY; y++){//加入判断是等经纬度还是墨卡托for(int x=startX; x<=endX; x++){final String newUrl = url.replace("{server}", servers[(int) (Math.random()*servers.length)]).replace("{z}", z+"").replace("{y}", y+"").replace("{x}", x+"");//System.out.println(newUrl);final String filePath = basePath + "/" + layerName + "/" + z + "/" + y + "/" + x + ".png";exe.execute(new Runnable() {@Overridepublic 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;}}
这个亲测确实能够下载,但是存在下载的瓦片出现nginx发布后,引用实际cesium贴图位置错乱。应该是生成的XYZ目录存在一些问题。没有细究。
方式3、通过QGIS下载(极力推荐)
QGIS下载地址:Download QGIS
操作界面切换到中文界面如下:
自定义下载瓦片区域,如下图第4步,会出现下拉选择列表,自行选择即可
层级可以批处理分层下载结果如下图。
注:根目录是层级Z,下一级目录是X,下下一级目录.png是Y
2、Nginx发布离线瓦片数据
nginx配置nginx.conf
server {listen 9006;server_name 127.0.0.1;# 这里需要写相对路径 绝对路径会报错404root ../tianmap;autoindex on;location / {# 支持跨域add_header Access-Control-Allow-Origin *;add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';}# 不存缓存 每次重新验证add_header Cache-Control "no-cache,must-revalidate";}
发布访问结果如下:
3、cesium工程加载离线影像瓦片
var MapModel = new Cesium.UrlTemplateImageryProvider({url:"http://127.0.0.1:9006/{z}/{x}/{y}.png",tilingScheme: new Cesium.WebMercatorTilingScheme(),fileExtension: 'png',minimumLevel: 1,maximumLevel: 18});viewer.imageryLayers.addImageryProvider(MapModel);
到此结束