tile.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { Tile } from "ol/layer";
  2. import TileWMTS from "ol/tilegrid/WMTS";
  3. import { WMTS } from "ol/source";
  4. import { Projection, get as getProjection, getTransform } from "ol/proj";
  5. import { register } from "ol/proj/proj4";
  6. import proj4 from "proj4";
  7. import { applyTransform, getTopLeft, getWidth } from "ol/extent";
  8. // 注册cgcs2000坐标转换器
  9. proj4.defs(
  10. "EPSG:4490",
  11. 'GEOGCS["China Geodetic Coordinate System 2000",DATUM["China_2000",SPHEROID["CGCS2000",6378137,298.257222101,AUTHORITY["EPSG","1024"]],AUTHORITY["EPSG","1043"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4490"]]'
  12. );
  13. register(proj4);
  14. const getTileWMTSTileGrid = (from: Projection, to: Projection) => {
  15. const projectionExtent = to.getExtent();
  16. const origin = projectionExtent ? getTopLeft(projectionExtent) : [-180, 90];
  17. const fromLonLat = getTransform(from, to);
  18. const width = projectionExtent
  19. ? getWidth(projectionExtent)
  20. : getWidth(applyTransform([-180.0, -90.0, 180.0, 90.0], fromLonLat));
  21. const resolutions = [];
  22. const matrixIds = [];
  23. for (let z = 1; z < 19; z++) {
  24. resolutions[z] = width / (256 * Math.pow(2, z));
  25. matrixIds[z] = z.toString();
  26. }
  27. return new TileWMTS({
  28. origin: origin,
  29. resolutions: resolutions,
  30. matrixIds: matrixIds,
  31. });
  32. };
  33. const tileTMaps = {
  34. 全球境界: "ibo",
  35. 地形注记: "cta",
  36. 地形晕渲: "ter",
  37. 影像注记: "cia",
  38. 影像底图: "img",
  39. 矢量注记: "cva",
  40. 矢量底图: "vec",
  41. };
  42. const wmtsProjection = getProjection("EPSG:4490")!;
  43. const tileGridCache: { [key in string]: TileWMTS } = {};
  44. const typeWMTSCacne: { [key in string]: WMTS } = {};
  45. const getWMTS = (type: TileType, mapEpsg: string) => {
  46. if (typeWMTSCacne[mapEpsg + type]) {
  47. return typeWMTSCacne[mapEpsg + type];
  48. }
  49. const wmtsTileGrid =
  50. mapEpsg in tileGridCache
  51. ? tileGridCache[mapEpsg]
  52. : getTileWMTSTileGrid(getProjection(mapEpsg)!, wmtsProjection);
  53. const layer = tileTMaps[type];
  54. const key = "69167db5c31974a619fe60f0c4cd21b5";
  55. const url = `https://t0.tianditu.gov.cn/${layer}_c/wmts?tk=${key}`;
  56. return new WMTS({
  57. url,
  58. layer,
  59. version: "1.0.0",
  60. matrixSet: "c",
  61. format: "tiles",
  62. projection: wmtsProjection,
  63. requestEncoding: "KVP",
  64. style: "default",
  65. tileGrid: wmtsTileGrid,
  66. });
  67. };
  68. export type TileType = keyof typeof tileTMaps;
  69. export const baseTileLayer = new Tile();
  70. export const geoTileLayer = new Tile({
  71. source: getWMTS("矢量注记", "EPSG:4326"),
  72. });
  73. export const setBaseTileType = (type: TileType) => {
  74. baseTileLayer.setSource(getWMTS(type, "EPSG:4326"));
  75. };