首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何基于对象过滤GeoJson中的对象?

如何基于对象过滤GeoJson中的对象?
EN

Stack Overflow用户
提问于 2018-10-25 05:56:43
回答 1查看 240关注 0票数 1

我希望根据在多选择下拉菜单中所做的选择创建的对象来过滤我的GeoJSON数据。

GeoJSON数据

代码语言:javascript
复制
    {
    "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"
    }
  },

所选值的对象:

代码语言:javascript
复制
sel_data_category = {country:['US','AF'], city: ['Brawley','Zaybak'], magType:['mw']}
sel_data_quant ={horizontalError:[0.68,0.90]}

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

代码语言:javascript
复制
{
    "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"
    }
  }

有什么办法可以让我做这个吗?

编辑:缺少一个horizontalError值

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-25 06:03:25

您可以使用Array.filter

解决方案1:特定筛选器参数

代码语言:javascript
复制
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:泛型过滤器参数(数组中的所有值)

代码语言:javascript
复制
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);

编辑

代码语言:javascript
复制
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);

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52982236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档