背景
OpenLayers 3 新版本能够以不同于服务器提供的坐标系显示来自WMS,WMTS,静态图像和许多其他来源的栅格数据。图像的地图投影的转换直接在Web浏览器中进行。任何Proj4js支持的坐标参考系统中的视图都是可能的,并且以前不兼容的图层现在可以组合和覆盖。
Raster Reproject意义
可以在浏览器端实现不同资源在不同投影下的转换,不再依赖于服务端处理,比如在Geoserver中指定投影类型,还是相当给力的
效果图
image还可以看以下效果对比图
4326 3857
3857使用web Mercator 中国区域是方形,Reproject到4326时,就有些扁了,在相同的中心点,缩放级别下,地图的视察范围也不太一致,4326经过压扁后,能显示更多的范围。
再来一张放大后的,变形明显。
image分析过程
以reprojection-image为例说明,使用的是三角仿射变换triangle affine transformation。
三角形动态计算,以正好铺满当前视口
不同的缩放级别1
不同的缩放级别1
不同的缩放级别2
不同的缩放级别2
不同的缩放级别3
不同的缩放级别3
不同的缩放级别4
不同的缩放级别4
不同的比例尺下会更新三角格网的大小,以恰好平分地图视口,至少两个三角
每个三角形单独进行Reproject,不同三角之间互不影响
对三角格网渲染进行Degbu过滤后示意
渲染其中一部分三角
渲染偶数三角
这个图能方便明白Reproject原理
- 首先划分三角格网
- 根据位置,计算每个三角网对应的原始raster位置和区域,内部使用数学方法,求出转换参数,对每个三角网进行转换,这里其实并没有对每个像素进行Reproject,而是以三角为最小单位,进行Reproject, 理论上只要三角足够小,通过积分是可以达到同样的效果的,相比像素角度的处理效率也会高很多
- 将多个三角进行无缘拼接
核心实现
三角Reproject
源码ol/reproj.js
, render
funcction
/**
* Renders the source data into new canvas based on the triangulation.
*
* @param {number} width Width of the canvas.
* @param {number} height Height of the canvas.
* @param {number} pixelRatio Pixel ratio.
* @param {number} sourceResolution Source resolution.
* @param {import("./extent.js").Extent} sourceExtent Extent of the data source.
* @param {number} targetResolution Target resolution.
* @param {import("./extent.js").Extent} targetExtent Target extent.
* @param {import("./reproj/Triangulation.js").default} triangulation
* Calculated triangulation.
* @param {Array<{extent: import("./extent.js").Extent,
* image: (HTMLCanvasElement|HTMLImageElement|HTMLVideoElement)}>} sources
* Array of sources.
* @param {number} gutter Gutter of the sources.
* @param {boolean=} opt_renderEdges Render reprojection edges.
* @return {HTMLCanvasElement} Canvas with reprojected data.
*/
export function render(width, height, pixelRatio,
sourceResolution, sourceExtent, targetResolution, targetExtent,
triangulation, sources, gutter, opt_renderEdges) {
var context = createCanvasContext2D(Math.round(pixelRatio * width),
Math.round(pixelRatio * height));
if (sources.length === 0) {
return context.canvas;
}
context.scale(pixelRatio, pixelRatio);
var sourceDataExtent = createEmpty();
sources.forEach(function(src, i, arr) {
extend(sourceDataExtent, src.extent);
});
var canvasWidthInUnits = getWidth(sourceDataExtent);
var canvasHeightInUnits = getHeight(sourceDataExtent);
// 加载image的内部canvas
var stitchContext = createCanvasContext2D(
Math.round(pixelRatio * canvasWidthInUnits / sourceResolution),
Math.round(pixelRatio * canvasHeightInUnits / sourceResolution));
var stitchScale = pixelRatio / sourceResolution;
sources.forEach(function(src, i, arr) {
var xPos = src.extent[0] - sourceDataExtent[0];
var yPos = -(src.extent[3] - sourceDataExtent[3]);
var srcWidth = getWidth(src.extent);
var srcHeight = getHeight(src.extent);
stitchContext.drawImage(
src.image,
gutter, gutter,
src.image.width - 2 * gutter, src.image.height - 2 * gutter,
xPos * stitchScale, yPos * stitchScale,
srcWidth * stitchScale, srcHeight * stitchScale);
});
var targetTopLeft = getTopLeft(targetExtent);
// 对三角进行循环处理
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.
*
* To optimize number of context calls and increase numerical stability,
* we also do the following operations:
* trans(-topLeftExtentCorner), scale(1 / targetResolution), scale(1, -1)
* here before solving the linear system so [ui, vi] are pixel coordinates.
*
* Src points: xi, yi
* Dst points: ui, vi
* Affine coefficients: aij
*
* | x0 y0 1 0 0 0 | |a00| |u0|
* | x1 y1 1 0 0 0 | |a01| |u1|
* | x2 y2 1 0 0 0 | x |a02| = |u2|
* | 0 0 0 x0 y0 1 | |a10| |v0|
* | 0 0 0 x1 y1 1 | |a11| |v1|
* | 0 0 0 x2 y2 1 | |a12| |v2|
*/
var source = triangle.source;
var target = triangle.target;
var x0 = source[0][0], y0 = source[0][1];
var x1 = source[1][0], y1 = source[1][1];
var x2 = source[2][0], y2 = source[2][1];
var u0 = (target[0][0] - targetTopLeft[0]) / targetResolution;
var v0 = -(target[0][1] - targetTopLeft[1]) / targetResolution;
var u1 = (target[1][0] - targetTopLeft[0]) / targetResolution;
var v1 = -(target[1][1] - targetTopLeft[1]) / targetResolution;
var u2 = (target[2][0] - targetTopLeft[0]) / targetResolution;
var v2 = -(target[2][1] - targetTopLeft[1]) / targetResolution;
// Shift all the source points to improve numerical stability
// of all the subsequent calculations. The [x0, y0] is used here.
// This is also used to simplify the linear system.
var sourceNumericalShiftX = x0;
var sourceNumericalShiftY = y0;
x0 = 0;
y0 = 0;
x1 -= sourceNumericalShiftX;
y1 -= sourceNumericalShiftY;
x2 -= sourceNumericalShiftX;
y2 -= sourceNumericalShiftY;
var augmentedMatrix = [
[x1, y1, 0, 0, u1 - u0],
[x2, y2, 0, 0, u2 - u0],
[0, 0, x1, y1, v1 - v0],
[0, 0, x2, y2, v2 - v0]
];
var affineCoefs = solveLinearSystem(augmentedMatrix);
if (!affineCoefs) {
return;
}
context.save();
context.beginPath();
var centroidX = (u0 + u1 + u2) / 3;
var centroidY = (v0 + v1 + v2) / 3;
var p0 = enlargeClipPoint(centroidX, centroidY, u0, v0);
var p1 = enlargeClipPoint(centroidX, centroidY, u1, v1);
var p2 = enlargeClipPoint(centroidX, centroidY, u2, v2);
// 设置三角的切割范围,只显示这个范围内的图片,多个三角拼接起来就是整体图像
context.moveTo(p1[0], p1[1]);
context.lineTo(p0[0], p0[1]);
context.lineTo(p2[0], p2[1]);
context.clip();
// 最关键的三个方法,通过一系列转换,保证在相应的比例尺下在当前范围内
// 显示正确的Reproject图像
// 直接设置下面3个参数,不好容易能达到想要的效果
// 可以通过将相同的参数输出在本地,进行调试,观察三角内的图像
context.transform(
affineCoefs[0], affineCoefs[2], affineCoefs[1], affineCoefs[3], u0, v0);
context.translate(sourceDataExtent[0] - sourceNumericalShiftX,
sourceDataExtent[3] - sourceNumericalShiftY);
context.scale(sourceResolution / pixelRatio,
-sourceResolution / pixelRatio);
context.drawImage(stitchContext.canvas, 0, 0);
context.restore();
});
// 调试使用,是否显示三角
if (opt_renderEdges) {
context.save();
context.strokeStyle = 'black';
context.lineWidth = 1;
triangulation.getTriangles().forEach(function(triangle, i, arr) {
var target = triangle.target;
var u0 = (target[0][0] - targetTopLeft[0]) / targetResolution;
var v0 = -(target[0][1] - targetTopLeft[1]) / targetResolution;
var u1 = (target[1][0] - targetTopLeft[0]) / targetResolution;
var v1 = -(target[1][1] - targetTopLeft[1]) / targetResolution;
var u2 = (target[2][0] - targetTopLeft[0]) / targetResolution;
var v2 = -(target[2][1] - targetTopLeft[1]) / targetResolution;
context.beginPath();
context.moveTo(u1, v1);
context.lineTo(u0, v0);
context.lineTo(u2, v2);
context.closePath();
context.stroke();
});
context.restore();
}
return context.canvas;
}
我以其中一个三角的计算参数为示例,可以看到参数还是比较复杂的
context.moveTo(1136, 199);
context.lineTo(850, 200);
context.lineTo(1136, 401);
context.clip();
context.transform(
0.011129369548469296, 0.00006403305449769778, 0.00009456173415458175, -0.0111338055978892, 851.2500000000001, 200.00000000000063);
context.translate(-413988.7802610396,
835088.5497620346);
context.scale(351.73160173160176,
-351.73160173160176);
context.drawImage(stitchContext.canvas, 0, 0);
context.restore();
image
其中,进行仿射变换的转换过程及数学原理,还需要进一步研究。
网友评论