Search code examples
openlayers

How to select features via attribute property values


I'm using OpenLayers 7.3 and am trying to select features of a GeoJSON vector layer based on attribute values. I've tried using the Select Class with filters but haven't been able to get it to work. I just need to run some preset queries from the button above the map; for example, select all with "[fid] > 5":

var selectAttributes = new Select({
  layers: [VectorLayer],
  filter: new GreaterThan({propertyName: 'fid', expression: 5}),
});

Is there a way to do this? I've read about approaches from earlier OL versions but haven't found anything that works well. I've tried the very nice ol-ext Select control, which gives me hope this type of selection is possible at 7.3, but I can't use a control and just need to have something that allows the user to run and see the results of the selection from a single button push. Thanks for any help.


Solution

  • As Mike said in the comments above:

    Select is intended for user interactions but you can populate its feature collection as in codesandbox.io/s/modify-features-forked-3u6thw?file=/main.js