You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenLayers WMTS图层配置求助:波兰地理门户WMTS服务设置

波兰地理门户WMTS图层配置修正

你的代码存在几个关键问题,导致WMTS图层无法正确加载,以下是修正方案:

核心问题分析

  1. 投影不匹配:WMTS服务的matrixSet是EPSG:2180,但你代码中使用了EPSG:4326作为图层投影,两者坐标系不一致,无法正确请求瓦片。
  2. URL错误:WMTS源的URL不应包含GetCapabilities参数,只需提供服务基础地址即可。
  3. TileGrid参数错误:你基于EPSG:2180以外的投影生成了resolutions和matrixIds,与服务实际的瓦片网格不匹配。
  4. 视图投影未指定:地图视图的中心坐标是EPSG:2180格式,但未明确设置视图投影,默认会使用EPSG:3857,导致定位错误。

修正后的完整代码

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} from 'ol/extent.js';

// 注册EPSG:2180投影(若OpenLayers未默认支持,需引入proj4并注册)
// import proj4 from 'proj4';
// import {register} from 'ol/proj/proj4.js';
// proj4.defs('EPSG:2180', '+proj=tmerc +lat_0=0 +lon_0=19 +k=0.9993 +x_0=500000 +y_0=-5300000 +ellps=GRS80 +units=m +no_defs');
// register(proj4);

const projection = getProjection('EPSG:2180');
const projectionExtent = projection.getExtent();

// 根据波兰WMTS服务的GetCapabilities信息,构建正确的瓦片网格参数
const matrixIds = [];
const resolutions = [];
const tileSize = 256;

// 服务支持的缩放级别0-18,对应参数来自GetCapabilities
const scales = [
  8595555.17105, 4297777.585525, 2148888.7927625, 1074444.39638125,
  537222.198190625, 268611.0990953125, 134305.54954765625, 67152.77477382812,
  33576.38738691406, 16788.19369345703, 8394.096846728515, 4197.048423364258,
  2098.524211682129, 1049.2621058410645, 524.6310529205322, 262.3155264602661,
  131.15776323013305, 65.57888161506652, 32.78944080753326
];

scales.forEach((scale, z) => {
  resolutions[z] = scale * 0.00028; // 转换为分辨率(米/像素)
  matrixIds[z] = `${z}`;
});

const tileGrid = new WMTSTileGrid({
  origin: getTopLeft(projectionExtent),
  resolutions: resolutions,
  matrixIds: matrixIds,
  tileSize: tileSize
});

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',
        layer: 'ORTOFOTOMAPA',
        matrixSet: 'EPSG:2180',
        format: 'image/png',
        projection: projection,
        tileGrid: tileGrid,
        style: 'default',
        wrapX: false, // EPSG:2180是局部投影,不需要横向循环显示
      }),
    }),
  ],
  target: 'map',
  view: new View({
    projection: projection,
    center: [2008582, 6753697],
    zoom: 7,
  }),
});

关键修正说明

  • 投影一致性:图层和视图统一使用EPSG:2180,与服务的matrixSet匹配。
  • 正确的服务URL:移除了GetCapabilities参数,使用服务的基础访问地址。
  • 瓦片网格匹配:根据服务GetCapabilities中的缩放级别参数生成resolutions和matrixIds,确保与服务瓦片网格完全对齐。
  • 禁用wrapX:EPSG:2180是波兰本地投影,不需要横向循环显示,因此关闭wrapX。

内容的提问来源于stack exchange,提问作者piorkoo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 13:15:29