Get pixel data
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
* @module ol/renderer/canvas/TileLayer
|
||||
*/
|
||||
import CanvasLayerRenderer from './Layer.js';
|
||||
import ImageTile from '../../ImageTile.js';
|
||||
import ReprojTile from '../../reproj/Tile.js';
|
||||
import TileRange from '../../TileRange.js';
|
||||
import TileState from '../../TileState.js';
|
||||
import {IMAGE_SMOOTHING_DISABLED, IMAGE_SMOOTHING_ENABLED} from './common.js';
|
||||
@@ -13,6 +15,7 @@ import {
|
||||
} from '../../transform.js';
|
||||
import {assign} from '../../obj.js';
|
||||
import {
|
||||
containsCoordinate,
|
||||
createEmpty,
|
||||
equals,
|
||||
getIntersection,
|
||||
@@ -22,6 +25,7 @@ import {cssOpacity} from '../../css.js';
|
||||
import {fromUserExtent} from '../../proj.js';
|
||||
import {getUid} from '../../util.js';
|
||||
import {numberSafeCompareFunction} from '../../array.js';
|
||||
import {toSize} from '../../size.js';
|
||||
|
||||
/**
|
||||
* @classdesc
|
||||
@@ -136,6 +140,79 @@ class CanvasTileLayerRenderer extends CanvasLayerRenderer {
|
||||
return tile;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import("../../pixel.js").Pixel} pixel Pixel.
|
||||
* @return {Uint8ClampedArray} Data at the pixel location.
|
||||
*/
|
||||
getData(pixel) {
|
||||
const frameState = this.frameState;
|
||||
if (!frameState) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const layer = this.getLayer();
|
||||
const coordinate = applyTransform(
|
||||
frameState.pixelToCoordinateTransform,
|
||||
pixel.slice()
|
||||
);
|
||||
|
||||
const layerExtent = layer.getExtent();
|
||||
if (layerExtent) {
|
||||
if (!containsCoordinate(layerExtent, coordinate)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const pixelRatio = frameState.pixelRatio;
|
||||
const projection = frameState.viewState.projection;
|
||||
const viewState = frameState.viewState;
|
||||
const source = layer.getRenderSource();
|
||||
const tileGrid = source.getTileGridForProjection(viewState.projection);
|
||||
const tilePixelRatio = source.getTilePixelRatio(frameState.pixelRatio);
|
||||
|
||||
for (
|
||||
let z = tileGrid.getZForResolution(viewState.resolution);
|
||||
z >= tileGrid.getMinZoom();
|
||||
--z
|
||||
) {
|
||||
const tileCoord = tileGrid.getTileCoordForCoordAndZ(coordinate, z);
|
||||
const tile = source.getTile(
|
||||
z,
|
||||
tileCoord[1],
|
||||
tileCoord[2],
|
||||
pixelRatio,
|
||||
projection
|
||||
);
|
||||
if (!(tile instanceof ImageTile || tile instanceof ReprojTile)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (tile.getState() !== TileState.LOADED) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const tileOrigin = tileGrid.getOrigin(z);
|
||||
const tileSize = toSize(tileGrid.getTileSize(z));
|
||||
const tileResolution = tileGrid.getResolution(z);
|
||||
|
||||
const col = Math.floor(
|
||||
tilePixelRatio *
|
||||
((coordinate[0] - tileOrigin[0]) / tileResolution -
|
||||
tileCoord[1] * tileSize[0])
|
||||
);
|
||||
|
||||
const row = Math.floor(
|
||||
tilePixelRatio *
|
||||
((tileOrigin[1] - coordinate[1]) / tileResolution -
|
||||
tileCoord[2] * tileSize[1])
|
||||
);
|
||||
|
||||
return this.getImageData(tile.getImage(), col, row);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Object<number, Object<string, import("../../Tile.js").default>>} tiles Lookup of loaded tiles by zoom level.
|
||||
* @param {number} zoom Zoom level.
|
||||
|
||||
Reference in New Issue
Block a user