Openlayers:不支持的GeoJSON类型:undefined [英] Openlayers: Unsupported GeoJSON type: undefined

查看:1033
本文介绍了Openlayers:不支持的GeoJSON类型:undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以防万一有人记得或看过我的较早文章,我试图解析GeoJSON字符串,但效果有限.该问题已解决,但是我有一个大约80k行的geojson文件.我在.js文件中取出了字符串,并试图将geojsonObject指向geojson文件的文件路径.看起来很简单,但是现在我在Microsoft Edge的控制台中得到不受支持的GeoJSON类型:未定义".错误指向bundle-url.js

In case anyone remembers or have seen my earlier post, I was trying to parse a GeoJSON string with limited success. That issue has been resolved, however I have a geojson file with roughly 80k lines. I took out the string in my .js file and trying to point my geojsonObject at the filepath of the geojson file. It seemed simple enough, but now I get "Unsupported GeoJSON type: undefined" in Microsoft Edge's console. The error points to the bundle-url.js

不知道出了什么问题.

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

The code from the .js file linked in the console:

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文件中的代码.网址指向.js相同文件夹中的geojson文件:

The code from my .js file. The url points to the geojson file that is in the same folder of the .js:

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

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

我已将我的geojson通过两个验证器,但没有问题.全部使用npm在本地主机(Ubuntu VPS)上.

I have put my geojson through two validators, which have come up with no issues. This is all on a localhost (Ubuntu VPS), using npm.

如上所述,geojson文件的长度为80k行,因此我无法将其全部粘贴在这里,因此这是一个代码段;

As stated above, the geojson file is 80k lines long so I can't stick it all in here so here is a snippet;

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

推荐答案

您要对非GeoJSON格式的对象要求GeoJSONreadFeatures. geojsonObject没有type,因此会出现不支持的GeoJSON类型:未定义"错误.

You are asking GeoJSON to readFeatures on an object that is not in GeoJSON form. geojsonObject has no type so you get the "Unsupported GeoJSON type: undefined" error.

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

请查看此复制品作为对象readFeatures类型的示例期望.

Have a look at this reproduction for an example of the type of object readFeatures expects.

我怀疑您真正想要的是这样的东西:

I suspect what you actually want is something like:

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

您通常会使用url& formatfeatures& readFeatures视情况而定.

You'd usually use either url & format or features & readFeatures depending on what suits.

这篇关于Openlayers:不支持的GeoJSON类型:undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆