From fe21d9d8ead10d6541382b55a237b9a4e3e7eb91 Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Thu, 11 Jan 2024 00:35:36 +0100 Subject: =?UTF-8?q?Basemap=20from=20Lantm=C3=A4teriet.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.js | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 main.js (limited to 'main.js') diff --git a/main.js b/main.js new file mode 100644 index 0000000..f1d36e9 --- /dev/null +++ b/main.js @@ -0,0 +1,103 @@ +/*********************************************************************** + * Copyright © 2024 Guilhem Moulin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + **********************************************************************/ + +import Map from 'ol/Map.js'; +import View from 'ol/View.js'; +import TileLayer from 'ol/layer/Tile.js'; + +import WMTS from 'ol/source/WMTS.js'; +import WMTSTileGrid from 'ol/tilegrid/WMTS.js'; + +import proj4 from 'proj4'; +import {get as getProjection} from 'ol/proj.js'; +import {register as registerProjection} from 'ol/proj/proj4.js'; + +import './style.css'; + + +proj4.defs('EPSG:3006', '+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs +type=crs'); +registerProjection(proj4); +const projection = getProjection('EPSG:3006'); + + +/* Lantmäteriet uses a tile-scheme where the origin (top-left corner) is + * at N8500000 E-1200000 (SWEREF99 TM), where each tile is 256x256 + * pixels, and where the resolution at level 0 is 4096m per pixel + * (each side is 1048.576km long). + * + * https://www.lantmateriet.se/globalassets/geodata/geodatatjanster/tb_twk_visning_cache_v1.1.0.pdf + * https://www.lantmateriet.se/globalassets/geodata/geodatatjanster/tb_twk_visning-oversiktlig_v1.0.3.pdf + * + * We set the extent to a 4x4 tiles square at level 2 (1024px = + * 1048.576km per side) somehow centered on Norrbotten and Västerbotten, + * and zoom in from there. This represent a TILEROW (x) offset of 5, + * and a TILECOL (y) offset of 2. + */ +const extent = [110720, 6927136, 1159296, 7975712]; + +const map = new Map({ + view: new View({ + projection: projection, + extent: extent, + showFullExtent: true, + /* center of the bbox of the Norrbotten and Västerbotten geometries */ + center: [694767.48, 7338176.57], + zoom: 1, + enableRotation: false, + resolutions: [1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1, .5], + constrainResolution: false, + }), + layers: [ + new TileLayer({ + /* XXX using the topowebbcache WMTS is fine for testing (as it + * doesn't require authentication) but not in production in a public + * instance as doing so would violate its current terms of use (as + * of January 2024 it's not CC0 open data). See + * + * https://www.lantmateriet.se/sv/om-lantmateriet/Rattsinformation/upphovsratt-och-publicering-av-lantmateriets-geografiska-information/ + * https://www.lantmateriet.se/sv/kartor/vara-karttjanster/min-karta/#anchor-2 + * https://help.locusmap.eu/topic/support-for-swedish-lantmateriets-min-karta-wms + * + * More precise background maps might be available in the future + * as open data, though: + * + * https://www.lantmateriet.se/sv/om-lantmateriet/press/nyheter/lantmateriets-arbete-mot-oppna-data-i-full-gang/ + */ + source: new WMTS({ + url: 'https://minkarta.lantmateriet.se/map/topowebbcache', + version: '1.0.0', + layer: 'topowebb', + style: 'default', + matrixSet: '3006', + format: 'image/png', + tileGrid: new WMTSTileGrid({ + extent: extent, + // https://www.lantmateriet.se/globalassets/geodata/geodatatjanster/tb_twk_visning_cache_v1.1.0.pdf + tileSize: 256, + origin: [-1200000, 8500000], + resolutions: [4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1, .5], + matrixIds: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], + }), + projection: projection, + wrapX: false, + crossOrigin: 'anonymous', + attributions: '© Lantmäteriet', + }), + }), + ], + target: document.getElementById('map'), +}); -- cgit v1.2.3