Using OpenLayers with a Select interaction to select features, I'm attempting to convert the selected features to JSON for further processing within the select.on('select', ...)
as per the code below.
select.on('select', function() {
try {
var json = new ol.format.GeoJSON().writeFeatures(select.getFeatures(), {});
console.log(select.getFeatures().getLength());
console.log(json);
doStuffWithJSON(json);
} catch(err) {
console.log(err.message);
}
});
However, the output from the two console.log() lines is:
2
{"type":"FeatureCollection","features":[]}
Ie, there are 2 features selected, but the GeoJSON includes no features.
What am I doing wrong here, and how I can I convert this to correct GeoJSON?
select.getFeatures()
returns an OpenLayers collection,writeFeatures
expects an array of featuresselect.getFeatures().getArray()