简体   繁体   English

OpenLayers 读/写 GeoJSON

[英]OpenLayers read / write GeoJSON

i try to store features of a OpenLayers Map Layer to a database and therefore I try to write the features objects into a GeoJSON object.我尝试将 OpenLayers 地图层的特征存储到数据库中,因此我尝试将特征对象写入 GeoJSON 对象。 But I get an Unsupported GeoJSON type: undefined error when I try to read the object.但是当我尝试读取对象时,出现Unsupported GeoJSON type: undefined错误。 Here what i tried:这是我尝试过的:

       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);

The first log of the "geoJsonObject": “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
}

Here the error from the log of the importObject:这里是 importObject 日志中的错误:

Unsupported GeoJSON type: undefined

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

Thanks!谢谢!

As well as needing an array of features with writeFeaturesObject your use of geoJsonObject was wrong除了需要具有 writeFeaturesObject 的一系列功能外,您对 geoJsonObject 的使用也是错误的

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM