CesiumForUnreal代理加载XYZ瓦片服务

天地图代理的实现效果

参考资料

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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容