Make code prettier
This updates ESLint and our shared eslint-config-openlayers to use Prettier. Most formatting changes were automatically applied with this:
npm run lint -- --fix
A few manual changes were required:
* In `examples/offscreen-canvas.js`, the `//eslint-disable-line` comment needed to be moved to the appropriate line to disable the error about the `'worker-loader!./offscreen-canvas.worker.js'` import.
* In `examples/webpack/exapmle-builder.js`, spaces could not be added after a couple `function`s for some reason. While editing this, I reworked `ExampleBuilder` to be a class.
* In `src/ol/format/WMSGetFeatureInfo.js`, the `// @ts-ignore` comment needed to be moved down one line so it applied to the `parsersNS` argument.
This commit is contained in:
169
src/ol/reproj.js
169
src/ol/reproj.js
@@ -1,11 +1,20 @@
|
||||
/**
|
||||
* @module ol/reproj
|
||||
*/
|
||||
import {createCanvasContext2D} from './dom.js';
|
||||
import {containsCoordinate, createEmpty, extend, forEachCorner, getCenter, getHeight, getTopLeft, getWidth} from './extent.js';
|
||||
import {solveLinearSystem} from './math.js';
|
||||
import {getPointResolution, transform} from './proj.js';
|
||||
import {assign} from './obj.js';
|
||||
import {
|
||||
containsCoordinate,
|
||||
createEmpty,
|
||||
extend,
|
||||
forEachCorner,
|
||||
getCenter,
|
||||
getHeight,
|
||||
getTopLeft,
|
||||
getWidth,
|
||||
} from './extent.js';
|
||||
import {createCanvasContext2D} from './dom.js';
|
||||
import {getPointResolution, transform} from './proj.js';
|
||||
import {solveLinearSystem} from './math.js';
|
||||
|
||||
let brokenDiagonalRendering_;
|
||||
|
||||
@@ -41,7 +50,10 @@ function drawTestTriangle(ctx, u1, v1, u2, v2) {
|
||||
*/
|
||||
function verifyBrokenDiagonalRendering(data, offset) {
|
||||
// the values ought to be close to the rgba(210, 0, 0, 0.75)
|
||||
return Math.abs(data[offset * 4] - 210) > 2 || Math.abs(data[offset * 4 + 3] - 0.75 * 255) > 2;
|
||||
return (
|
||||
Math.abs(data[offset * 4] - 210) > 2 ||
|
||||
Math.abs(data[offset * 4 + 3] - 0.75 * 255) > 2
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,9 +74,10 @@ function isBrokenDiagonalRendering() {
|
||||
drawTestTriangle(ctx, 4, 5, 4, 0);
|
||||
drawTestTriangle(ctx, 4, 5, 0, 5);
|
||||
const data = ctx.getImageData(0, 0, 3, 3).data;
|
||||
brokenDiagonalRendering_ = verifyBrokenDiagonalRendering(data, 0) ||
|
||||
verifyBrokenDiagonalRendering(data, 4) ||
|
||||
verifyBrokenDiagonalRendering(data, 8);
|
||||
brokenDiagonalRendering_ =
|
||||
verifyBrokenDiagonalRendering(data, 0) ||
|
||||
verifyBrokenDiagonalRendering(data, 4) ||
|
||||
verifyBrokenDiagonalRendering(data, 8);
|
||||
}
|
||||
|
||||
return brokenDiagonalRendering_;
|
||||
@@ -82,13 +95,20 @@ function isBrokenDiagonalRendering() {
|
||||
* @param {number} targetResolution Target resolution.
|
||||
* @return {number} The best resolution to use. Can be +-Infinity, NaN or 0.
|
||||
*/
|
||||
export function calculateSourceResolution(sourceProj, targetProj,
|
||||
targetCenter, targetResolution) {
|
||||
|
||||
export function calculateSourceResolution(
|
||||
sourceProj,
|
||||
targetProj,
|
||||
targetCenter,
|
||||
targetResolution
|
||||
) {
|
||||
const sourceCenter = transform(targetCenter, targetProj, sourceProj);
|
||||
|
||||
// calculate the ideal resolution of the source data
|
||||
let sourceResolution = getPointResolution(targetProj, targetResolution, targetCenter);
|
||||
let sourceResolution = getPointResolution(
|
||||
targetProj,
|
||||
targetResolution,
|
||||
targetCenter
|
||||
);
|
||||
|
||||
const targetMetersPerUnit = targetProj.getMetersPerUnit();
|
||||
if (targetMetersPerUnit !== undefined) {
|
||||
@@ -105,8 +125,9 @@ export function calculateSourceResolution(sourceProj, targetProj,
|
||||
|
||||
const sourceExtent = sourceProj.getExtent();
|
||||
if (!sourceExtent || containsCoordinate(sourceExtent, sourceCenter)) {
|
||||
const compensationFactor = getPointResolution(sourceProj, sourceResolution, sourceCenter) /
|
||||
sourceResolution;
|
||||
const compensationFactor =
|
||||
getPointResolution(sourceProj, sourceResolution, sourceCenter) /
|
||||
sourceResolution;
|
||||
if (isFinite(compensationFactor) && compensationFactor > 0) {
|
||||
sourceResolution /= compensationFactor;
|
||||
}
|
||||
@@ -115,7 +136,6 @@ export function calculateSourceResolution(sourceProj, targetProj,
|
||||
return sourceResolution;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Calculates ideal resolution to use from the source in order to achieve
|
||||
* pixel mapping as close as possible to 1:1 during reprojection.
|
||||
@@ -128,15 +148,28 @@ export function calculateSourceResolution(sourceProj, targetProj,
|
||||
* @param {number} targetResolution Target resolution.
|
||||
* @return {number} The best resolution to use. Can be +-Infinity, NaN or 0.
|
||||
*/
|
||||
export function calculateSourceExtentResolution(sourceProj, targetProj,
|
||||
targetExtent, targetResolution) {
|
||||
|
||||
export function calculateSourceExtentResolution(
|
||||
sourceProj,
|
||||
targetProj,
|
||||
targetExtent,
|
||||
targetResolution
|
||||
) {
|
||||
const targetCenter = getCenter(targetExtent);
|
||||
let sourceResolution = calculateSourceResolution(sourceProj, targetProj, targetCenter, targetResolution);
|
||||
let sourceResolution = calculateSourceResolution(
|
||||
sourceProj,
|
||||
targetProj,
|
||||
targetCenter,
|
||||
targetResolution
|
||||
);
|
||||
|
||||
if (!isFinite(sourceResolution) || sourceResolution <= 0) {
|
||||
forEachCorner(targetExtent, function(corner) {
|
||||
sourceResolution = calculateSourceResolution(sourceProj, targetProj, corner, targetResolution);
|
||||
forEachCorner(targetExtent, function (corner) {
|
||||
sourceResolution = calculateSourceResolution(
|
||||
sourceProj,
|
||||
targetProj,
|
||||
corner,
|
||||
targetResolution
|
||||
);
|
||||
return isFinite(sourceResolution) && sourceResolution > 0;
|
||||
});
|
||||
}
|
||||
@@ -144,7 +177,6 @@ export function calculateSourceExtentResolution(sourceProj, targetProj,
|
||||
return sourceResolution;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Renders the source data into new canvas based on the triangulation.
|
||||
*
|
||||
@@ -165,12 +197,24 @@ export function calculateSourceExtentResolution(sourceProj, targetProj,
|
||||
* @param {object=} opt_contextOptions Properties to set on the canvas context.
|
||||
* @return {HTMLCanvasElement} Canvas with reprojected data.
|
||||
*/
|
||||
export function render(width, height, pixelRatio,
|
||||
sourceResolution, sourceExtent, targetResolution, targetExtent,
|
||||
triangulation, sources, gutter, opt_renderEdges, opt_contextOptions) {
|
||||
|
||||
const context = createCanvasContext2D(Math.round(pixelRatio * width),
|
||||
Math.round(pixelRatio * height));
|
||||
export function render(
|
||||
width,
|
||||
height,
|
||||
pixelRatio,
|
||||
sourceResolution,
|
||||
sourceExtent,
|
||||
targetResolution,
|
||||
targetExtent,
|
||||
triangulation,
|
||||
sources,
|
||||
gutter,
|
||||
opt_renderEdges,
|
||||
opt_contextOptions
|
||||
) {
|
||||
const context = createCanvasContext2D(
|
||||
Math.round(pixelRatio * width),
|
||||
Math.round(pixelRatio * height)
|
||||
);
|
||||
assign(context, opt_contextOptions);
|
||||
|
||||
if (sources.length === 0) {
|
||||
@@ -186,20 +230,21 @@ export function render(width, height, pixelRatio,
|
||||
context.globalCompositeOperation = 'lighter';
|
||||
|
||||
const sourceDataExtent = createEmpty();
|
||||
sources.forEach(function(src, i, arr) {
|
||||
sources.forEach(function (src, i, arr) {
|
||||
extend(sourceDataExtent, src.extent);
|
||||
});
|
||||
|
||||
const canvasWidthInUnits = getWidth(sourceDataExtent);
|
||||
const canvasHeightInUnits = getHeight(sourceDataExtent);
|
||||
const stitchContext = createCanvasContext2D(
|
||||
Math.round(pixelRatio * canvasWidthInUnits / sourceResolution),
|
||||
Math.round(pixelRatio * canvasHeightInUnits / sourceResolution));
|
||||
Math.round((pixelRatio * canvasWidthInUnits) / sourceResolution),
|
||||
Math.round((pixelRatio * canvasHeightInUnits) / sourceResolution)
|
||||
);
|
||||
assign(stitchContext, opt_contextOptions);
|
||||
|
||||
const stitchScale = pixelRatio / sourceResolution;
|
||||
|
||||
sources.forEach(function(src, i, arr) {
|
||||
sources.forEach(function (src, i, arr) {
|
||||
const xPos = src.extent[0] - sourceDataExtent[0];
|
||||
const yPos = -(src.extent[3] - sourceDataExtent[3]);
|
||||
const srcWidth = getWidth(src.extent);
|
||||
@@ -209,16 +254,21 @@ export function render(width, height, pixelRatio,
|
||||
if (src.image.width > 0 && src.image.height > 0) {
|
||||
stitchContext.drawImage(
|
||||
src.image,
|
||||
gutter, gutter,
|
||||
src.image.width - 2 * gutter, src.image.height - 2 * gutter,
|
||||
xPos * stitchScale, yPos * stitchScale,
|
||||
srcWidth * stitchScale, srcHeight * stitchScale);
|
||||
gutter,
|
||||
gutter,
|
||||
src.image.width - 2 * gutter,
|
||||
src.image.height - 2 * gutter,
|
||||
xPos * stitchScale,
|
||||
yPos * stitchScale,
|
||||
srcWidth * stitchScale,
|
||||
srcHeight * stitchScale
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
const targetTopLeft = getTopLeft(targetExtent);
|
||||
|
||||
triangulation.getTriangles().forEach(function(triangle, i, arr) {
|
||||
triangulation.getTriangles().forEach(function (triangle, i, arr) {
|
||||
/* Calculate affine transform (src -> dst)
|
||||
* Resulting matrix can be used to transform coordinate
|
||||
* from `sourceProjection` to destination pixels.
|
||||
@@ -241,9 +291,12 @@ export function render(width, height, pixelRatio,
|
||||
*/
|
||||
const source = triangle.source;
|
||||
const target = triangle.target;
|
||||
let x0 = source[0][0], y0 = source[0][1];
|
||||
let x1 = source[1][0], y1 = source[1][1];
|
||||
let x2 = source[2][0], y2 = source[2][1];
|
||||
let x0 = source[0][0],
|
||||
y0 = source[0][1];
|
||||
let x1 = source[1][0],
|
||||
y1 = source[1][1];
|
||||
let x2 = source[2][0],
|
||||
y2 = source[2][1];
|
||||
const u0 = (target[0][0] - targetTopLeft[0]) / targetResolution;
|
||||
const v0 = -(target[0][1] - targetTopLeft[1]) / targetResolution;
|
||||
const u1 = (target[1][0] - targetTopLeft[0]) / targetResolution;
|
||||
@@ -267,7 +320,7 @@ export function render(width, height, pixelRatio,
|
||||
[x1, y1, 0, 0, u1 - u0],
|
||||
[x2, y2, 0, 0, u2 - u0],
|
||||
[0, 0, x1, y1, v1 - v0],
|
||||
[0, 0, x2, y2, v2 - v0]
|
||||
[0, 0, x2, y2, v2 - v0],
|
||||
];
|
||||
const affineCoefs = solveLinearSystem(augmentedMatrix);
|
||||
if (!affineCoefs) {
|
||||
@@ -293,10 +346,16 @@ export function render(width, height, pixelRatio,
|
||||
const vd = v0r - v1r;
|
||||
for (let step = 0; step < steps; step++) {
|
||||
// Go horizontally
|
||||
context.lineTo(u1r + pixelRound((step + 1) * ud / steps), v1r + pixelRound(step * vd / (steps - 1)));
|
||||
context.lineTo(
|
||||
u1r + pixelRound(((step + 1) * ud) / steps),
|
||||
v1r + pixelRound((step * vd) / (steps - 1))
|
||||
);
|
||||
// Go vertically
|
||||
if (step != (steps - 1)) {
|
||||
context.lineTo(u1r + pixelRound((step + 1) * ud / steps), v1r + pixelRound((step + 1) * vd / (steps - 1)));
|
||||
if (step != steps - 1) {
|
||||
context.lineTo(
|
||||
u1r + pixelRound(((step + 1) * ud) / steps),
|
||||
v1r + pixelRound(((step + 1) * vd) / (steps - 1))
|
||||
);
|
||||
}
|
||||
}
|
||||
// We are almost at u0r, v0r
|
||||
@@ -310,13 +369,23 @@ export function render(width, height, pixelRatio,
|
||||
context.clip();
|
||||
|
||||
context.transform(
|
||||
affineCoefs[0], affineCoefs[2], affineCoefs[1], affineCoefs[3], u0, v0);
|
||||
affineCoefs[0],
|
||||
affineCoefs[2],
|
||||
affineCoefs[1],
|
||||
affineCoefs[3],
|
||||
u0,
|
||||
v0
|
||||
);
|
||||
|
||||
context.translate(sourceDataExtent[0] - sourceNumericalShiftX,
|
||||
sourceDataExtent[3] - sourceNumericalShiftY);
|
||||
context.translate(
|
||||
sourceDataExtent[0] - sourceNumericalShiftX,
|
||||
sourceDataExtent[3] - sourceNumericalShiftY
|
||||
);
|
||||
|
||||
context.scale(sourceResolution / pixelRatio,
|
||||
-sourceResolution / pixelRatio);
|
||||
context.scale(
|
||||
sourceResolution / pixelRatio,
|
||||
-sourceResolution / pixelRatio
|
||||
);
|
||||
|
||||
context.drawImage(stitchContext.canvas, 0, 0);
|
||||
context.restore();
|
||||
@@ -328,7 +397,7 @@ export function render(width, height, pixelRatio,
|
||||
context.strokeStyle = 'black';
|
||||
context.lineWidth = 1;
|
||||
|
||||
triangulation.getTriangles().forEach(function(triangle, i, arr) {
|
||||
triangulation.getTriangles().forEach(function (triangle, i, arr) {
|
||||
const target = triangle.target;
|
||||
const u0 = (target[0][0] - targetTopLeft[0]) / targetResolution;
|
||||
const v0 = -(target[0][1] - targetTopLeft[1]) / targetResolution;
|
||||
|
||||
Reference in New Issue
Block a user