使用 OpenLayers 5 显示地理参考图像

2024-01-06

我正在尝试制作一个应用程序,用户可以在其中对扫描地图进行地理配准。您可以在此处查看示例:https://codesandbox.io/s/2o99jvrnyy https://codesandbox.io/s/2o99jvrnyy有两个图像:

  • 资产/测试.png https://uploads.codesandbox.io/uploads/user/c5698c75-7c6e-4ce1-aade-62711b8a02d2/Coew-test.png- 不旋转
  • 资产/test_rotation.png https://uploads.codesandbox.io/uploads/user/c5698c75-7c6e-4ce1-aade-62711b8a02d2/M6Pw-test_rotation.png- 带旋转

第一个图像已正确加载到地图上,但旋转的图像则不然。

我找不到有关 OpenLayers 5 是否可以处理具有存储在世界文件中的变换参数的图像的信息。可能我错过了一些东西,但不知道是什么。

我的逻辑是这样的:

使用 4 个点通过仿射变换计算变换参数。你可以看到其中的逻辑仿射.js文件。从源图像和地图中至少选取 4 个点。然后使用这 4 个点计算变换参数。之后我计算图像的范围:

width = image.width in pixels
height = image.height in pixels
width *= Math.sqrt(Math.pow(parameters.A, 2) + Math.pow(parameters.D, 2));
height *= Math.sqrt(Math.pow(parameters.B, 2) + Math.pow(parameters.E, 2));

// then the extent in projection units is
extent = [parameters.C, parameters.F - height, parameters.C + width, parameters.F];

世界文件参数按照定义计算here https://en.wikipedia.org/wiki/World_file#Definition.

问题可能是在 OpenLayers 5 中作为静态图像加载时,带有旋转的图像不会旋转,但找不到方法来做到这一点。

我尝试使用计算的参数在 QGIS 和 ArcMap 中加载这两个图像,并且它们都正确加载。你可以看到第二张图的结果:

您可以在此处查看每个图像的参数:

Image: test.png
Calculated extent: [436296.79726721847, 4666723.973240128, 439864.3389057907, 4669253.416495154]
Calculated parameters (for world file):
3.8359372067274027
-0.03146800786355865
-0.03350636818089405
-3.820764346376064
436296.79726721847
4669253.416495154

Image: test_rotation.png
Calculated extent: [437178.8291026594, 4667129.767589236, 440486.91675884253, 4669768.939256327]
Calculated parameters (for world file):
3.506332904308879
-1.2831186688536016
-1.3644002712982917
-3.7014921022625864
437178.8291026594
4669768.939256327

我意识到我的做法是错误的。地图投影中无需计算图像的范围并将其设置在图层中。我可以简单地添加一个转换函数,负责在图像投影和地图投影之间转换坐标。这样,图像层始终将其投影设置为图像投影,将范围设置为图像大小(以像素为单位)。

添加变换函数如下:

import { addCoordinateTransforms } from 'ol/proj.js';

addCoordinateTransforms(
  mapProjection,
  imageProjection,
  coords => {
    // forward
    return Affine.transform(coords);
  },
  coords => {
    // inverse
  }
)

仿射参数再次从至少 3 个点计算:

// mapPoints - coordinates in map projection
// imagePoints - coordinates in image projection
Affine.calculate(mapPoints, imagePoints);

您可以在这里查看完整的示例 -https://kw9l85y5po.codesandbox.io/ https://kw9l85y5po.codesandbox.io/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 OpenLayers 5 显示地理参考图像 的相关文章

随机推荐