2

I'm using MapBOX GL JS with GeoJSON data source. The content of the GeoJSON is changing dynamically. It is a FeatureCollection which contains polygons. I would like to update my MapBox data source very frequently (60 ms). Because the size of the GeoJSON is growing I want to filter the data to maintain the performance of my application. Thus, I would like to pre-filter the data and use only the last 100 polygon from my GeoJSON.

This is original GeoJSON with only one element:

var geojson = {'type':'FeatureCollection','features':[{'type':'Feature','geometry':{'type':'Polygon','coordinates':[]},'properties':{'c':'0'}}]};

I tried to pre-filter the data before updating the source. I can do the filtering based on the category of the features:

 var newGeoJSON = geojson;
newGeoJSON2.features = geojson.features.filter(feature => feature.properties.c ==='1');

And set the map source:

map.getSource('geojson').setData(newGeoJSON);

It works, however, I would like to filter the GeoJSON by the index of the features. For example filter out the last 100 elements of the array.

Do you have any suggestions how to do it?

1 Answer 1

2

Slicing the features array might work. For example, something like:

var newGeoJSON = geojson;
newGeoJSON2.features = geojson.features.slice(-100);

The negative index (-100) indicates an offset from the end of the array.

This approach assumes that the features array contains more than 100 elements. Depending on your situation that would be something to test for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.