Merge pull request #12373 from mike-000/patch-1
Rework TileDebug to allow TMS coordinates and to fix reprojection
This commit is contained in:
@@ -0,0 +1,14 @@
|
|||||||
|
---
|
||||||
|
layout: example.html
|
||||||
|
title: Custom Canvas Tiles
|
||||||
|
shortdesc: Renders tiles with TMS coordinates for debugging.
|
||||||
|
docs: >
|
||||||
|
The black grid tiles are generated on the client with an HTML5 canvas.
|
||||||
|
The displayed TMS tile coordinates are produced using a custom template
|
||||||
|
for TMS, the vector tile source's 512 pixel tile grid and the
|
||||||
|
<code>zDirection</code> setting for vector tiles.
|
||||||
|
Notice how the country polygons can be split between tiles and vector
|
||||||
|
labels may appear in each tile.
|
||||||
|
tags: "layers, vector tiles, tms, canvas"
|
||||||
|
---
|
||||||
|
<div id="map" class="map"></div>
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
import MVT from '../src/ol/format/MVT.js';
|
||||||
|
import Map from '../src/ol/Map.js';
|
||||||
|
import TileDebug from '../src/ol/source/TileDebug.js';
|
||||||
|
import TileLayer from '../src/ol/layer/Tile.js';
|
||||||
|
import VectorTileLayer from '../src/ol/layer/VectorTile.js';
|
||||||
|
import VectorTileSource from '../src/ol/source/VectorTile.js';
|
||||||
|
import View from '../src/ol/View.js';
|
||||||
|
import {Fill, Stroke, Style, Text} from '../src/ol/style.js';
|
||||||
|
|
||||||
|
const style = new Style({
|
||||||
|
fill: new Fill({
|
||||||
|
color: 'rgba(255, 255, 255, 0.6)',
|
||||||
|
}),
|
||||||
|
stroke: new Stroke({
|
||||||
|
color: '#319FD3',
|
||||||
|
width: 1,
|
||||||
|
}),
|
||||||
|
text: new Text({
|
||||||
|
font: '12px Calibri,sans-serif',
|
||||||
|
fill: new Fill({
|
||||||
|
color: '#000',
|
||||||
|
}),
|
||||||
|
stroke: new Stroke({
|
||||||
|
color: '#fff',
|
||||||
|
width: 3,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const vtLayer = new VectorTileLayer({
|
||||||
|
declutter: true,
|
||||||
|
source: new VectorTileSource({
|
||||||
|
maxZoom: 15,
|
||||||
|
format: new MVT(),
|
||||||
|
url:
|
||||||
|
'https://ahocevar.com/geoserver/gwc/service/tms/1.0.0/' +
|
||||||
|
'ne:ne_10m_admin_0_countries@EPSG%3A900913@pbf/{z}/{x}/{-y}.pbf',
|
||||||
|
}),
|
||||||
|
style: function (feature) {
|
||||||
|
style.getText().setText(feature.get('name'));
|
||||||
|
return style;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const debugLayer = new TileLayer({
|
||||||
|
source: new TileDebug({
|
||||||
|
template: 'z:{z} x:{x} y:{-y}',
|
||||||
|
projection: vtLayer.getSource().getProjection(),
|
||||||
|
tileGrid: vtLayer.getSource().getTileGrid(),
|
||||||
|
zDirection: 1,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const map = new Map({
|
||||||
|
layers: [vtLayer, debugLayer],
|
||||||
|
target: 'map',
|
||||||
|
view: new View({
|
||||||
|
center: [0, 6000000],
|
||||||
|
zoom: 4,
|
||||||
|
}),
|
||||||
|
});
|
||||||
@@ -10,18 +10,24 @@ tags: "reprojection, projection, proj4js, epsg.io, graticule"
|
|||||||
---
|
---
|
||||||
<div id="map" class="map"></div>
|
<div id="map" class="map"></div>
|
||||||
<form class="form-inline">
|
<form class="form-inline">
|
||||||
<label for="epsg-query">Search projection:</label>
|
<label for="epsg-query">Search projection: </label>
|
||||||
<input type="text" id="epsg-query" placeholder="4326, 27700, 3031, US National Atlas, Swiss, France, ..." class="form-control" size="50" />
|
<input type="text" id="epsg-query" placeholder="4326, 27700, 3031, US National Atlas, Swiss, France, ..." class="form-control" size="50" />
|
||||||
<button id="epsg-search" class="btn">Search</button>
|
<button id="epsg-search" class="btn">Search</button>
|
||||||
<span id="epsg-result"></span>
|
<span id="epsg-result"></span>
|
||||||
<div>
|
</form>
|
||||||
<label for="render-edges">
|
<form class="form-inline">
|
||||||
Render reprojection edges
|
<label for="render-edges">
|
||||||
<input type="checkbox" id="render-edges">
|
Render reprojection edges:
|
||||||
</label>
|
<input type="checkbox" id="render-edges" />
|
||||||
<label for="show-graticule">
|
</label>
|
||||||
Show graticule
|
|
||||||
<input type="checkbox" id="show-graticule" />
|
<label for="show-tiles">
|
||||||
</label>
|
Show tile coordinates:
|
||||||
</div>
|
<input type="checkbox" id="show-tiles" />
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<label for="show-graticule">
|
||||||
|
Show graticule:
|
||||||
|
<input type="checkbox" id="show-graticule" />
|
||||||
|
</label>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import Graticule from '../src/ol/layer/Graticule.js';
|
|||||||
import Map from '../src/ol/Map.js';
|
import Map from '../src/ol/Map.js';
|
||||||
import OSM from '../src/ol/source/OSM.js';
|
import OSM from '../src/ol/source/OSM.js';
|
||||||
import Stroke from '../src/ol/style/Stroke.js';
|
import Stroke from '../src/ol/style/Stroke.js';
|
||||||
import TileImage from '../src/ol/source/TileImage.js';
|
import TileDebug from '../src/ol/source/TileDebug.js';
|
||||||
import TileLayer from '../src/ol/layer/Tile.js';
|
import TileLayer from '../src/ol/layer/Tile.js';
|
||||||
import View from '../src/ol/View.js';
|
import View from '../src/ol/View.js';
|
||||||
import proj4 from 'proj4';
|
import proj4 from 'proj4';
|
||||||
@@ -10,6 +10,16 @@ import {applyTransform} from '../src/ol/extent.js';
|
|||||||
import {get as getProjection, getTransform} from '../src/ol/proj.js';
|
import {get as getProjection, getTransform} from '../src/ol/proj.js';
|
||||||
import {register} from '../src/ol/proj/proj4.js';
|
import {register} from '../src/ol/proj/proj4.js';
|
||||||
|
|
||||||
|
const osmSource = new OSM();
|
||||||
|
|
||||||
|
const debugLayer = new TileLayer({
|
||||||
|
source: new TileDebug({
|
||||||
|
tileGrid: osmSource.getTileGrid(),
|
||||||
|
projection: osmSource.getProjection(),
|
||||||
|
}),
|
||||||
|
visible: false,
|
||||||
|
});
|
||||||
|
|
||||||
const graticule = new Graticule({
|
const graticule = new Graticule({
|
||||||
// the style to use for the lines, optional.
|
// the style to use for the lines, optional.
|
||||||
strokeStyle: new Stroke({
|
strokeStyle: new Stroke({
|
||||||
@@ -25,8 +35,9 @@ const graticule = new Graticule({
|
|||||||
const map = new Map({
|
const map = new Map({
|
||||||
layers: [
|
layers: [
|
||||||
new TileLayer({
|
new TileLayer({
|
||||||
source: new OSM(),
|
source: osmSource,
|
||||||
}),
|
}),
|
||||||
|
debugLayer,
|
||||||
graticule,
|
graticule,
|
||||||
],
|
],
|
||||||
target: 'map',
|
target: 'map',
|
||||||
@@ -41,6 +52,7 @@ const queryInput = document.getElementById('epsg-query');
|
|||||||
const searchButton = document.getElementById('epsg-search');
|
const searchButton = document.getElementById('epsg-search');
|
||||||
const resultSpan = document.getElementById('epsg-result');
|
const resultSpan = document.getElementById('epsg-result');
|
||||||
const renderEdgesCheckbox = document.getElementById('render-edges');
|
const renderEdgesCheckbox = document.getElementById('render-edges');
|
||||||
|
const showTilesCheckbox = document.getElementById('show-tiles');
|
||||||
const showGraticuleCheckbox = document.getElementById('show-graticule');
|
const showGraticuleCheckbox = document.getElementById('show-graticule');
|
||||||
|
|
||||||
function setProjection(code, name, proj4def, bbox) {
|
function setProjection(code, name, proj4def, bbox) {
|
||||||
@@ -125,22 +137,14 @@ searchButton.onclick = function (event) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle checkbox change event.
|
* Handle checkbox change events.
|
||||||
*/
|
*/
|
||||||
renderEdgesCheckbox.onchange = function () {
|
renderEdgesCheckbox.onchange = function () {
|
||||||
map.getLayers().forEach(function (layer) {
|
osmSource.setRenderReprojectionEdges(renderEdgesCheckbox.checked);
|
||||||
if (layer instanceof TileLayer) {
|
};
|
||||||
const source = layer.getSource();
|
showTilesCheckbox.onchange = function () {
|
||||||
if (source instanceof TileImage) {
|
debugLayer.setVisible(showTilesCheckbox.checked);
|
||||||
source.setRenderReprojectionEdges(renderEdgesCheckbox.checked);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle checkbox change event.
|
|
||||||
*/
|
|
||||||
showGraticuleCheckbox.onchange = function () {
|
showGraticuleCheckbox.onchange = function () {
|
||||||
graticule.setVisible(showGraticuleCheckbox.checked);
|
graticule.setVisible(showGraticuleCheckbox.checked);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -72,6 +72,17 @@ class ImageTile extends Tile {
|
|||||||
return this.image_;
|
return this.image_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets an HTML image element for this tile (may be a Canvas or preloaded Image).
|
||||||
|
* @param {HTMLCanvasElement|HTMLImageElement} element Element.
|
||||||
|
*/
|
||||||
|
setImage(element) {
|
||||||
|
this.image_ = element;
|
||||||
|
this.state = TileState.LOADED;
|
||||||
|
this.unlistenImage_();
|
||||||
|
this.changed();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tracks loading or read errors.
|
* Tracks loading or read errors.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -334,15 +334,15 @@ class ReprojTile extends Tile {
|
|||||||
}.bind(this)
|
}.bind(this)
|
||||||
);
|
);
|
||||||
|
|
||||||
this.sourceTiles_.forEach(function (tile, i, arr) {
|
|
||||||
const state = tile.getState();
|
|
||||||
if (state == TileState.IDLE) {
|
|
||||||
tile.load();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (leftToLoad === 0) {
|
if (leftToLoad === 0) {
|
||||||
setTimeout(this.reproject_.bind(this), 0);
|
setTimeout(this.reproject_.bind(this), 0);
|
||||||
|
} else {
|
||||||
|
this.sourceTiles_.forEach(function (tile, i, arr) {
|
||||||
|
const state = tile.getState();
|
||||||
|
if (state == TileState.IDLE) {
|
||||||
|
tile.load();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-106
@@ -2,82 +2,10 @@
|
|||||||
* @module ol/source/TileDebug
|
* @module ol/source/TileDebug
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Tile from '../Tile.js';
|
|
||||||
import TileState from '../TileState.js';
|
|
||||||
import XYZ from './XYZ.js';
|
import XYZ from './XYZ.js';
|
||||||
import {createCanvasContext2D} from '../dom.js';
|
import {createCanvasContext2D} from '../dom.js';
|
||||||
import {getKeyZXY} from '../tilecoord.js';
|
|
||||||
import {toSize} from '../size.js';
|
import {toSize} from '../size.js';
|
||||||
|
|
||||||
class LabeledTile extends Tile {
|
|
||||||
/**
|
|
||||||
* @param {import("../tilecoord.js").TileCoord} tileCoord Tile coordinate.
|
|
||||||
* @param {import("../size.js").Size} tileSize Tile size.
|
|
||||||
* @param {string} text Text.
|
|
||||||
*/
|
|
||||||
constructor(tileCoord, tileSize, text) {
|
|
||||||
super(tileCoord, TileState.LOADED);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @private
|
|
||||||
* @type {import("../size.js").Size}
|
|
||||||
*/
|
|
||||||
this.tileSize_ = tileSize;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @private
|
|
||||||
* @type {string}
|
|
||||||
*/
|
|
||||||
this.text_ = text;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @private
|
|
||||||
* @type {HTMLCanvasElement}
|
|
||||||
*/
|
|
||||||
this.canvas_ = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the image element for this tile.
|
|
||||||
* @return {HTMLCanvasElement} Image.
|
|
||||||
*/
|
|
||||||
getImage() {
|
|
||||||
if (this.canvas_) {
|
|
||||||
return this.canvas_;
|
|
||||||
} else {
|
|
||||||
const tileSize = this.tileSize_;
|
|
||||||
const context = createCanvasContext2D(tileSize[0], tileSize[1]);
|
|
||||||
|
|
||||||
context.strokeStyle = 'grey';
|
|
||||||
context.strokeRect(0.5, 0.5, tileSize[0] + 0.5, tileSize[1] + 0.5);
|
|
||||||
|
|
||||||
context.fillStyle = 'grey';
|
|
||||||
context.strokeStyle = 'white';
|
|
||||||
context.textAlign = 'center';
|
|
||||||
context.textBaseline = 'middle';
|
|
||||||
context.font = '24px sans-serif';
|
|
||||||
context.lineWidth = 4;
|
|
||||||
context.strokeText(
|
|
||||||
this.text_,
|
|
||||||
tileSize[0] / 2,
|
|
||||||
tileSize[1] / 2,
|
|
||||||
tileSize[0]
|
|
||||||
);
|
|
||||||
context.fillText(
|
|
||||||
this.text_,
|
|
||||||
tileSize[0] / 2,
|
|
||||||
tileSize[1] / 2,
|
|
||||||
tileSize[0]
|
|
||||||
);
|
|
||||||
|
|
||||||
this.canvas_ = context.canvas;
|
|
||||||
return context.canvas;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
load() {}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} Options
|
* @typedef {Object} Options
|
||||||
* @property {import("../proj.js").ProjectionLike} [projection='EPSG:3857'] Optional projection.
|
* @property {import("../proj.js").ProjectionLike} [projection='EPSG:3857'] Optional projection.
|
||||||
@@ -88,6 +16,8 @@ class LabeledTile extends Tile {
|
|||||||
* the view resolution does not match any resolution of the tile source. If 0, the nearest
|
* the view resolution does not match any resolution of the tile source. If 0, the nearest
|
||||||
* resolution will be used. If 1, the nearest lower resolution will be used. If -1, the
|
* resolution will be used. If 1, the nearest lower resolution will be used. If -1, the
|
||||||
* nearest higher resolution will be used.
|
* nearest higher resolution will be used.
|
||||||
|
* @property {string} [template='z:{z} x:{x} y:{y}'] Template for labeling the tiles.
|
||||||
|
* Should include `{x}`, `{y}` or `{-y}`, and `{z}` placeholders.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -95,8 +25,6 @@ class LabeledTile extends Tile {
|
|||||||
* A pseudo tile source, which does not fetch tiles from a server, but renders
|
* A pseudo tile source, which does not fetch tiles from a server, but renders
|
||||||
* a grid outline for the tile grid/projection along with the coordinates for
|
* a grid outline for the tile grid/projection along with the coordinates for
|
||||||
* each tile. See examples/canvas-tiles for an example.
|
* each tile. See examples/canvas-tiles for an example.
|
||||||
*
|
|
||||||
* Uses Canvas context2d, so requires Canvas support.
|
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
class TileDebug extends XYZ {
|
class TileDebug extends XYZ {
|
||||||
@@ -115,39 +43,29 @@ class TileDebug extends XYZ {
|
|||||||
tileGrid: options.tileGrid,
|
tileGrid: options.tileGrid,
|
||||||
wrapX: options.wrapX !== undefined ? options.wrapX : true,
|
wrapX: options.wrapX !== undefined ? options.wrapX : true,
|
||||||
zDirection: options.zDirection,
|
zDirection: options.zDirection,
|
||||||
});
|
url: options.template || 'z:{z} x:{x} y:{y}',
|
||||||
}
|
tileLoadFunction: (tile, text) => {
|
||||||
|
const z = tile.getTileCoord()[0];
|
||||||
|
const tileSize = toSize(this.tileGrid.getTileSize(z));
|
||||||
|
const context = createCanvasContext2D(tileSize[0], tileSize[1]);
|
||||||
|
|
||||||
/**
|
context.strokeStyle = 'grey';
|
||||||
* @param {number} z Tile coordinate z.
|
context.strokeRect(0.5, 0.5, tileSize[0] + 0.5, tileSize[1] + 0.5);
|
||||||
* @param {number} x Tile coordinate x.
|
|
||||||
* @param {number} y Tile coordinate y.
|
context.fillStyle = 'grey';
|
||||||
* @return {!LabeledTile} Tile.
|
context.strokeStyle = 'white';
|
||||||
*/
|
context.textAlign = 'center';
|
||||||
getTile(z, x, y) {
|
context.textBaseline = 'middle';
|
||||||
const tileCoordKey = getKeyZXY(z, x, y);
|
context.font = '24px sans-serif';
|
||||||
if (this.tileCache.containsKey(tileCoordKey)) {
|
context.lineWidth = 4;
|
||||||
return /** @type {!LabeledTile} */ (this.tileCache.get(tileCoordKey));
|
context.strokeText(text, tileSize[0] / 2, tileSize[1] / 2, tileSize[0]);
|
||||||
} else {
|
context.fillText(text, tileSize[0] / 2, tileSize[1] / 2, tileSize[0]);
|
||||||
const tileSize = toSize(this.tileGrid.getTileSize(z));
|
|
||||||
const tileCoord = [z, x, y];
|
/** @type {import("../ImageTile.js").default} */ (tile).setImage(
|
||||||
const textTileCoord = this.getTileCoordForTileUrlFunction(tileCoord);
|
context.canvas
|
||||||
let text;
|
);
|
||||||
if (textTileCoord) {
|
},
|
||||||
text =
|
});
|
||||||
'z:' +
|
|
||||||
textTileCoord[0] +
|
|
||||||
' x:' +
|
|
||||||
textTileCoord[1] +
|
|
||||||
' y:' +
|
|
||||||
textTileCoord[2];
|
|
||||||
} else {
|
|
||||||
text = 'none';
|
|
||||||
}
|
|
||||||
const tile = new LabeledTile(tileCoord, tileSize, text);
|
|
||||||
this.tileCache.set(tileCoordKey, tile);
|
|
||||||
return tile;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 39 KiB |
@@ -0,0 +1,49 @@
|
|||||||
|
import Map from '../../../../src/ol/Map.js';
|
||||||
|
import TileLayer from '../../../../src/ol/layer/Tile.js';
|
||||||
|
import View from '../../../../src/ol/View.js';
|
||||||
|
import {TileDebug, XYZ} from '../../../../src/ol/source.js';
|
||||||
|
import {createForProjection, createXYZ} from '../../../../src/ol/tilegrid.js';
|
||||||
|
import {get, toLonLat} from '../../../../src/ol/proj.js';
|
||||||
|
|
||||||
|
const tileGrid = createXYZ();
|
||||||
|
const extent = tileGrid.getTileCoordExtent([5, 5, 12]);
|
||||||
|
const center = [(extent[0] + extent[2]) / 2, extent[1]];
|
||||||
|
|
||||||
|
const source = new XYZ({
|
||||||
|
transition: 0,
|
||||||
|
minZoom: 5,
|
||||||
|
maxZoom: 5,
|
||||||
|
url: '/data/tiles/osm/{z}/{x}/{y}.png',
|
||||||
|
});
|
||||||
|
|
||||||
|
const sourceDebug = new TileDebug({tileGrid: source.getTileGrid()});
|
||||||
|
|
||||||
|
source.setTileGridForProjection(
|
||||||
|
get('EPSG:4326'),
|
||||||
|
createForProjection(get('EPSG:4326'), 7, [64, 64])
|
||||||
|
);
|
||||||
|
|
||||||
|
sourceDebug.setTileGridForProjection(
|
||||||
|
get('EPSG:4326'),
|
||||||
|
createForProjection(get('EPSG:4326'), 7, [64, 64])
|
||||||
|
);
|
||||||
|
|
||||||
|
new Map({
|
||||||
|
pixelRatio: 1,
|
||||||
|
target: 'map',
|
||||||
|
layers: [
|
||||||
|
new TileLayer({
|
||||||
|
source: source,
|
||||||
|
}),
|
||||||
|
new TileLayer({
|
||||||
|
source: sourceDebug,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
view: new View({
|
||||||
|
projection: 'EPSG:4326',
|
||||||
|
center: toLonLat(center),
|
||||||
|
zoom: 5,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
render();
|
||||||
Reference in New Issue
Block a user