【问题标题】:Openlayers WMTS custom configuration problemOpenlayers WMTS自定义配置问题
【发布时间】:2023-01-19 02:52:26
【问题描述】:

我正在尝试在 openlayers 项目中使用 WMTS 图层。这是我第一次尝试使用 WMTS 而不是 WMS。不幸的是我被困住了。我不知道如何设置。

请帮我设置正确的设置:

WMTS url GetCapabilities

我的代码(我使用了 opnelayers.org 示例中的模板):

import Map from 'ol/Map.js';
import OSM from 'ol/source/OSM.js';
import TileLayer from 'ol/layer/Tile.js';
import View from 'ol/View.js';
import WMTS from 'ol/source/WMTS.js';
import WMTSTileGrid from 'ol/tilegrid/WMTS.js';
import {get as getProjection} from 'ol/proj.js';
import {getTopLeft, getWidth} from 'ol/extent.js';

const projection = getProjection('EPSG:4326');
const projectionExtent = projection.getExtent();
const size = getWidth(projectionExtent) / 256;
const resolutions = new Array(19);
const matrixIds = new Array(19);
for (let z = 0; z < 19; ++z) {
  // generate resolutions and matrixIds arrays for this WMTS
  resolutions[z] = size / Math.pow(2, z);
  matrixIds[z] = z;
}

const map = new Map({
  layers: [
    new TileLayer({
      source: new OSM(),
    }),
    new TileLayer({
      opacity: 0.7,
      source: new WMTS({
        url: 'https://mapy.geoportal.gov.pl/wss/service/PZGIK/ORTO/WMTS/HighResolution?SERVICE=WMTS&REQUEST=GetCapabilities',
        layer: 'ORTOFOTOMAPA',
        matrixSet: 'EPSG:2180',
        format: 'image/png',
        projection: projection,
        tileGrid: new WMTSTileGrid({
          origin: getTopLeft(projectionExtent),
          resolutions: resolutions,
          matrixIds: matrixIds,
        }),
        // style: 'default',
        wrapX: true,
      }),
    }),
  ],
  target: 'map',
  view: new View({
    center: [2008582, 6753697],
    zoom: 7,
  }),
});

谢谢!

【问题讨论】:

    标签: javascript openlayers


    【解决方案1】:

    如果您有自定义投影,也会有一个自定义瓦片网格,因此更容易解析功能并让 OpenLayers 获取选项。您还需要使用 proj4 定义和注册 EPSG:2180 投影(包括 N-E 轴顺序)。

    import Map from 'ol/Map.js';
    import OSM from 'ol/source/OSM.js';
    import TileLayer from 'ol/layer/Tile.js';
    import View from 'ol/View.js';
    import WMTS, {optionsFromCapabilities} from 'ol/source/WMTS.js';
    import WMTSCapabilities from 'ol/format/WMTSCapabilities.js';
    import proj4 from 'proj4';
    import {register} from 'ol/proj/proj4';
    
    proj4.defs(
      'EPSG:2180',
      '+proj=tmerc +lat_0=0 +lon_0=19 +k=0.9993 +x_0=500000 +y_0=-5300000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs +type=crs +axis=neu'
    );
    register(proj4);
    
    const parser = new WMTSCapabilities();
    let map;
    
    fetch(
      'https://mapy.geoportal.gov.pl/wss/service/PZGIK/ORTO/WMTS/HighResolution?SERVICE=WMTS&REQUEST=GetCapabilities'
    )
      .then(function (response) {
        return response.text();
      })
      .then(function (text) {
        const result = parser.read(text);
        const options = optionsFromCapabilities(result, {
          layer: 'ORTOFOTOMAPA',
          matrixSet: 'EPSG:2180',
        });
    
        map = new Map({
          layers: [
            new TileLayer({
              source: new OSM(),
            }),
            new TileLayer({
              opacity: 0.7,
              source: new WMTS(options),
            }),
          ],
          target: 'map',
          view: new View({
            center: [2008582, 6753697],
            zoom: 7,
          }),
        });
      });
    

    https://codesandbox.io/s/wmts-layer-from-capabilities-forked-6gu26y?file=/main.js

    【讨论】:

      猜你喜欢
      • 2011-05-15
      • 1970-01-01
      • 1970-01-01
      • 2019-02-24
      • 1970-01-01
      • 2018-06-21
      • 2011-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多