Openlayers raster reprojection 柵格重投影原理分析

背景

OpenLayers 3 新版本能夠以不同于服務(wù)器提供的坐標(biāo)系顯示來自WMS,WMTS,靜態(tài)圖像和許多其他來源的柵格數(shù)據(jù)。圖像的地圖投影的轉(zhuǎn)換直接在Web瀏覽器中進(jìn)行。任何Proj4js支持的坐標(biāo)參考系統(tǒng)中的視圖都是可能的,并且以前不兼容的圖層現(xiàn)在可以組合和覆蓋。

Raster Reproject意義

可以在瀏覽器端實現(xiàn)不同資源在不同投影下的轉(zhuǎn)換,不再依賴于服務(wù)端處理,比如在Geoserver中指定投影類型,還是相當(dāng)給力的

效果圖

image

還可以看以下效果對比圖


4326
3857

3857使用web Mercator 中國區(qū)域是方形,Reproject到4326時,就有些扁了,在相同的中心點,縮放級別下,地圖的視察范圍也不太一致,4326經(jīng)過壓扁后,能顯示更多的范圍。

再來一張放大后的,變形明顯。

image

分析過程

reprojection-image為例說明,使用的是三角仿射變換triangle affine transformation。

三角形動態(tài)計算,以正好鋪滿當(dāng)前視口

不同的縮放級別1


不同的縮放級別1

不同的縮放級別2


不同的縮放級別2

不同的縮放級別3


不同的縮放級別3

不同的縮放級別4


不同的縮放級別4

不同的比例尺下會更新三角格網(wǎng)的大小,以恰好平分地圖視口,至少兩個三角

每個三角形單獨(dú)進(jìn)行Reproject,不同三角之間互不影響

對三角格網(wǎng)渲染進(jìn)行Degbu過濾后示意

渲染其中一部分三角


渲染偶數(shù)三角

這個圖能方便明白Reproject原理

  • 首先劃分三角格網(wǎng)
  • 根據(jù)位置,計算每個三角網(wǎng)對應(yīng)的原始raster位置和區(qū)域,內(nèi)部使用數(shù)學(xué)方法,求出轉(zhuǎn)換參數(shù),對每個三角網(wǎng)進(jìn)行轉(zhuǎn)換,這里其實并沒有對每個像素進(jìn)行Reproject,而是以三角為最小單位,進(jìn)行Reproject, 理論上只要三角足夠小,通過積分是可以達(dá)到同樣的效果的,相比像素角度的處理效率也會高很多
  • 將多個三角進(jìn)行無緣拼接

核心實現(xiàn)

三角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的內(nèi)部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);

  // 對三角進(jìn)行循環(huán)處理
  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);

    // 設(shè)置三角的切割范圍,只顯示這個范圍內(nèi)的圖片,多個三角拼接起來就是整體圖像
    context.moveTo(p1[0], p1[1]);
    context.lineTo(p0[0], p0[1]);
    context.lineTo(p2[0], p2[1]);
    context.clip();

    // 最關(guān)鍵的三個方法,通過一系列轉(zhuǎn)換,保證在相應(yīng)的比例尺下在當(dāng)前范圍內(nèi)
    // 顯示正確的Reproject圖像
    // 直接設(shè)置下面3個參數(shù),不好容易能達(dá)到想要的效果
    // 可以通過將相同的參數(shù)輸出在本地,進(jìn)行調(diào)試,觀察三角內(nèi)的圖像
    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();
  });

  // 調(diào)試使用,是否顯示三角
  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;
}

我以其中一個三角的計算參數(shù)為示例,可以看到參數(shù)還是比較復(fù)雜的

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

其中,進(jìn)行仿射變換的轉(zhuǎn)換過程及數(shù)學(xué)原理,還需要進(jìn)一步研究。

參考

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容