简体   繁体   English

Javascript-根据其他json的值过滤geojson

[英]Javascript - Filter geojson based on other json's values

I am struggling with filtering a geojson based on the values of another JSON. 我正在努力根据另一个JSON的值过滤geojson。 Specifically, Iam trying to filter the geojson based on the id's in the other json. 具体来说,Iam尝试根据另一个json中的ID过滤geojson。 Every time, my filtered geojson returns null. 每次,我过滤的geojson返回null。

I have created a JSfiddle here showing what I am trying to do. 我在这里创建了一个JSfiddle 显示我正在尝试做的事情。

The code is 该代码是

    mygeojson={crs: {properties: {name: "EPSG:4326"}, type: "name"},
features: [
    {geometry: {coordinates: [25, 37], type: "Point"},properties: {"wineryid":3},type:"Feature"},
  {geometry: {coordinates: [26, 37], type: "Point"},properties: {"wineryid":45},type:"Feature"},
  {geometry: {coordinates: [25, 38], type: "Point"},properties: {"wineryid":34},type:"Feature"},
  {geometry: {coordinates: [28, 37], type: "Point"},properties: {"wineryid":42},type:"Feature"},
  {geometry: {coordinates: [25, 342], type: "Point"},properties: {"wineryid":80},type:"Feature"},
  {geometry: {coordinates: [25.24, 37.12], type: "Point"},properties: {"wineryid":120},type:"Feature"}], type:"FeatureCollection"};
mylist=[{id:1, name:"test1"},{id:34,name:"test2"}];
tempgeojson = mygeojson.features.filter(function (item) {
  return (mylist.every(f => item.properties.id === f.id) )
});
filteredgeojson={};
filteredgeojson.features=tempgeojson;
filteredgeojson.crs=mygeojson.crs;
filteredgeojson.type="FeatureCollection";

How can I get the filtered geojson I need? 如何获取所需的过滤出的geojson? (In this example, the expected geojson is about id's 1 and 34.) (在此示例中,预期的geojson约为id的1和34。)

Try the following code for features filtering 尝试以下代码进行功能过滤

filteredgeojson.features = mygeojson.features.filter(item => { 
     if(mylist.filter(myitem => myitem.id === item.properties.wineryid).length > 0) { 
          return item;
      }
});

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

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