【问题标题】:I can't transform geometries to a projection defined in proj4.defs in openlayers 5.3我无法将几何图形转换为 openlayers 5.3 中 proj4.defs 中定义的投影
【发布时间】:2019-05-22 04:13:29
【问题描述】:

我有一个使用 openlayers V5.3.0 的节点映射应用程序。当我尝试将几何图形从 EPSG:4326 转换为使用 proj4.defs 定义的投影时,我收到以下错误消息: TypeError:无法读取 null 的属性“getCode”

如果我将 openLayers 更改为 4.6.4 版,则转换几何图形的代码可以工作。

有其他人遇到过这个问题吗?

下面的 HTML 演示了这个问题。

<html>
<head>
    <meta http-equiv="Cache-Control" content="no-cache">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" >
    <title>Projection Test</title>

    <script
            src="https://code.jquery.com/jquery-2.2.4.min.js"
            integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
            crossorigin="anonymous"></script>

   <!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.4/ol-debug.js"></script> -->

    <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.5.0/proj4.js"></script>


    <script>proj4.defs("EPSG:28355","+proj=utm +zone=55 +south +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs");</script>

     <script>

         $(document).ready(function() {
             let gJSONOpts = {
                 dataProjection: "EPSG:3857",
                 featureProjection: "EPSG:28355"
             };

             let jsonPt = {
                 "type": "Feature",
                 "id": "GEO_BRM_STOPS.fid--79f50be5_16adc4d0ce7_6a6b",
                 "geometry": {
                     "type": "Point",
                     "coordinates": [16154413.50099648, -4988654.4460995]
                 },
                 "geometry_name": "OBJ"
             };

             let feature = new ol.format.GeoJSON().readFeature(jsonPt.geometry, gJSONOpts);
             let tg = feature.getGeometry().transform('EPSG:28355', "EPSG:4326");
             let out = 'Projected Coordinates: ' + tg.getCoordinates()[0] + ', ' +tg.getCoordinates()[1];

             $("#mapvwr").html(out);

         });
     </script>
</head>
<body>
    <div id="mapvwr">

    </div>
</body>
</html>

将以上内容粘贴到 html 文件中并在浏览器中显示。

取消注释这一行:

 <!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.4/ol-debug.js"></script> -->

并注释掉以下行:

<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>

要查看正确的结果:

Projected Coordinates: 145.11756553931886, -40.83921285742692

【问题讨论】:

    标签: javascript openlayers-5 proj4js


    【解决方案1】:

    我发现了问题。 定义投影后,我需要将其添加为脚本ol.proj.proj4.register(proj4);

    【讨论】:

    • 可以在添加proj4的地方添加源代码吗?
    • 是的,你可以在那里编写任何有效的 JavaScript 代码
    • proj4的值是多少
    • 我正在尝试将投影 32640 转换为 3857,但同样的错误是 getCode 未定义。所以我想知道 ol.proj.proj4.register() 的参数
    • proj4.defs("EPSG:28355","+proj=utm +zone=55 +south +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +单位=m +no_defs");
    猜你喜欢
    • 2013-01-15
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多