Forráskód Böngészése

瓦片接口上传

Koishi 6 hónapja
szülő
commit
f06823004a
4 módosított fájl, 59 hozzáadás és 6 törlés
  1. 4 4
      app.js
  2. 2 2
      bin/www
  3. 45 0
      router_handler/tiles/map.js
  4. 8 0
      routes/tiles/map.js

+ 4 - 4
app.js

@@ -70,12 +70,12 @@ app.use('/api', test);
 const getGlobalWeatherData = require('./routes/grib2/getGlobalWeatherData.js');
 app.use('/weather', getGlobalWeatherData);
 
-//天气
-const weatherInchinaRounter = require('./routes/weather/inchina.js');
-app.use('/weather/inchina', weatherInchinaRounter);
-
 const tempapiRounter = require('./routes/weather/temperature.js');
 app.use('/weather/tempapi', tempapiRounter);
+
+const mapTiles = require('./routes/tiles/map.js');
+app.use('/tiles', mapTiles);
+
 //疫苗接种
 // const vaccinesRounter = require('./routes/vaccines/vaccine.js');
 // app.use('/my/vaccines', vaccinesRounter);

+ 2 - 2
bin/www

@@ -31,7 +31,7 @@ const { getGrib2FileUrl } = require("../schema/getGrb2FileUrl");
 
 async function initDownloadManager() {
   global.downloadManager = { manager: new grb2DownloadManager(), getGrib2FileUrl, downloadTimmer: null, downloadTimmerInterval: 3600 };
-  console.log("气象文件下载器已初始化完成...随项目初次启动默认执行一次下载任务...");
+  // console.log("气象文件下载器已初始化完成...随项目初次启动默认执行一次下载任务...");
 
   const downloader = global.downloadManager.manager;
 
@@ -109,7 +109,7 @@ function formatTime(seconds) {
 
 server.listen(port, () => {
   console.log("api server running at http://127.0.0.1:" + port)
-  initDownloadManager();
+  // initDownloadManager();
 });
 server.on('error', onError);
 server.on('listening', onListening);

+ 45 - 0
router_handler/tiles/map.js

@@ -0,0 +1,45 @@
+const fs = require('fs');
+const path = require('path');
+const basieTilesPath = "F:/";
+
+//获取气象层级
+exports.getMap = async (req, res) => {
+    const { z, x, y } = req.params;
+
+    // 支持多种图片格式,优先尝试jpg
+    // const tilePaths = [
+    //     path.join('F:', (z > 10 ? '地图瓦片/河南-安阳-滑县11-18级_瓦片:谷歌/tiles/' : '地图瓦片全球1-10级_瓦片:谷歌/tiles/'), z, x, `${y}.jpg`),
+    //     path.join('F:', (z > 10 ? '地图瓦片/河南-安阳-滑县11-18级_瓦片:谷歌/tiles/' : '地图瓦片全球1-10级_瓦片:谷歌/tiles/'), z, x, `${y}.png`),
+    //     path.join('F:', (z > 10 ? '地图瓦片/河南-安阳-滑县11-18级_瓦片:谷歌/tiles/' : '地图瓦片全球1-10级_瓦片:谷歌/tiles/'), z, x, `${y}.webp`)
+    // ];
+
+    const path = z > 10 ? `${basieTilesPath}地图瓦片/河南-安阳-滑县11-18级_瓦片:谷歌/tiles/${z}/${x}/${y}` : `${basieTilesPath}地图瓦片/全球1-10级_瓦片:谷歌/tiles/${z}/${x}/${y}`;
+    const tilePaths = [
+        `${path}.jpg`,
+        `${path}.png`,
+        `${path}.webp`,
+    ];
+
+    // 查找存在的瓦片文件
+    let foundTile = null;
+    for (const tilePath of tilePaths) {
+        console.log(1122, tilePath)
+        if (fs.existsSync(tilePath)) {
+            foundTile = tilePath;
+            break;
+        }
+    }
+
+
+    if (foundTile) {
+        // 设置缓存头
+        res.setHeader('Cache-Control', 'public, max-age=604800'); // 缓存一周
+        res.setHeader('Expires', new Date(Date.now() + 604800000).toUTCString());
+
+        // 发送文件
+        res.sendFile(foundTile);
+    } else {
+        res.status(404).send('Tile not found');
+    }
+    // res.rescc("获取气象层级成功", 200, lvArray);
+}

+ 8 - 0
routes/tiles/map.js

@@ -0,0 +1,8 @@
+const express = require("express");
+const router = express.Router();
+
+const mapTiles = require('../../router_handler/tiles/map.js');
+
+router.get('/map/:z/:x/:y', mapTiles.getMap);
+
+module.exports = router;