admin 管理员组

文章数量: 887007

天地图使用vue+node实现离线瓦片下载

记录天地图使用vue+node实现离线瓦片下载
思路解析
配置package.json直接复制即可
openleary中加载
思路解析
一、准备好webstorm,将以下内容复制进去

// An highlighted block
var Bagpipe = require('bagpipe')
var fs = require("fs");
var request = require("request");
var bou = [121.648978, 40.867574, 131.325647,46.311072];//下载范围var Minlevel = 2;//最小层级
var Maxlevel = 14;//最大层级
var token = '901339dbb203e65286a9b9702278dbd8';//天地图key(如果失效去天地图官网申请)
var zpath = './text'; // 瓦片目录
var speed = 100;//并发数
var mapstyle = 'vec_w';//地图类型(img_w:影像底图 cia_w:影像标注 vec_w:街道底图 cva_w街道标注,备注,自己再api找相对于的)var all = [];
var user_agent_list_2 = ["Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 OPR/26.0.1656.60","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0","Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36","Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11","Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36","Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.11 TaoBrowser/2.0 Safari/536.11","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; LBBROWSER)","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E; LBBROWSER)","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)","Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 SE 2.X MetaSr 1.0","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SV1; QQDownload 732; .NET4.0C; .NET4.0E; SE 2.X MetaSr 1.0)","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.3.4000 Chrome/30.0.1599.101 Safari/537.36","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 UBrowser/4.0.3214.0 Safari/537.36"
]/*** 计算经纬度转换成瓦片坐标* @param {Number} lng 经度 * @param {Number} lat 纬度* @param {Number} level 层级 */
function calcXY(lng, lat, level) {let x = (lng + 180) / 360let title_X = Math.floor(x * Math.pow(2, level))let lat_rad = lat * Math.PI / 180let y = (1 - Math.log(Math.tan(lat_rad) + 1 / Math.cos(lat_rad)) / Math.PI) / 2let title_Y = Math.floor(y * Math.pow(2, level))return { title_X, title_Y }
}
/*** 计算每个层级的瓦片坐标* @param {Arr} bounding 范围* @param {Number} Minlevel 最小层级* @param {Number} Maxlevel 最大层级*/
function mainnAllXY(bounding, Minlevel, Maxlevel) {for (i = Minlevel; i <= Maxlevel; i++) {alli = {}let p1 = calcXY(bounding[2], bounding[3], i);let p2 = calcXY(bounding[0], bounding[1], i);alli.t = i // 层级alli.x = [p2.title_X, p1.title_X] // 瓦片横坐标范围(左至右)alli.y = [p1.title_Y, p2.title_Y] // 瓦片纵坐标范围(下至上)all.push(alli)}createDir()
}
mainnAllXY(bou, Minlevel, Maxlevel)function createDir() {fs.access(zpath, fs.constants.F_OK, err => {// 创建tiles文件夹if (err) fs.mkdir(zpath, err => { })for (let z = 0; z <= all.length - 1; z++) {fs.access(`${zpath}/${all[z].t}`, fs.constants.F_OK, err => {// 创建tiles/Z文件夹 ,Z是层级if (err) fs.mkdir(`${zpath}/${all[z].t}`, err => { })for (let x = all[z].x[0]; x <= all[z].x[1]; x++) {fs.access(`${zpath}/${all[z].t}/${x}`, fs.constants.F_OK, err => {// 创建tiles/Z/X文件夹 ,X是瓦片横坐标if (err) fs.mkdir(`${zpath}/${all[z].t}/${x}`, err => { })})}})}// 文件夹可能较多,等待2s开始下载setTimeout(() => {task()}, 2000)})
}/*** 创建下载队列*/var sum = 0;
var bag = new Bagpipe(speed, { timeout: 1000 })
function task() {for (let z = 0; z <= all.length - 1; z++) {for (let x = all[z].x[0]; x <= all[z].x[1]; x++) {for (let y = all[z].y[0]; y <= all[z].y[1]; y++) {// 将下载任务推入队列++sumbag.push(download, x, y, all[z].t)}}}
}/*** 下载图片方法* @param {Number} x * @param {Number} y * @param {Number} z */
function download(x, y, z) {var ts = Math.floor(Math.random() * 8)//随机生成0-7台服务器let imgurl = `http://t${ts}.tianditu.gov/DataServer?T=${mapstyle}&x=${x}&y=${y}&l=${z}&tk=${token}`;var ip = Math.floor(Math.random() * 256)//随机生成IP迷惑服务器+ "." + Math.floor(Math.random() * 256)+ "." + Math.floor(Math.random() * 256)+ "." + Math.floor(Math.random() * 256)var v = Math.floor(Math.random() * 9)var options = {method: 'GET',url: imgurl,headers: {'User-Agent': user_agent_list_2[v],'X-Forwarded-For': ip,"Connection": 'keep-alive'},timeout: 5000,forever: true};request(options, (err, res, body) => {if (err) {bag.push(download, x, y, z)console.log("request错误", err)}}).pipe(fs.createWriteStream(`${zpath}/${z}/${x}/${y}.png`).on('finish', () => {console.log(`图片下载成功,第${z}层`)console.log(--sum)}).on('error', (err) => {console.log('发生异常:', err);}));
}

配置package.json直接复制即可

// An highlighted block
{"name": "mapdownload","version": "1.0.0","main": "index.js","license": "MIT","dependencies": {"bagpipe": "^0.3.5","request": "^2.88.0","tile-lnglat-transform": "^1.3.2"},"scripts": {"start": "node ./src/index"}
}

强调文本 强调文本

安装依赖后运行

openleary中加载

               let vm = this;vm.tianditulayer = new TileLayer({ //天地图线画图// source: new XYZ({//     url://         "=901339dbb203e65286a9b9702278dbd8" + "&SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=c&FORMAT=tiles" + "&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}",//     projection: 'EPSG:4326',// })source: new XYZ({tileUrlFunction: function (coordinate) {console.log(coordinate[0], coordinate[1], coordinate[2]);var z = coordinate[0];var x = coordinate[1];var y = Math.pow(2, z - 1) + coordinate[2];return "/MapDownload-master/text/" + z + "/" + x + "/" + y + ".png";},// url: "=901339dbb203e65286a9b9702278dbd8" + "&SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=c&FORMAT=tiles" + "&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}",projection: 'EPSG:4326',})});vm.tianditulabel = new TileLayer({ //天地图注记图source: new XYZ({tileUrlFunction: function (coordinate) {console.log(coordinate[0], coordinate[1], coordinate[2]);var z = coordinate[0];var x = coordinate[1];var y = Math.pow(2, z - 1) + coordinate[2];return "/MapDownload-master/tiles/" + z + "/" + x + "/" + y + ".png";},// url: "=bb3927811976d4d451ed2159271b76ba" + "&SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=c&FORMAT=tiles" + "&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}",projection: 'EPSG:4326'})// source: new XYZ({//     url://         "=bb3927811976d4d451ed2159271b76ba" + "&SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=c&FORMAT=tiles" + "&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}",//     projection: 'EPSG:4326'// })});

注意:有些不识别本地盘符地址,请用本地电脑发布服务,或者用服务器发布

本文标签: 天地图使用vuenode实现离线瓦片下载