需求:将天地图底图切换为WMTS瓦片底图
材料:
- arcgis服务端wmts的mapserver服务
- arcgis for js 4.X
第一次操作:
方案构思:考虑到是wmts服务,首先使用BaseTileLayer
spatialReference选择4490
export const tdtlayer = async () => {
let BaseTileLayer = await arcgisPackage.BaseTileLayer;
let Request = await arcgisPackage.Request;
let TDT = await BaseTileLayer.createSubclass({
properties: {
urlTemplate: null,
},
spatialReference: config.centerPoint.spatialReference,
title: "天地图影像地图",
getTileUrl: function (level, row, col) {
let url ="https://192.168.***.***:6443/arcgis/rest/services/*******/ImageBaseMap/MapServer" +
col +
"&y=" +
row +
"&l=" +
level;
return url;
},
fetchTile: function (level, row, col) {
let url = this.getTileUrl(level, row, col);
return Request(url, {
responseType: "image",
}).then((response) => {
let image = response.data;
let width = this.tileInfo.size[0];
let height = this.tileInfo.size[0];
let canvas = document.createElement("canvas");
let context = canvas.getContext("2d");
canvas.width = width;
canvas.height = height;
context.drawImage(image, 0, 0, width, height);
return canvas;
});
},
});
let layer = await new TDT();
return layer;
};
结果:失败,始终得到3857投影。
第二次操作
构思方案:采用4.X常用的WebTileLayer
export const tdtJWDShiLianglayer = async () => {
let TileInfo = await arcgisPackage.TileInfo;
let WebTileLayer = await arcgisPackage.WebTileLayer;
var tileInfo = new TileInfo({
dpi: 90.71428571427429,//解析度,即像素
rows: 256,
cols: 256,
compressionQuality: 0,//压缩像素值
origin: {
x: -180,
y: 90
},
spatialReference: {
wkid: 4490
},
lods: [//定义平铺方案的细节级别数组
{ level: 2, levelValue: 2, resolution: 0.3515625, scale: 147748796.52937502 },
{ level: 3, levelValue: 3, resolution: 0.17578125, scale: 73874398.264687508 },
{ level: 4, levelValue: 4, resolution: 0.087890625, scale: 36937199.132343754 },
{ level: 5, levelValue: 5, resolution: 0.0439453125, scale: 18468599.566171877 },
{ level: 6, levelValue: 6, resolution: 0.02197265625, scale: 9234299.7830859385 },
{ level: 7, levelValue: 7, resolution: 0.010986328125, scale: 4617149.8915429693 },
{ level: 8, levelValue: 8, resolution: 0.0054931640625, scale: 2308574.9457714846 },
{ level: 9, levelValue: 9, resolution: 0.00274658203125, scale: 1154287.4728857423 },
{ level: 10, levelValue: 10, resolution: 0.001373291015625, scale: 577143.73644287116 },
{ level: 11, levelValue: 11, resolution: 0.0006866455078125, scale: 288571.86822143558 },
{ level: 12, levelValue: 12, resolution: 0.00034332275390625, scale: 144285.93411071779 },
{ level: 13, levelValue: 13, resolution: 0.000171661376953125, scale: 72142.967055358895 },
{ level: 14, levelValue: 14, resolution: 8.58306884765625e-005, scale: 36071.483527679447 },
{ level: 15, levelValue: 15, resolution: 4.291534423828125e-005, scale: 18035.741763839724 },
{ level: 16, levelValue: 16, resolution: 2.1457672119140625e-005, scale: 9017.8708819198619 },
{ level: 17, levelValue: 17, resolution: 1.0728836059570313e-005, scale: 4508.9354409599309 },
{ level: 18, levelValue: 18, resolution: 5.3644180297851563e-006, scale: 2254.4677204799655 },
{ level: 19, levelValue: 19, resolution: 2.68220901489257815e-006, scale: 1127.23386023998275 },
{ level: 20, levelValue: 20, resolution: 1.341104507446289075e-006, scale: 563.616930119991375 }
]
});
var tiledLayer = new WebTileLayer('https://192.168.***.***:6443/arcgis/rest/services/*******/ImageBaseMap/MapServer?T=vec_c&X={col}&Y={row}&L={level}',{
tileInfo: tileInfo,
spatialReference: {
wkid: 4490
},
});
return tiledLayer
}
结果:失败,原因还是投影坐标系改不了
第三次操作:
构思方案:选择MapImageLayer
结果:成功,但是性能非常差,需要一次加载,舍弃。
第”三.九“次操作:
构思方案:选择WMTSLayer
如果是arcgis发的服务一定要记得在url后面加上WMTS,这个官方文档没有提及,非常坑!!!!!!!
export const TestCustomWMTS = async () => {
//wmtslayer
const WMTSLayer = await arcgisPackage.WMTSLayer;
let wmtsLayers = new WMTSLayer({
url: "https://192.168.***.***:6443/arcgis/rest/services/*******/ImageBaseMap/MapServer/WMTS", // url to the service
});
return wmtsLayers
}
结果:成功,但是无法定位,观察layer对象,发现他的投影坐标系是4326,估计大致原因就是这个导致的。于是观察api文档,发现wmtslayer没有SpatialReference
属性,随后放弃。
第四次操作
构思方案:选择TileLayer
,主要考虑到该项wmts服务是用arcgis server发布的,所以尝试一下。
export const TestCustomWMTS = async () => {
let TileLayer = await arcgisPackage.TileLayer;
var tiledLayer = new TileLayer({
url:'https://192.168.***.***:6443/arcgis/rest/services/*******/ImageBaseMap/MapServer',
});
return tiledLayer
}
结果:成功