简体   繁体   English

如何根据对象过滤GeoJson中的对象?

[英]How to filter objects in GeoJson based on an object?

I want to filter my GeoJSON data based on an object created based on the selections done in the multi-select dropdown menu. 我想基于根据多选下拉菜单中所做选择创建的对象来过滤我的GeoJSON数据。

GeoJSON data 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"
    }
  },

The object of the selected values: 所选值的对象:

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

I want to filter data based on these selected values. 我想根据这些选定的值过滤数据。 So the expected output should be - 所以预期的产量应该是 -

{
    "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"
    }
  }

Is there any way i could perform this? 有什么方法可以执行此操作吗?

EDIT: Missed a horizontalError Value 编辑:错过了一个horizo​​ntalError值

You can use Array.filter 您可以使用Array.filter

SOLUTION 1: Specific Filter Params 解决方案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); 

SOLUTION 2: Generic Filter Params (all values in array) 解决方案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); 

EDIT 编辑

 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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM