Remove deprecated method PluggableMap#forEachLayerAtPixel

This commit is contained in:
Maximilian Krög
2022-07-24 21:39:34 +02:00
parent 0f8de89318
commit 2897f03ea5
12 changed files with 39 additions and 634 deletions

View File

@@ -693,51 +693,6 @@ class PluggableMap extends BaseObject {
return layers;
}
/**
* Please the `layer.getData()` method for {@link module:ol/layer/Tile~TileLayer#getData tile layers} or
* {@link module:ol/layer/Image~ImageLayer#getData image layers} instead of using this method.
*
* Detect layers that have a color value at a pixel on the viewport, and
* execute a callback with each matching layer. Layers included in the
* detection can be configured through `opt_layerFilter`.
*
* Note: In maps with more than one layer, this method will typically return pixel data
* representing the composed image of all layers visible at the given pixel because layers
* will generally share the same rendering context. To force layers to render separately, and
* to get pixel data representing only one layer at a time, you can assign each layer a unique
* `className` in its constructor.
*
* @param {import("./pixel.js").Pixel} pixel Pixel.
* @param {function(this: S, import("./layer/Layer.js").default, (Uint8ClampedArray|Uint8Array)): T} callback
* Layer callback. This callback will receive two arguments: first is the
* {@link module:ol/layer/Layer~Layer layer}, second argument is an array representing
* [R, G, B, A] pixel values (0 - 255) and will be `null` for layer types
* that do not currently support this argument. To stop detection, callback
* functions can return a truthy value.
* @param {AtPixelOptions} [opt_options] Configuration options.
* @return {T|undefined} Callback result, i.e. the return value of last
* callback execution, or the first truthy callback return value.
* @template S,T
* @api
* @deprecated
*/
forEachLayerAtPixel(pixel, callback, opt_options) {
if (!this.frameState_ || !this.renderer_) {
return;
}
const options = opt_options || {};
const hitTolerance =
options.hitTolerance !== undefined ? options.hitTolerance : 0;
const layerFilter = options.layerFilter || TRUE;
return this.renderer_.forEachLayerAtPixel(
pixel,
this.frameState_,
hitTolerance,
callback,
layerFilter
);
}
/**
* Detect if features intersect a pixel on the viewport. Layers included in the
* detection can be configured through `opt_layerFilter`.

View File

@@ -83,11 +83,6 @@ import {listen, unlistenByKey} from '../events.js';
*
* A generic `change` event is fired when the state of the source changes.
*
* Please note that for performance reasons several layers might get rendered to
* the same HTML element, which will cause {@link import("../PluggableMap.js").default#forEachLayerAtPixel map.forEachLayerAtPixel()} to
* give false positives. To avoid this, apply different `className` properties to the
* layers at creation time.
*
* @fires import("../render/Event.js").RenderEvent#prerender
* @fires import("../render/Event.js").RenderEvent#postrender
*
@@ -310,8 +305,7 @@ class Layer extends BaseLayer {
/**
* Sets the layer to be rendered on top of other layers on a map. The map will
* not manage this layer in its layers collection, and the callback in
* {@link module:ol/Map~Map#forEachLayerAtPixel} will receive `null` as layer. This
* not manage this layer in its layers collection. This
* is useful for temporary layers. To remove an unmanaged layer from the map,
* use `#setMap(null)`.
*

View File

@@ -149,50 +149,6 @@ class CompositeMapRenderer extends MapRenderer {
this.scheduleExpireIconCache(frameState);
}
/**
* @param {import("../pixel.js").Pixel} pixel Pixel.
* @param {import("../PluggableMap.js").FrameState} frameState FrameState.
* @param {number} hitTolerance Hit tolerance in pixels.
* @param {function(import("../layer/Layer.js").default<import("../source/Source").default>, (Uint8ClampedArray|Uint8Array)): T} callback Layer
* callback.
* @param {function(import("../layer/Layer.js").default<import("../source/Source").default>): boolean} layerFilter Layer filter
* function, only layers which are visible and for which this function
* returns `true` will be tested for features. By default, all visible
* layers will be tested.
* @return {T|undefined} Callback result.
* @template T
*/
forEachLayerAtPixel(pixel, frameState, hitTolerance, callback, layerFilter) {
const viewState = frameState.viewState;
const layerStates = frameState.layerStatesArray;
const numLayers = layerStates.length;
for (let i = numLayers - 1; i >= 0; --i) {
const layerState = layerStates[i];
const layer = layerState.layer;
if (
layer.hasRenderer() &&
inView(layerState, viewState) &&
layerFilter(layer)
) {
const layerRenderer = layer.getRenderer();
const data = layerRenderer.getDataAtPixel(
pixel,
frameState,
hitTolerance
);
if (data) {
const result = callback(layer, data);
if (result) {
return result;
}
}
}
}
return undefined;
}
}
export default CompositeMapRenderer;

View File

@@ -133,19 +133,6 @@ class LayerRenderer extends Observable {
return undefined;
}
/**
* @abstract
* @param {import("../pixel.js").Pixel} pixel Pixel.
* @param {import("../PluggableMap.js").FrameState} frameState FrameState.
* @param {number} hitTolerance Hit tolerance in pixels.
* @return {Uint8ClampedArray|Uint8Array} The result. If there is no data at the pixel
* location, null will be returned. If there is data, but pixel values cannot be
* returned, and empty array will be returned.
*/
getDataAtPixel(pixel, frameState, hitTolerance) {
return null;
}
/**
* @return {LayerType} Layer.
*/

View File

@@ -170,24 +170,6 @@ class MapRenderer extends Disposable {
return result;
}
/**
* @abstract
* @param {import("../pixel.js").Pixel} pixel Pixel.
* @param {import("../PluggableMap.js").FrameState} frameState FrameState.
* @param {number} hitTolerance Hit tolerance in pixels.
* @param {function(import("../layer/Layer.js").default<import("../source/Source").default>, (Uint8ClampedArray|Uint8Array)): T} callback Layer
* callback.
* @param {function(import("../layer/Layer.js").default<import("../source/Source").default>): boolean} layerFilter Layer filter
* function, only layers which are visible and for which this function
* returns `true` will be tested for features. By default, all visible
* layers will be tested.
* @return {T|undefined} Callback result.
* @template T
*/
forEachLayerAtPixel(pixel, frameState, hitTolerance, callback, layerFilter) {
return abstract();
}
/**
* @param {import("../coordinate.js").Coordinate} coordinate Coordinate.
* @param {import("../PluggableMap.js").FrameState} frameState FrameState.

View File

@@ -10,15 +10,14 @@ import {
create as createTransform,
} from '../../transform.js';
import {asArray} from '../../color.js';
import {createCanvasContext2D} from '../../dom.js';
import {equals} from '../../array.js';
import {
containsCoordinate,
getBottomLeft,
getBottomRight,
getTopLeft,
getTopRight,
} from '../../extent.js';
import {createCanvasContext2D} from '../../dom.js';
import {equals} from '../../array.js';
/**
* @type {Array<HTMLCanvasElement>}
@@ -315,65 +314,6 @@ class CanvasLayerRenderer extends LayerRenderer {
);
}
/**
* @param {import("../../pixel.js").Pixel} pixel Pixel.
* @param {import("../../PluggableMap.js").FrameState} frameState FrameState.
* @param {number} hitTolerance Hit tolerance in pixels.
* @return {Uint8ClampedArray|Uint8Array} The result. If there is no data at the pixel
* location, null will be returned. If there is data, but pixel values cannot be
* returned, and empty array will be returned.
*/
getDataAtPixel(pixel, frameState, hitTolerance) {
const renderPixel = applyTransform(
this.inversePixelTransform,
pixel.slice()
);
const context = this.context;
const layer = this.getLayer();
const layerExtent = layer.getExtent();
if (layerExtent) {
const renderCoordinate = applyTransform(
frameState.pixelToCoordinateTransform,
pixel.slice()
);
/** get only data inside of the layer extent */
if (!containsCoordinate(layerExtent, renderCoordinate)) {
return null;
}
}
const x = Math.round(renderPixel[0]);
const y = Math.round(renderPixel[1]);
let pixelContext = this.pixelContext_;
if (!pixelContext) {
const pixelCanvas = document.createElement('canvas');
pixelCanvas.width = 1;
pixelCanvas.height = 1;
pixelContext = pixelCanvas.getContext('2d');
this.pixelContext_ = pixelContext;
}
pixelContext.clearRect(0, 0, 1, 1);
let data;
try {
pixelContext.drawImage(context.canvas, x, y, 1, 1, 0, 0, 1, 1);
data = pixelContext.getImageData(0, 0, 1, 1).data;
} catch (err) {
if (err.name === 'SecurityError') {
// tainted canvas, we assume there is data at the given pixel (although there might not be)
this.pixelContext_ = null;
return new Uint8Array();
}
return data;
}
if (data[3] === 0) {
return null;
}
return data;
}
/**
* Clean up.
*/

View File

@@ -7,11 +7,9 @@ import RenderEvent from '../../render/Event.js';
import RenderEventType from '../../render/EventType.js';
import WebGLHelper from '../../webgl/Helper.js';
import {
apply as applyTransform,
compose as composeTransform,
create as createTransform,
} from '../../transform.js';
import {containsCoordinate} from '../../extent.js';
/**
* @typedef {Object} PostProcessesOptions
@@ -259,68 +257,6 @@ class WebGLLayerRenderer extends LayerRenderer {
postRender(context, frameState) {
this.dispatchRenderEvent_(RenderEventType.POSTRENDER, context, frameState);
}
/**
* @param {import("../../pixel.js").Pixel} pixel Pixel.
* @param {import("../../PluggableMap.js").FrameState} frameState FrameState.
* @param {number} hitTolerance Hit tolerance in pixels.
* @return {Uint8ClampedArray|Uint8Array} The result. If there is no data at the pixel
* location, null will be returned. If there is data, but pixel values cannot be
* returned, and empty array will be returned.
*/
getDataAtPixel(pixel, frameState, hitTolerance) {
const renderPixel = applyTransform(
[frameState.pixelRatio, 0, 0, frameState.pixelRatio, 0, 0],
pixel.slice()
);
const gl = this.helper.getGL();
if (!gl) {
return null;
}
const layer = this.getLayer();
const layerExtent = layer.getExtent();
if (layerExtent) {
const renderCoordinate = applyTransform(
frameState.pixelToCoordinateTransform,
pixel.slice()
);
/** get only data inside of the layer extent */
if (!containsCoordinate(layerExtent, renderCoordinate)) {
return null;
}
}
const attributes = gl.getContextAttributes();
if (!attributes || !attributes.preserveDrawingBuffer) {
// we assume there is data at the given pixel (although there might not be)
return new Uint8Array();
}
const x = Math.round(renderPixel[0]);
const y = Math.round(renderPixel[1]);
let pixelContext = this.pixelContext_;
if (!pixelContext) {
const pixelCanvas = document.createElement('canvas');
pixelCanvas.width = 1;
pixelCanvas.height = 1;
pixelContext = pixelCanvas.getContext('2d');
this.pixelContext_ = pixelContext;
}
pixelContext.clearRect(0, 0, 1, 1);
let data;
try {
pixelContext.drawImage(gl.canvas, x, y, 1, 1, 0, 0, 1, 1);
data = pixelContext.getImageData(0, 0, 1, 1).data;
} catch (err) {
return data;
}
if (data[3] === 0) {
return null;
}
return data;
}
}
export default WebGLLayerRenderer;