UNPKG

geotile_sdk

Version:

A tile sdk for Cloud Optimised Geotiff and shapefile

78 lines (71 loc) 2.46 kB
/* * @Description: * @Author: luojun1 * @Date: 2021-10-21 17:25:12 * @LastEditTime: 2021-12-15 15:36:20 */ var fs = require('fs') var path = require('path') var geotileSDK = require('../../src') module.exports = async (req, res) => { // fs.appendFile(__dirname + '/url.txt', 'curl -X GET "http://localhost:5000' + req.path + '?&OP=0"\n', function () { // }) // fs.appendFile(__dirname + '/urlOP.txt', 'curl -X GET "http://localhost:5000' + req.path + '"\n', function () { // }) // 返回结果 var start = new Date().getTime() const OP = req.query.OP ? req.query.OP : 0 const CachePath = req.query.Cache ? req.query.Cache : '' const format = req.path.split('?')[0].substring(req.path.split('?')[0].lastIndexOf('.') + 1) console.log('#####format: ', format) if (OP === 1 && format === 'png') { geotileSDK.fetchTileOptimized(req.params.x, req.params.y, req.params.z, req.params.id, (err, buffer) => { // res.contentType('image/png') if (err) { res.send(buffer) console.log('#####error:', err) } else { res.send(buffer) // var processTime = new Date().getTime() // console.log('###fetchTileOptimized Time:', processTime - start) } }) } else { geotileSDK.fetchTile(req.params.x, req.params.y, req.params.z, req.params.id, format, (err, buffer) => { if (format === 'png') { res.contentType('image/png') } else { // res.contentType('mapbox') res.setHeader('Content-Encoding', 'deflate') res.setHeader('Content-Type', 'application/x-protobuf') } if (err) { res.send(buffer) console.log('#####error:', err) } else { res.send(buffer) var processTime = new Date().getTime() console.log('###fetchTile Time,', processTime - start) // disk cache if (CachePath !== '') { let dir = path.join(CachePath, req.params.id, req.params.z, req.params.x) mkdirs(dir, () => { fs.writeFile(path.join(CachePath, req.params.id, req.params.z, req.params.x, req.params.y + '.' + format), buffer, () => {}) }) } } }) } } // 递归创建目录 异步方法 function mkdirs (dirname, callback) { fs.access(dirname, fs.constants.F_OK, (err) => { if (err) { mkdirs(path.dirname(dirname), function () { fs.mkdir(dirname, callback) }) } else { callback() } }) }