筛选 geojson TS

Filter geojson TS

我使用 angular 8. 我的资产文件夹中有一个 geojson 填充物:

{ "type": "FeatureCollection",
  "features": [
    { "type": "Feature", "geometry": {"type": "Point", "coordinates": [9.15926605,41.38718765]},"properties": {"f":"2A0000212","n":"HOPITAL LOCAL DE BONIFACIO","c":"20169 BONIFACIO","t":"2"}},
    { "type": "Feature", "geometry": {"type": "Point", "coordinates": [9.15926605,41.38718765]},"properties": {"f":"2A0003141","n":"ANTENNE DU SMUR  BONIFACIO","c":"20169 BONIFACIO","t":"2"}},
    ...
]}

我想通过属性 "t" 过滤我的 geojson 数据(使用 mat-select-form 和 NgModel。

例如,过滤 json 以获得 属性 "t" = 2

的项目
//Component.ts

json;

constructor( private http: HttpClient) {}

ngOnInit() {this.http.get('assets/es.json').subscribe((json: any) => { this.json = json;});}

以下 rxjs 运算符应该按照您想要的方式进行过滤:

import { of } from 'rxjs'; 
import { filter, flatMap, map } from 'rxjs/operators';

let filteredData = this.http.get('assets/es.json').pipe(
  map(response => JSON.parse(response)),
  flatMap(obj => obj.features),
  filter((feature: any) => feature.properties.t == 2)
);

filteredData.subscribe(x => {
  console.log(x);
});