map.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const fs = require('fs');
  2. const path = require('path');
  3. const basieTilesPath = "F:/";
  4. //获取气象层级
  5. exports.getMap = async (req, res) => {
  6. const { z, x, y } = req.params;
  7. // 支持多种图片格式,优先尝试jpg
  8. // const tilePaths = [
  9. // path.join('F:', (z > 10 ? '地图瓦片/河南-安阳-滑县11-18级_瓦片:谷歌/tiles/' : '地图瓦片全球1-10级_瓦片:谷歌/tiles/'), z, x, `${y}.jpg`),
  10. // path.join('F:', (z > 10 ? '地图瓦片/河南-安阳-滑县11-18级_瓦片:谷歌/tiles/' : '地图瓦片全球1-10级_瓦片:谷歌/tiles/'), z, x, `${y}.png`),
  11. // path.join('F:', (z > 10 ? '地图瓦片/河南-安阳-滑县11-18级_瓦片:谷歌/tiles/' : '地图瓦片全球1-10级_瓦片:谷歌/tiles/'), z, x, `${y}.webp`)
  12. // ];
  13. const path = z > 10 ? `${basieTilesPath}地图瓦片/河南-安阳-滑县11-18级_瓦片:谷歌/tiles/${z}/${x}/${y}` : `${basieTilesPath}地图瓦片/全球1-10级_瓦片:谷歌/tiles/${z}/${x}/${y}`;
  14. const tilePaths = [
  15. `${path}.jpg`,
  16. `${path}.png`,
  17. `${path}.webp`,
  18. ];
  19. // 查找存在的瓦片文件
  20. let foundTile = null;
  21. for (const tilePath of tilePaths) {
  22. if (fs.existsSync(tilePath)) {
  23. foundTile = tilePath;
  24. break;
  25. }
  26. }
  27. if (foundTile) {
  28. // 设置缓存头
  29. res.setHeader('Cache-Control', 'public, max-age=604800'); // 缓存一周
  30. res.setHeader('Expires', new Date(Date.now() + 604800000).toUTCString());
  31. // 发送文件
  32. res.sendFile(foundTile);
  33. } else {
  34. res.status(404).send('Tile not found');
  35. }
  36. }