Sunday, 8 July 2018

javascript - OpenLayers 3 how to get only the modified feature?


I have a problem with OL3 and it's ol.interaction.Modify Event. Whenever I modify a feature, just one, it returns all features inside the layer. Currently I have 77 features (polygons) and I can modify them with the modify interaction. Inside the callback function it returns the event with the feature member array and this seems to be buggy:


modify.on('modifyend', function(e) {

console.log('=== Modify end ===');
var modifiedFeature = e.features.item(0);

//Why do I get 77 modified features if I have only touched one?
// e.features size is = 77
console.log(e.features);

});

Do you know a working way to get the one modified feature?



Answer



I guess you pass all the features to the modify interaction. That is why it returns all of them (If you dont provide your code I can only guess). You can always use a select interaction and the pass the selected feature to modify interction. For your case you may iterate through your features and check for the revision, each time a feature is modified, its version number will be incremented.


So do something like this


var modifiedFeatures;
modify.on('modifyend', function(e) {
var features = e.features.getArray();
console.log(features.length);

for (var i = 0; i < features.length; i++) {
var rev = features[i].getRevision();
if (rev > 1) {
console.log("feature with revision:" + rev + " has been modified");
modifiedFeatures.push(features[i]);
}
}
});

check this fiddle.



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...