vue3-openlayers
Version:
OpenLayers Wrapper for Vue3
101 lines (100 loc) • 3.28 kB
JavaScript
import WMTSSource from "ol/source/WMTS";
import TileGridWMTS from "ol/tilegrid/WMTS";
class Tianditu extends WMTSSource {
static layerLabelMap = {};
static layerZoomMap = {};
static tileProxy;
constructor(opts) {
Tianditu.layerLabelMap = {
vec: "cva",
ter: "cta",
img: "cia"
};
Tianditu.layerZoomMap = {
vec: 18,
ter: 14,
img: 18
};
const options = { ...opts };
options.layerType = options.layerType || "vec";
options.layerType = options.isLabel ? Tianditu.layerLabelMap[options.layerType] : options.layerType;
options.matrixSet = options.projection === "EPSG:4326" || options.projection === "EPSG:4490" ? "c" : "w";
if (!options.url && !options.urls) {
options.url = "https://t{0-7}.tianditu.gov.cn/{layer}_{proj}/wmts?";
}
if (options.tk) {
options.url = `${options.url}tk=${options.tk}`;
}
options.url = (options.url || "").replace("{layer}", options.layerType).replace("{proj}", options.matrixSet);
const tileGrid = options.tileGrid || Tianditu.getTileGrid(options.projection);
const crossOrigin = options.crossOrigin !== void 0 ? options.crossOrigin : "anonymous";
const superOptions = {
version: options.version || "1.0.0",
format: options.format || "tiles",
dimensions: options.dimensions || {},
layer: options.layerType,
matrixSet: options.matrixSet,
tileGrid,
style: options.style || "default",
cacheSize: options.cacheSize,
crossOrigin,
maxZoom: Tianditu.layerZoomMap[options.layerType],
reprojectionErrorThreshold: options.reprojectionErrorThreshold,
url: options.url,
urls: options.urls,
projection: options.projection || "EPSG:3857",
wrapX: options.wrapX
};
if (options.tileProxy) {
Tianditu.tileProxy = options.tileProxy;
superOptions.tileLoadFunction = (imageTile, src) => {
imageTile.getImage().src = Tianditu.tileProxy + encodeURIComponent(src);
};
}
super(superOptions);
}
static getTileGrid(projection) {
if (projection === "EPSG:4326" || projection === "EPSG:4490") {
return Tianditu.default4326TileGrid();
}
return Tianditu.default3857TileGrid();
}
static default4326TileGrid() {
const tdt_WGS84_resolutions = [];
const matrixIds = [];
for (let i = 1; i < 19; i++) {
tdt_WGS84_resolutions.push(0.703125 * 2 / 2 ** i);
matrixIds.push(i.toString());
}
const tileGird = new TileGridWMTS({
extent: [-180, -90, 180, 90],
resolutions: tdt_WGS84_resolutions,
origin: [-180, 90],
matrixIds
});
return tileGird;
}
static default3857TileGrid() {
const tdt_Mercator_resolutions = [];
const matrixIds = [];
for (let i = 1; i < 19; i++) {
tdt_Mercator_resolutions.push(78271.51696402031 * 2 / 2 ** i);
matrixIds.push(i.toString());
}
const tileGird = new TileGridWMTS({
extent: [
-200375083427892e-7,
-200375083427892e-7,
200375083427892e-7,
200375083427892e-7
],
resolutions: tdt_Mercator_resolutions,
matrixIds,
origin: [-200375083427892e-7, 200375083427892e-7]
});
return tileGird;
}
}
export {
Tianditu as default
};