如何从单个GeoJSON属性创建值数组? [重复]

时间:2022-02-13 01:49:14

This question already has an answer here:

这个问题在这里已有答案:

Note: it's been pointed out that some possible solutions were already proposed here. However, .map is exactly what I was looking for and doesn't appear in that otherwise-comprehensive post. Thomas's answer below covers my use case.

注意:有人指出,这里已经提出了一些可能的解决方案。但是,.map正是我所寻找的,并没有出现在那个全面的帖子中。托马斯在下面的回答涵盖了我的用例。

Using javascript, how can I make an array of values that represents an arbitrary property value for each feature in a GeoJSON file? I realize I'm failing JSON 101 here, but how can I create this:

使用javascript,我如何创建一个值数组,表示GeoJSON文件中每个功能的任意属性值?我意识到我在这里失败了JSON 101,但我怎么能创建它:

['caiparinha','caucasian','margarita']

from this?

由此?

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "name": "brasil",
        "beverage": "caiparinha"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -56.953125,
          -8.754794702435605
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "russia",
        "beverage": "caucasian"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          39.0234375,
          57.136239319177434
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "mexico",
        "beverage": "margarita"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -105.1171875,
          25.165173368663954
        ]
      }
    }
  ]
}

If the source were a CSV, it would be as easy as selecting/parsing a single column from the attributes, but with GeoJSON the properties (i.e. columns) are so deeply-nested that I haven't had any luck with .map, and I'd like to avoid looping, but I'll take anything at this point.

如果源是CSV,那么就像从属性中选择/解析单个列一样简单,但是使用GeoJSON,属性(即列)是如此深层嵌套,以至于我没有运气.map,以及我想避免循环,但我会在这一点上采取任何措施。

Context is that I'd like to be able to extract distribution information from each "variable" attached to the features I'm mapping, for example.

上下文是我希望能够从附加到我正在映射的特征的每个“变量”中提取分布信息。

1 个解决方案

#1


2  

Try something like below

尝试类似下面的内容

var geojson = {
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "name": "brasil",
        "beverage": "caiparinha"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -56.953125,
          -8.754794702435605
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "russia",
        "beverage": "caucasian"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          39.0234375,
          57.136239319177434
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "mexico",
        "beverage": "margarita"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -105.1171875,
          25.165173368663954
        ]
      }
    }
  ]
};

var expected_result = geojson.features.map(function (el) {
  return el.properties.beverage;
});

console.log(expected_result);

If your GeoJSON is coming from an Ajax call, so it's a string in first, juste use JSON.parse(yourGeoJSON)

如果您的GeoJSON来自Ajax调用,那么它首先是一个字符串,juste使用JSON.parse(yourGeoJSON)

#1


2  

Try something like below

尝试类似下面的内容

var geojson = {
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "name": "brasil",
        "beverage": "caiparinha"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -56.953125,
          -8.754794702435605
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "russia",
        "beverage": "caucasian"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          39.0234375,
          57.136239319177434
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "mexico",
        "beverage": "margarita"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -105.1171875,
          25.165173368663954
        ]
      }
    }
  ]
};

var expected_result = geojson.features.map(function (el) {
  return el.properties.beverage;
});

console.log(expected_result);

If your GeoJSON is coming from an Ajax call, so it's a string in first, juste use JSON.parse(yourGeoJSON)

如果您的GeoJSON来自Ajax调用,那么它首先是一个字符串,juste使用JSON.parse(yourGeoJSON)