【问题标题】:OpenLayers GeoJSON Layer projection not workingOpenLayers GeoJSON 图层投影不起作用
【发布时间】:2021-06-29 19:53:17
【问题描述】:

使用下面的代码,我无法让我的单个 GeoJSON 点特征出现在地图上。或者,如果它确实出现在正确位置的地图上。它出现在德国,而它应该出现在澳大利亚的塔斯马尼亚。如果我根本不提供投影,它会出现在相同的位置。因此,我设置投影的方式似乎存在一些问题。

我感觉我使用了 defaultDataProjection 和/或 featureProjection 不正确,但我发现文档中关于如何使用它们的内容相当不清楚。

同样的投影适用于我的其他(非 GeoJSON)层。

如何更正我的代码以使其正常工作?

var geojsonObject = {"type":"FeatureCollection","features":[{"geometry":{"type":"Point","coordinates":[503619.026899999939,5420925.347199999727]},"properties":{"label":{}},"type":"Feature"}]};

proj4.defs('layerProj', '+proj=utm +zone=55 +south +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs ')

var vectorSource = new ol.source.Vector({
    features: (new ol.format.GeoJSON({
        defaultDataProjection: 'layerProj',
    }).readFeatures(geojsonObject, { featureProjection: 'layerProj' }))
})

var vectorLayer = new ol.layer.Vector({
  source: vectorSource,
  style: new ol.style.Style({image: new ol.style.Circle({fill: new ol.style.Fill({color: '#8888FF'}), radius: 10, points: 0})}),
});
    
map.addLayer(vectorLayer);

【问题讨论】:

    标签: javascript geojson openlayers-3 projection


    【解决方案1】:

    您的format: 应该是features: 并且在OpenLayers 4 的GeoJSON 选项中的defaultDataProjection 在更高版本中更改为dataProjection,因此最好在readFeatures 选项中指定dataProjection,其中名称在所有版本中都相同。 featureProjection 应始终设置为视图投影

            features: new ol.format.GeoJSON().readFeatures(geojsonObject, { 
              dataProjection: 'layerProj',
              featureProjection: map.getView().getProjection()
            })
    

    如果您使用的是 OpenLayers 5 或更高版本,您还需要在 layerproj 定义后注册 proj4

    ol.proj.proj4.register(proj4);
    

    【讨论】:

    • format: 是一个错字。我的代码中有features:。不知道我的复制/粘贴到这里是如何变化的。我现在已经在原始问题中解决了这个问题。感谢您提供其他信息。我会试一试并报告。另外,我正在使用OL 3(非常老派!我需要更新一天!)
    • 谢谢。您对dataProjection:featureProjection: 的配置是正确的。我的 GeoJSON 功能现在完美地显示在我的 OL3 地图上。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    相关资源
    最近更新 更多