OpenLayers – Create Source and Layer from Vector Tile Features

featuresopenlayersvectorvector-tiles

So I retrieve all my features from a vectorTile layer (PBF from Geoserver), and now I want to recreate a layer by filtering on those features and getting only those that satisfy a request.
I successfully stored my filtered features in an array, but when I try to add them to a new vector source, I get the error: typeError e is null.

See my code below:

var filteredFeatures= [];


for (var i = 0; i < data1.length; i++) {
     for (var j = 0; j < features.length; j++) {

         if (features[j].get(data1[i].type) == data1[i].valeur) {

             filteredFeatures.push(features[j]);
         }

     }

     var source = new ol.source.Vector({
         features: filteredFeatures,
         projection: proj2154,
     });
      map.addLayer(new ol.layer.Vector({
          source:source,
           style:styles.red
      }));


     filteredFeatures = [];

 }

Best Answer

Finally it is best to use functions styles to filter a vectorTile Layer because features will be changing. thanks to Olivier Guyot's answer

Aside from that, it appears to me that the logic behind your code isn't right. Vector Tile layers hold simplified features from the RenderFeature class, and I'm not sure these can be used in a regular Vector source. Besides, how often are you planning to run this filtering process? The features in the Vector Tile layer will evolve over time, so a simpler solution would be to use a style function that only renders feature with a certain type (i. e. apply your filtering logic in the style function).