Openlayers:不支持的 GeoJSON 类型:未定义

Openlayers: Unsupported GeoJSON type: undefined

以防有人记得或看过我之前的 post,我试图解析 GeoJSON 字符串,但收效甚微。该问题已解决,但我有一个大约 80k 行的 geojson 文件。我取出了我的 .js 文件中的字符串,并试图将我的 geojsonObject 指向 geojson 文件的文件路径。看起来很简单,但现在我在 Microsoft Edge 的控制台中得到 "Unsupported GeoJSON type: undefined"。错误指向bundle-url.js

不确定出了什么问题。

控制台中链接的 .js 文件中的代码:

var bundleURL = null;
function getBundleURLCached() {
  if (!bundleURL) {
    bundleURL = getBundleURL();
  }

  return bundleURL;
}

function getBundleURL() {
  // Attempt to find the URL of the current script and use that as the base URL
  try {
    throw new Error;
  } catch (err) {
    var matches = ('' + err.stack).match(/(https?|file|ftp|chrome-extension|moz-extension):$
    if (matches) {
      return getBaseURL(matches[0]);
    }
  }

  return '/';
}

function getBaseURL(url) {
  return ('' + url).replace(/^((?:https?|file|ftp|chrome-extension|moz-extension):\/\/.+)\/$
}

exports.getBundleURL = getBundleURLCached;
exports.getBaseURL = getBaseURL;

我的 .js 文件中的代码。 url 指向与 .js 位于同一文件夹中的 geojson 文件:

var geojsonObject = {
        url: './locality.geojson',
        format: new GeoJSON()
}

var vectorSource = new VectorSource({
        features: new GeoJSON().readFeatures(geojsonObject, {
                dataProjection: 'EPSG:4326',
                featureProjection: 'EPSG:3857'
        })
});

我已经将我的 geojson 通过了两个验证器,没有出现任何问题。这一切都在本地主机 (Ubuntu VPS) 上,使用 npm。

如上所述,geojson 文件有 80k 行长,所以我不能把它全部贴在这里,所以这里是一个片段;

{
   "type": "FeatureCollection",
   "features": [
   {
    "type": "Feature",
    "geometry": {
       "type": "Point",
       "coordinates":  [ -6.65073,54.34794 ]
    },
    "properties": {
    "Site":"ARMAGH"
    }
  },

您在非 GeoJSON 形式的对象上请求 GeoJSONreadFeaturesgeojsonObject 没有 type 所以你会得到 "Unsupported GeoJSON type: undefined" 错误。

...readFeatures({ url: ..., format: ... /* there's no type! */})

查看 this reproduction 以获取 readFeatures 期望的对象类型的示例。

我怀疑你真正想要的是这样的:

var vectorSource = new VectorSource({
    url: './locality.json',
    format: new GeoJSON({ featureProjection: "EPSG:3857" })
});

您通常会使用 url & formatfeatures & readFeatures,具体视情况而定。

您可以将所有数据存储到 'data.json' 文件中,而不是 .geojson。 require 你的js中的文件然后你可以使用readFeatures

读取它

const data = require('./data.json')
var geojsonObject = data;

var vectorSource = new VectorSource({
    features: (new GeoJSON()).readFeatures(geojsonObject,
            {
            dataProjection: 'EPSG:4326',
            featureProjection: 'EPSG:3857'
            })
    });