过滤geojson TS

时间:2019-12-03 17:09:19

标签: angular typescript filter rxjs geojson

我使用的是angular8。我的资产文件夹中有一个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”(使用mat-select-form和NgModel来过滤我的geojson数据。

例如,为属性为“ t” = 2的项目过滤json

//Component.ts

json;

constructor( private http: HttpClient) {}

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

1 个答案:

答案 0 :(得分:1)

以下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);
});