【问题标题】:How to filter objects in GeoJson based on an object?如何根据对象过滤 GeoJson 中的对象?
【发布时间】:2018-10-25 05:56:43
【问题描述】:

我想根据基于多选下拉菜单中的选择创建的对象过滤我的 GeoJSON 数据。

GeoJSON 数据

    {
    "type": "Feature",
    "geometry": {
       "type": "Point",
       "coordinates":  [ -115.55783329999998,32.9646667 ]
    },
    "properties": {  "magType":"mb", "type":"earthquake","horizontalError":0.32,"depthError":0.58,    "city":"Brawley",    "state":"California",    "country":"US"}
  },
  {
    "type": "Feature",
    "geometry": {
       "type": "Point",
       "coordinates":  [ -115.54583329999998,32.98 ]
    },
    "properties": {   "magType":"mb", "type":"earthquake",    "horizontalError":0.24, "depthError":0.46,    "city":"Brawley",    "state":"California", "country":"US"
    }
  },
  {
    "type": "Feature",
    "geometry": {
       "type": "Point",
       "coordinates":  [ -118.13383329999999,33.777333299999995 ]
    },
    "properties": {"magType":"ml","type":"earthquake","horizontalError":0.77,"depthError":0.9, "city":"Brawley","state":"California","country":"US"
    }
  },
  {
    "type": "Feature",
    "geometry": {
       "type": "Point",
       "coordinates":  [ -115.555,32.967 ]
    },
    "properties": {"magType":"ml","type":"earthquake",    "horizontalError":0.43,    "depthError":0.67,    "city":"Isangel","state":"Tafea","country":"VU"
  },
  {
    "type": "Feature",
    "geometry": {
       "type": "Point",
       "coordinates":  [ -115.55216670000001,32.9658333 ]
    },
    "properties": {"magType":"mw","type":"tsunami", "horizontalError":0.79, "depthError":1.35, "city":"Zaybak", "state":"Badakhshan", "country":"AF"
    }
  },

选中值的对象:

sel_data_category = {country:['US','AF'], city: ['Brawley','Zaybak'], magType:['mw']}
sel_data_quant ={horizontalError:[0.68,0.90]}

我想根据这些选定的值过滤数据。所以预期的输出应该是 -

{
    "type": "Feature",
    "geometry": {
       "type": "Point",
       "coordinates":  [ -115.55216670000001,32.9658333 ]
    },
    "properties": {"magType":"mw","type":"tsunami", "horizontalError":0.79, "depthError":1.35, "city":"Zaybak", "state":"Badakhshan", "country":"AF"
    }
  }

有什么办法可以做到吗?

编辑:错过了一个水平错误值

【问题讨论】:

    标签: javascript json d3.js filter geojson


    【解决方案1】:

    您可以使用Array.filter

    解决方案 1:特定过滤器参数

    let arr = [{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.55783329999998,32.9646667]},"properties":{"magType":"mb","type":"earthquake","horizontalError":0.32,"depthError":0.58,"city":"Brawley","state":"California","country":"US"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.54583329999998,32.98]},"properties":{"magType":"mb","type":"earthquake","horizontalError":0.24,"depthError":0.46,"city":"Brawley","state":"California","country":"US"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-118.13383329999999,33.777333299999995]},"properties":{"magType":"ml","type":"earthquake","horizontalError":0.77,"depthError":0.9,"city":"Brawley","state":"California","country":"US"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.555,32.967]},"properties":{"magType":"ml","type":"earthquake","horizontalError":0.43,"depthError":0.67,"city":"Isangel","state":"Tafea","country":"VU"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.55216670000001,32.9658333]},"properties":{"magType":"mw","type":"tsunami","horizontalError":0.79,"depthError":1.35,"city":"Zaybak","state":"Badakhshan","country":"AF"}}];
    
    let filterObj = {country:['US','AF'], city: ['Brawley','Zaybak'], magType:['mw']};
    
    let result = arr.filter(o => filterObj.country.includes(o.properties.country) 
                                 && filterObj.city.includes(o.properties.city) 
                                 && filterObj.magType.includes(o.properties.magType));
    
    console.log(result);

    解决方案 2:通用过滤器参数(数组中的所有值)

    let arr = [{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.55783329999998,32.9646667]},"properties":{"magType":"mb","type":"earthquake","horizontalError":0.32,"depthError":0.58,"city":"Brawley","state":"California","country":"US"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.54583329999998,32.98]},"properties":{"magType":"mb","type":"earthquake","horizontalError":0.24,"depthError":0.46,"city":"Brawley","state":"California","country":"US"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-118.13383329999999,33.777333299999995]},"properties":{"magType":"ml","type":"earthquake","horizontalError":0.77,"depthError":0.9,"city":"Brawley","state":"California","country":"US"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.555,32.967]},"properties":{"magType":"ml","type":"earthquake","horizontalError":0.43,"depthError":0.67,"city":"Isangel","state":"Tafea","country":"VU"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.55216670000001,32.9658333]},"properties":{"magType":"mw","type":"tsunami","horizontalError":0.79,"depthError":1.35,"city":"Zaybak","state":"Badakhshan","country":"AF"}}];
    
    let filterObj = {country:['US','AF'], city: ['Brawley','Zaybak'], magType:['mw']};
    let filterObjArray = Object.entries(filterObj);
    
    let result = arr.filter(o => filterObjArray.every(([k,v]) => v.includes(o.properties[k])));
    
    console.log(result);

    编辑

    let arr = [{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.55783329999998,32.9646667]},"properties":{"magType":"mb","type":"earthquake","horizontalError":0.32,"depthError":0.58,"city":"Brawley","state":"California","country":"US"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.54583329999998,32.98]},"properties":{"magType":"mb","type":"earthquake","horizontalError":0.24,"depthError":0.46,"city":"Brawley","state":"California","country":"US"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-118.13383329999999,33.777333299999995]},"properties":{"magType":"ml","type":"earthquake","horizontalError":0.77,"depthError":0.9,"city":"Brawley","state":"California","country":"US"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.555,32.967]},"properties":{"magType":"ml","type":"earthquake","horizontalError":0.43,"depthError":0.67,"city":"Isangel","state":"Tafea","country":"VU"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-115.55216670000001,32.9658333]},"properties":{"magType":"mw","type":"tsunami","horizontalError":0.79,"depthError":1.35,"city":"Zaybak","state":"Badakhshan","country":"AF"}}];
    
    let sel_data_category = {country:['US','AF'], city: ['Brawley','Zaybak'], magType:['mw']}
    let sel_data_quant ={horizontalError:[0.68,0.90]}
    let filterObjArray = Object.entries(sel_data_category);
    let filterQuantArray = Object.entries(sel_data_quant);
    
    let result = arr.filter(o => filterObjArray.every(([k,v]) => v.includes(o.properties[k])) && filterQuantArray.every(([k,[l,h]]) => o.properties[k] >= l && o.properties[k] <= h));
    
    console.log(result);

    【讨论】:

    • 谢谢,尼基尔。解决方案 2 对我很有帮助。但是我错过了 Horizo​​ntalError 的值,这是一个选定的值范围。你能调查一下吗?
    • @gkuhu - 您是否可以控制filterObj,即您可以按需要在范围内或匹配数组和/或其他过滤条件的确切值的类别进行分组吗?因为如果您正在寻找解决方案 2,那么必须有一些通用的方法来隔离类型
    • 是的,我可以有两个不同的对象,一个用于类别,一个用于范围,但我不知道如何根据这两种类型的过滤器过滤数据。你能调查一下吗?我刚刚进行了编辑
    猜你喜欢
    • 1970-01-01
    • 2021-04-02
    • 2022-11-30
    • 2020-12-03
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 2018-12-25
    • 2020-12-14
    相关资源
    最近更新 更多