Filter Feature's outside the viewport in Mapbox

Viewed 622

I have a huge geojson file having around 100K feature's and I want to filter feature's outside the viewport. I tried multiple approach given on:

https://github.com/mapbox/mapbox-gl-js/issues/8350 and https://gis.stackexchange.com/questions/300292/how-to-load-geojson-features-outside-view-with-mapbox-gl?rq=1

But nothing seems to work. My Code is:

//Here i have already added empty geojson source
 this.instance.addSource('seats_geojson', {
            type: 'geojson',
            data: {
                "type": "FeatureCollection",
                "features": []
            }
        });

//Here i have axios call with await and i am getting all the feature's in console
//console.log(e.data.featuresCollection.features) <--- 100K features

this.instance.once('idle', () => {                      

          //Once i have the reponse ready I am setting source using setData
          this.instance.getSource('seats_geojson').setData(e.data.featuresCollection);


          //Here i am creating empty layer with above source
                         this.instance.addLayer({
                         id: "rowSelectionDup",
                         type: 'circle',
                            source: 'seats_geojson',
                            paint: {
                                'circle-color': "#4400d9"
                            },
                            filter: [
                                "in", "s", ""
                            ]
                        });
                    });

but on doing something like:

this.instance.querySourceFeatures('seats_geojson', {
  sourceLayer: 'rowSelectionDup',
  filter: ['in', 's', "1C"] //This section "s" 1C is in viewport and i am getting 207 feature's
});
this.instance.querySourceFeatures('seats_geojson', {
  sourceLayer: 'rowSelectionDup',
  filter: ['in', 's', "7C"]. //This section is outside viewport and result is []
});

Note: this.instance is the map instance(new Map(config....)) of mapbox-gl

Am I doing something wrong here?

or is there any other approach to get feature's from geojson?

Thank you in advance...

1 Answers

As i posted this question on mapbox-gl Github:

https://github.com/mapbox/mapbox-gl-js/issues/9720

As of current mapbox-gl version, there isn't a direct way to fetch the feature's outside of viewport. Hence, as answered by @steve-bennett, I am fetching the geojson url, save as a reference in the javascript oject and applying filter on it is working for me now...

Hope it helps...

Related