WMTSLayer.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import LTBaseObject from './KBaseObject'
  2. import TileLayer from 'ol/layer/Tile'
  3. import sourceWMTS from 'ol/source/WMTS';
  4. import WMTSTileGrid from 'ol/tilegrid/WMTS';
  5. import * as olExtent from 'ol/extent';
  6. /**
  7. * @description KMap.WMTSLayer WMS图层类
  8. */
  9. class WMTSLayer extends LTBaseObject{
  10. /**
  11. * @description 构造函数
  12. * @param {String} url wms图层服务地址
  13. * @param {LTMap.Map} [mapInstance=null] map对象,单地图的时候可不传,多地图时候需要传
  14. * @memberof WMTSLayer
  15. */
  16. constructor(wmtsData,projection,mapInstance = null){
  17. super(mapInstance)
  18. const vm = this;
  19. vm.initLayer(wmtsData,projection)
  20. }
  21. /**
  22. * @description 初始化ImageLayer
  23. * @memberof WMTSLayer
  24. */
  25. initLayer(wmtsData,projection){
  26. const vm = this;
  27. let projectionExtent = projection.getExtent();
  28. let size = olExtent.getWidth(projectionExtent) / 256;
  29. let resolutions = new Array(19);
  30. let matrixIds = new Array(19);
  31. for (var z = 1; z < 19; ++z) {
  32. // generate resolutions and matrixIds arrays for this WMTS
  33. resolutions[z] = size / Math.pow(2, z);
  34. matrixIds[z] = z;
  35. }
  36. let source = new sourceWMTS({
  37. url: wmtsData.url,
  38. layer: wmtsData.layer,
  39. matrixSet: wmtsData.matrixSet,
  40. format: 'tiles',
  41. projection: projection,
  42. crossOrigin:'anonymous',
  43. tileGrid: new WMTSTileGrid({
  44. origin: olExtent.getTopLeft(projectionExtent),
  45. resolutions: resolutions,
  46. matrixIds: matrixIds
  47. }),
  48. style: 'default',
  49. wrapX: true
  50. })
  51. let layer = new TileLayer({
  52. zIndex:wmtsData.zIndex,
  53. source: source,
  54. // visible: false,
  55. });
  56. layer.id = wmtsData.layer+"_"+wmtsData.matrixSet;
  57. vm.layer = layer;
  58. vm.source = source;
  59. vm.map.addLayer(vm.layer)
  60. return layer;
  61. //地图加载WMS图层
  62. }
  63. /**
  64. * @description 添加WMS图层到地图
  65. * @memberof WMTSLayer
  66. */
  67. add(){
  68. const vm = this
  69. if(!vm.state) {
  70. vm.map.addLayer(vm.layer)
  71. vm.state = true
  72. }
  73. }
  74. /**
  75. * @description 从当前地图中移除WMS图层
  76. * @memberof WMTSLayer
  77. */
  78. remove() {
  79. const vm = this
  80. if(vm.state) {
  81. vm.map.removeLayer(vm.layer)
  82. vm.state = false
  83. }
  84. }
  85. /**
  86. * @description 显示WMS图层数据
  87. * @memberof WMTSLayer
  88. */
  89. show(){
  90. const vm = this
  91. if(vm.state) {
  92. vm.layer.setVisible(true)
  93. }
  94. }
  95. /**
  96. * @description 隐藏WMS图层数据
  97. * @memberof WMTSLayer
  98. */
  99. hide(){
  100. const vm = this
  101. if(vm.state) {
  102. vm.layer.setVisible(false)
  103. }
  104. }
  105. }
  106. export default WMTSLayer