rlayers
Version:
React Components for OpenLayers
60 lines • 2.36 kB
JavaScript
import { Tile as LayerTile } from 'ol/layer';
import { default as SourceWMTS, optionsFromCapabilities } from 'ol/source/WMTS';
import WMTSCapabilities from 'ol/format/WMTSCapabilities';
import { default as RLayerRaster } from './RLayerRaster';
import debug from '../debug';
/**
* A layer for WMTS-compatible raster tile servers
*
* Requires an `RMap` context
*/
export default class RLayerWMTS extends RLayerRaster {
constructor(props) {
super(props);
this.ol = new LayerTile({ source: this.source });
this.parser = new WMTSCapabilities();
}
createSource() {
debug('createSource', this);
return fetch(this.props.url)
.then((r) => r.text())
.then((text) => {
const caps = this.parser.read(text);
this.options = optionsFromCapabilities(caps, {
layer: this.props.layer
});
if (this.props.attributions)
this.options.attributions = this.props.attributions;
this.options.crossOrigin = '';
if (this.props.projection)
this.options.projection = this.props.projection;
this.options.wrapX = false;
this.source = new SourceWMTS(this.options);
this.ol.setSource(this.source);
this.eventSources = [this.ol, this.source];
if (this.props.onCapabilities)
this.props.onCapabilities.call(this, this.options);
return this.source;
})
.catch((e) => {
// eslint-disable-next-line no-console
console.error('failed loading WMTS', this.props.url, this.props.layer, e);
this.source = undefined;
return null;
});
}
refresh(prevProps) {
super.refresh();
if ((prevProps === null || prevProps === void 0 ? void 0 : prevProps.url) !== this.props.url || (prevProps === null || prevProps === void 0 ? void 0 : prevProps.layer) !== this.props.layer) {
this.createSource().then(() => {
this.ol.setSource(this.source);
this.attachOldEventHandlers(this.source);
});
}
else {
if (this.props.onCapabilities)
this.props.onCapabilities.call(this, this.options);
}
}
}
//# sourceMappingURL=RLayerWMTS.js.map