美文网首页
CesiumForUnreal代理加载XYZ瓦片服务

CesiumForUnreal代理加载XYZ瓦片服务

作者: YGIS | 来源:发表于2023-07-16 17:44 被阅读0次
天地图代理的实现效果

参考资料

https://cesium.com/learn/unreal/unreal-datasets/#loading-assets-from-a-local-server

提出问题

由于CesiumForUnreal中采用TileMapServiceRasterOverlay加载TMS格式的栅格底图,现有项目多使用XYZ瓦片,为实现兼容,需要将Tile Map service(TMS)请求转换为XYZ瓦片。

实现步骤

Service

// app/service/tilemap.js
const Service = require('egg').Service;

class TilemapService extends Service {
  async getTilemapresource() {
    const { ctx } = this;
    try {
      const cache = await this.read(this.config.tilemapresource);
      ctx.body = cache;
    } catch (error) {
      console.error(error);
      ctx.status = 500;
      ctx.body = 'Internal server error';
    }
  }

  async getTile() {
    const { ctx } = this;
    const { z, x, y } = ctx.params;

    if (z === undefined) {
      return this.getTilemapresource();
    }

    const realY = Math.pow(2, z) - y - 1;
    try {
      const response = await ctx.curl(
        // Replace the URL with the actual TMS tile server URL
        `https://t${sub}.tianditu.gov.cn/DataServer?T=img_w&tk=${tk}&x={x}&y={y}&l={z}`,
        {
          method: ctx.method,
          headers: {
            'User-Agent':
              'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
          },
          data: ctx.request.body,
        }
      );

      ctx.set(response.headers);
      ctx.body = response.data;
    } catch (error) {
      console.error(error);
      ctx.status = 500;
      ctx.body = 'Internal server error';
    }
  }
}

module.exports = TilemapService;

Controller

// app/controller/tilemap.js
const Controller = require('egg').Controller;

class TilemapController extends Controller {
  async tilemapresource() {
    await this.ctx.service.tilemap.getTilemapresource();
  }

  async getTile() {
    await this.ctx.service.tilemap.getTile();
  }
}

module.exports = TilemapController;

Route

// app/router.js
module.exports = app => {
  const { router, controller } = app;
  router.get('/tms/tilemapresource', controller.tilemap.tilemapresource);
  router.get('/tms/:z/:x/:y', controller.tilemap.getTile);
};

启动

$ npm run dev

相关文章

网友评论

      本文标题:CesiumForUnreal代理加载XYZ瓦片服务

      本文链接:https://www.haomeiwen.com/subject/qldwudtx.html