按属性过滤GeoJson数据以获取数据的子集

时间:2018-11-14 00:45:25

标签: javascript geojson

我正在尝试通过属性获取Geojson数据的子集。

 {
   "type": "FeatureCollection",
   "features": [
  {
    "type": "Feature",
    "geometry": {
       "type": "Point",
       "coordinates":  [ -115.5578333,32.9646667 ]
    },
    "properties": {
    "Year1979_03":2.92606854,
    "Year1979_06":2.963032273,
    "Year1979_09":2.968127935
    }
  }]

如果用户选择年份“ Year1979_03,那么它应该返回

{
       "type": "FeatureCollection",
       "features": [
      {
        "type": "Feature",
        "geometry": {
           "type": "Point",
           "coordinates":  [ -115.5578333,32.9646667 ]
        },
        "properties": {
        "Year1979_03":2.92606854,
        }
      }]

1 个答案:

答案 0 :(得分:0)

不确定我是否正确理解了您的问题,但是不确定您是否有用户输入并将其包含在变量中:

let userInput = "Year1979_03"

您将获得原始格式的返回数据

let originalData = {
   "type": "FeatureCollection",
   "features": [
  {
    "type": "Feature",
    "geometry": {
       "type": "Point",
       "coordinates":  [ -115.5578333,32.9646667 ]
    },
    "properties": {
        "Year1979_03":2.92606854,
        "Year1979_06":2.963032273,
        "Year1979_09":2.968127935
    }
   }
  ]
 }

然后,您可以使用es6传播运算符来创建要创建的对象:

let reformedData = {
    ...originalData, 
    "features": [
    {
     ...originalData.features[0], 
     "properties": 
    {
     [userInput]: originalData.features[0].properties[userInput] 
    } 
   }] 
  }

以上,我使用散布运算符从原始数据对象检索要保留的所有键和值,但指定要更改/替换的键和值。这将为您带来所需的对象。