【问题标题】:OpenLayers read / write GeoJSONOpenLayers 读/写 GeoJSON
【发布时间】:2020-03-31 03:46:53
【问题描述】:

我尝试将 OpenLayers 地图图层的要素存储到数据库中,因此我尝试将要素对象写入 GeoJSON 对象。但是当我尝试读取对象时出现Unsupported GeoJSON type: undefined 错误。这是我尝试过的:

       const testFeature = new Feature({
          geometry: new Point([0, 0]),
          name: 'Test Point '
        });
        const geoJsonObject = new GeoJSON();
        geoJsonObject.writeFeaturesObject(testFeature);
        console.log(geoJsonObject);

        const importObject = new GeoJSON().readFeatures(geoJsonObject);
        console.log(importObject);

“geoJsonObject”的第一个日志:

{
  "dataProjection": {
    "code_": "EPSG:4326",
    "units_": "degrees",
    "extent_": [
      -180,
      -90,
      180,
      90
    ],
    "worldExtent_": [
      -180,
      -90,
      180,
      90
    ],
    "axisOrientation_": "neu",
    "global_": true,
    "canWrapX_": true,
    "defaultTileGrid_": null,
    "metersPerUnit_": 111319.49079327358
  },
  "defaultFeatureProjection": null
}

这里是 importObject 日志中的错误:

Unsupported GeoJSON type: undefined

OpenLayers 版本:6.2.1 文档:https://openlayers.org/en/latest/apidoc/module-ol_format_GeoJSON-GeoJSON.html#writeFeature

谢谢!

【问题讨论】:

  • 你能尝试在数组中写入和读取任何简单的特征吗?喜欢 [new Feature(new Circle([5e6, 7e6], 1e6))]?
  • 感谢您的评论 - 我已经编辑了问题,是的,同样的错误
  • 您可以将您的代码与示例进行比较:openlayers.org/en/latest/examples/geojson.html
  • 如果你传递一个特性,你应该调用 writeFeatureObject 或者 writeFeaturesObject 并将 [testFeature] 传递给它。

标签: openlayers openlayers-6


【解决方案1】:

除了需要具有 writeFeaturesObject 的一系列功能外,您对 geoJsonObject 的使用是错误的

const geoJsonObject = new GeoJSON().writeFeaturesObject([testFeature]);
console.log(geoJsonObject);

const importObject = new GeoJSON().readFeatures(geoJsonObject);
console.log(importObject);

【讨论】:

    猜你喜欢
    • 2013-08-22
    • 2017-07-15
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多